@@ -15,7 +15,7 @@ import ast, idents, msgs, options
1515import lineinfos as astli
1616import pathutils
1717import " ../dist/nimony/src/lib" / [bitabs, nifstreams, nifcursors, lineinfos,
18- nifindexes]
18+ nifindexes, nifreader ]
1919import " ../dist/nimony/src/gear2" / modnames
2020
2121import icnif / [enum2nif]
@@ -522,19 +522,37 @@ proc loadBool(n: var Cursor): bool =
522522 raiseAssert " (true)/(false) expected"
523523
524524type
525+ NifModule = object
526+ stream: nifstreams.Stream
527+ symCounter: int32
528+ index: NifIndex
529+
525530 DecodeContext * = object
526531 infos: LineInfoWriter
527532 moduleIds: Table [string , int32 ]
528533 types: Table [ItemId , (PType , NifIndexEntry )]
529534 syms: Table [ItemId , (PSym , NifIndexEntry )]
530- indexes: seq [NifIndex ]
531- symCounters: Table [int32 , int32 ] # Module ID -> counter
535+ mods: seq [NifModule ]
532536 cache: IdentCache
537+ moduleToNifSuffix: Table [FileIndex , string ]
533538
534539proc createDecodeContext * (config: ConfigRef ; cache: IdentCache ): DecodeContext =
535540 # # Supposed to be a global variable
536541 result = DecodeContext (infos: LineInfoWriter (config: config), cache: cache)
537542
543+ proc idToIdx (x: int32 ): int {.inline .} =
544+ assert x <= - 2 'i32
545+ result = - (x+ 2 )
546+
547+ proc cursorFromIndexEntry (c: var DecodeContext ; module: int32 ; entry: NifIndexEntry ;
548+ buf: var TokenBuf ): Cursor =
549+ let m = idToIdx (module)
550+ let s = addr c.mods[m].stream
551+ s.r.jumpTo entry.offset
552+ var buf = createTokenBuf (30 )
553+ nifcursors.parse (s[], buf, entry.info)
554+ result = cursorAt (buf, 0 )
555+
538556proc moduleId (c: var DecodeContext ; suffix: string ): int32 =
539557 # We don't know the "real" FileIndex due to our mapping to a short "Module suffix"
540558 # This is not a problem, we use negative `ItemId.module` values here and then
@@ -543,13 +561,16 @@ proc moduleId(c: var DecodeContext; suffix: string): int32 =
543561 result = c.moduleIds.getOrDefault (suffix)
544562 if result == 0 :
545563 result = - int32 (c.moduleIds.len + 2 ) # negative index!
564+ let modFile = (getNimcacheDir (c.infos.config) / RelativeFile (suffix & " .nif" )).string
565+ let idxFile = (getNimcacheDir (c.infos.config) / RelativeFile (suffix & " .idx.nif" )).string
546566 c.moduleIds[suffix] = result
547- c.indexes.add readIndex ((getNimcacheDir (c.infos.config) / RelativeFile (suffix & " .idx.nif" )).string )
567+ c.mods.add NifModule (stream: nifstreams.open (modFile), index: readIndex (idxFile))
568+ assert c.mods.len- 1 == idToIdx (result )
548569
549570proc getOffset (c: var DecodeContext ; module: int32 ; nifName: string ): NifIndexEntry =
550571 assert module < 0 'i32
551- let index = ( - module) - 2 'i32
552- let ii = addr c.indexes [index]
572+ let index = idToIdx ( module)
573+ let ii = addr c.mods [index].index
553574 result = ii.public.getOrDefault (nifName)
554575 if result .offset == 0 :
555576 result = ii.private.getOrDefault (nifName)
@@ -594,7 +615,7 @@ proc loadSymStub(c: var DecodeContext; t: SymId): PSym =
594615 let symAsStr = pool.syms[t]
595616 let sn = parseSymName (symAsStr)
596617 let module = moduleId (c, sn.module)
597- let val = addr c.symCounters. mgetOrPut (module, 0 )
618+ let val = addr c.mods[ idToIdx (module)].symCounter
598619 inc val[]
599620
600621 let id = ItemId (module: module, item: val[])
@@ -661,7 +682,8 @@ proc loadLoc(c: var DecodeContext; n: var Cursor; loc: var TLoc) =
661682
662683proc loadType * (c: var DecodeContext ; t: PType ) =
663684 if t.kind != tyStub: return
664- var n = default (Cursor ) # getCursorAt()
685+ var buf = createTokenBuf (30 )
686+ var n = cursorFromIndexEntry (c, t.itemId.module, c.types[t.itemId][1 ], buf)
665687
666688 expect n, ParLe
667689 if n.tagId != tdefTag:
@@ -711,7 +733,8 @@ proc loadAnnex(c: var DecodeContext; n: var Cursor): PLib =
711733
712734proc loadSym * (c: var DecodeContext ; s: PSym ) =
713735 if s.kind != skStub: return
714- var n = default (Cursor ) # getCursorAt()
736+ var buf = createTokenBuf (30 )
737+ var n = cursorFromIndexEntry (c, s.itemId.module, c.syms[s.itemId][1 ], buf)
715738
716739 expect n, ParLe
717740 if n.tagId != sdefTag:
@@ -841,13 +864,19 @@ proc loadNode(c: var DecodeContext; n: var Cursor): PNode =
841864 raiseAssert " Not yet implemented " & $ n.kind
842865
843866
844- proc loadNifModule * (config: ConfigRef ; f: FileIndex ): PNode =
845- var moduleToNifSuffix = initTable [FileIndex , string ]()
867+ proc loadNifModule * (c: var DecodeContext ; f: FileIndex ): PNode =
868+ let moduleSuffix = modname (c.moduleToNifSuffix, f.int , c.infos.config)
869+ let modFile = toGeneratedFile (c.infos.config, AbsoluteFile (moduleSuffix), " .nif" ).string
846870
847- let m = modname (moduleToNifSuffix, f.int , config)
848- let d = toGeneratedFile (config, AbsoluteFile (m), " .nif" ).string
849-
850- result = nil
871+ var buf = createTokenBuf (300 )
872+ var s = nifstreams.open (modFile)
873+ # XXX We can optimize this here and only load the top level entries!
874+ try :
875+ nifcursors.parse (s, buf, NoLineInfo )
876+ finally :
877+ nifstreams.close (s)
878+ var n = cursorAt (buf, 0 )
879+ result = loadNode (c, n)
851880
852881when isMainModule :
853882 import std / syncio
0 commit comments