From 25122dac3047b3b02e82be5490a757470bf45d95 Mon Sep 17 00:00:00 2001 From: araq Date: Sat, 6 Dec 2025 14:29:08 +0100 Subject: [PATCH] IC: compilerprocs mechanism --- compiler/ast2nif.nim | 24 +++++++++++++++++++++++- compiler/main.nim | 1 + compiler/modulegraphs.nim | 31 ++++++++++++++++++++++++++++--- compiler/nifbackend.nim | 21 ++++++++++++++++----- 4 files changed, 68 insertions(+), 9 deletions(-) diff --git a/compiler/ast2nif.nim b/compiler/ast2nif.nim index d7ed56b87f..d7da6c50cd 100644 --- a/compiler/ast2nif.nim +++ b/compiler/ast2nif.nim @@ -1420,10 +1420,32 @@ proc resolveHookSym*(c: var DecodeContext; symId: nifstreams.SymId): PSym = disamb: sn.count.int32, state: Partial) c.syms[id] = (result, offs) +proc tryResolveCompilerProc*(c: var DecodeContext; name: string; moduleFileIdx: FileIndex): PSym = + ## Tries to resolve a compiler proc from a module by checking the NIF index. + ## Returns nil if the symbol doesn't exist. + let suffix = moduleSuffix(c.infos.config, moduleFileIdx) + let symName = name & ".0." & suffix + + # Check if module index is loaded, if not load it + let module = moduleId(c, suffix) + + # Check if symbol exists in the index (check both public and private) + var offs = c.mods[module].index.public.getOrDefault(symName) + if offs.offset == 0: + offs = c.mods[module].index.private.getOrDefault(symName) + if offs.offset == 0: + return nil + + # Get or create the SymId for this symbol name + let symId = pool.syms.getOrIncl(symName) + # Now resolve it - this will create the PSym stub + result = resolveHookSym(c, symId) + proc loadNifModule*(c: var DecodeContext; f: FileIndex; interf, interfHidden: var TStrTable; hooks: var Table[nifstreams.SymId, HooksPerType]; converters: var seq[(string, string)]; - classes: var seq[ClassIndexEntry]): PNode = + classes: var seq[ClassIndexEntry]; + loadFullAst: bool = false): PNode = let suffix = moduleSuffix(c.infos.config, f) # Ensure module index is loaded - moduleId returns the FileIndex for this suffix diff --git a/compiler/main.nim b/compiler/main.nim index 8aecccc488..d63c7d3fbf 100644 --- a/compiler/main.nim +++ b/compiler/main.nim @@ -447,6 +447,7 @@ proc mainCommand*(graph: ModuleGraph) = of cmdNifC: # Generate C code from NIF files wantMainModule(conf) + setOutFile(conf) commandNifC(graph) of cmdDeps: # Generate .build.nif for nifmake diff --git a/compiler/modulegraphs.nim b/compiler/modulegraphs.nim index afb98d67c2..b52ee9f4f0 100644 --- a/compiler/modulegraphs.nim +++ b/compiler/modulegraphs.nim @@ -435,7 +435,30 @@ proc copyTypeProps*(g: ModuleGraph; module: int; dest, src: PType) = proc loadCompilerProc*(g: ModuleGraph; name: string): PSym = result = nil - if g.config.symbolFiles == disabledSf: return nil + if g.config.symbolFiles == disabledSf: + # For NIF-based compilation, search in loaded NIF modules + when not defined(nimKochBootstrap): + # Only try to resolve from NIF if we're actually using NIF files (cmdNifC) + if g.config.cmd == cmdNifC: + # First try system module (most compilerprocs are there) + let systemFileIdx = g.config.m.systemFileIdx + if systemFileIdx != InvalidFileIdx: + result = tryResolveCompilerProc(ast.program, name, systemFileIdx) + if result != nil: + strTableAdd(g.compilerprocs, result) + return result + + # Try threadpool module (some compilerprocs like FlowVar are there) + # Find threadpool module by searching loaded modules + for moduleIdx in 0..