mirror of
https://github.com/nim-lang/Nim.git
synced 2026-08-25 16:11:44 +00:00
IC: code generation progress
This commit is contained in:
@@ -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):
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user