From 88685fded1216fde8c0aa14d3713d2998473e32e Mon Sep 17 00:00:00 2001 From: Araq Date: Tue, 16 Dec 2025 11:56:31 +0100 Subject: [PATCH] progress --- compiler/modulegraphs.nim | 4 +++- compiler/nifbackend.nim | 20 +++++++++++++------- compiler/typekeys.nim | 3 ++- 3 files changed, 18 insertions(+), 9 deletions(-) diff --git a/compiler/modulegraphs.nim b/compiler/modulegraphs.nim index cac48aae55..67abc12070 100644 --- a/compiler/modulegraphs.nim +++ b/compiler/modulegraphs.nim @@ -368,11 +368,13 @@ proc getAttachedOp*(g: ModuleGraph; t: PType; op: TTypeAttachedOp): PSym = ## if no such operation exists. if g.attachedOps[op].contains(t.itemId): result = resolveAttachedOp(g, g.attachedOps[op][t.itemId]) - else: + elif g.config.cmd in {cmdNifC, cmdM}: # Fall back to key-based lookup for NIF-loaded hooks let key = typeKey(t, g.config, loadTypeCallback, loadSymCallback) result = g.loadedOps[op].getOrDefault(key) #echo "fallback ", key, " ", op, " ", result + else: + result = nil proc setAttachedOp*(g: ModuleGraph; module: int; t: PType; op: TTypeAttachedOp; value: PSym) = ## we also need to record this to the packed module. diff --git a/compiler/nifbackend.nim b/compiler/nifbackend.nim index 65f2eee276..3d77afcb4c 100644 --- a/compiler/nifbackend.nim +++ b/compiler/nifbackend.nim @@ -82,14 +82,13 @@ proc generateCode*(g: ModuleGraph; mainFileIdx: FileIndex) = resetForBackend(g) let mainModule = g.getModule(mainFileIdx) - # Also ensure system module is set up and generated if it exists - if g.systemModule != nil and g.systemModule != mainModule: - let systemBmod = BModuleList(g.backend).modules[g.systemModule.position] - if systemBmod == nil: - discard setupNifBackendModule(g, g.systemModule) - generateCodeForModule(g, g.systemModule) + # Load system module first - it's always needed and contains essential hooks + var cachedModules: seq[FileIndex] = @[] + if g.config.m.systemFileIdx != InvalidFileIdx: + discard moduleFromNifFile(g, g.config.m.systemFileIdx, cachedModules) # Load all modules in dependency order using stack traversal + # This must happen BEFORE any code generation so that hooks are loaded into loadedOps let modules = loadModuleDependencies(g, mainFileIdx) if modules.len == 0: rawMessage(g.config, errGenerated, @@ -100,10 +99,17 @@ proc generateCode*(g: ModuleGraph; mainFileIdx: FileIndex) = for module in modules: discard setupNifBackendModule(g, module) + # Also ensure system module is set up and generated first if it exists + if g.systemModule != nil and g.systemModule != mainModule: + let systemBmod = BModuleList(g.backend).modules[g.systemModule.position] + if systemBmod == nil: + discard setupNifBackendModule(g, g.systemModule) + generateCodeForModule(g, g.systemModule) + # Generate code for all modules except main (main goes last) # This ensures all modules are added to modulesClosed for module in modules: - if module != mainModule: + if module != mainModule and module != g.systemModule: generateCodeForModule(g, module) # Generate main module last (so all init procs are registered) diff --git a/compiler/typekeys.nim b/compiler/typekeys.nim index c58a0377ed..06e633b317 100644 --- a/compiler/typekeys.nim +++ b/compiler/typekeys.nim @@ -250,7 +250,8 @@ proc typeKey(c: var Context; t: PType; flags: set[ConsiderFlag]; conf: ConfigRef of tyStatic: withTree c.m, toNifTag(t.kind): c.treeKey(t.nImpl, {}, conf) - c.typeKey(t.skipModifierB, flags, conf) + if t.sonsImpl.len > 0: + c.typeKey(t.skipModifierB, flags, conf) of tyProc: withTree c.m, (if tfIterator in t.flagsImpl: "itertype" else: "proctype"): if CoProc in flags and t.nImpl != nil: