diff --git a/compiler/cgen.nim b/compiler/cgen.nim index c271cbda31..58d5b44376 100644 --- a/compiler/cgen.nim +++ b/compiler/cgen.nim @@ -1717,9 +1717,12 @@ proc genMainProcs(m: BModule) = proc genMainProcsWithResult(m: BModule) = genMainProcs(m) - var res = "nim_program_result" - if m.hcrOn: res = cDeref(res) - m.s[cfsProcs].addReturn(res) + if m.config.cmd != cmdNifC: + var res = "nim_program_result" + if m.hcrOn: res = cDeref(res) + m.s[cfsProcs].addReturn(res) + else: + m.s[cfsProcs].addReturn(cIntValue(0)) proc genNimMainInner(m: BModule) = m.s[cfsProcs].addDeclWithVisibility(Private): diff --git a/compiler/nifbackend.nim b/compiler/nifbackend.nim index b061591bf8..2f65ffe220 100644 --- a/compiler/nifbackend.nim +++ b/compiler/nifbackend.nim @@ -56,26 +56,30 @@ proc setupNifBackendModule(g: ModuleGraph; module: PSym): BModule = g.backend = cgendata.newModuleList(g) result = cgen.newModule(BModuleList(g.backend), module, g.config, idGeneratorFromModule(module)) -proc generateCodeForModule(g: ModuleGraph; module: PSym) = - ## Generate C code for a single module. - let moduleId = module.position - var bmod = BModuleList(g.backend).modules[moduleId] - if bmod == nil: - bmod = setupNifBackendModule(g, module) - - # Generate code for the module's top-level statements - if module.ast != nil: - cgen.genTopLevelStmt(bmod, module.ast) - +proc finishModule(g: ModuleGraph; bmod: BModule) = # Finalize the module (this adds it to modulesClosed) # Create an empty stmt list as the init body - genInitCode in writeModule will set it up properly - let initStmt = newNodeI(nkStmtList, module.info) + let initStmt = newNode(nkStmtList) finalCodegenActions(g, bmod, initStmt) # Generate dispatcher methods for disp in getDispatchers(g): genProcLvl3(bmod, disp) +proc generateCodeForModule(g: ModuleGraph; module: PSym) = + ## Generate C code for a single module. + when false: + let moduleId = module.position + var bmod = BModuleList(g.backend).modules[moduleId] + if bmod == nil: + bmod = setupNifBackendModule(g, module) + + # Generate code for the module's top-level statements + if module.ast != nil: + cgen.genTopLevelStmt(bmod, module.ast) + + finishModule(g, bmod) + proc generateCode*(g: ModuleGraph; mainFileIdx: FileIndex) = ## Main entry point for NIF-based C code generation. ## Traverses the module dependency graph and generates C code. @@ -110,6 +114,7 @@ proc generateCode*(g: ModuleGraph; mainFileIdx: FileIndex) = # 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 and module != g.systemModule: generateCodeForModule(g, module) @@ -118,6 +123,11 @@ proc generateCode*(g: ModuleGraph; mainFileIdx: FileIndex) = if mainModule != nil: generateCodeForModule(g, mainModule) + for m in BModuleList(g.backend).modules: + if m != nil: + assert m.module != nil + finishModule g, m + # Write C files if g.backend != nil: cgenWriteModules(g.backend, g.config)