From 868805734e169971f5e64e2291a0962d0c22311a Mon Sep 17 00:00:00 2001 From: Araq Date: Sat, 27 Jun 2026 17:58:07 +0200 Subject: [PATCH] preparing the IC core for binary bif files --- compiler/ast2nif.nim | 1658 ++++++++++++++++++------------------ compiler/ccgtypes.nim | 10 + compiler/ccgutils.nim | 14 +- compiler/cgen.nim | 23 +- compiler/deps.nim | 37 +- compiler/lambdalifting.nim | 13 + compiler/mangleutils.nim | 14 +- compiler/nifbackend.nim | 85 +- compiler/nim.cfg | 4 + compiler/options.nim | 13 +- 10 files changed, 984 insertions(+), 887 deletions(-) diff --git a/compiler/ast2nif.nim b/compiler/ast2nif.nim index 3503040f9b..e1aad5e16b 100644 --- a/compiler/ast2nif.nim +++ b/compiler/ast2nif.nim @@ -18,11 +18,19 @@ import "../dist/checksums/src/checksums" / sha1 import astdef, idents, msgs, options import lineinfos as astli import pathutils #, modulegraphs -import "../dist/nimony/src/lib" / [bitabs, nifstreams, nifcursors, lineinfos, +import "../dist/nimony/src/lib" / [bitabs, nifstreams, lineinfos, nifindexes, nifreader] +# Step 2b: the READER speaks nifcore; the WRITER keeps nifstreams (global `pool`, +# PackedToken/PackedLineInfo). nifstreams does NOT export Cursor/TokenBuf/NifKind, +# so those resolve unambiguously to nifcore. `except pool`: nifcore's +# `pool(c: Cursor)` accessor would shadow nifstreams' global `pool` var the writer +# uses; the reader reaches pools via `symName(c)`/`strVal(c)` etc. +import "../dist/nimony/src/lib/nifcore" except pool +from "../dist/nimony/src/lib" / bif import load, BifModule import "../dist/nimony/src/gear2" / modnames import "../dist/nimony/src/models" / nifindex_tags import typekeys +import icnifcore import ic / [enum2nif] const SysModuleSuffix* = "@sys" @@ -152,22 +160,17 @@ proc nifLineInfoWithComment(w: var LineInfoWriter; info: TLineInfo; doc: string) let fid = get(w, info.fileIndex) result = packWithComment(pool.man, fid, info.line.int32, info.col, cid) -proc oldLineInfo(w: var LineInfoWriter; info: PackedLineInfo): TLineInfo = - if info == NoLineInfo: +proc oldLineInfo(w: var LineInfoWriter; info: NifLineInfo; p: Pool): TLineInfo = + ## Step 2b: the reader's line info arrives as a nifcore `NifLineInfo`; resolve + ## it to a `TLineInfo`. `info.file` indexes the loaded buffer's OWN filename + ## pool `p` (= `cursorPool(n)`), which is the shared `icPool` for a text-parsed + ## module but a fresh per-file pool for a `bif`-loaded one. + if info.file == NoFile: result = unknownLineInfo else: - var x = unpack(pool.man, info) - var fileIdx: FileIndex - if w.fileV == x.file: - fileIdx = w.fileK - elif x.file in w.revTab: - fileIdx = w.revTab[x.file] - else: - # Need to look up FileId -> FileIndex via the file path - let filePath = pool.files[x.file] - fileIdx = msgs.fileInfoIdx(w.config, AbsoluteFile filePath) - w.revTab[x.file] = fileIdx - result = TLineInfo(line: x.line.uint16, col: x.col.int16, fileIndex: fileIdx) + let filePath = p.filenames[info.file] + let fileIdx = msgs.fileInfoIdx(w.config, AbsoluteFile filePath) + result = TLineInfo(line: info.line.uint16, col: info.col.int16, fileIndex: fileIdx) # ------------- Writer --------------------------------------------------------------- @@ -200,7 +203,7 @@ var type Writer = object - deps: TokenBuf # include&import deps + deps: IcBuilder # include&import deps infos: LineInfoWriter currentModule: int32 decodedFileIndices: HashSet[FileIndex] @@ -211,7 +214,11 @@ type # can keep mutating its still-live query targets writtenPackages: HashSet[string] depSuffixes: HashSet[string] # module suffixes already emitted as `(import ...)` deps - emittedBackendTypes: HashSet[int32] # backend-local type items already def'd this module + emittedBackendTypes: HashSet[(int32, int32)] # backend-local types already def'd this + # module, keyed by (kind, item): the NIF name is `t..@bk`, + # so two `@bk` types sharing an item but differing in kind (e.g. `int` + # and `typedesc[int]`, both item 12) are DISTINCT defs — keying by item + # alone deduped the second to a dangling `SymUse` (`symbol has no offset`). emittedBackendSyms: HashSet[int32] # backend-local sym items already def'd this module lowering: bool # serializing the `lower` stage's whole-module `.t.nif` emittedFieldSyms: HashSet[ItemId] # lowering: derived env-field syms already def'd @@ -261,7 +268,21 @@ proc toNifSymName(w: var Writer; sym: PSym): string = if sym.kindImpl == skPackage: result.add PkgMarker result.add '.' - result.addInt sym.disamb + if sym.kindImpl == skField and sym.itemId.item < 0: + # Derived closure-env field (lowerings.addField mints it with + # `derivedFieldId`, which NEGATES the item). Its mangled name is + # `&` (e.g. captured `n` at env position 2 -> "n2"), + # which can EQUAL a real local's name (a param literally named "n2"); with the + # same disamb (0) they share the NIF name `n2.0.` -> the name-keyed index + # collides -> a param use resolves to the field (`expr(skField); unknown + # symbol`). Use the field's unique |item| as the disamb so its NIF name is + # distinct. Safe: env fields are module-local (a closure crossing modules is + # type-erased to `(ClP, ClE)`, its fields never referenced by name elsewhere), + # so no foreign stub re-derives this name; and `globalName` round-trips via the + # loaded stub's parsed disamb. + result.addInt abs(sym.itemId.item) + else: + result.addInt sym.disamb if not isLocalSym(sym) and sym.itemId notin w.locals: # Global symbol: ident.disamb.moduleSuffix result.add '.' @@ -327,15 +348,62 @@ proc stubKindAndName(cache: IdentCache; rawName: string): (TSymKind, PIdent) = else: (skStub, cache.getIdent(rawName)) -template buildTree(dest: var TokenBuf; tag: TagId; body: untyped) = +# --- nifcore writer adapter ------------------------------------------------- +# The `write*` procs build the module into an `IcBuilder` (nifcore). These give +# the IcBuilder the SAME call shapes the old `nifstreams` TokenBuf had (taking +# `nifstreams` TagId/SymId/PackedToken + a PackedLineInfo), so the writer bodies +# are unchanged apart from the `var TokenBuf` -> `var IcBuilder` parameter type. +# Line info is unpacked from the PackedLineInfo and re-emitted via +# `IcBuilder.lineInfo` (absolute file/line/col; nifcore makes it relative on +# serialization), exactly as the deleted `serializeViaNifcore` bridge did. + +proc emitInfo(b: var IcBuilder; info: PackedLineInfo) {.inline.} = + if info.isValid: + let u = unpack(pool.man, info) + let fname = if u.file.isValid: pool.files[u.file] else: "" + let cstr = if u.comment != 0'u32: pool.strings[nifstreams.StrId(u.comment)] else: "" + b.lineInfo(fname, u.line, u.col, cstr) + +proc addParLe(b: var IcBuilder; tag: nifstreams.TagId; info = NoLineInfo) = + b.openTag(pool.tags[tag]); b.emitInfo(info) +proc addParRi(b: var IcBuilder) {.inline.} = b.closeTag() +proc addSymDef(b: var IcBuilder; s: nifstreams.SymId; info = NoLineInfo) = + b.addSymDef(pool.syms[s]); b.emitInfo(info) +proc addSymUse(b: var IcBuilder; s: nifstreams.SymId; info = NoLineInfo) = + b.addSymUse(pool.syms[s]); b.emitInfo(info) + +proc add(b: var IcBuilder; t: PackedToken) = + ## Bridge a single old-API token constructor (symToken/strToken/floatToken/ + ## charToken) into the IcBuilder. + case t.kind + of DotToken: b.addDotToken() + of Ident: b.addIdent(pool.strings[t.litId]) + of Symbol: b.addSymUse(pool.syms[t.symId]) + of SymbolDef: b.addSymDef(pool.syms[t.symId]) + of IntLit: b.addIntLit(pool.integers[t.intId]) + of UIntLit: b.addUIntLit(pool.uintegers[t.uintId]) + of FloatLit: b.addFloatLit(pool.floats[t.floatId]) + of CharLit: b.addCharLit(char(t.uoperand)) + of StringLit: b.addStrLit(pool.strings[t.litId]) + else: discard + if t.kind != ParRi: b.emitInfo(t.info) + +template buildTree(dest: var IcBuilder; tag: nifstreams.TagId; body: untyped) = dest.addParLe tag body dest.addParRi -template buildTree(dest: var TokenBuf; tag: string; body: untyped) = - buildTree dest, pool.tags.getOrIncl(tag), body +template buildTree(dest: var IcBuilder; tag: string; body: untyped) = + dest.openTag tag + body + dest.closeTag() -proc writeFlags[E](dest: var TokenBuf; flags: set[E]) = +template buildTree(dest: var IcBuilder; tag: nifstreams.TagId; info: PackedLineInfo; body: untyped) = + dest.addParLe tag, info + body + dest.addParRi + +proc writeFlags[E](dest: var IcBuilder; flags: set[E]) = var flagsAsIdent = "" genFlags(flags, flagsAsIdent) if flagsAsIdent.len > 0: @@ -346,11 +414,11 @@ proc writeFlags[E](dest: var TokenBuf; flags: set[E]) = proc trLineInfo(w: var Writer; info: TLineInfo): PackedLineInfo {.inline.} = result = nifLineInfo(w.infos, info) -proc writeNode(w: var Writer; dest: var TokenBuf; n: PNode; forAst = false) -proc writeType(w: var Writer; dest: var TokenBuf; typ: PType) -proc writeSym(w: var Writer; dest: var TokenBuf; sym: PSym) +proc writeNode(w: var Writer; dest: var IcBuilder; n: PNode; forAst = false) +proc writeType(w: var Writer; dest: var IcBuilder; typ: PType) +proc writeSym(w: var Writer; dest: var IcBuilder; sym: PSym) -proc writeLoc(w: var Writer; dest: var TokenBuf; loc: TLoc) = +proc writeLoc(w: var Writer; dest: var IcBuilder; loc: TLoc) = dest.addIdent toNifTag(loc.k) dest.addIdent toNifTag(loc.storage) writeFlags(dest, loc.flags) # TLocFlags @@ -371,7 +439,7 @@ proc nifTypeName(w: Writer; typ: PType): string = else: result = typeToNifSym(typ, w.infos.config) -proc writeTypeDef(w: var Writer; dest: var TokenBuf; typ: PType) = +proc writeTypeDef(w: var Writer; dest: var IcBuilder; typ: PType) = dest.buildTree tdefTag: dest.addSymDef pool.syms.getOrIncl(nifTypeName(w, typ)), NoLineInfo dest.addDotToken # always private for the index generator @@ -414,7 +482,7 @@ proc writeTypeDef(w: var Writer; dest: var TokenBuf; typ: PType) = writeType(w, dest, ch) -proc writeType(w: var Writer; dest: var TokenBuf; typ: PType) = +proc writeType(w: var Writer; dest: var IcBuilder; typ: PType) = if typ == nil: dest.addDotToken() elif typ.uniqueId.isBackendMinted: @@ -422,7 +490,7 @@ proc writeType(w: var Writer; dest: var TokenBuf; typ: PType) = # `@bk` def the first time it is reached in this module, reference it after. # Per-Writer dedup (NOT the shared `state`), since every referencing module # must emit its own copy. - if not w.emittedBackendTypes.containsOrIncl(typ.uniqueId.item): + if not w.emittedBackendTypes.containsOrIncl((ord(typ.kind).int32, typ.uniqueId.item)): writeTypeDef(w, dest, typ) else: dest.addSymUse pool.syms.getOrIncl(nifTypeName(w, typ)), NoLineInfo @@ -440,11 +508,11 @@ proc writeType(w: var Writer; dest: var TokenBuf; typ: PType) = else: dest.addSymUse pool.syms.getOrIncl(nifTypeName(w, typ)), NoLineInfo -proc writeBool(dest: var TokenBuf; b: bool) = +proc writeBool(dest: var IcBuilder; b: bool) = dest.buildTree (if b: "true" else: "false"): discard -proc writeLib(w: var Writer; dest: var TokenBuf; lib: PLib) = +proc writeLib(w: var Writer; dest: var IcBuilder; lib: PLib) = if lib == nil: dest.addDotToken() else: @@ -471,7 +539,7 @@ proc docOfSym(sym: PSym): string = return nodeCommentReader(body[0]) return "" -proc writeSymDef(w: var Writer; dest: var TokenBuf; sym: PSym) = +proc writeSymDef(w: var Writer; dest: var IcBuilder; sym: PSym) = dest.addParLe sdefTag, nifLineInfoWithComment(w.infos, sym.infoImpl, docOfSym(sym)) dest.addSymDef pool.syms.getOrIncl(w.toNifSymName(sym)), NoLineInfo # The `x` marker means "importable as a bare identifier into an importer's @@ -580,9 +648,16 @@ proc isLoweredPerEntryField(w: Writer; sym: PSym): bool {.inline.} = ## `shouldWriteSymDef` would seal it after the first entry and later entries ## would reference a def that their indexed copy does not contain. Re-emit it ## as a full def per entry, deduped within the entry via `emittedFieldSyms`. - w.lowering and sym.kindImpl == skField and not sym.itemId.isBackendMinted + ## ONLY for a field this module OWNS: a foreign field (e.g. an `asyntivci` field + ## reached through a `tyGenericInst` this module instantiated) must stay a + ## `SymUse` resolved via its home module — emitting a local def here would index + ## it under the wrong module and corrupt the seek (a stale `c.syms` offset → out + ## of bounds). Mirrors `shouldWriteSymDef`'s `currentModule` ownership test, + ## which is what keeps `.s.nif` (lowering off) from emitting foreign field defs. + w.lowering and sym.kindImpl == skField and not sym.itemId.isBackendMinted and + sym.itemId.module == w.currentModule -proc writeSym(w: var Writer; dest: var TokenBuf; sym: PSym) = +proc writeSym(w: var Writer; dest: var IcBuilder; sym: PSym) = if sym == nil: dest.addDotToken() elif isLoweredPerEntryField(w, sym): @@ -606,7 +681,7 @@ proc writeSym(w: var Writer; dest: var TokenBuf; sym: PSym) = # unlike what we do for types! dest.addSymUse pool.syms.getOrIncl(w.toNifSymName(sym)), NoLineInfo -proc writeSymNode(w: var Writer; dest: var TokenBuf; n: PNode; sym: PSym) = +proc writeSymNode(w: var Writer; dest: var IcBuilder; n: PNode; sym: PSym) = if sym == nil: dest.addDotToken() return @@ -631,7 +706,8 @@ proc writeSymNode(w: var Writer; dest: var TokenBuf; n: PNode; sym: PSym) = # A field reached while writing its own type's reclist MUST be a self-contained # def: the type can be seek-loaded by name in isolation, so a deduped SymUse # (whose def lives elsewhere in the entry) would resolve to nil. - let reclistField = w.lowering and w.inTypeReclist > 0 and sym.kindImpl == skField + let reclistField = w.lowering and w.inTypeReclist > 0 and sym.kindImpl == skField and + sym.itemId.module == w.currentModule let wantDef = if reclistField: true elif sym.itemId.isBackendMinted: not w.emittedBackendSyms.containsOrIncl(sym.itemId.item) @@ -657,10 +733,10 @@ proc writeSymNode(w: var Writer; dest: var TokenBuf; n: PNode; sym: PSym) = else: dest.addSymUse pool.syms.getOrIncl(w.toNifSymName(sym)), info -proc writeNodeFlags(dest: var TokenBuf; flags: set[TNodeFlag]) {.inline.} = +proc writeNodeFlags(dest: var IcBuilder; flags: set[TNodeFlag]) {.inline.} = writeFlags(dest, flags) -template withNode(w: var Writer; dest: var TokenBuf; n: PNode; body: untyped) = +template withNode(w: var Writer; dest: var IcBuilder; n: PNode; body: untyped) = dest.addParLe pool.tags.getOrIncl(toNifTag(n.kind)), trLineInfo(w, n.info) writeNodeFlags(dest, n.flags) writeType(w, dest, n.typField) @@ -788,7 +864,7 @@ proc registerNifAstTags*() = typeOfferTag = registerTag("toffer") modulesrcTag = registerTag("modulesrc") -proc writeNode(w: var Writer; dest: var TokenBuf; n: PNode; forAst = false) = +proc writeNode(w: var Writer; dest: var IcBuilder; n: PNode; forAst = false) = if n == nil: dest.addDotToken else: @@ -931,7 +1007,7 @@ proc writeNode(w: var Writer; dest: var TokenBuf; n: PNode; forAst = false) = for i in 0 ..< n.len: writeNode(w, dest, n[i], forAst) -proc writeGlobal(w: var Writer; dest: var TokenBuf; n: PNode) = +proc writeGlobal(w: var Writer; dest: var IcBuilder; n: PNode) = case n.kind of nkVarTuple: writeNode(w, dest, n) @@ -949,12 +1025,12 @@ proc writeGlobal(w: var Writer; dest: var TokenBuf; n: PNode) = else: discard -proc writeGlobals(w: var Writer; dest: var TokenBuf; n: PNode) = +proc writeGlobals(w: var Writer; dest: var IcBuilder; n: PNode) = w.withNode dest, n: for child in n: writeGlobal(w, dest, child) -proc writeToplevelNode(w: var Writer; dest, bottom: var TokenBuf; n: PNode) = +proc writeToplevelNode(w: var Writer; dest, bottom: var IcBuilder; n: PNode) = case n.kind of nkStmtList, nkStmtListExpr: for son in n: writeToplevelNode(w, dest, bottom, son) @@ -972,12 +1048,12 @@ proc writeToplevelNode(w: var Writer; dest, bottom: var TokenBuf; n: PNode) = else: writeNode w, dest, n -proc createStmtList(buf: var TokenBuf; info: PackedLineInfo) {.inline.} = +proc createStmtList(buf: var IcBuilder; info: PackedLineInfo) {.inline.} = buf.addParLe pool.tags.getOrIncl(toNifTag(nkStmtList)), info buf.addDotToken # flags buf.addDotToken # type -proc writeOp(w: var Writer; content: var TokenBuf; op: LogEntry) = +proc writeOp(w: var Writer; content: var IcBuilder; op: LogEntry) = case op.kind of HookEntry: case op.op @@ -1073,100 +1149,98 @@ proc writeOp(w: var Writer; content: var TokenBuf; op: LogEntry) = type CookieCtx = object selfSuffix: string - tdRanges: Table[SymId, int] # td sym -> start of its first (td ...) tree - memo: Table[SymId, string] # td sym -> structural digest - expanding: HashSet[SymId] # cycle guard for recursive td expansion + tdRanges: Table[uint32, int] # td sym -> start of its first (td ...) tree + memo: Table[uint32, string] # td sym -> structural digest + expanding: HashSet[uint32] # cycle guard for recursive td expansion depSuffixes: seq[string] # module suffixes of the direct imports -proc nextTree(buf: TokenBuf; i: int): int = +proc nextTree(flat: seq[CookieTok]; i: int): int = ## Index just past the atom or balanced subtree starting at `i`. result = i+1 - if buf[i].kind != ParLe: return + if flat[i].kind != ckParLe: return var nested = 0 var j = i - while j < buf.len: - case buf[j].kind - of ParLe: inc nested - of ParRi: + while j < flat.len: + case flat[j].kind + of ckParLe: inc nested + of ckParRi: dec nested if nested == 0: return j+1 else: discard inc j - result = buf.len + result = flat.len -proc updateAtom(s: var Sha1State; t: PackedToken) = +proc updateAtom(s: var Sha1State; t: CookieTok) = # mirrors nimony's nifchecksums.update: token content only, no line infos case t.kind - of ParLe: + of ckParLe: s.update "(" - s.update pool.tags[t.tagId] - of ParRi: s.update ")" - of Ident: + s.update t.tag + of ckParRi: s.update ")" + of ckIdent: s.update " " - s.update pool.strings[t.litId] - of StringLit: + s.update t.str + of ckStr: s.update " \"" - s.update pool.strings[t.litId] - of IntLit: + s.update t.str + of ckInt: s.update " " - s.update $pool.integers[t.intId] - of UIntLit: + s.update $t.ival + of ckUInt: s.update " " - s.update $pool.uintegers[t.uintId] - of FloatLit: + s.update $t.uval + of ckFloat: # hash the bit pattern, not a formatted float (no formatting variance) s.update " f" - s.update $cast[uint64](pool.floats[t.floatId]) - of CharLit: + s.update $cast[uint64](t.fval) + of ckChar: s.update " c" - s.update $t.uoperand - of DotToken: s.update "." - of UnknownToken: s.update "?" - of EofToken: s.update "!" - of Symbol, SymbolDef: discard "handled by hashRegion" + s.update $t.cval + of ckDot: s.update "." + of ckSym, ckSymDef: discard "handled by hashRegion" proc isModuleLocalName(c: CookieCtx; name: string): bool = let sn = parseSymName(name) result = sn.module.len == 0 or sn.module == c.selfSuffix -proc hashRegion(s: var Sha1State; c: var CookieCtx; buf: TokenBuf; +proc hashRegion(s: var Sha1State; c: var CookieCtx; flat: seq[CookieTok]; start, theEnd: int; skipFrom = -1; skipTo = -1; keepFirstDefLiteral = false) -proc expandTd(c: var CookieCtx; buf: TokenBuf; name: SymId): string = +proc expandTd(c: var CookieCtx; flat: seq[CookieTok]; name: uint32; nameStr: string): string = ## Structural digest of a module-local type def: hashes the `(td ...)` tree ## instead of the volatile `tK.item counter name. Memoized; cycles fall back ## to the literal name (sound — at worst a spurious cookie change). if c.memo.hasKey(name): return c.memo[name] if not c.tdRanges.hasKey(name) or c.expanding.contains(name): - return pool.syms[name] + return nameStr c.expanding.incl name let start = c.tdRanges[name] var sub = newSha1State() - hashRegion(sub, c, buf, start, nextTree(buf, start)) + hashRegion(sub, c, flat, start, nextTree(flat, start)) result = "&" & $SecureHash(sub.finalize()) c.expanding.excl name c.memo[name] = result -proc hashRegion(s: var Sha1State; c: var CookieCtx; buf: TokenBuf; +proc hashRegion(s: var Sha1State; c: var CookieCtx; flat: seq[CookieTok]; start, theEnd: int; skipFrom = -1; skipTo = -1; keepFirstDefLiteral = false) = # pass 1: assign ordinals to every symbol DEFINED in the hashed region # (params, locals, embedded type defs). The region's own top-level name # (first SymbolDef) stays literal when requested — it is what importers # reference. - var ords = initTable[SymId, int]() + var ords = initTable[uint32, int]() var first = keepFirstDefLiteral var i = start while i < theEnd: if i == skipFrom: i = skipTo continue - if buf[i].kind == SymbolDef: - let sym = buf[i].symId + if flat[i].kind == ckSymDef: + let sym = flat[i].sym if first: first = false - elif isModuleLocalName(c, pool.syms[sym]) and not ords.hasKey(sym): + elif isModuleLocalName(c, flat[i].name) and not ords.hasKey(sym): ords[sym] = ords.len inc i # pass 2: hash @@ -1176,31 +1250,31 @@ proc hashRegion(s: var Sha1State; c: var CookieCtx; buf: TokenBuf; if i == skipFrom: i = skipTo continue - let t = buf[i] - if t.kind in {Symbol, SymbolDef}: - let sym = t.symId - let name = pool.syms[sym] - s.update(if t.kind == SymbolDef: " :" else: " ") - if t.kind == SymbolDef and first: + let t = flat[i] + if t.kind in {ckSym, ckSymDef}: + let sym = t.sym + let name = t.name + s.update(if t.kind == ckSymDef: " :" else: " ") + if t.kind == ckSymDef and first: first = false s.update name elif ords.hasKey(sym): s.update "%" s.update $ords[sym] elif name.startsWith("`t") and isModuleLocalName(c, name): - s.update expandTd(c, buf, sym) + s.update expandTd(c, flat, sym, name) else: s.update name else: updateAtom s, t inc i -proc cookieSd(s: var Sha1State; c: var CookieCtx; buf: TokenBuf; start: int): int = +proc cookieSd(s: var Sha1State; c: var CookieCtx; flat: seq[CookieTok]; start: int): int = ## Contributes one `(sd ...)` subtree to the cookie; returns the index past it. - result = nextTree(buf, start) - if buf[start+1].kind != SymbolDef: return - let marker = buf[start+2] - if not (marker.kind == Ident and pool.strings[marker.litId] == "x"): + result = nextTree(flat, start) + if flat[start+1].kind != ckSymDef: return + let marker = flat[start+2] + if not (marker.kind == ckIdent and marker.str == "x"): return # not importable -> invisible to dependents' sem (nimony parity) # field layout, see writeSymDef: kind magic flags options offset position # annex type owner ast loc constraint instantiatedFrom @@ -1208,10 +1282,10 @@ proc cookieSd(s: var Sha1State; c: var CookieCtx; buf: TokenBuf; start: int): in var i = start + 3 for f in 0 ..< 13: fields[f] = i - i = nextTree(buf, i) + i = nextTree(flat, i) var kind = skUnknown {.cast(uncheckedAssign).}: - kind = parse(TSymKind, pool.tags[buf[fields[0]].tagId]) + kind = parse(TSymKind, flat[fields[0]].tag) var skipFrom = -1 var skipTo = -1 if kind in routineKinds: @@ -1225,53 +1299,53 @@ proc cookieSd(s: var Sha1State; c: var CookieCtx; buf: TokenBuf; start: int): in # nifc backend's transitive NIF-mtime invalidation re-codegens their users. # So no routine body needs to live in the iface cookie. let ast = fields[9] - if buf[ast].kind == ParLe: + if flat[ast].kind == ckParLe: # skip son `bodyPos` (6) of the routine ast tree; NOT the last element — # sem appends the result sym at `resultPos` (7) after the body. - let astEnd = nextTree(buf, ast) + let astEnd = nextTree(flat, ast) var p = ast + 1 # the flags atom var ok = true for _ in 0 ..< 2 + bodyPos: # flags, type, sons 0..5 - p = nextTree(buf, p) + p = nextTree(flat, p) if p >= astEnd - 1: ok = false break if ok: skipFrom = p - skipTo = nextTree(buf, p) + skipTo = nextTree(flat, p) # non-routine kinds (consts carry their value, types their structure incl. # default field values): hash everything. - hashRegion(s, c, buf, start, result, skipFrom, skipTo, keepFirstDefLiteral = true) + hashRegion(s, c, flat, start, result, skipFrom, skipTo, keepFirstDefLiteral = true) -proc scanStmtsForCookie(s: var Sha1State; c: var CookieCtx; buf: TokenBuf) = +proc scanStmtsForCookie(s: var Sha1State; c: var CookieCtx; flat: seq[CookieTok]) = ## Walks the whole written module, hashing only the importer-visible pieces; ## unknown structure is descended into (var/let/type section wrappers, ## top-level code) but contributes nothing itself — nimony-style. - let exportTag = pool.tags.getOrIncl(toNifTag(nkExportStmt)) - let exportExceptTag = pool.tags.getOrIncl(toNifTag(nkExportExceptStmt)) + let exportName = toNifTag(nkExportStmt) + let exportExceptName = toNifTag(nkExportExceptStmt) var i = 0 - while i < buf.len: - let t = buf[i] - if t.kind == ParLe: - let tid = t.tagId - if tid == sdefTag: - i = cookieSd(s, c, buf, i) - elif tid == implTag: - i = nextTree(buf, i) - elif tid == replayTag or tid == repConverterTag or tid == repDestroyTag or - tid == repWasMovedTag or tid == repCopyTag or tid == repSinkTag or - tid == repDupTag or tid == repTraceTag or tid == repDeepCopyTag or - tid == repEnumToStrTag or tid == repMethodTag or - tid == exportTag or tid == exportExceptTag or tid == includeTag: - let e = nextTree(buf, i) - hashRegion(s, c, buf, i, e) + while i < flat.len: + let t = flat[i] + if t.kind == ckParLe: + let tg = t.tag + if tg == symDefTagName: + i = cookieSd(s, c, flat, i) + elif tg == "implementation": + i = nextTree(flat, i) + elif tg == "replay" or tg == "repconverter" or tg == "repdestroy" or + tg == "repwasmoved" or tg == "repcopy" or tg == "repsink" or + tg == "repdup" or tg == "reptrace" or tg == "repdeepcopy" or + tg == "repenumtostr" or tg == "repmethod" or + tg == exportName or tg == exportExceptName or tg == "include": + let e = nextTree(flat, i) + hashRegion(s, c, flat, i, e) i = e - elif tid == importTag: - let e = nextTree(buf, i) - hashRegion(s, c, buf, i, e) + elif tg == "import": + let e = nextTree(flat, i) + hashRegion(s, c, flat, i, e) for j in i ..< e: - if buf[j].kind == StringLit: - let suffix = pool.strings[buf[j].litId] + if flat[j].kind == ckStr: + let suffix = flat[j].str if suffix notin c.depSuffixes: c.depSuffixes.add suffix i = e else: @@ -1288,41 +1362,43 @@ proc icGroupSuffixes(config: ConfigRef): HashSet[string] = result.incl cachedModuleSuffix(config, fileInfoIdx(config, AbsoluteFile p)) proc writeCookieFile(config: ConfigRef; selfSuffix, tag, hex, ext: string) = - var dest = createTokenBuf(4) - dest.addParLe pool.tags.getOrIncl(tag), NoLineInfo - dest.addStrLit hex - dest.addParRi + # Binary NIF cookie `(tag "hex")`, content-stable so an unchanged hash keeps the + # sidecar mtime (nifmake prunes the re-sem cascade behind it). + var b = newIcBuilder(4) + b.openTag tag + b.addStrLit hex + b.closeTag() let path = toGeneratedFile(config, AbsoluteFile(selfSuffix), ext).string - writeFile(dest, path, OnlyIfChanged) + storeBifStable(b, path, "." & extractModuleSuffix(path)) -proc writeIfaceCookie(config: ConfigRef; thisModule: int32; buf: TokenBuf): string = +proc writeIfaceCookie(config: ConfigRef; thisModule: int32; flat: seq[CookieTok]): string = let selfSuffix = modname(thisModule, config) var c = CookieCtx(selfSuffix: selfSuffix) # pre-pass: first (td ...) occurrence per type name, wherever it is embedded var i = 0 - while i < buf.len: - if buf[i].kind == ParLe and buf[i].tagId == tdefTag and i+1 < buf.len and - buf[i+1].kind == SymbolDef: - let nm = buf[i+1].symId + while i < flat.len: + if flat[i].kind == ckParLe and flat[i].tag == typeDefTagName and i+1 < flat.len and + flat[i+1].kind == ckSymDef: + let nm = flat[i+1].sym if not c.tdRanges.hasKey(nm): c.tdRanges[nm] = i inc i var s = newSha1State() - scanStmtsForCookie(s, c, buf) + scanStmtsForCookie(s, c, flat) # chain the direct deps' cookies; co-members of an --icGroup cycle are # excluded (their sidecars are being produced by this very rule — chaining # them would make the hash depend on within-group write order). let groupSuffixes = icGroupSuffixes(config) for dep in c.depSuffixes: if dep == selfSuffix or dep in groupSuffixes: continue - let depIface = toGeneratedFile(config, AbsoluteFile(dep), ".iface.nif").string + let depIface = toGeneratedFile(config, AbsoluteFile(dep), ".iface.bif").string s.update "|" s.update dep s.update ":" s.update(try: readFile(depIface) except IOError, OSError: "") result = $SecureHash(s.finalize()) - writeCookieFile(config, selfSuffix, "iface", result, ".iface.nif") + writeCookieFile(config, selfSuffix, "iface", result, ".iface.bif") -proc writeImplCookie(config: ConfigRef; thisModule: int32; buf: TokenBuf; +proc writeImplCookie(config: ConfigRef; thisModule: int32; flat: seq[CookieTok]; ifaceHex: string) = ## The implementation cookie: a line-info-free hash of the module's ENTIRE ## serialized content (private defs and routine bodies included), with the @@ -1336,16 +1412,15 @@ proc writeImplCookie(config: ConfigRef; thisModule: int32; buf: TokenBuf; ## body change is exactly what NeedsImpl dependents must see. let selfSuffix = modname(thisModule, config) var s = newSha1State() - for i in 0 ..< buf.len: - let t = buf[i] - if t.kind in {Symbol, SymbolDef}: - s.update(if t.kind == SymbolDef: " :" else: " ") - s.update pool.syms[t.symId] + for t in flat: + if t.kind in {ckSym, ckSymDef}: + s.update(if t.kind == ckSymDef: " :" else: " ") + s.update t.name else: updateAtom s, t s.update "|iface:" s.update ifaceHex - writeCookieFile(config, selfSuffix, "impl", $SecureHash(s.finalize()), ".impl.nif") + writeCookieFile(config, selfSuffix, "impl", $SecureHash(s.finalize()), ".impl.bif") proc writeEdgesFile(config: ConfigRef; thisModule: int32; implDeps: seq[int]) = ## Records which modules' bodies this compilation consumed at compile time @@ -1362,20 +1437,21 @@ proc writeEdgesFile(config: ConfigRef; thisModule: int32; implDeps: seq[int]) = continue if suffix notin suffixes: suffixes.add suffix sort suffixes - var dest = createTokenBuf(4 + 2*suffixes.len) - dest.addParLe pool.tags.getOrIncl("edges"), NoLineInfo + # Native nifcore writer (Stage 2): `(edges "suffix" ...)`, byte-identical. + var b = newIcBuilder(4 + 2*suffixes.len) + b.openTag "edges" for suffix in suffixes: - dest.addStrLit suffix - dest.addParRi - let path = toGeneratedFile(config, AbsoluteFile(selfSuffix), ".edges.nif").string + b.addStrLit suffix + b.closeTag() + let path = toGeneratedFile(config, AbsoluteFile(selfSuffix), ".edges.bif").string # Deliberately ALWAYS written (unlike every other output of the nim_m rule): # nothing gates on this file's mtime — deps.nim only reads its content — so # it doubles as the rule's freshness stamp. nifmake's `needsRebuild` takes # the freshest output as proof of "ran since the inputs changed"; without an # always-written output a rule whose re-run produces only content-identical # (mtime-preserved) files would re-fire on every warm build (e.g. after an - # edit was reverted). Nimony's analog is its always-written `.s.nif`. - writeFile(dest, path) + # edit was reverted). Nimony's analog is its always-written `.s.bif`. + storeBif(b, path, "." & extractModuleSuffix(path)) proc writeSemDeps*(config: ConfigRef; thisModule: int32; importPaths: seq[string]) = ## The module's REAL direct imports as `nim m` sem resolved them — static @@ -1384,16 +1460,11 @@ proc writeSemDeps*(config: ConfigRef; thisModule: int32; importPaths: seq[string ## missed become new nodes (replacing the old build-failure discovery loop), ## and `when false` imports the scanner over-included are pruned. Always ## written so it is current after every successful sem (like `.edges`). - let selfSuffix = modname(thisModule, config) - var paths = importPaths - sort paths - var dest = createTokenBuf(4 + 2*paths.len) - dest.addParLe pool.tags.getOrIncl("semdeps"), NoLineInfo - for p in paths: - dest.addStrLit p - dest.addParRi - let path = toGeneratedFile(config, AbsoluteFile(selfSuffix), ".s.deps.nif").string - writeFile(dest, path) + ## + ## Ported to nifcore: delegates to `icnifcore.writeSemDeps` (Stage 1 of the + ## NIF-stack migration; see doc/ic_nifcore_port.md). Output is byte-identical + ## to the previous `nifstreams` writer. + icnifcore.writeSemDeps(config, thisModule, importPaths) proc writeNifModule*(config: ConfigRef; thisModule: int32; n: PNode; opsLog: seq[LogEntry]; @@ -1406,7 +1477,8 @@ proc writeNifModule*(config: ConfigRef; thisModule: int32; n: PNode; typeOffers: seq[tuple[generic: PSym; inst: PType]] = @[]; resolvedImportDeps: seq[FileIndex] = @[]) = var w = Writer(infos: LineInfoWriter(config: config), currentModule: thisModule) - var content = createTokenBuf(300) + w.deps = newIcBuilder(64) + var content = newIcBuilder(300) let rootInfo = trLineInfo(w, n.info) createStmtList(content, rootInfo) @@ -1422,7 +1494,7 @@ proc writeNifModule*(config: ConfigRef; thisModule: int32; n: PNode; if op.module == thisModule.int: writeOp(w, content, op) - var bottom = createTokenBuf(300) + var bottom = newIcBuilder(300) w.writeToplevelNode content, bottom, n # Resolved import edges that left no syntactic `import` node in the top-level @@ -1526,19 +1598,17 @@ proc writeNifModule*(config: ConfigRef; thisModule: int32; n: PNode; # bottom of the file is the implementation of the module: content.addParLe implTag, NoLineInfo content.addParRi() - content.add bottom + addAll(content, bottom) content.addParRi() let m = modname(w.currentModule, w.infos.config) - let nifFilename = AbsoluteFile(m).changeFileExt(".s.nif") - let d = completeGeneratedFilePath(config, nifFilename).string + let bifPath = completeGeneratedFilePath(config, AbsoluteFile(m).changeFileExt(".s.bif")).string - var dest = createTokenBuf(600) + var dest = newIcBuilder(600) createStmtList(dest, rootInfo) - dest.add w.deps + addAll(dest, w.deps) # do not write the (stmts .. ) wrapper: - for i in 3 ..< content.len-1: - dest.add content[i] + addStmtsBody(dest, content) # ensure the hooks we announced end up in the NIF file regardless of # whether they have been used: @@ -1567,73 +1637,68 @@ proc writeNifModule*(config: ConfigRef; thisModule: int32; n: PNode; # mtime-based `needsRebuild` then prunes the rebuild cascade level by # level, and the nifc backend can trust "semmed NIF older than the cnif # artifact" as an honest per-module unchanged stamp. - writeFile(dest, d, OnlyIfChanged) + # Step 3: emit the compact binary NIF as the SOLE on-disk module artifact (no + # text `.s.nif` twin — writing two files per module only slows the build; debug + # a `.bif` via `tools/bif2nif`). Re-homed into a private fresh pool by `storeBif` + # so the file holds only THIS module's literals. + storeBif(dest, bifPath, "." & extractModuleSuffix(bifPath)) if not isDefined(config, "icNoIfaceGate"): - let ifaceHex = writeIfaceCookie(config, thisModule, dest) - writeImplCookie(config, thisModule, dest, ifaceHex) + var flat = flattenForCookie(dest) + let ifaceHex = writeIfaceCookie(config, thisModule, flat) + writeImplCookie(config, thisModule, flat, ifaceHex) writeEdgesFile(config, thisModule, implDeps) # --------------------------- Loader (lazy!) ----------------------------------------------- -proc nodeKind(n: Cursor): TNodeKind {.inline.} = - assert n.kind == ParLe - parse(TNodeKind, pool.tags[n.tagId]) +# Step 2b reader shims over nifcore cursors: +template info(n: Cursor): NifLineInfo = rawLineInfo(n) + ## line info of the token at `n` (was the inline `n.info` of nifcursors). +template cursorTag(n: Cursor): string = n.tags.tagName(cursorTagId(n)) + ## tag name of the TagLit at `n` — for VALUE uses only (parse into an enum, + ## error messages). For tag *checks* use `tagIs`. Resolved via the cursor's OWN + ## tag pool (`n.tags`), not the shared `icTags`: a `bif`-loaded module carries a + ## fresh per-file tag pool whose ids only line up with its own `tagName`. +template tagIs(n: Cursor; name: string): bool = n.tags.tagName(cursorTagId(n)) == name + ## True iff `n`'s TagLit is the IC tag `name`. A string compare against the + ## cursor's own tag pool — id comparison against a process-global cache is + ## impossible once `bif` mints fresh per-file tag pools (ids are per-pool). -proc expect(n: Cursor; k: set[NifKind]) = +proc nodeKind(n: Cursor): TNodeKind {.inline.} = + assert n.kind == TagLit + parse(TNodeKind, cursorTag(n)) + +proc expect(n: Cursor; k: set[nifcore.NifKind]) = if n.kind notin k: when defined(debug): writeStackTrace() - quit "[NIF decoder] expected: " & $k & " but got: " & $n.kind & toString n + quit "[NIF decoder] expected: " & $k & " but got: " & $n.kind -proc expect(n: Cursor; k: NifKind) {.inline.} = +proc expect(n: Cursor; k: nifcore.NifKind) {.inline.} = expect n, {k} -proc incExpect(n: var Cursor; k: set[NifKind]) = - inc n - expect n, k - -proc incExpect(n: var Cursor; k: NifKind) {.inline.} = - incExpect n, {k} - -proc skipParRi(n: var Cursor) = - expect n, {ParRi} - inc n - proc firstSon*(n: Cursor): Cursor {.inline.} = + ## Non-consuming peek at the first child of a TagLit. The `inc` is on a copy, + ## so it never advances the caller's cursor. result = n inc result -proc expectTag(n: Cursor; tagId: TagId) = - if n.kind == ParLe and n.tagId == tagId: - discard - else: - when defined(debug): - writeStackTrace() - if n.kind != ParLe: - quit "[NIF decoder] expected: ParLe but got: " & $n.kind & toString n - else: - quit "[NIF decoder] expected: " & pool.tags[tagId] & " but got: " & pool.tags[n.tagId] & toString n - -proc incExpectTag(n: var Cursor; tagId: TagId) = - inc n - expectTag(n, tagId) - proc loadBool(n: var Cursor): bool = - if n.kind == ParLe: - result = pool.tags[n.tagId] == "true" - inc n - skipParRi n + if n.kind == TagLit: + result = tagIs(n, "true") + n.into: + discard else: raiseAssert "(true)/(false) expected" type NifModule = ref object - stream: nifstreams.Stream + buf: TokenBuf # the WHOLE module, parsed eagerly (Step 2: replaces the + # lazy byte-offset stream entirely — symbol/type loading + # AND the body reader now cursor over this resident buffer) symCounter: int32 - index: Table[string, NifIndexEntry] # Simple embedded index for offsets + index: Table[string, NifIndexEntry] # name -> entry; `offset` is a TOKEN + # position in `buf` (was a byte offset) suffix: string - contentStart: int # stream offset of the module body, so a full-AST load can - # rewind after lazy symbol loads moved the cursor DecodeContext* = object infos: LineInfoWriter @@ -1672,63 +1737,57 @@ proc loadedState(c: DecodeContext): ItemState {.inline.} = ## must stay `Sealed` so accidental mutations are caught. if c.infos.config.cmd == cmdNifC or c.infos.config.ideActive: Complete else: Sealed -proc cursorFromIndexEntry(c: var DecodeContext; module: FileIndex; entry: NifIndexEntry; - buf: var TokenBuf): Cursor = - let s = addr c.mods[module].stream - s.r.jumpTo entry.offset - # A seek-load is self-contained: its tokens must decode their relative line - # info against `entry.info` ALONE. The stream's `parents` stack can be left at - # depth >1 by a prior non-seek read (e.g. loadNifModule reads `(stmts`/ - # `(implementation` without consuming their `)`), and `parse` only overwrites - # parents[0] while `rawNext` reads parents[^1] — so a stale top entry (the last - # symbol decoded, e.g. excpt.nim:746) would become the base. This is benign on - # a freshly-opened stream (cold/stdin) but corrupts EPC recompiles, which reuse - # the per-module stream (the global DecodeContext survives resetAllModules). - # Collapse the stack so parse's parentInfo is the sole base. - s[].parents.setLen 1 - nifcursors.parse(s[], buf, entry.info) - result = cursorAt(buf, 0) +proc cursorFromIndexEntry(c: var DecodeContext; module: FileIndex; entry: NifIndexEntry): Cursor = + ## Step 2a: O(1) cursor into the module's resident `buf` at the def's token + ## position. No I/O, no per-symbol materialization — and, because each call + ## returns an INDEPENDENT cursor, none of the old stream-cursor clobber hazards + ## (the `jumpTo(saved)` save/restore dance) apply anymore. + result = cursorAt(c.mods[module].buf, entry.offset) type LoadFlag* = enum LoadFullAst, AlwaysLoadInterface -proc readEmbeddedIndex(s: var Stream): Table[string, NifIndexEntry] = - ## Reads the simple embedded index (index (kv sym offset)...) from indexStartsAt position. +proc isGlobalIndexSym(s, dottedSuffix: string): bool = + ## Mirror of `nifbuilder.addSymbolDefRetIsGlobal` / `bif.isGlobalSymbol`: a sym + ## gets an index entry when its name — with a self-module `dottedSuffix` + ## compressed to one trailing dot — has >= 2 dots (counting from index 1). + var lim = s.len + if dottedSuffix.len > 0 and s.endsWith(dottedSuffix): + lim = s.len - dottedSuffix.len + 1 + if lim > s.len: lim = s.len + var dots = 0 + for i in 1 ..< lim: + if s[i] == '.': inc dots + dots >= 2 + +proc buildPosIndex(buf: var TokenBuf; suffix: string): Table[string, NifIndexEntry] = + ## Step 2a token-position index: scan the eagerly-parsed module `buf` for the + ## global `SymbolDef`s it OWNS and record each at the token position of its + ## enclosing tag (`(sd`/`(td`), with visibility from the marker that follows + ## the def. Replaces `readEmbeddedIndex` (whose byte offsets are meaningless + ## once the file is parsed); mirrors `bif.buildIndex` and the text writer's + ## `(.index …)`. Foreign symbols appear only as `Symbol` uses (never + ## `SymbolDef`s) so they are naturally excluded. result = initTable[string, NifIndexEntry]() - let indexPos = indexStartsAt(s.r) - if indexPos <= 0: - return - let contentPos = offset(s.r) # Save position - s.r.jumpTo(indexPos) - - var previousOffset = 0 - var t = next(s) - let exportedTagId = pool.tags.getOrIncl("x") - if t.kind == ParLe and pool.tags[t.tagId] == ".index": - t = next(s) - while t.kind != EofToken and t.kind != ParRi: - if t.kind == ParLe: - let vis = if t.tagId == exportedTagId: Exported else: Hidden - let info = t.info - t = next(s) # skip (kv - var key = "" - if t.kind == Symbol: - key = pool.syms[t.symId] - elif t.kind == Ident: - key = pool.strings[t.litId] - t = next(s) # skip symbol - if t.kind == IntLit: - let offset = int(pool.integers[t.intId]) + previousOffset - result[key] = NifIndexEntry(offset: offset, info: info, vis: vis) - previousOffset = offset - t = next(s) # skip offset - if t.kind == ParRi: - t = next(s) # skip ) - else: - t = next(s) - - s.r.jumpTo(contentPos) # Restore position + let dotted = "." & suffix + if buf.len == 0: return + var c = buf.beginRead() + var mostRecentTagPos = 0 + while c.hasMore: + case c.kind + of TagLit: + mostRecentTagPos = cursorToPosition(buf, c) + inc c # descend into the body (visit every token) + of SymbolDef: + let nm = symName(c) + let tagPos = mostRecentTagPos + inc c # advance to the marker / next sibling + if isGlobalIndexSym(nm, dotted): + let vis = if c.hasMore and c.kind == DotToken: Hidden else: Exported + result[nm] = NifIndexEntry(offset: tagPos, info: NoLineInfo, vis: vis) + else: + inc c proc moduleId(c: var DecodeContext; suffix: string; flags: set[LoadFlag] = {}): FileIndex = var isKnownFile = false @@ -1738,17 +1797,27 @@ proc moduleId(c: var DecodeContext; suffix: string; flags: set[LoadFlag] = {}): # but haven't had their NIF index loaded yet let hasEntry = c.mods.hasKey(result) if not hasEntry or AlwaysLoadInterface in flags: - let modFile = (getNimcacheDir(c.infos.config) / RelativeFile(suffix & ".s.nif")).string + # Module artifacts are binary NIF (`.bif`). The `cg`/`emit` backend stages + # load the LOWERED whole-module `.t.bif` (transformed bodies + lambda-lifted + # signatures/entities baked in by the `lower` stage — see writeLoweredModule); + # the `lower` stage and the frontend (`cmdM`) load the semchecked `.s.bif`. + # This mirrors `toNifFilename` (kept in sync). `bif.load` mints FRESH per-file + # pools, so the buffer's literals/tags resolve through its own + # `cursorPool(n)`/`n.tags` (the reader is pool-agnostic); the token-position + # index is rebuilt name-based via `buildPosIndex`. + let conf = c.infos.config + let useLowered = conf.cmd == cmdNifC and + (conf.icBackendStage == "cg" or conf.icBackendStage == "emit") + var modFile = (getNimcacheDir(conf) / RelativeFile(suffix & ".t.bif")).string + if not (useLowered and fileExists(modFile)): + modFile = (getNimcacheDir(conf) / RelativeFile(suffix & ".s.bif")).string if not fileExists(modFile): raiseAssert "NIF file not found for module suffix '" & suffix & "': " & modFile & ". This can happen when loading a module from NIF that references another module " & "whose NIF file hasn't been written yet." - var stream = nifstreams.open(modFile) - let index = readEmbeddedIndex(stream) - # `readEmbeddedIndex` leaves the cursor at the start of the module body. - let contentStart = offset(stream.r) - c.mods[result] = NifModule(stream: stream, index: index, suffix: suffix, - contentStart: contentStart) + var m = bif.load(modFile) + let index = buildPosIndex(m.buf, suffix) + c.mods[result] = NifModule(buf: ensureMove m.buf, index: index, suffix: suffix) proc getOffset(c: var DecodeContext; module: FileIndex; nifName: string): NifIndexEntry = let ii = addr c.mods[module].index @@ -1775,10 +1844,11 @@ proc reconstructSysType(c: var DecodeContext; name: string; k: int; itemVal: int result.alignImpl = int16 c.infos.config.target.ptrSize c.types[name] = (result, NifIndexEntry()) -proc tryCreateTypeStub(c: var DecodeContext; t: SymId): PType = +proc tryCreateTypeStub(c: var DecodeContext; name: string): PType = ## Like `createTypeStub` but returns nil instead of raising when the type has ## no offset in its module index (used by the best-effort `(offer …)` loader). - let name = pool.syms[t] + ## Step 2b: takes the sym NAME string (pool-agnostic) — the reader never juggles + ## a nifcore/nifstreams `SymId`. if not name.startsWith("`t"): return nil result = c.types.getOrDefault(name)[0] if result == nil: @@ -1807,8 +1877,7 @@ proc tryCreateTypeStub(c: var DecodeContext; t: SymId): PType = result = PType(itemId: id, uniqueId: id, kind: TTypeKind(k), state: Partial) c.types[name] = (result, offs) -proc createTypeStub(c: var DecodeContext; t: SymId): PType = - let name = pool.syms[t] +proc createTypeStub(c: var DecodeContext; name: string): PType = assert name.startsWith("`t") result = c.types.getOrDefault(name)[0] if result == nil: @@ -1843,57 +1912,46 @@ proc extractLocalSymsFromTree(c: var DecodeContext; n: var Cursor; thisModule: s ## Scan a tree for local symbol definitions (sdef tags) and add them to localSyms. ## For local symbols, fully load them immediately since they have no index offsets. ## After this proc returns, n is positioned AFTER the tree. - # Handle atoms (non-compound nodes) - just skip them - if n.kind != ParLe: - inc n + # Atoms (non-compound nodes): nothing to scan, just skip past them. + if n.kind != TagLit: + skip n return - var depth = 0 - while true: - if n.kind == ParLe: - if n.tagId == sdefTag: - # Found an sdef - check if it's local - let name = n.firstSon - expect name, SymbolDef - let symName = pool.syms[name.symId] - let sn = parseSymName(symName) - if sn.module.len == 0 and symName notin localSyms: - # Local symbol - create stub and immediately load it fully - # since local symbols have no index offsets for lazy loading - let module = moduleId(c, thisModule) - let val = addr c.mods[module].symCounter - inc val[] - let id = itemId(module.int32, val[]) - let sym = PSym(itemId: id, kindImpl: skStub, name: c.cache.getIdent(sn.name), - disamb: sn.count.int32, state: Complete) - localSyms[symName] = sym - # Load the full symbol definition immediately - # We're currently at the `(sd` position, need to skip to SymbolDef - inc n # skip past `sd` tag to get to SymbolDef - loadSymFromCursor(c, sym, n, thisModule, localSyms) - sym.state = c.loadedState # mark as fully loaded - # Continue processing - loadSymFromCursor already advanced n past the closing `)` - continue - inc depth - elif n.kind == ParRi: - dec depth - if depth == 0: - inc n # Move PAST the closing ) - break - inc n + if tagIs(n, symDefTagName): + # Found an sdef - check if it's a new local symbol. + let name = n.firstSon + expect name, SymbolDef + let symName = symName(name) + let sn = parseSymName(symName) + if sn.module.len == 0 and symName notin localSyms: + # Local symbol - create stub and immediately load it fully + # since local symbols have no index offsets for lazy loading + let module = moduleId(c, thisModule) + let val = addr c.mods[module].symCounter + inc val[] + let id = itemId(module.int32, val[]) + let sym = PSym(itemId: id, kindImpl: skStub, name: c.cache.getIdent(sn.name), + disamb: sn.count.int32, state: Complete) + localSyms[symName] = sym + # `loadSymFromCursor` enters the `(sd` and consumes the whole block, + # leaving n positioned after the closing `)`. + loadSymFromCursor(c, sym, n, thisModule, localSyms) + sym.state = c.loadedState # mark as fully loaded + return + # Otherwise descend into every child, scanning each for nested local sdefs. + n.loopInto: + extractLocalSymsFromTree(c, n, thisModule, localSyms) proc loadTypeFromCursor(c: var DecodeContext; n: var Cursor; t: PType; localSyms: var Table[string, PSym]) proc loadTypeStub(c: var DecodeContext; n: var Cursor; localSyms: var Table[string, PSym]): PType = if n.kind == DotToken: result = nil - inc n + skip n elif n.kind == Symbol: - let s = n.symId - result = createTypeStub(c, s) - inc n - elif n.kind == ParLe and n.tagId == tdefTag: - let s = n.firstSon.symId - result = createTypeStub(c, s) + result = createTypeStub(c, symName(n)) + skip n + elif n.kind == TagLit and tagIs(n, typeDefTagName): + result = createTypeStub(c, symName(n.firstSon)) if result.state == Partial: result.state = c.loadedState # Mark as loaded to prevent loadType from re-loading with empty localSyms loadTypeFromCursor(c, n, result, localSyms) @@ -1902,9 +1960,8 @@ proc loadTypeStub(c: var DecodeContext; n: var Cursor; localSyms: var Table[stri else: raiseAssert "type expected but got " & $n.kind -proc loadSymStub(c: var DecodeContext; t: SymId; thisModule: string; +proc loadSymStub(c: var DecodeContext; symAsStr: string; thisModule: string; localSyms: var Table[string, PSym]): PSym = - let symAsStr = pool.syms[t] let sn = parseSymName(symAsStr) # For local symbols (no module suffix), they MUST be in localSyms. # Local symbols are not in the index - they're defined inline in the NIF file. @@ -1949,18 +2006,17 @@ proc loadSymStub(c: var DecodeContext; n: var Cursor; thisModule: string; localSyms: var Table[string, PSym]): PSym = if n.kind == DotToken: result = nil - inc n + skip n elif n.kind == Symbol: - let s = n.symId - result = loadSymStub(c, s, thisModule, localSyms) - inc n - elif n.kind == ParLe and n.tagId == sdefTag: - let s = n.firstSon.symId + result = loadSymStub(c, symName(n), thisModule, localSyms) + skip n + elif n.kind == TagLit and tagIs(n, symDefTagName): + let s = symName(n.firstSon) skip n result = loadSymStub(c, s, thisModule, localSyms) else: raiseAssert "sym expected but got " & $n.kind & ( - if n.kind == Ident: " '" & pool.strings[n.litId] & "'" else: "") + if n.kind == Ident: " '" & strVal(n) & "'" else: "") proc isStub*(t: PType): bool {.inline.} = t.state == Partial proc isStub*(s: PSym): bool {.inline.} = s.state == Partial @@ -1968,30 +2024,30 @@ proc isStub*(s: PSym): bool {.inline.} = s.state == Partial proc loadAtom[T](t: typedesc[set[T]]; n: var Cursor): set[T] = if n.kind == DotToken: result = {} - inc n + skip n else: expect n, Ident - result = parse(T, pool.strings[n.litId]) - inc n + result = parse(T, strVal(n)) + skip n proc loadAtom[T: enum](t: typedesc[T]; n: var Cursor): T = if n.kind == DotToken: result = default(T) - inc n + skip n else: expect n, Ident - result = parse(T, pool.strings[n.litId]) - inc n + result = parse(T, strVal(n)) + skip n proc loadAtom(t: typedesc[string]; n: var Cursor): string = - expect n, StringLit - result = pool.strings[n.litId] - inc n + expect n, StrLit + result = strVal(n) + skip n proc loadAtom[T: int16|int32|int64](t: typedesc[T]; n: var Cursor): T = expect n, IntLit - result = pool.integers[n.intId].T - inc n + result = intVal(n).T + skip n template loadField(field) {.dirty.} = field = loadAtom(typeof(field), n) @@ -2003,12 +2059,12 @@ proc loadLoc(c: var DecodeContext; n: var Cursor; loc: var TLoc) = loadField loc.snippet proc loadTypeFromCursor(c: var DecodeContext; n: var Cursor; t: PType; localSyms: var Table[string, PSym]) = - expect n, ParLe - if n.tagId != tdefTag: + expect n, TagLit + if not tagIs(n, typeDefTagName): raiseAssert "(td) expected" var scanCursor = n # copy cursor at start of type - var typesModule = parseSymName(pool.syms[n.firstSon.symId]).module + var typesModule = parseSymName(symName(n.firstSon)).module if typesModule.endsWith(BackendLocalMarker): # A backend-minted (`@bk`) type's name carries the marker in its module part; # strip it so the nested-local pre-scan resolves the real module, not a @@ -2016,42 +2072,39 @@ proc loadTypeFromCursor(c: var DecodeContext; n: var Cursor; t: PType; localSyms typesModule = typesModule[0 ..< typesModule.len - BackendLocalMarker.len] extractLocalSymsFromTree(c, scanCursor, typesModule, localSyms) - inc n # move past (td - expect n, SymbolDef - # ignore the type's name, we have already used it to create this PType's itemId! - inc n - expect n, DotToken - inc n - #loadField t.kind - loadField t.flagsImpl - loadField t.callConvImpl - loadField t.sizeImpl - loadField t.alignImpl - loadField t.paddingAtEndImpl - t.itemId = itemId(t.itemId.module, loadAtom(int32, n)) # nonUniqueId - if n.kind == StringLit: - # itemId.module differs from uniqueId.module (an `exactReplica` of a - # foreign type): restore the canonical module half - t.itemId = itemId(int32(moduleId(c, pool.strings[n.litId])), t.itemId.item) - inc n - elif n.kind == DotToken: - inc n + n.into: # enter (td, body consumes all children, closing ) is consumed by `into` + expect n, SymbolDef + # ignore the type's name, we have already used it to create this PType's itemId! + skip n + expect n, DotToken + skip n + #loadField t.kind + loadField t.flagsImpl + loadField t.callConvImpl + loadField t.sizeImpl + loadField t.alignImpl + loadField t.paddingAtEndImpl + t.itemId = itemId(t.itemId.module, loadAtom(int32, n)) # nonUniqueId + if n.kind == StrLit: + # itemId.module differs from uniqueId.module (an `exactReplica` of a + # foreign type): restore the canonical module half + t.itemId = itemId(int32(moduleId(c, strVal(n))), t.itemId.item) + skip n + elif n.kind == DotToken: + skip n - t.typeInstImpl = loadTypeStub(c, n, localSyms) - t.nImpl = loadNode(c, n, typesModule, localSyms) - t.ownerFieldImpl = loadSymStub(c, n, typesModule, localSyms) - t.symImpl = loadSymStub(c, n, typesModule, localSyms) - loadLoc c, n, t.locImpl + t.typeInstImpl = loadTypeStub(c, n, localSyms) + t.nImpl = loadNode(c, n, typesModule, localSyms) + t.ownerFieldImpl = loadSymStub(c, n, typesModule, localSyms) + t.symImpl = loadSymStub(c, n, typesModule, localSyms) + loadLoc c, n, t.locImpl - while n.kind != ParRi: - t.sonsImpl.add loadTypeStub(c, n, localSyms) - - skipParRi n + while n.hasMore: + t.sonsImpl.add loadTypeStub(c, n, localSyms) proc loadType*(c: var DecodeContext; t: PType) = if t.state != Partial: return t.state = c.loadedState - var buf = createTokenBuf(30) # A backend-minted (`@bk`) closure-env type produced by the `lower` stage lives # ONLY in the `.t.nif` and is keyed by its `@bk` name (see nifTypeName), not the # canonical `typeToNifSym` (which asserts non-`@bk`). Reconstruct that name so a @@ -2063,112 +2116,119 @@ proc loadType*(c: var DecodeContext; t: PType) = else: typeToNifSym(t, c.infos.config) let modFi = t.itemId.module.FileIndex - var n = cursorFromIndexEntry(c, modFi, c.types[typeName][1], buf) + var n = cursorFromIndexEntry(c, modFi, c.types[typeName][1]) var localSyms = initTable[string, PSym]() loadTypeFromCursor(c, n, t, localSyms) proc loadAnnex(c: var DecodeContext; n: var Cursor; thisModule: string; localSyms: var Table[string, PSym]): PLib = if n.kind == DotToken: result = nil - inc n - elif n.kind == ParLe: - result = PLib(kind: parse(TLibKind, pool.tags[n.tagId])) - inc n - result.generated = loadBool(n) - result.isOverridden = loadBool(n) - expect n, StringLit - result.name = pool.strings[n.litId] - inc n - result.path = loadNode(c, n, thisModule, localSyms) - skipParRi n + skip n + elif n.kind == TagLit: + result = PLib(kind: parse(TLibKind, cursorTag(n))) + n.into: + result.generated = loadBool(n) + result.isOverridden = loadBool(n) + expect n, StrLit + result.name = strVal(n) + skip n + result.path = loadNode(c, n, thisModule, localSyms) else: raiseAssert "`lib/annex` information expected" proc loadSymFromCursor(c: var DecodeContext; s: PSym; n: var Cursor; thisModule: string; localSyms: var Table[string, PSym]) = - ## Loads a symbol definition from the current cursor position. - ## The cursor should be positioned after the opening (sd tag. - expect n, SymbolDef - # ignore the symbol's name, we have already used it to create this PSym instance! - inc n - if n.kind == Ident: - if pool.strings[n.litId] == "x": - s.flagsImpl.incl sfExported - inc n - else: - raiseAssert "expected `x` as the export marker" - elif n.kind == DotToken: - inc n - else: - raiseAssert "expected `x` or '.' but got " & $n.kind - - expect n, ParLe - {.cast(uncheckedAssign).}: - s.kindImpl = parse(TSymKind, pool.tags[n.tagId]) - inc n - - if s.kindImpl == skPackage and s.name.s.endsWith(PkgMarker): - # Fallback: stubs are normally created with the clean name already - # (see stubKindAndName); strip the NIF-only marker if one slipped through. - s.name = c.cache.getIdent(s.name.s[0 ..< s.name.s.len - PkgMarker.len]) - - case s.kindImpl - of skLet, skVar, skField, skForVar: - s.guardImpl = loadSymStub(c, n, thisModule, localSyms) - loadField s.bitsizeImpl - loadField s.alignmentImpl - else: - discard - skipParRi n - - loadField s.magicImpl - loadField s.flagsImpl - loadField s.optionsImpl - loadField s.offsetImpl - - if s.kindImpl == skModule: - expect n, DotToken - inc n - var isKnownFile = false - s.positionImpl = int c.infos.config.registerNifSuffix(thisModule, isKnownFile) - # do to the precompiled mechanism things end up as main modules which are not! - excl s.flagsImpl, sfMainModule - else: - loadField s.positionImpl - - s.annexImpl = loadAnnex(c, n, thisModule, localSyms) - - # Local symbols were already extracted upfront in loadSym, so we can use - # the simple loadTypeStub here. - s.typImpl = loadTypeStub(c, n, localSyms) - s.ownerFieldImpl = loadSymStub(c, n, thisModule, localSyms) - # Load the AST for routine symbols and constants - # Constants need their AST for astdef() to return the constant's value - s.astImpl = loadNode(c, n, thisModule, localSyms) - loadLoc c, n, s.locImpl - s.constraintImpl = loadNode(c, n, thisModule, localSyms) - s.instantiatedFromImpl = loadSymStub(c, n, thisModule, localSyms) - # The TRANSFORMED body slot (see writeSymDef). Reconstruct it ONLY in the - # backend (`cmdNifC`), where `transformBody` short-circuits on it; during - # frontend sem (`cmdM`) skip the tokens — a dependent never needs a foreign - # routine's lowered body, and reconstructing one must not perturb effect/ - # exception inference (the "never change frontend node-typing for IC" rule). - if c.infos.config.cmd == cmdNifC and s.kindImpl in routineKinds: - s.transformedBodyImpl = loadNode(c, n, thisModule, localSyms) - else: + ## Loads a symbol definition. The cursor must be positioned AT the opening + ## `(sd` TagLit; `into` consumes the whole sdef including its closing `)`. + n.into: + expect n, SymbolDef + # ignore the symbol's name, we have already used it to create this PSym instance! skip n - skipParRi n + if n.kind == Ident: + if strVal(n) == "x": + s.flagsImpl.incl sfExported + skip n + else: + raiseAssert "expected `x` as the export marker" + elif n.kind == DotToken: + skip n + else: + raiseAssert "expected `x` or '.' but got " & $n.kind + + expect n, TagLit + {.cast(uncheckedAssign).}: + s.kindImpl = parse(TSymKind, cursorTag(n)) + + if s.kindImpl == skPackage and s.name.s.endsWith(PkgMarker): + # Fallback: stubs are normally created with the clean name already + # (see stubKindAndName); strip the NIF-only marker if one slipped through. + s.name = c.cache.getIdent(s.name.s[0 ..< s.name.s.len - PkgMarker.len]) + + n.into: # the (kind ...) sub-block + case s.kindImpl + of skLet, skVar, skField, skForVar: + s.guardImpl = loadSymStub(c, n, thisModule, localSyms) + loadField s.bitsizeImpl + loadField s.alignmentImpl + else: + discard + + loadField s.magicImpl + loadField s.flagsImpl + loadField s.optionsImpl + loadField s.offsetImpl + + if s.kindImpl == skModule: + expect n, DotToken + skip n + var isKnownFile = false + s.positionImpl = int c.infos.config.registerNifSuffix(thisModule, isKnownFile) + # do to the precompiled mechanism things end up as main modules which are not! + excl s.flagsImpl, sfMainModule + else: + loadField s.positionImpl + + s.annexImpl = loadAnnex(c, n, thisModule, localSyms) + + # Local symbols were already extracted upfront in loadSym, so we can use + # the simple loadTypeStub here. + s.typImpl = loadTypeStub(c, n, localSyms) + s.ownerFieldImpl = loadSymStub(c, n, thisModule, localSyms) + # Load the AST for routine symbols and constants + # Constants need their AST for astdef() to return the constant's value + s.astImpl = loadNode(c, n, thisModule, localSyms) + loadLoc c, n, s.locImpl + s.constraintImpl = loadNode(c, n, thisModule, localSyms) + s.instantiatedFromImpl = loadSymStub(c, n, thisModule, localSyms) + # The TRANSFORMED body slot (see writeSymDef). It means DIFFERENT things by + # which file `moduleId` loaded (see toNifFilename): + # * `cg`/`emit` read `.t.bif` — the slot is the `lower` stage's AUTHORITATIVE + # lowered body; ALWAYS load it so `transformBody` short-circuits and the + # backend NEVER re-derives (the whole point of the artifact). + # * the `lower` stage reads `.s.bif` — the slot is the VM/CT lowering sem + # cached; load it only when REUSE is on (`icReuseSemLowering`), else leave + # `transformedBody` nil so the `lower` stage re-derives from the pristine + # body (the 2026-06-27 simplicity spec, doc/ic_backend_simplify.md §6). + # * frontend `cmdM` never loads it (a dependent needs no foreign lowered body, + # and reconstructing one must not perturb effect/exception inference). + let conf = c.infos.config + let loadSlot = s.kindImpl in routineKinds and conf.cmd == cmdNifC and + (conf.icBackendStage == "cg" or conf.icBackendStage == "emit" or + (conf.icBackendStage == "lower" and icReuseSemLowering(conf))) + if loadSlot: + s.transformedBodyImpl = loadNode(c, n, thisModule, localSyms) + else: + skip n proc loadSym*(c: var DecodeContext; s: PSym) = if s.state != Partial: return s.state = c.loadedState - var buf = createTokenBuf(30) let symsModule = s.itemId.module.FileIndex let nifname = globalName(s, c.infos.config) - var n = cursorFromIndexEntry(c, symsModule, c.syms[nifname][1], buf) + var n = cursorFromIndexEntry(c, symsModule, c.syms[nifname][1]) - expect n, ParLe - if n.tagId != sdefTag: + expect n, TagLit + if not tagIs(n, symDefTagName): raiseAssert "(sd) expected" # Pre-scan the ENTIRE symbol definition to extract ALL local symbols upfront. @@ -2179,15 +2239,15 @@ proc loadSym*(c: var DecodeContext; s: PSym) = extractLocalSymsFromTree(c, scanCursor, c.mods[symsModule].suffix, localSyms) # Now parse the symbol definition with all local symbols pre-registered - s.infoImpl = c.infos.oldLineInfo(n.info) + s.infoImpl = c.infos.oldLineInfo(n.info, cursorPool(n)) # The `##` doc comment (if any) rides as a NIF comment on the sym def token; # capture it before advancing, then restore it onto the loaded AST so that # suggest's `extractDocComment` (findDocComment on `s.ast`) finds it. - let docId = unpack(pool.man, n.info).comment - inc n + let docId = rawLineInfo(n).comment # nifcore StrId of the `#..#` doc comment + let docPool = cursorPool(n) # the buffer's own strings pool (shared or bif-fresh) loadSymFromCursor(c, s, n, c.mods[symsModule].suffix, localSyms) - if docId != 0'u32 and s.astImpl != nil and nodeCommentWriter != nil: - nodeCommentWriter(s.astImpl, pool.strings[StrId(docId)]) + if uint32(docId) != 0'u32 and s.astImpl != nil and nodeCommentWriter != nil: + nodeCommentWriter(s.astImpl, docPool.strings[docId]) proc sealLoadedRoutines*(c: var DecodeContext) = ## Before `writeLoweredModule` re-serializes the lowered module, seal ONLY the @@ -2203,62 +2263,60 @@ proc sealLoadedRoutines*(c: var DecodeContext) = if v[0] != nil and v[0].state == Complete and v[0].kindImpl in routineKinds: v[0].state = Sealed -proc resolveHookSym*(c: var DecodeContext; symId: nifstreams.SymId): PSym +proc resolveHookSym*(c: var DecodeContext; name: string): PSym template withNode(c: var DecodeContext; n: var Cursor; result: PNode; kind: TNodeKind; body: untyped) = - let info = c.infos.oldLineInfo(n.info) - inc n - let flags = loadAtom(TNodeFlags, n) + let info = c.infos.oldLineInfo(n.info, cursorPool(n)) result = newNodeI(kind, info) - result.flags = flags - result.typField = c.loadTypeStub(n, localSyms) - body - skipParRi n + n.into: + result.flags = loadAtom(TNodeFlags, n) + result.typField = c.loadTypeStub(n, localSyms) + body proc loadNode(c: var DecodeContext; n: var Cursor; thisModule: string; localSyms: var Table[string, PSym]): PNode = result = nil case n.kind of Symbol: - let info = c.infos.oldLineInfo(n.info) - let symName = pool.syms[n.symId] + let info = c.infos.oldLineInfo(n.info, cursorPool(n)) + let symName = symName(n) # Check local symbols first let localSym = localSyms.getOrDefault(symName) if localSym != nil: result = newSymNode(localSym, info) - inc n + skip n else: result = newSymNode(c.loadSymStub(n, thisModule, localSyms), info) if result.typField == nil: result.flags.incl nfLazyType of DotToken: result = nil - inc n - of StringLit: - result = newStrNode(pool.strings[n.litId], c.infos.oldLineInfo(n.info)) - inc n - of ParLe: + skip n + of StrLit: + result = newStrNode(strVal(n), c.infos.oldLineInfo(n.info, cursorPool(n))) + skip n + of TagLit: let kind = n.nodeKind case kind of nkNone: # special NIF introduced tag? - case pool.tags[n.tagId] - of hiddenTypeTagName: - inc n - let typ = c.loadTypeStub(n, localSyms) - let info = c.infos.oldLineInfo(n.info) - result = newSymNode(c.loadSymStub(n, thisModule, localSyms), info) - result.typField = typ - skipParRi n - of symDefTagName: - let info = c.infos.oldLineInfo(n.info) + if tagIs(n, hiddenTypeTagName): + n.into: + let typ = c.loadTypeStub(n, localSyms) + let info = c.infos.oldLineInfo(n.info, cursorPool(n)) + result = newSymNode(c.loadSymStub(n, thisModule, localSyms), info) + result.typField = typ + elif tagIs(n, symDefTagName): + let info = c.infos.oldLineInfo(n.info, cursorPool(n)) let name = n.firstSon assert name.kind == SymbolDef - let symName = pool.syms[name.symId] + let symName = symName(name) # Check if this is a local symbol (no module suffix in name) let sn = parseSymName(symName) let isLocal = sn.module.len == 0 var sym: PSym + # In every branch below `n` stays at the `(sd` TagLit; `loadSymFromCursor` + # enters and consumes the whole block, and `skip n` consumes it wholesale. if isLocal: # Local symbol - not in the index, defined inline in NIF. # Check if we already have a stub from extractLocalSymsFromType @@ -2273,7 +2331,6 @@ proc loadNode(c: var DecodeContext; n: var Cursor; thisModule: string; disamb: sn.count.int32, state: Complete) localSyms[symName] = sym # register for later references # Now fully load the symbol from the sdef - inc n # skip `sd` tag loadSymFromCursor(c, sym, n, thisModule, localSyms) sym.state = c.loadedState # mark as fully loaded result = newSymNode(sym, info) @@ -2282,10 +2339,9 @@ proc loadNode(c: var DecodeContext; n: var Cursor; thisModule: string; # (not in any module index): create/find its cached stub and FILL it # from the sdef instead of skipping (which would leave the skModule/ # Partial stub `loadSymStub` made unresolved). - sym = c.loadSymStub(name.symId, thisModule, localSyms) + sym = c.loadSymStub(symName, thisModule, localSyms) if sym.state == Partial: sym.state = c.loadedState - inc n # skip `sd` tag loadSymFromCursor(c, sym, n, thisModule, localSyms) else: skip n @@ -2303,7 +2359,7 @@ proc loadNode(c: var DecodeContext; n: var Cursor; thisModule: string; let m = moduleId(c, sn.module) let indexed = c.mods[m].index.hasKey(symName) if indexed: - sym = c.loadSymStub(name.symId, thisModule, localSyms) + sym = c.loadSymStub(symName, thisModule, localSyms) skip n # skip the entire sdef for indexed symbols else: sym = c.syms.getOrDefault(symName)[0] @@ -2315,86 +2371,81 @@ proc loadNode(c: var DecodeContext; n: var Cursor; thisModule: string; state: Partial) c.syms[symName] = (sym, NifIndexEntry()) sym.state = c.loadedState - inc n # skip `sd` tag loadSymFromCursor(c, sym, n, thisModule, localSyms) result = newSymNode(sym, info) result.flags.incl nfLazyType - of typeDefTagName: + elif tagIs(n, typeDefTagName): raiseAssert "`td` tag in invalid context" - of "none": - result = newNodeI(nkNone, c.infos.oldLineInfo(n.info)) - inc n - result.flags = loadAtom(TNodeFlags, n) - skipParRi n + elif tagIs(n, "none"): + result = newNodeI(nkNone, c.infos.oldLineInfo(n.info, cursorPool(n))) + n.into: + result.flags = loadAtom(TNodeFlags, n) else: - raiseAssert "Unknown NIF tag " & pool.tags[n.tagId] + raiseAssert "Unknown NIF tag " & cursorTag(n) of nkEmpty: - result = newNodeI(nkEmpty, c.infos.oldLineInfo(n.info)) - inc n - if n.kind != ParRi: - result.flags = loadAtom(TNodeFlags, n) - result.typField = c.loadTypeStub(n, localSyms) - skipParRi n + result = newNodeI(nkEmpty, c.infos.oldLineInfo(n.info, cursorPool(n))) + n.into: + if n.hasMore: + result.flags = loadAtom(TNodeFlags, n) + result.typField = c.loadTypeStub(n, localSyms) of nkIdent: - let info = c.infos.oldLineInfo(n.info) - inc n - let flags = loadAtom(TNodeFlags, n) - let typ = c.loadTypeStub(n, localSyms) - expect n, Ident - result = newIdentNode(c.cache.getIdent(pool.strings[n.litId]), info) - inc n - result.flags = flags - result.typField = typ - skipParRi n + let info = c.infos.oldLineInfo(n.info, cursorPool(n)) + n.into: + let flags = loadAtom(TNodeFlags, n) + let typ = c.loadTypeStub(n, localSyms) + expect n, Ident + result = newIdentNode(c.cache.getIdent(strVal(n)), info) + skip n + result.flags = flags + result.typField = typ of nkSym: - #let info = c.infos.oldLineInfo(n.info) + #let info = c.infos.oldLineInfo(n.info, cursorPool(n)) #result = newSymNode(c.loadSymStub n, info) raiseAssert "nkSym should be mapped to a NIF symbol, not a tag" of nkCharLit: c.withNode n, result, kind: expect n, CharLit result.intVal = n.charLit.int - inc n + skip n of nkIntLit .. nkInt64Lit: c.withNode n, result, kind: expect n, IntLit - result.intVal = pool.integers[n.intId] - inc n + result.intVal = intVal(n) + skip n of nkUIntLit .. nkUInt64Lit: c.withNode n, result, kind: expect n, UIntLit - result.intVal = cast[BiggestInt](pool.uintegers[n.uintId]) - inc n + result.intVal = cast[BiggestInt](uintVal(n)) + skip n of nkFloatLit .. nkFloat128Lit: c.withNode n, result, kind: if n.kind == FloatLit: - result.floatVal = pool.floats[n.floatId] - inc n - elif n.kind == ParLe: - case pool.tags[n.tagId] - of "inf": + result.floatVal = floatVal(n) + skip n + elif n.kind == TagLit: + if tagIs(n, "inf"): result.floatVal = Inf - of "nan": + elif tagIs(n, "nan"): result.floatVal = NaN - of "neginf": + elif tagIs(n, "neginf"): result.floatVal = NegInf else: - raiseAssert "expected float literal but got " & pool.tags[n.tagId] - inc n - skipParRi n + raiseAssert "expected float literal but got " & cursorTag(n) + n.into: + discard else: raiseAssert "expected float literal but got " & $n.kind of nkStrLit .. nkTripleStrLit: c.withNode n, result, kind: - expect n, StringLit - result.strVal = pool.strings[n.litId] - inc n + expect n, StrLit + result.strVal = strVal(n) + skip n of nkNilLit: c.withNode n, result, kind: discard else: c.withNode n, result, kind: - while n.kind != ParRi: + while n.hasMore: result.sons.add c.loadNode(n, thisModule, localSyms) else: raiseAssert "expected string literal but got " & $n.kind @@ -2475,17 +2526,41 @@ proc moduleSymbolStubs*(c: var DecodeContext; module: FileIndex): seq[PSym] = if sym != nil: result.add sym c.mods[module].index = move indexTab +proc moduleTypeStubs*(c: var DecodeContext; module: FileIndex): seq[PType] = + ## Complete-loaded `PType`s for every TYPE this module serialized in its NIF + ## index. `writeLoweredModule` needs these because the lowered `.t.nif` is the + ## SOLE source the `cg`/`emit` stages read: a routine body/signature in ANY + ## module may reference one of this module's types as a `SymUse`, so the def + ## must live in this module's `.t.nif` — exactly as it did in `.s.nif`, where + ## the full top-level AST walk emitted it. The backend-loaded `topLevel` has no + ## type sections, so without force-emitting them the def is absent and the + ## consumer asserts `symbol has no offset`. Mirrors `moduleSymbolStubs`: collect + ## the names first (force-loading a type registers modules / mutates `c.types`, + ## which must not invalidate an active index iterator), then force each to + ## `Complete` so `writeType` emits a real def. + result = @[] + if not c.mods.hasKey(module): return + var names: seq[string] = @[] + for nifName in c.mods[module].index.keys: + if nifName.startsWith("`t"): names.add nifName + for nm in names: + let t = createTypeStub(c, nm) + if t != nil: + loadType(c, t) + result.add t + proc toNifFilename*(conf: ConfigRef; f: FileIndex): string = let suffix = moduleSuffix(conf, f) # The `cg`/`emit` backend stages load the lowered whole-module NIF (transformed # bodies + lifted sigs baked in); the `lower` stage and the frontend (`cmdM`) - # read the semchecked `.s.nif`. + # read the semchecked `.s.bif`. All module artifacts are binary NIF (`.bif`) + # now — one file per stage, no text twin (debug via `tools/bif2nif`). if conf.cmd == cmdNifC and (conf.icBackendStage == "cg" or conf.icBackendStage == "emit"): - let t = toGeneratedFile(conf, AbsoluteFile(suffix), ".t.nif").string + let t = toGeneratedFile(conf, AbsoluteFile(suffix), ".t.bif").string if fileExists(t): return t - result = toGeneratedFile(conf, AbsoluteFile(suffix), ".s.nif").string + result = toGeneratedFile(conf, AbsoluteFile(suffix), ".s.bif").string proc resolveSym(c: var DecodeContext; symAsStr: string; alsoConsiderPrivate: bool): PSym = result = c.syms.getOrDefault(symAsStr)[0] @@ -2519,11 +2594,10 @@ proc resolveSym(c: var DecodeContext; symAsStr: string; alsoConsiderPrivate: boo disamb: sn.count.int32, state: Partial) c.syms[symAsStr] = (result, offs) -proc resolveHookSym*(c: var DecodeContext; symId: nifstreams.SymId): PSym = - ## Resolves a hook SymId to PSym. +proc resolveHookSym*(c: var DecodeContext; name: string): PSym = + ## Resolves a hook symbol NAME to a PSym. ## Hook symbols are often private (generated =destroy, =wasMoved, etc.) - let symAsStr = pool.syms[symId] - result = resolveSym(c, symAsStr, true) + result = resolveSym(c, name, true) proc tryResolveCompilerProc*(c: var DecodeContext; name: string; moduleFileIdx: FileIndex): PSym = ## Tries to resolve a compiler proc from a module by checking the NIF index. @@ -2551,51 +2625,19 @@ proc tryResolveCompilerProc*(c: var DecodeContext; name: string; moduleFileIdx: if sfCompilerProc in sym.flagsImpl: return sym -proc loadLogOp(c: var DecodeContext; logOps: var seq[LogEntry]; s: var Stream; kind: LogEntryKind; op: TTypeAttachedOp; module: int): PackedToken = - result = next(s) - var key = "" - if result.kind == StringLit: - key = pool.strings[result.litId] - result = next(s) - else: - raiseAssert "expected StringLit but got " & $result.kind - if result.kind == Symbol: - let sym = resolveHookSym(c, result.symId) - if sym != nil: - logOps.add LogEntry(kind: kind, op: op, module: module, key: key, sym: sym) - # else: symbol not indexed, skip this hook entry - result = next(s) - if result.kind == ParRi: - result = next(s) - else: - raiseAssert "expected ParRi but got " & $result.kind - -proc skipTree(s: var Stream): PackedToken = - result = next(s) - var nested = 1 - while nested > 0: - if result.kind == ParLe: - inc nested - elif result.kind == ParRi: - dec nested - elif result.kind == EofToken: - break - result = next(s) - -proc nextSubtree(r: var Stream; dest: var TokenBuf; tok: var PackedToken) = - r.parents[0] = tok.info - var nested = 1 - dest.add tok # tag - while true: - tok = r.next() - dest.add tok - if tok.kind == EofToken: - break - elif tok.kind == ParLe: - inc nested - elif tok.kind == ParRi: - dec nested - if nested == 0: break +proc loadLogOp(c: var DecodeContext; logOps: var seq[LogEntry]; cur: var Cursor; + kind: LogEntryKind; op: TTypeAttachedOp; module: int) = + ## Step 2 phase 2: read one `(rep* "key" sym)` from the resident-buffer cursor. + cur.into: + expect cur, StrLit + let key = strVal(cur) + skip cur + if cur.hasMore and cur.kind == Symbol: + let sym = resolveHookSym(c, symName(cur)) + if sym != nil: + logOps.add LogEntry(kind: kind, op: op, module: module, key: key, sym: sym) + # else: symbol not indexed, skip this hook entry + skip cur type ModuleSuffix* = distinct string @@ -2621,35 +2663,23 @@ type # nimsuggest can map a query in an include file back to # this module (`parentModule`) and recompile it. -proc loadImport(c: var DecodeContext; s: var Stream; deps: var seq[ModuleSuffix]; tok: var PackedToken) = - tok = next(s) # skip `(import` - if tok.kind == DotToken: - tok = next(s) # skip dot - if tok.kind == DotToken: - tok = next(s) # skip dot - if tok.kind == StringLit: - deps.add ModuleSuffix(pool.strings[tok.litId]) - tok = next(s) - else: - raiseAssert "expected StringLit but got " & $tok.kind - if tok.kind == ParRi: - tok = next(s) # skip ) - else: - raiseAssert "expected ParRi but got " & $tok.kind +proc loadImport(c: var DecodeContext; cur: var Cursor; deps: var seq[ModuleSuffix]) = + cur.into: + while cur.hasMore and cur.kind == DotToken: skip cur # flags / type + if cur.hasMore and cur.kind == StrLit: + deps.add ModuleSuffix(strVal(cur)) + skip cur + else: + raiseAssert "expected StrLit but got " & $cur.kind -proc loadInclude(c: var DecodeContext; s: var Stream; includes: var seq[string]; tok: var PackedToken) = +proc loadInclude(c: var DecodeContext; cur: var Cursor; includes: var seq[string]) = ## Reads an `(include . . "path"...)` entry written by `trInclude`. The paths ## are resolved full paths (see semstmts.evalInclude under cmdM/optCompress). - tok = next(s) # skip `(include` - if tok.kind == DotToken: tok = next(s) # flags - if tok.kind == DotToken: tok = next(s) # type - while tok.kind == StringLit: - includes.add pool.strings[tok.litId] - tok = next(s) - if tok.kind == ParRi: - tok = next(s) - else: - raiseAssert "expected ParRi in (include ...) but got " & $tok.kind + cur.into: + while cur.hasMore and cur.kind == DotToken: skip cur # flags / type + while cur.hasMore and cur.kind == StrLit: + includes.add strVal(cur) + skip cur proc scanIncludeGraph*(config: ConfigRef): seq[tuple[includer: string; includes: seq[string]]] = ## Standalone "full table" scan of every `.nif` in the nimcache: reads @@ -2665,37 +2695,35 @@ proc scanIncludeGraph*(config: ConfigRef): seq[tuple[includer: string; includes: result = @[] let dir = getNimcacheDir(config) if not dirExists(dir.string): return - for f in walkFiles((dir / RelativeFile"*.nif").string): - # only the primary module NIFs; skip the sidecars - # (.iface.nif/.impl.nif/.edges.nif/.s.deps.nif). - if f.endsWith(".iface.nif") or f.endsWith(".impl.nif") or - f.endsWith(".edges.nif") or f.endsWith(".deps.nif"): - continue - var s = nifstreams.open(f) + # The primary module artifacts are `.s.bif` (the sidecars are + # `.iface.nif`/`.impl.nif`/`.edges.nif`/`.s.deps.nif`, which this glob excludes). + for f in walkFiles((dir / RelativeFile"*.s.bif").string): + var m = bif.load(f) var includer = "" var includes: seq[string] = @[] - var t = next(s) # (stmts - if t.kind == ParLe: - t = next(s) # flags dot - t = next(s) # type dot - t = next(s) # first child (matches loadNifModule's priming) - # the dep records (import/include/reexpmod/modulesrc) are written first and - # contiguously; stop at the first body node or the (implementation) marker. - while t.kind == ParLe: - if t.tagId == includeTag or t.tagId == modulesrcTag: - let isInc = t.tagId == includeTag - t = next(s) # into the node (past its ParLe) - while t.kind != ParRi and t.kind != EofToken: - if t.kind == StringLit: - if isInc: includes.add pool.strings[t.litId] - else: includer = pool.strings[t.litId] - t = next(s) - if t.kind == ParRi: t = next(s) # past the ParRi - elif t.tagId == importTag or t.tagId == reexpModTag: - t = skip(s, t) + var c = m.buf.beginRead() + if c.kind == TagLit and tagIs(c, toNifTag(nkStmtList)): + # The dep records (import/include/reexpmod/modulesrc) are written first and + # contiguously; `done` short-circuits once the first body node is seen + # (`into` forbids an early `break`, so we skip the remainder instead). + var done = false + c.loopInto: + if done or c.kind != TagLit: + skip c + elif tagIs(c, "include") or tagIs(c, "modulesrc"): + let isInc = tagIs(c, "include") + var ic = c + ic.loopInto: + if ic.kind == StrLit: + if isInc: includes.add strVal(ic) + else: includer = strVal(ic) + skip ic + skip c + elif tagIs(c, "import") or tagIs(c, "reexpmod"): + skip c else: - break - close s + done = true + skip c if includer.len > 0 and includes.len > 0: result.add (includer, includes) @@ -2707,23 +2735,23 @@ proc nifModuleHasIncludes*(config: ConfigRef; fileIdx: FileIndex): bool = ## owner/line-info on reload). let f = toNifFilename(config, fileIdx) if not fileExists(f): return false - var s = nifstreams.open(f) + var m = bif.load(f) result = false - var t = next(s) # (stmts - if t.kind == ParLe: - t = next(s) # flags dot - t = next(s) # type dot - t = next(s) # first child - while t.kind == ParLe: - if t.tagId == includeTag: + var c = m.buf.beginRead() + if c.kind == TagLit and tagIs(c, toNifTag(nkStmtList)): + var done = false + c.loopInto: + if done or c.kind != TagLit: + skip c + elif tagIs(c, "include"): result = true - break - elif t.tagId == modulesrcTag or t.tagId == importTag or - t.tagId == reexpModTag: - t = skip(s, t) + done = true + skip c + elif tagIs(c, "modulesrc") or tagIs(c, "import") or tagIs(c, "reexpmod"): + skip c else: - break - close s + done = true + skip c proc addReexportedEnumFields(c: var DecodeContext; sym: PSym; interf: var TStrTable) = ## When a non-pure enum type is (re-)exported, its fields must also become @@ -2744,213 +2772,148 @@ proc addReexportedEnumFields(c: var DecodeContext; sym: PSym; interf: var TStrTa if f != nil and f.kind == nkSym and f.sym != nil: strTableAdd(interf, f.sym) -proc processTopLevel(c: var DecodeContext; s: var Stream; flags: set[LoadFlag]; +proc processTopLevel(c: var DecodeContext; cur: var Cursor; flags: set[LoadFlag]; interf: var TStrTable; suffix: string; module: int): PrecompiledModule = + ## Step 2 phase 2: walk the module body directly over the resident `buf` cursor + ## (was a `next(s)` stream walk). `cur` enters at the `(stmts` type dot. Lazy + ## loads done here (resolveSym/loadType/…) read INDEPENDENT cursors into the + ## resident buffers, never `cur` — so the old export/toffer `jumpTo(saved)` + ## save/restore dance is gone. result = PrecompiledModule(topLevel: newNode(nkStmtList)) var localSyms = initTable[string, PSym]() - var t = next(s) # skip dot - var cont = true - let exportTag = pool.tags.getOrIncl"export" + skip cur # the (stmts type dot # Top-level `let`/`var` sections are loaded even without LoadFullAst: they may # declare `{.compileTime.}` globals whose VM slots the importer initializes # eagerly (pipelines.initLoadedCompileTimeGlobals), which needs them visible in # `topLevel`. They sit in the module header before `(implementation)`. - let letTag = pool.tags.getOrIncl(toNifTag(nkLetSection)) - let varTag = pool.tags.getOrIncl(toNifTag(nkVarSection)) - while cont and t.kind != EofToken: - if t.kind == ParLe: - if t.tagId == replayTag: + var cont = true + while cont and cur.hasMore: + if cur.kind != TagLit: + cont = false + else: + if tagIs(cur, "replay"): # Always load replay actions (macro cache operations) - t = next(s) # move past (replay - while t.kind != ParRi and t.kind != EofToken: - if t.kind == ParLe: - var buf = createTokenBuf(50) - nextSubtree(s, buf, t) - var cursor = cursorAt(buf, 0) - let replayNode = loadNode(c, cursor, suffix, localSyms) + cur.into: + while cur.hasMore: + let replayNode = loadNode(c, cur, suffix, localSyms) if replayNode != nil: result.topLevel.sons.add replayNode - t = next(s) - if t.kind == ParRi: - t = next(s) - else: - raiseAssert "expected ParRi but got " & $t.kind - elif t.tagId == repConverterTag: - t = loadLogOp(c, result.logOps, s, ConverterEntry, attachedTrace, module) - elif t.tagId == repDestroyTag: - t = loadLogOp(c, result.logOps, s, HookEntry, attachedDestructor, module) - elif t.tagId == repWasMovedTag: - t = loadLogOp(c, result.logOps, s, HookEntry, attachedWasMoved, module) - elif t.tagId == repCopyTag: - t = loadLogOp(c, result.logOps, s, HookEntry, attachedAsgn, module) - elif t.tagId == repSinkTag: - t = loadLogOp(c, result.logOps, s, HookEntry, attachedSink, module) - elif t.tagId == repDupTag: - t = loadLogOp(c, result.logOps, s, HookEntry, attachedDup, module) - elif t.tagId == repTraceTag: - t = loadLogOp(c, result.logOps, s, HookEntry, attachedTrace, module) - elif t.tagId == repDeepCopyTag: - t = loadLogOp(c, result.logOps, s, HookEntry, attachedDeepCopy, module) - elif t.tagId == repEnumToStrTag: - t = loadLogOp(c, result.logOps, s, EnumToStrEntry, attachedTrace, module) - elif t.tagId == repMethodTag: - t = loadLogOp(c, result.logOps, s, MethodEntry, attachedTrace, module) - elif t.tagId == repPureEnumTag: - t = loadLogOp(c, result.logOps, s, PureEnumEntry, attachedTrace, module) - #elif t.tagId == repClassTag: - # t = loadLogOp(c, logOps, s, ClassEntry, attachedTrace, module) - elif t.tagId == exportTag: - var lastGood = "" - t = next(s) # skip (export - if t.kind == DotToken: - t = next(s) # skip dot - if t.kind == DotToken: - t = next(s) # skip dot - while true: - if t.kind == Symbol: - let symAsStr = pool.syms[t.symId] - lastGood = symAsStr - # Skip symbols that are re-exported by this dependency but actually - # belong to the module we are compiling fresh: loading them as stubs - # would shadow/collide with the freshly compiled originals. - if c.mainModuleSuffix.len == 0 or - parseSymName(symAsStr).module != c.mainModuleSuffix: - # Resolving an exported symbol of this very module (`export` of a - # symbol that lives in a `when` branch of the same file) lazily - # loads it from the stream we are currently iterating, moving the - # cursor into the symbol's `(sd ...)` definition. Save/restore the - # position so the export-list parse continues where it left off. - let saved = offset(s.r) - let sym = resolveSym(c, symAsStr, false) - if sym != nil: - strTableAdd(interf, sym) - addReexportedEnumFields(c, sym, interf) - s.r.jumpTo(saved) - t = next(s) - elif t.kind == ParRi: - break - else: - raiseAssert "expected Symbol or ParRi but got " & $t.kind & - " (" & (if t.kind == ParLe: pool.tags[t.tagId] else: "") & - ") in export list of module " & suffix & ", last symbol: " & lastGood - t = next(s) - elif t.tagId == includeTag: - loadInclude(c, s, result.includes, t) - elif t.tagId == importTag: - loadImport(c, s, result.deps, t) - elif t.tagId == reexpModTag: - # a re-exported MODULE: (reexpmod "name" "suffix"); the module sym - # is a qualifier in this module's interface — materialized by the - # caller (modulegraphs), which can register interface tables - t = next(s) - var mname = "" - var msuffix = "" - if t.kind == StringLit: - mname = pool.strings[t.litId] - t = next(s) - if t.kind == StringLit: - msuffix = pool.strings[t.litId] - t = next(s) - if t.kind != ParRi: - raiseAssert "expected ParRi in reexpmod entry of module " & suffix - t = next(s) + elif tagIs(cur, "repconverter"): loadLogOp(c, result.logOps, cur, ConverterEntry, attachedTrace, module) + elif tagIs(cur, "repdestroy"): loadLogOp(c, result.logOps, cur, HookEntry, attachedDestructor, module) + elif tagIs(cur, "repwasmoved"): loadLogOp(c, result.logOps, cur, HookEntry, attachedWasMoved, module) + elif tagIs(cur, "repcopy"): loadLogOp(c, result.logOps, cur, HookEntry, attachedAsgn, module) + elif tagIs(cur, "repsink"): loadLogOp(c, result.logOps, cur, HookEntry, attachedSink, module) + elif tagIs(cur, "repdup"): loadLogOp(c, result.logOps, cur, HookEntry, attachedDup, module) + elif tagIs(cur, "reptrace"): loadLogOp(c, result.logOps, cur, HookEntry, attachedTrace, module) + elif tagIs(cur, "repdeepcopy"): loadLogOp(c, result.logOps, cur, HookEntry, attachedDeepCopy, module) + elif tagIs(cur, "repenumtostr"): loadLogOp(c, result.logOps, cur, EnumToStrEntry, attachedTrace, module) + elif tagIs(cur, "repmethod"): loadLogOp(c, result.logOps, cur, MethodEntry, attachedTrace, module) + elif tagIs(cur, "reppureenum"): loadLogOp(c, result.logOps, cur, PureEnumEntry, attachedTrace, module) + elif tagIs(cur, "export"): + cur.into: + while cur.hasMore and cur.kind == DotToken: skip cur # flags / type + while cur.hasMore: + if cur.kind == Symbol: + let symAsStr = symName(cur) + # Skip symbols re-exported by this dependency but owned by the module + # being compiled fresh (they would collide with the fresh originals). + if c.mainModuleSuffix.len == 0 or + parseSymName(symAsStr).module != c.mainModuleSuffix: + let sym = resolveSym(c, symAsStr, false) + if sym != nil: + strTableAdd(interf, sym) + addReexportedEnumFields(c, sym, interf) + skip cur + else: + raiseAssert "expected Symbol or ParRi but got " & $cur.kind & + " in export list of module " & suffix + elif tagIs(cur, "include"): loadInclude(c, cur, result.includes) + elif tagIs(cur, "import"): loadImport(c, cur, result.deps) + elif tagIs(cur, "reexpmod"): + # a re-exported MODULE: (reexpmod "name" "suffix"); the module sym is a + # qualifier in this module's interface — materialized by modulegraphs. + var mname, msuffix = "" + cur.into: + if cur.hasMore and cur.kind == StrLit: (mname = strVal(cur); skip cur) + if cur.hasMore and cur.kind == StrLit: (msuffix = strVal(cur); skip cur) if mname.len > 0 and msuffix.len > 0: result.reexportedModules.add (mname, msuffix) - elif t.tagId == offerTag: - # (offer ...) — see the - # writer. Resolve to PSyms/PTypes here; modulegraphs registers them into - # `procInstCache`. Best-effort: a type that fails to resolve drops the - # whole offer (the consumer then re-instantiates, the prior behaviour). - t = next(s) # skip (offer + elif tagIs(cur, "offer"): + # (offer ...) — resolve + # to PSyms/PTypes; modulegraphs registers them into `procInstCache`. + # Best-effort: a type that fails to resolve drops the whole offer. var genSym, instSym: PSym = nil var paramsCount = 0 var cts: seq[PType] = @[] var idx = 0 var ok = true - while t.kind != ParRi and t.kind != EofToken: - if t.kind == Symbol: - if idx == 0: genSym = resolveHookSym(c, t.symId) - elif idx == 1: instSym = resolveHookSym(c, t.symId) - else: - let ct = tryCreateTypeStub(c, t.symId) - if ct == nil: ok = false - else: cts.add ct - inc idx - elif t.kind == IntLit: - paramsCount = int(pool.integers[t.intId]) - t = next(s) - if t.kind != ParRi: - raiseAssert "expected ParRi in offer entry of module " & suffix - t = next(s) + cur.into: + while cur.hasMore: + if cur.kind == Symbol: + if idx == 0: genSym = resolveHookSym(c, symName(cur)) + elif idx == 1: instSym = resolveHookSym(c, symName(cur)) + else: + let ct = tryCreateTypeStub(c, symName(cur)) + if ct == nil: ok = false + else: cts.add ct + inc idx + skip cur + elif cur.kind == IntLit: + paramsCount = int(intVal(cur)) + skip cur + else: skip cur if ok and genSym != nil and instSym != nil: result.genericOffers.add (genSym, instSym, cts, paramsCount) - elif t.tagId == typeOfferTag: - # (toffer "" "") — see the writer. The two - # full names arrive as string literals; intern them and resolve to a - # PSym/PType, then FULLY load the instance (its array bounds/fields) so - # `searchInstTypes` can match its params (a Partial stub has empty kids). - # Best-effort: a type that fails to resolve drops the offer. - t = next(s) # skip (toffer + elif tagIs(cur, "toffer"): + # (toffer "" "") — intern the two full names, + # resolve, FULLY load the instance (so `searchInstTypes` can match its + # params). Best-effort: a failure to resolve drops the offer. var genName, instName = "" var idx = 0 - while t.kind != ParRi and t.kind != EofToken: - if t.kind == StringLit: - if idx == 0: genName = pool.strings[t.litId] - elif idx == 1: instName = pool.strings[t.litId] - inc idx - t = next(s) - if t.kind != ParRi: - raiseAssert "expected ParRi in toffer entry of module " & suffix - t = next(s) + cur.into: + while cur.hasMore: + if cur.kind == StrLit: + if idx == 0: genName = strVal(cur) + elif idx == 1: instName = strVal(cur) + inc idx + skip cur + else: skip cur if genName.len > 0 and instName.len > 0: - # Resolving/loading these entities lazily reads from the SAME stream we - # are iterating, moving its cursor — save/restore around it (cf. the - # export-list handling above). - let saved = offset(s.r) - let genSym = resolveHookSym(c, pool.syms.getOrIncl(genName)) - let inst = tryCreateTypeStub(c, pool.syms.getOrIncl(instName)) + let genSym = resolveHookSym(c, genName) + let inst = tryCreateTypeStub(c, instName) if genSym != nil and inst != nil: loadType(c, inst) result.typeOffers.add (genSym, inst) - s.r.jumpTo(saved) - elif t.tagId == modulesrcTag: + elif tagIs(cur, "modulesrc"): # self-identification record for the standalone include-graph scanner; - # not needed by the lazy loader, just skip past it. - t = skip(s, t) - elif t.tagId == implTag: + # not needed by the loader, just skip past it. + skip cur + elif tagIs(cur, "implementation"): cont = false - elif LoadFullAst in flags or t.tagId == letTag or t.tagId == varTag: + elif LoadFullAst in flags or tagIs(cur, toNifTag(nkLetSection)) or tagIs(cur, toNifTag(nkVarSection)): # Parse the full statement. let/var sections are loaded unconditionally # (see above) so `{.compileTime.}` globals reach the eager initializer. - var buf = createTokenBuf(50) - nextSubtree(s, buf, t) - t = next(s) # skip ParRi - var cursor = cursorAt(buf, 0) - let stmtNode = loadNode(c, cursor, suffix, localSyms) + let stmtNode = loadNode(c, cur, suffix, localSyms) if stmtNode != nil: result.topLevel.sons.add stmtNode else: cont = false - else: - cont = false proc loadNifModule*(c: var DecodeContext; suffix: ModuleSuffix; interf, interfHidden: var TStrTable; flags: set[LoadFlag] = {}): PrecompiledModule = # Ensure module index is loaded - moduleId returns the FileIndex for this suffix let module = moduleId(c, string(suffix), flags) - # Load the module AST (or just replay actions if loadFullAst is false) - # processTopLevel also collects export instructions. - # Lazy symbol loading may have moved the stream cursor since the module was - # opened, so rewind to the start of the module body before reading it. - let s = addr c.mods[module].stream - s[].r.jumpTo(c.mods[module].contentStart) - var t = next(s[]) - if t.kind == ParLe and pool.tags[t.tagId] == toNifTag(nkStmtList): - t = next(s[]) # skip (stmts - t = next(s[]) # skip flags - result = processTopLevel(c, s[], flags, interf, string(suffix), module.int) + # Load the module AST (or just replay actions if loadFullAst is false). + # processTopLevel also collects export instructions. Step 2 phase 2: read the + # body straight from the resident `buf` cursor (no stream, no rewind — lazy + # loads use independent cursors so they never disturb this one). + var cur = beginRead(c.mods[module].buf) + if cur.kind == TagLit and tagIs(cur, toNifTag(nkStmtList)): + inc cur # enter (stmts (past the tag head, onto the flags dot) + skip cur # flags dot (processTopLevel skips the type dot itself) + result = processTopLevel(c, cur, flags, interf, string(suffix), module.int) else: result = PrecompiledModule(topLevel: newNode(nkStmtList)) @@ -2983,9 +2946,10 @@ proc writeLoweredModule*(c: var DecodeContext; config: ConfigRef; # the sole source the cg stage reads — no `.s.nif` fallback for them). sealLoadedRoutines(c) var w = Writer(infos: LineInfoWriter(config: config), currentModule: thisModule) + w.deps = newIcBuilder(64) w.inProc = 1 w.lowering = true - var content = createTokenBuf(300) + var content = newIcBuilder(300) let rootInfo = trLineInfo(w, precomp.topLevel.info) createStmtList(content, rootInfo) @@ -2998,15 +2962,36 @@ proc writeLoweredModule*(c: var DecodeContext; config: ConfigRef; for op in hooks: writeOp(w, content, op) - var bottom = createTokenBuf(300) + var bottom = newIcBuilder(300) # Imperative init code + global let/var/const sections + replay actions — all # that a backend-loaded `topLevel` carries (routines are lazy index sdefs, not # here). Emits + seals the module's globals. w.writeToplevelNode content, bottom, precomp.topLevel - # Routine DEFS with transformed bodies, sourced from the index. + # TYPE DEFS this module owns. Unlike `writeNifModule` (whose `n` is the full + # module AST, so `writeToplevelNode` walks every type section), the lowered + # `topLevel` carries only init code + globals — routines and types are lazy + # index sdefs. So force every owned type from the `.s.nif` index to `Complete` + # and emit its def here; a cg/emit consumer that `SymUse`s one of them resolves + # through this `.t.nif` index, which would otherwise lack the entry (`symbol has + # no offset`). Emit BEFORE the routines so a routine signature referencing a + # type writes a `SymUse` to the def already present. Guard on `Complete`: a type + # reached as an owned son of an earlier def is already `Sealed` (emitted inline) + # — skip it to avoid a stray top-level `SymUse` (mirrors the offer loops). + for t in moduleTypeStubs(c, FileIndex thisModule): + if t.state == Complete: + writeType(w, bottom, t) + + # Routine DEFS (with transformed bodies) + CONST DEFS this module owns, sourced + # from the index. Consts are lazy index sdefs too (like routines): the + # backend-loaded `topLevel` carries only runtime init (module var/let sections), + # NOT consts — especially `importc`/magic consts (`SIG_DFL`, `hasAllocStack`, …) + # which have no runtime init at all. `writeNifModule` emitted them via the full + # AST walk; here we must enumerate them from the index, else a cross-module + # `SymUse` resolves to a nil `skModule` stub (`expr(skModule); unknown symbol`). for s in moduleSymbolStubs(c, FileIndex thisModule): - if s.kindImpl in routineKinds and s.itemId.module == thisModule: + if (s.kindImpl in routineKinds or s.kindImpl == skConst) and + s.itemId.module == thisModule: writeSymDef(w, bottom, s) # Lifted hook ROUTINES (`@bk`, NEW in the lower stage — no `.s.nif` sdef, so @@ -3059,16 +3044,17 @@ proc writeLoweredModule*(c: var DecodeContext; config: ConfigRef; # (implementation) ). content.addParLe implTag, NoLineInfo content.addParRi() - content.add bottom + addAll(content, bottom) content.addParRi() - var dest = createTokenBuf(600) + var dest = newIcBuilder(600) createStmtList(dest, rootInfo) - dest.add w.deps - for i in 3 ..< content.len-1: - dest.add content[i] + addAll(dest, w.deps) + addStmtsBody(dest, content) dest.addParRi() - writeFile(dest, outfile) + # Step 3: the lowered whole-module artifact is binary NIF (`.t.bif`) too — the + # cg/emit stages load it via `toNifFilename`. No text twin (debug via bif2nif). + storeBif(dest, outfile, "." & extractModuleSuffix(outfile)) when isMainModule: import std / syncio diff --git a/compiler/ccgtypes.nim b/compiler/ccgtypes.nim index 2fc37f6562..58343f2c44 100644 --- a/compiler/ccgtypes.nim +++ b/compiler/ccgtypes.nim @@ -1807,6 +1807,16 @@ proc generateRttiDestructor(g: ModuleGraph; typ: PType; owner: PSym; kind: TType incl result.flagsImpl, sfFromGeneric incl result.flagsImpl, sfGeneratedOp + # Under IC the `rttiDestroy` wrapper is generated independently in every cg + # process that emits `typ`'s RTTI (the type-info is emit-everywhere). A plain + # counter `disamb` renumbers per process, so the RTTI table baked in module A + # references `rttiDestroy_c` while module B (the =destroy owner) defines a + # different number → undefined at link. Give it a content-derived `disamb` + # (stable across processes) + `HookDisambBit`, exactly like `symPrototype` does + # for the hook itself: same `typ` ⇒ same C name everywhere, and the bit makes + # `emitsBodyInThisModule` emit the body in every demander (merge dedups). The + # `"rttiDestroy"` op-name keeps its key disjoint from the real `=destroy` hook's. + setHookDisamb(g, result, "rttiDestroy", typ) proc genHook(m: BModule; t: PType; info: TLineInfo; op: TTypeAttachedOp; result: var Builder) = let theProc = getAttachedOp(m.g.graph, t, op) diff --git a/compiler/ccgutils.nim b/compiler/ccgutils.nim index 9960ea807b..430c16f172 100644 --- a/compiler/ccgutils.nim +++ b/compiler/ccgutils.nim @@ -11,7 +11,7 @@ import ast, types, msgs, wordrecg, - platform, trees, options, cgendata, mangleutils, renderer + platform, trees, options, cgendata, mangleutils, renderer, modulegraphs import std/[hashes, strutils, formatfloat] @@ -116,7 +116,17 @@ proc makeUnique(m: BModule; s: PSym, name: string = ""): string = # restarts at 0 and would collide with loaded symbols' ids if s.itemId.isBackendMinted: result.add "_c" - result.add $s.itemId.item + if (s.disamb and HookDisambBit) != 0'i32: + # A backend-minted sym whose `disamb` is content-derived (setHookDisamb gave + # it HookDisambBit) — e.g. the `rttiDestroy` wrapper. Its `itemId.item` is a + # PER-PROCESS backend counter, so using it makes the C name diverge across + # the emit-everywhere processes: the type's RTTI table (emit-everywhere, + # merge-deduped) ends up referencing one process's `_c` while the + # wrapper is defined with another's -> undefined at link (`rttiDestroy_c23`). + # The content-derived disamb is stable across processes, so use it. + result.add $s.disamb + else: + result.add $s.itemId.item else: result.add "_u" # Mirror `mangleProcNameExt`: use the per-(module,name) `disamb`, NOT diff --git a/compiler/cgen.nim b/compiler/cgen.nim index 411be49778..3cf6c87b39 100644 --- a/compiler/cgen.nim +++ b/compiler/cgen.nim @@ -1218,8 +1218,17 @@ proc closeNamespaceNim(result: var Builder) = proc closureSetup(p: BProc, prc: PSym) = if tfCapturesEnv notin prc.typ.flags: return - # prc.ast[paramsPos].last contains the type we're after: - var ls = lastSon(prc.ast[paramsPos]) + # prc.ast[paramsPos].last contains the type we're after — BUT a closure loaded + # from a `.t.bif` (a lambda-lifted nested proc / generic instance the `lower` + # stage transformed) can arrive with an EMPTY AST param node: the lifted hidden + # `:env` param lives in `typ.n`, the authoritative signature (`genProc` already + # reads `typ.n`, not the AST). The two param nodes diverge across the NIF + # boundary; fall back to `typ.n` so the env param resolves instead of indexing + # an empty container. + var params = prc.ast[paramsPos] + if params.safeLen == 0 and prc.typ.n != nil and prc.typ.n.kind == nkFormalParams: + params = prc.typ.n + var ls = lastSon(params) if ls.kind != nkSym: internalError(p.config, prc.info, "closure generation failed") var env = ls.sym @@ -1464,8 +1473,16 @@ proc genProcLvl3*(m: BModule, prc: PSym) = var returnStmt: Snippet = "" assert(prc.ast != nil) + # A body LOADED from `.t.bif` was already FULLY lowered by the `lower` stage — + # transformed AND destructor-injected (see nifbackend.generateLowerStage). The + # `.t.bif` is the authoritative backend artifact; re-injecting here would lower + # it twice (double `=destroy` calls) and, worse, re-lift the env hooks per cg + # process (owned by nobody → undefined at link). So inject ONLY when the body + # was re-derived in this process (`wasLoaded == false`). Capture before + # `transformBody`, which returns the cached body (non-nil) when it was loaded. + let wasLoaded = prc.transformedBody != nil var procBody = transformBody(m.g.graph, m.idgen, prc, {}) - if sfInjectDestructors in prc.flags: + if sfInjectDestructors in prc.flags and not wasLoaded: procBody = injectDestructorCalls(m.g.graph, m.idgen, prc, procBody) let tmpInfo = prc.info diff --git a/compiler/deps.nim b/compiler/deps.nim index fb77792c3d..f95a884da9 100644 --- a/compiler/deps.nim +++ b/compiler/deps.nim @@ -16,6 +16,7 @@ import options, msgs, lineinfos, pathutils, condsyms, import "../dist/nimony/src/lib" / [nifstreams, bitabs, nifreader, nifbuilder] import "../dist/nimony/src/gear2" / modnames +import icnifcore type FilePair = object @@ -51,24 +52,24 @@ proc parsedFile(c: DepContext; f: FilePair): string = getNimcacheDir(c.config).string / f.modname & ".p.nif" proc semmedFile(c: DepContext; f: FilePair): string = - getNimcacheDir(c.config).string / f.modname & ".s.nif" + getNimcacheDir(c.config).string / f.modname & ".s.bif" proc ifaceFile(c: DepContext; f: FilePair): string = ## Interface-cookie sidecar written by `nim m` (ast2nif.writeIfaceCookie, ## OnlyIfChanged). Dependents' nim_m rules use it as their input instead of ## the semmed NIF: a body-only change in a dependency then keeps the sidecar ## mtime and nifmake prunes the whole re-sem cascade behind it. - getNimcacheDir(c.config).string / f.modname & ".iface.nif" + getNimcacheDir(c.config).string / f.modname & ".iface.bif" proc implFile(c: DepContext; suffix: string): string = ## Implementation-cookie sidecar (ast2nif.writeImplCookie): flips on ANY ## content change of the module (private bodies included; supersedes the ## iface cookie). Used as the edge for dependents that consumed the ## module's bodies at compile time (NeedsImpl edges). - getNimcacheDir(c.config).string / suffix & ".impl.nif" + getNimcacheDir(c.config).string / suffix & ".impl.bif" proc edgesFile(c: DepContext; f: FilePair): string = - getNimcacheDir(c.config).string / f.modname & ".edges.nif" + getNimcacheDir(c.config).string / f.modname & ".edges.bif" proc readNeedsImpl(c: DepContext; f: FilePair): seq[string] = ## Reads the module's recorded NeedsImpl edge set (module suffixes whose @@ -79,19 +80,10 @@ proc readNeedsImpl(c: DepContext; f: FilePair): seq[string] = ## gated input of its rule, so the rule re-fires and re-records. result = @[] if fileExists(c.edgesFile(f)): - var s = nifstreams.open(c.edgesFile(f)) - try: - discard processDirectives(s.r) - while true: - let t = next(s) - if t.kind == EofToken: break - if t.kind == StringLit: - result.add pool.strings[t.litId] - finally: - close s + result = collectBifStrLits(c.edgesFile(f)) proc semDepsFile(c: DepContext; f: FilePair): string = - getNimcacheDir(c.config).string / f.modname & ".s.deps.nif" + getNimcacheDir(c.config).string / f.modname & ".s.deps.bif" proc readSemDeps(c: DepContext; f: FilePair): seq[string] = ## The module's REAL direct imports (full source paths) as sem resolved them, @@ -99,16 +91,7 @@ proc readSemDeps(c: DepContext; f: FilePair): seq[string] = ## (ast2nif.writeSemDeps). Missing file (not yet semmed) -> empty. result = @[] if fileExists(c.semDepsFile(f)): - var s = nifstreams.open(c.semDepsFile(f)) - try: - discard processDirectives(s.r) - while true: - let t = next(s) - if t.kind == EofToken: break - if t.kind == StringLit: - result.add pool.strings[t.litId] - finally: - close s + result = collectBifStrLits(c.semDepsFile(f)) proc findNifler(): string = # Look for nifler in common locations @@ -981,12 +964,12 @@ proc generateBackendBuildFile(c: DepContext; forwardedArgs: seq[string]): string var cFiles = newSeq[string](c.nodes.len) var tFiles = newSeq[string](c.nodes.len) # The `lower` stage writes a PROPER module NIF the cg/emit stages load via - # `toNifFilename` (a `.s.nif` sibling), so its `.t.nif` lives at the suffix base + # `toNifFilename` (a `.s.bif` sibling), so its `.t.bif` lives at the suffix base # (mirroring `semmedFile`), not next to the throwaway `.c`. for i, node in c.nodes: cFiles[i] = backendCFile(c, node) cnifFiles[i] = cFiles[i] & ".nif" - tFiles[i] = nimcache / node.files[0].modname & ".t.nif" + tFiles[i] = nimcache / node.files[0].modname & ".t.bif" # Only code-generate modules the real program actually reaches; statically # over-approximated nodes (e.g. `winlean` on Linux) are sem'd but not emitted. diff --git a/compiler/lambdalifting.nim b/compiler/lambdalifting.nim index 3418bad729..616f5c1148 100644 --- a/compiler/lambdalifting.nim +++ b/compiler/lambdalifting.nim @@ -309,6 +309,19 @@ proc markAsClosure(g: ModuleGraph; owner: PSym; n: PNode) = [s.name.s, owner.name.s, $owner.typ.callConv]) unsealForTransform(owner.typ) incl(owner.typ, tfCapturesEnv) + # A closure proc type that captures an env owns a REF to it: copying the closure + # value must incref the env and destroying it must decref. That is exactly what + # `tfHasAsgn` signals to `injectDestructorCalls` (so a closure assignment becomes + # `=copy`, not a raw field store). Set it HERE, at closure-type creation, so the + # flag is DETERMINISTIC and serializes with the type (writeTypeDef) — rather than + # depending on it being set as a side effect of the first `createTypeBoundOps` + # lift (liftdestructors ~1498, "XXX Breaks IC!"). Under IC the per-module `lower` + # stage is a separate process that lowers routines in index order; if a consumer + # (e.g. `workNimAsyncContinue`) was lowered before the closure type's ops were + # lifted, the env store emitted a RAW assign with no incref → the env was freed + # before the async callback ran → "yielded `nil`". Setting it at creation fixes + # that for both the in-process and the loaded (`.t.bif`) consumer. + incl(owner.typ, tfHasAsgn) if not isEnv: owner.typ.callConv = ccClosure diff --git a/compiler/mangleutils.nim b/compiler/mangleutils.nim index 7bb633f61d..d9215135a8 100644 --- a/compiler/mangleutils.nim +++ b/compiler/mangleutils.nim @@ -61,12 +61,22 @@ proc mangleProcNameExt*(graph: ModuleGraph, s: PSym): string = # starts with an EMPTY per-name disamb table, so its `disamb` restarts at 0 # and collides with same-named sem-time symbols loaded from NIFs (two # `=destroy` hooks both mangling to `_u2` → "conflicting types for ..." in - # the generated C). These symbols never cross a process boundary (nifc + # the generated C). Most such symbols never cross a process boundary (nifc # lifts, emits and compiles them in one run), so the per-module-unique # item id is a safe and deterministic discriminator; the `_c` marker keeps # the namespace disjoint from `_u`. result = "_c" - result.addInt s.itemId.item + if (s.disamb and HookDisambBit) != 0'i32: + # EXCEPTION: a backend-minted sym whose `disamb` is content-derived + # (setHookDisamb gave it HookDisambBit) — e.g. the `rttiDestroy` wrapper — + # DOES cross process boundaries: its C name is baked into the type's RTTI + # table, which is emit-everywhere and merge-deduped, so one process's + # `_c` (a per-process backend counter) ends up referenced while the + # wrapper is defined with another's → undefined at link (`rttiDestroy_c23`). + # The content-derived disamb is stable across processes; use it. + result.addInt s.disamb + else: + result.addInt s.itemId.item else: result = "_u" # Use `disamb` rather than `itemId.item`: under incremental compilation a diff --git a/compiler/nifbackend.nim b/compiler/nifbackend.nim index 9e90aa05e8..c58b61864c 100644 --- a/compiler/nifbackend.nim +++ b/compiler/nifbackend.nim @@ -27,6 +27,7 @@ import ast, options, lineinfos, modulegraphs, cgendata, cgen, cnif from cgmeth import generateIfMethodDispatchers from transf import transformBody +from injectdestructors import injectDestructorCalls import ic / replayer proc loadModuleDependencies(g: ModuleGraph; mainFileIdx: FileIndex; @@ -156,7 +157,7 @@ proc signatureHasMetaType(t: PType; depth: int = 0): bool = for k in t.kids: if signatureHasMetaType(k, depth + 1): return true -proc ownsRuntimeRoutine(s: PSym; modPos: int): bool = +proc ownsRuntimeRoutine(s: PSym; modPos: int; forLowering = false): bool = ## A concrete, non-generic, runtime routine with a real body, OWNED by the ## module at `modPos`. Shared by the `cg` stage's owned-routine seeding (so a ## routine called only from other modules is still emitted by somebody) and @@ -178,12 +179,21 @@ proc ownsRuntimeRoutine(s: PSym; modPos: int): bool = ## A `{.closure.}` iterator IS a standalone runtime routine (unlike an inline ## iterator, which is expanded at each call site) and must be emitted by its ## owner — else a cross-module `for` over it links to nothing. + ## + ## `forLowering`: the `lower` stage must ALSO transform the generic INSTANCES + ## this module serializes into its `.t.bif` (each demander keeps its own copy, + ## `itemId.module == modPos`). The `.t.bif` is the authoritative backend + ## artifact — every routine `cg` emits must arrive with its lowered body baked + ## in, NEVER re-derived in `cg` (re-derivation on the partially-loaded backend + ## state is exactly what crashed `newSelector`). The `cg`/emit-everywhere path + ## keeps the `sfFromGeneric` exclusion (instances are still deduped by content + ## name at merge); only the body-producing `lower` pass relaxes it. s.itemId.module == modPos and (s.kind in {skProc, skFunc, skConverter, skMethod} or (s.kind == skIterator and s.typ != nil and s.typ.callConv == ccClosure)) and s.skipGenericOwner != nil and s.skipGenericOwner.kind == skModule and s.magic == mNone and - sfFromGeneric notin s.flags and + (forLowering or sfFromGeneric notin s.flags) and sfDispatcher notin s.flags and {sfForward, sfImportc, sfCompileTime, sfError} * s.flags == {} and s.typ != nil and not signatureHasMetaType(s.typ) and @@ -367,12 +377,34 @@ proc setNestedClosureBodies(g: ModuleGraph; idgen: IdGenerator; n: PNode; if n.kind == nkSym: let s = n.sym if s != nil and s.kind in routineKinds and s != owner and + s.skipGenericOwner != nil and s.skipGenericOwner.kind != skModule and not seen.containsOrIncl(s.id): - if s.ast != nil and getBody(g, s).kind != nkEmpty and - s.typ != nil and s.typ.callConv == ccClosure: - if s.transformedBody == nil: + # Covers ALL nested routines, not only ccClosure ones. A NIMCALL nested proc + # the async transform mints (e.g. workNimAsyncContinue) already has its + # lifted body set by the OWNER's transformBody, but it is NOT in the owned + # loop (owner is a proc, not the module). Without injecting it HERE it is + # serialized transform-only; cg loads it (wasLoaded) and skips injection, so + # a closure-env store stays a raw field assign with no incref -> the env is + # freed before the async callback runs -> "yielded nil". `seen` (shared + # across the owned loop) injects each routine exactly once. + if s.ast != nil and getBody(g, s).kind != nkEmpty: + # Only ccClosure routines are safe to `transformBody` standalone here; a + # nimcall nested proc already has its lifted body from the owner's lift, + # and transforming an arbitrary nested routine with no cached body crashes + # (not in a standalone-transformable state). + let weTransformed = s.transformedBody == nil and + s.typ != nil and s.typ.callConv == ccClosure + if weTransformed: s.transformedBody = transformBody(g, idgen, s, {}) - setNestedClosureBodies(g, idgen, s.transformedBody, s, seen) + if s.transformedBody != nil: + # Inject destructors so cg loads a fully-lowered body and never rebuilds + # (mirrors non-IC, which injects every nested proc separately). The + # importer `n2` skField collision this used to trigger is fixed at the + # NIF-naming layer (toNifSymName gives derived env fields a unique + # disamb), so injecting ccClosure nested procs here is safe. + if sfInjectDestructors in s.flags: + s.transformedBody = injectDestructorCalls(g, idgen, s, s.transformedBody) + setNestedClosureBodies(g, idgen, s.transformedBody, s, seen) else: for i in 0 ..< n.safeLen: setNestedClosureBodies(g, idgen, n[i], owner, seen) @@ -448,12 +480,19 @@ proc generateLowerStage(g: ModuleGraph; mainFileIdx: FileIndex) = # (`=destroy` etc.) into `g.opsLog`; snapshot its length so we serialize exactly # the ops THIS stage created (not those loaded from `.s.nif`). let opsLogStart = g.opsLog.len + # Shared across the owned loop so a nested routine reachable from more than one + # owner is transformed + destructor-injected EXACTLY once (double injection + # would emit two `=destroy`/`=copy` runs). + var seenNested = initIntSet() for s in moduleSymbolStubs(ast.program, FileIndex modPos): - if ownsRuntimeRoutine(s, modPos): - # `.s.nif` wins: a routine already transformed during sem (CT eval / macro / - # VM transform) carries its lowered body in the `.s.nif` slot — don't - # re-transform it here. - if s.transformedBody != nil: continue + if ownsRuntimeRoutine(s, modPos, forLowering = true): + # REUSE path (`icReuseSemLowering` ON): a routine already transformed during + # sem (CT eval / macro / VM transform) carries its lowered body in the + # `.s.nif` slot (loaded into `transformedBody`) — don't re-transform it. + # Default OFF: the slot is never loaded (see loadSymFromCursor), so + # `transformedBody` is nil here and we always re-derive below. See + # doc/ic_backend_simplify.md §6a/§6b. + if icReuseSemLowering(g.config) and s.transformedBody != nil: continue # A routine serialized as a forward-decl + impl pair (writeSymDef's # "separate forward declaration and implementation") loads as TWO syms; the # impl `s` we transform here can carry body entities (`result`, locals, @@ -469,9 +508,19 @@ proc generateLowerStage(g: ModuleGraph; mainFileIdx: FileIndex) = # Retain the transformed body on the sym so `writeSymDef` serializes it in # the routine's `(sd)` 2-way-body slot. s.transformedBody = transformBody(g, tb.idgen, s, {}) - # Cache the lifted body on nested ccClosure routines too, so a module-indexed - # nested closure serializes its lifted (capture-rewritten) body. - var seenNested = initIntSet() + # Run the destructor injection HERE so the `.t.bif` body is FULLY lowered: + # `injectDestructorCalls` is demand-driven (it decides where destructors go + # by move analysis) and LIFTS the type-bound ops it needs (e.g. a nested + # closure env's `=destroy`) into `g.opsLog` — which the `hooks` collection + # below then serializes. Done in `cg` instead, those ops were lifted per-cg + # process, owned by nobody, and emitted as a prototype-only → undefined at + # link (the `eqdestroy__c` gap). cg must NOT re-inject a loaded body + # (see genProcLvl3's `wasLoaded` gate) so this stays the single injection. + if sfInjectDestructors in s.flags: + s.transformedBody = injectDestructorCalls(g, tb.idgen, s, s.transformedBody) + # Cache the lifted+injected body on nested ccClosure routines too, so a + # module-indexed nested closure serializes its lifted (capture-rewritten, + # destructor-injected) body. setNestedClosureBodies(g, tb.idgen, s.transformedBody, s, seenNested) # Collect the hooks this stage lifted, and transform each hook ROUTINE's body # too (it is itself lowered into NIFC). The hooks' `(sd)` + transformed body go @@ -487,14 +536,18 @@ proc generateLowerStage(g: ModuleGraph; mainFileIdx: FileIndex) = hooks.add e # Transform the hook routine's body and cache it on the sym so `writeSymDef` # serializes it in the hook's `(sd)` transformed-body slot (`transformBody - # {}` returns the body but does not cache it). + # {}` returns the body but does not cache it). Inject the hook's own + # destructors here too (it can destroy fields/temporaries) so cg loads a + # fully-lowered hook and never re-injects. e.sym.transformedBody = transformBody(g, tb.idgen, e.sym, {}) + if sfInjectDestructors in e.sym.flags: + e.sym.transformedBody = injectDestructorCalls(g, tb.idgen, e.sym, e.sym.transformedBody) inc i # Re-serialize the whole module to its suffix-based `.t.nif` (the path # `toNifFilename` resolves for the cg/emit stages). `writeLoweredModule` seals # routines itself. let suffix = cachedModuleSuffix(g.config, FileIndex modPos) - let wholeArtifact = toGeneratedFile(g.config, AbsoluteFile(suffix), ".t.nif").string + let wholeArtifact = toGeneratedFile(g.config, AbsoluteFile(suffix), ".t.bif").string writeLoweredModule(ast.program, g.config, target, hooks, wholeArtifact) if isDefined(g.config, "icDceCheck"): stderr.writeLine "[icLower] " & extractFilename(wholeArtifact) & " " & diff --git a/compiler/nim.cfg b/compiler/nim.cfg index 19c1df344f..47b718b804 100644 --- a/compiler/nim.cfg +++ b/compiler/nim.cfg @@ -14,6 +14,10 @@ define:nimPreviewAsmSemSymbol define:nimPreviewCStringComparisons #define:nimPreviewDuplicateModuleError # Incompatible with Nimony's compat2.nim for now +# NOTE: `-d:virtualParRi` (jump-encoded ParLe + elided ParRi) is NOT yet enabled: +# the IC writer assembles buffers by raw token splicing (`dest.add content[i]`), +# which does not seal scopes the way `addParRi` does, so sealed `(stmts)` get +# jump=0 and serialize empty. Enabling it needs writer buffer-sealing work first. threads:off diff --git a/compiler/options.nim b/compiler/options.nim index 42c2aee49f..58486a606c 100644 --- a/compiler/options.nim +++ b/compiler/options.nim @@ -29,7 +29,7 @@ const nimEnableCovariance* = defined(nimEnableCovariance) - icFormatVersion* = "21" + icFormatVersion* = "23" ## Version of the IC cache format (the sem-NIF module layout written by ## ast2nif.nim plus the iface/impl/edges side files). Bump it whenever ## that layout changes: `commandIc` wipes a nimcache whose `ic.version` @@ -793,6 +793,17 @@ template quitOrRaise*(conf: ConfigRef, msg = "") = else: quit(msg) # quits with QuitFailure +proc icReuseSemLowering*(conf: ConfigRef): bool {.inline.} = + ## When ON, the per-module `lower` backend stage REUSES the VM/CT lowering that + ## sem cached in the `.s.nif` 2-way-body slot (the non-IC single-lowering + ## semantics) instead of re-deriving the transform. Default OFF: the backend + ## re-derives every body from the pristine semchecked body (simpler; allowed by + ## the 2026-06-27 spec that VM-requested frontend transforms need not influence + ## the backend). The switch exists so caching can be restored if a target (e.g. + ## Nimbus) depends on the cached lowering being reused, not re-derived. See + ## doc/ic_backend_simplify.md §6b. + isDefined(conf, "icReuseSemLowering") + proc importantComments*(conf: ConfigRef): bool {.inline.} = conf.ideActive or conf.cmd in cmdDocLike proc usesWriteBarrier*(conf: ConfigRef): bool {.inline.} = conf.selectedGC >= gcRefc proc usesSso*(conf: ConfigRef): bool {.inline.} = conf.selectedStrings == stringSso