Skip to content

Commit 43ff7b1

Browse files
committed
loads modules imported by system module and create module sym before decode
1 parent 54aa552 commit 43ff7b1

File tree

3 files changed

+47
-18
lines changed

3 files changed

+47
-18
lines changed

compiler/icnif/nifdecoder.nim

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ proc fromNifSymDef(c: var DecodeContext; n: var Cursor): PSym =
129129
# PNode, PSym or PType type fields in PSym can have cycles.
130130
# Add PSym to `c.symbols` before parsing these fields so that
131131
# they can refer this PSym.
132-
let nifItemId = ItemId(module: nifModId, item: itemId)
132+
let nifItemId = ItemId(module: itemIdModule.int32, item: itemId)
133133
assert nifItemId notin c.symbols
134134
c.symbols[nifItemId] = result
135135

@@ -231,10 +231,10 @@ proc fromNifSymbol(c: var DecodeContext; n: var Cursor): PSym =
231231
result = c.fromNifSymDef n
232232
elif n.tagId == symTag:
233233
incExpect n, IntLit
234-
let nifModId = pool.integers[n.intId].int32
234+
let nifModId = c.modules[pool.integers[n.intId].int32]
235235
incExpect n, IntLit
236236
let item = pool.integers[n.intId].int32
237-
let nifItemId = ItemId(module: nifModId, item: item)
237+
let nifItemId = ItemId(module: nifModId.int32, item: item)
238238
result = c.symbols[nifItemId]
239239
inc n
240240
skipParRi n
@@ -356,9 +356,33 @@ proc loadNif(stream: var Stream; graph: ModuleGraph; prog: NifProgram): PNode =
356356

357357
var buf = fromStream(stream)
358358
var n = beginRead(buf)
359-
360359
var c = DecodeContext(graph: graph, prog: prog)
361360

361+
var n2 = n
362+
var nested = 0
363+
while true:
364+
if n2.info != NoLineInfo:
365+
break
366+
elif n2.kind == EofToken:
367+
break
368+
elif n2.kind == ParLe:
369+
inc nested
370+
elif n2.kind == ParRi:
371+
dec nested
372+
if nested == 0: break
373+
inc n2
374+
assert n2.info != NoLineInfo
375+
let info = pool.man.unpack(n2.info)
376+
let fileIdx = c.graph.config.fileInfoIdx(pool.files[info.file].AbsoluteFile)
377+
var currentModule = graph.newModule(fileIdx)
378+
if currentModule.itemId.module == 0'i32:
379+
currentModule.flags = {sfMainModule, sfSystemModule}
380+
381+
c.symbols[currentModule.itemId] = currentModule
382+
let nifSym = toNifSym(currentModule, c.prog.moduleToNifSuffix, c.graph.config)
383+
let symId = pool.syms.getOrIncl(nifSym)
384+
c.prog.nifSymIdToPSym[symId] = currentModule
385+
362386
result = fromNif(c, n)
363387

364388
endRead(buf)

compiler/icnif/nifencoder.nim

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ proc initEncodeContext(conf: ConfigRef; currentModule: PSym): EncodeContext =
1717
result = EncodeContext(conf: conf,
1818
currentModule: currentModule,
1919
dest: createTokenBuf())
20+
result.decodedSyms.incl(currentModule.itemId)
2021

2122
template buildTree(dest: var TokenBuf; tag: TagId; body: untyped) =
2223
dest.addParLe tag

tests/icnif/tencode_node2node.nim

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,16 @@ proc newModuleGraphForSem(cache: IdentCache; conf: ConfigRef): ModuleGraph =
2525
graph.config.cmd = oldCmd
2626
result = graph
2727

28-
proc getSystemNif(graph: ModuleGraph): string =
29-
assert graph.systemModule != nil
30-
assert graph.systemModule.kind == skModule
31-
assert graph.systemModule.ast != nil
32-
33-
let n = graph.systemModule.ast
34-
# if nil is not assigned, it generates large NIF
35-
graph.systemModule.ast = nil
36-
result = saveNifToBuffer(n, graph.config, graph.systemModule)
37-
#writeFile("system.nif", result)
28+
proc getSystemNif(graph: ModuleGraph): seq[string] =
29+
result = newSeqOfCap[string](graph.ifaces.len)
30+
for i, iface in graph.ifaces.mpairs:
31+
if iface.module != nil:
32+
let n = iface.module.ast
33+
assert n != nil
34+
# if nil is not assigned, it generates large NIF
35+
iface.module.ast = nil
36+
result.add saveNifToBuffer(n, graph.config, iface.module)
37+
#writeFile(iface.module.name.s & ".nif", result[^1])
3838

3939
proc sem(graph: ModuleGraph; path: AbsoluteFile): (PNode, PSym) =
4040
result = (nil, nil)
@@ -169,10 +169,12 @@ proc eql(x, y: PSym; c: var EqlContext): bool =
169169
elif x.magic != y.magic:
170170
echo "symbol magic mismatch: ", x.magic, "/", y.magic
171171
result = false
172-
elif not eql(x.info, y.info, c):
172+
elif x.kind != skPackage and not eql(x.info, y.info, c):
173+
# fileIndex of info of skPackage is just a path of first semchecked module in the package
173174
echo "symbol line info mismatch"
174175
result = false
175-
elif x.flags != y.flags:
176+
elif x.kind != skModule and x.flags != y.flags:
177+
# TODO: check the flag of skModule
176178
echo "symbol flag mismatch: ", x.flags, "/", y.flags
177179
result = false
178180
elif x.options != y.options:
@@ -360,10 +362,11 @@ proc eql(x, y: PNode; c: var EqlContext): bool =
360362
debug(y)
361363
result = false
362364

363-
proc testNifEncDec(graph: ModuleGraph; src: string; systemNif: string) =
365+
proc testNifEncDec(graph: ModuleGraph; src: string; systemNif: openArray[string]) =
364366
let fullPath = TestCodeDir / RelativeFile(src)
365367
let (n, module) = sem(graph, fullPath)
366368
assert n != nil, "failed to sem " & $fullPath
369+
assert module.owner.kind == skPackage
367370

368371
#debug(n)
369372
let nif = saveNifToBuffer(n, graph.config, module)
@@ -374,7 +377,8 @@ proc testNifEncDec(graph: ModuleGraph; src: string; systemNif: string) =
374377
# Don't reuse the ModuleGraph used for semcheck when load NIF.
375378
var graphForLoad = newModuleGraph(newIdentCache(), newConfigRefForTest())
376379
var prog = NifProgram()
377-
discard loadNifFromBuffer(systemNif, graphForLoad, prog)
380+
for sysNif in systemNif:
381+
discard loadNifFromBuffer(sysNif, graphForLoad, prog)
378382
let n2 = loadNifFromBuffer(nif, graphForLoad, prog)
379383
#debug(n2)
380384
var c = EqlContext(confX: graph.config, confY: graphForLoad.config)

0 commit comments

Comments
 (0)