Skip to content

Commit 0486a2d

Browse files
authored
IC progress (#25283)
bugfix: produce the required nimcache subdir
1 parent 6543040 commit 0486a2d

File tree

16 files changed

+362
-172
lines changed

16 files changed

+362
-172
lines changed

compiler/ast.nim

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,16 @@ proc ensureMutable*(t: PType) {.inline.} =
5757
assert t.state != Sealed
5858
if t.state == Partial: loadType(t)
5959

60+
proc backendEnsureMutable*(s: PSym) {.inline.} =
61+
#assert s.state != Sealed
62+
# ^ IC review this later
63+
if s.state == Partial: loadSym(s)
64+
65+
proc backendEnsureMutable*(t: PType) {.inline.} =
66+
#assert t.state != Sealed
67+
# ^ IC review this later
68+
if t.state == Partial: loadType(t)
69+
6070
proc owner*(s: PSym): PSym {.inline.} =
6171
if s.state == Partial: loadSym(s)
6272
result = s.ownerFieldImpl

compiler/ast2nif.nim

Lines changed: 62 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import std / [assertions, tables, sets]
1313
from std / strutils import startsWith
1414
import astdef, idents, msgs, options
1515
import lineinfos as astli
16-
import pathutils
16+
import pathutils #, modulegraphs
1717
import "../dist/nimony/src/lib" / [bitabs, nifstreams, nifcursors, lineinfos,
1818
nifindexes, nifreader]
1919
import "../dist/nimony/src/gear2" / modnames
@@ -258,6 +258,10 @@ proc writeLib(w: var Writer; dest: var TokenBuf; lib: PLib) =
258258
proc writeSymDef(w: var Writer; dest: var TokenBuf; sym: PSym) =
259259
dest.addParLe sdefTag, trLineInfo(w, sym.infoImpl)
260260
dest.addSymDef pool.syms.getOrIncl(w.toNifSymName(sym)), NoLineInfo
261+
if sfExported in sym.flagsImpl:
262+
dest.addIdent "x"
263+
else:
264+
dest.addDotToken
261265
if sym.magicImpl == mNone:
262266
dest.addDotToken
263267
else:
@@ -352,14 +356,14 @@ proc trInclude(w: var Writer; n: PNode) =
352356
w.deps.addParRi
353357

354358
proc trImport(w: var Writer; n: PNode) =
355-
w.deps.addParLe pool.tags.getOrIncl(toNifTag(n.kind)), trLineInfo(w, n.info)
356359
for child in n:
357-
assert child.kind == nkSym
358-
let s = child.sym
359-
assert s.kindImpl == skModule
360-
let fp = toFullPath(w.infos.config, s.positionImpl.FileIndex)
361-
w.deps.addStrLit fp
362-
w.deps.addParRi
360+
if child.kind == nkSym:
361+
w.deps.addParLe pool.tags.getOrIncl(toNifTag(n.kind)), trLineInfo(w, n.info)
362+
let s = child.sym
363+
assert s.kindImpl == skModule
364+
let fp = toFullPath(w.infos.config, s.positionImpl.FileIndex)
365+
w.deps.addStrLit fp
366+
w.deps.addParRi
363367

364368
proc writeNode(w: var Writer; dest: var TokenBuf; n: PNode) =
365369
if n == nil:
@@ -421,6 +425,7 @@ proc writeNode(w: var Writer; dest: var TokenBuf; n: PNode) =
421425
var ast = n
422426
if n[namePos].kind == nkSym:
423427
ast = n[namePos].sym.astImpl
428+
if ast == nil: ast = n
424429
w.withNode dest, ast:
425430
# Process body and other parts
426431
for i in 0 ..< ast.len:
@@ -463,7 +468,8 @@ proc writeNifModule*(config: ConfigRef; thisModule: int32; n: PNode) =
463468
inner.addParRi()
464469

465470
let m = modname(w.moduleToNifSuffix, w.currentModule, w.infos.config)
466-
let d = toGeneratedFile(config, AbsoluteFile(m), ".nif").string
471+
let nifFilename = AbsoluteFile(m).changeFileExt(".nif")
472+
let d = completeGeneratedFilePath(config, nifFilename).string
467473

468474
var dest = createTokenBuf(600)
469475
dest.addParLe pool.tags.getOrIncl(toNifTag(nkStmtList)), rootInfo
@@ -472,7 +478,8 @@ proc writeNifModule*(config: ConfigRef; thisModule: int32; n: PNode) =
472478
dest.add inner
473479
dest.addParRi()
474480

475-
writeFileAndIndex d, dest
481+
writeFile(dest, d)
482+
createIndex(d, false, dest[0].info)
476483

477484

478485
# --------------------------- Loader (lazy!) -----------------------------------------------
@@ -536,48 +543,37 @@ type
536543

537544
DecodeContext* = object
538545
infos: LineInfoWriter
539-
moduleIds: Table[string, int32]
546+
#moduleIds: Table[string, int32]
540547
types: Table[ItemId, (PType, NifIndexEntry)]
541548
syms: Table[ItemId, (PSym, NifIndexEntry)]
542549
mods: seq[NifModule]
543550
cache: IdentCache
544-
moduleToNifSuffix: Table[FileIndex, string]
551+
#moduleToNifSuffix: Table[FileIndex, string]
545552

546553
proc createDecodeContext*(config: ConfigRef; cache: IdentCache): DecodeContext =
547554
## Supposed to be a global variable
548555
result = DecodeContext(infos: LineInfoWriter(config: config), cache: cache)
549556

550-
proc idToIdx(x: int32): int {.inline.} =
551-
assert x <= -2'i32
552-
result = -(x+2)
553-
554-
proc cursorFromIndexEntry(c: var DecodeContext; module: int32; entry: NifIndexEntry;
557+
proc cursorFromIndexEntry(c: var DecodeContext; module: FileIndex; entry: NifIndexEntry;
555558
buf: var TokenBuf): Cursor =
556-
let m = idToIdx(module)
557-
let s = addr c.mods[m].stream
559+
let s = addr c.mods[module.int32].stream
558560
s.r.jumpTo entry.offset
559561
var buf = createTokenBuf(30)
560562
nifcursors.parse(s[], buf, entry.info)
561563
result = cursorAt(buf, 0)
562564

563-
proc moduleId(c: var DecodeContext; suffix: string): int32 =
564-
# We don't know the "real" FileIndex due to our mapping to a short "Module suffix"
565-
# This is not a problem, we use negative `ItemId.module` values here and then
566-
# there is no interference with in-memory-modules. Modulegraphs.nim already uses -1
567-
# so we start at -2 here.
568-
result = c.moduleIds.getOrDefault(suffix)
569-
if result == 0:
570-
result = -int32(c.moduleIds.len + 2) # negative index!
565+
proc moduleId(c: var DecodeContext; suffix: string): FileIndex =
566+
var isKnownFile = false
567+
result = c.infos.config.registerNifSuffix(suffix, isKnownFile)
568+
if not isKnownFile:
571569
let modFile = (getNimcacheDir(c.infos.config) / RelativeFile(suffix & ".nif")).string
572570
let idxFile = (getNimcacheDir(c.infos.config) / RelativeFile(suffix & ".idx.nif")).string
573-
c.moduleIds[suffix] = result
574-
c.mods.add NifModule(stream: nifstreams.open(modFile), index: readIndex(idxFile))
575-
assert c.mods.len-1 == idToIdx(result)
576-
577-
proc getOffset(c: var DecodeContext; module: int32; nifName: string): NifIndexEntry =
578-
assert module < 0'i32
579-
let index = idToIdx(module)
580-
let ii = addr c.mods[index].index
571+
if result.int >= c.mods.len:
572+
c.mods.setLen(result.int + 1)
573+
c.mods[result.int] = NifModule(stream: nifstreams.open(modFile), index: readIndex(idxFile))
574+
575+
proc getOffset(c: var DecodeContext; module: FileIndex; nifName: string): NifIndexEntry =
576+
let ii = addr c.mods[module.int32].index
581577
result = ii.public.getOrDefault(nifName)
582578
if result.offset == 0:
583579
result = ii.private.getOrDefault(nifName)
@@ -601,10 +597,10 @@ proc loadTypeStub(c: var DecodeContext; t: SymId): PType =
601597
inc i
602598
if i < name.len and name[i] == '.': inc i
603599
let suffix = name.substr(i)
604-
let id = ItemId(module: moduleId(c, suffix), item: itemId)
600+
let id = ItemId(module: moduleId(c, suffix).int32, item: itemId)
605601
result = c.types.getOrDefault(id)[0]
606602
if result == nil:
607-
let offs = c.getOffset(id.module, name)
603+
let offs = c.getOffset(id.module.FileIndex, name)
608604
result = PType(itemId: id, uniqueId: id, kind: TTypeKind(k), state: Partial)
609605
c.types[id] = (result, offs)
610606

@@ -627,10 +623,10 @@ proc loadSymStub(c: var DecodeContext; t: SymId): PSym =
627623
let symAsStr = pool.syms[t]
628624
let sn = parseSymName(symAsStr)
629625
let module = moduleId(c, sn.module)
630-
let val = addr c.mods[idToIdx(module)].symCounter
626+
let val = addr c.mods[module.int32].symCounter
631627
inc val[]
632628

633-
let id = ItemId(module: module, item: val[])
629+
let id = ItemId(module: module.int32, item: val[])
634630
result = c.syms.getOrDefault(id)[0]
635631
if result == nil:
636632
let offs = c.getOffset(module, symAsStr)
@@ -696,7 +692,7 @@ proc loadType*(c: var DecodeContext; t: PType) =
696692
if t.state != Partial: return
697693
t.state = Sealed
698694
var buf = createTokenBuf(30)
699-
var n = cursorFromIndexEntry(c, t.itemId.module, c.types[t.itemId][1], buf)
695+
var n = cursorFromIndexEntry(c, t.itemId.module.FileIndex, c.types[t.itemId][1], buf)
700696

701697
expect n, ParLe
702698
if n.tagId != tdefTag:
@@ -745,7 +741,7 @@ proc loadSym*(c: var DecodeContext; s: PSym) =
745741
if s.state != Partial: return
746742
s.state = Sealed
747743
var buf = createTokenBuf(30)
748-
var n = cursorFromIndexEntry(c, s.itemId.module, c.syms[s.itemId][1], buf)
744+
var n = cursorFromIndexEntry(c, s.itemId.module.FileIndex, c.syms[s.itemId][1], buf)
749745

750746
expect n, ParLe
751747
if n.tagId != sdefTag:
@@ -754,6 +750,17 @@ proc loadSym*(c: var DecodeContext; s: PSym) =
754750
expect n, SymbolDef
755751
# ignore the symbol's name, we have already used it to create this PSym instance!
756752
inc n
753+
if n.kind == Ident:
754+
if pool.strings[n.litId] == "x":
755+
s.flagsImpl.incl sfExported
756+
inc n
757+
else:
758+
raiseAssert "expected `x` as the export marker"
759+
elif n.kind == DotToken:
760+
inc n
761+
else:
762+
raiseAssert "expected `x` or '.' but got " & $n.kind
763+
757764
loadField s.magicImpl
758765
loadField s.flagsImpl
759766
loadField s.optionsImpl
@@ -894,20 +901,20 @@ proc loadNode(c: var DecodeContext; n: var Cursor): PNode =
894901
else:
895902
raiseAssert "Not yet implemented " & $n.kind
896903

897-
898-
proc loadNifModule*(c: var DecodeContext; f: FileIndex): PNode =
899-
let moduleSuffix = modname(c.moduleToNifSuffix, f.int, c.infos.config)
900-
let modFile = toGeneratedFile(c.infos.config, AbsoluteFile(moduleSuffix), ".nif").string
901-
902-
var buf = createTokenBuf(300)
903-
var s = nifstreams.open(modFile)
904-
# XXX We can optimize this here and only load the top level entries!
905-
try:
906-
nifcursors.parse(s, buf, NoLineInfo)
907-
finally:
908-
nifstreams.close(s)
909-
var n = cursorAt(buf, 0)
910-
result = loadNode(c, n)
904+
when false:
905+
proc loadNifModule*(c: var DecodeContext; f: FileIndex): PNode =
906+
let moduleSuffix = moduleSuffix(c.infos.config, f)
907+
let modFile = toGeneratedFile(c.infos.config, AbsoluteFile(moduleSuffix), ".nif").string
908+
909+
var buf = createTokenBuf(300)
910+
var s = nifstreams.open(modFile)
911+
# XXX We can optimize this here and only load the top level entries!
912+
try:
913+
nifcursors.parse(s, buf, NoLineInfo)
914+
finally:
915+
nifstreams.close(s)
916+
var n = cursorAt(buf, 0)
917+
result = loadNode(c, n)
911918

912919
when isMainModule:
913920
import std / syncio

compiler/ccgexprs.nim

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ proc canMove(p: BProc, n: PNode; dest: TLoc): bool =
170170
template simpleAsgn(builder: var Builder, dest, src: TLoc) =
171171
let rd = rdLoc(dest)
172172
let rs = rdLoc(src)
173-
builder.addAssignment(rd, rs)
173+
builder.addAssignment(rd, rs)
174174

175175
proc genRefAssign(p: BProc, dest, src: TLoc) =
176176
if (dest.storage == OnStack and p.config.selectedGC != gcGo) or not usesWriteBarrier(p.config):
@@ -675,7 +675,7 @@ proc binaryArithOverflow(p: BProc, e: PNode, d: var TLoc, m: TMagic) =
675675
if e[2].kind in {nkIntLit..nkInt64Lit}:
676676
needsOverflowCheck = e[2].intVal == -1
677677
if canBeZero:
678-
# remove extra paren from `==` op here to avoid Wparentheses-equality:
678+
# remove extra paren from `==` op here to avoid Wparentheses-equality:
679679
p.s(cpsStmts).addSingleIfStmt(removeSinglePar(cOp(Equal, rdLoc(b), cIntValue(0)))):
680680
p.s(cpsStmts).addCallStmt(cgsymValue(p.module, "raiseDivByZero"))
681681
raiseInstr(p, p.s(cpsStmts))
@@ -696,7 +696,7 @@ proc unaryArithOverflow(p: BProc, e: PNode, d: var TLoc, m: TMagic) =
696696
let ra = rdLoc(a)
697697
if optOverflowCheck in p.options:
698698
let first = cIntLiteral(firstOrd(p.config, t))
699-
# remove extra paren from `==` op here to avoid Wparentheses-equality:
699+
# remove extra paren from `==` op here to avoid Wparentheses-equality:
700700
p.s(cpsStmts).addSingleIfStmt(removeSinglePar(cOp(Equal, ra, first))):
701701
p.s(cpsStmts).addCallStmt(cgsymValue(p.module, "raiseOverflow"))
702702
raiseInstr(p, p.s(cpsStmts))
@@ -3435,7 +3435,7 @@ proc genConstDefinition(q: BModule; p: BProc; sym: PSym) =
34353435

34363436
proc genConstStmt(p: BProc, n: PNode) =
34373437
# This code is only used in the new DCE implementation.
3438-
assert useAliveDataFromDce in p.module.flags
3438+
assert delayedCodegen(p.module)
34393439
let m = p.module
34403440
for it in n:
34413441
if it[0].kind == nkSym:
@@ -3453,7 +3453,7 @@ proc expr(p: BProc, n: PNode, d: var TLoc) =
34533453
var sym = n.sym
34543454
case sym.kind
34553455
of skMethod:
3456-
if useAliveDataFromDce in p.module.flags or {sfDispatcher, sfForward} * sym.flags != {}:
3456+
if delayedCodegen(p.module) or {sfDispatcher, sfForward} * sym.flags != {}:
34573457
# we cannot produce code for the dispatcher yet:
34583458
fillProcLoc(p.module, n)
34593459
genProcPrototype(p.module, sym)
@@ -3466,7 +3466,7 @@ proc expr(p: BProc, n: PNode, d: var TLoc) =
34663466
if sfCompileTime in sym.flags:
34673467
localError(p.config, n.info, "request to generate code for .compileTime proc: " &
34683468
sym.name.s)
3469-
if useAliveDataFromDce in p.module.flags and sym.typ.callConv != ccInline:
3469+
if delayedCodegen(p.module) and sym.typ.callConv != ccInline:
34703470
fillProcLoc(p.module, n)
34713471
genProcPrototype(p.module, sym)
34723472
else:
@@ -3479,7 +3479,7 @@ proc expr(p: BProc, n: PNode, d: var TLoc) =
34793479
var lit = newBuilder("")
34803480
genLiteral(p, sym.astdef, sym.typ, lit)
34813481
putIntoDest(p, d, n, extract(lit), OnStatic)
3482-
elif useAliveDataFromDce in p.module.flags:
3482+
elif delayedCodegen(p.module):
34833483
genConstHeader(p.module, p.module, p, sym)
34843484
assert((sym.loc.snippet != "") and (sym.loc.t != nil))
34853485
putLocIntoDest(p, d, sym.loc)
@@ -3611,7 +3611,7 @@ proc expr(p: BProc, n: PNode, d: var TLoc) =
36113611
of nkWhileStmt: genWhileStmt(p, n)
36123612
of nkVarSection, nkLetSection: genVarStmt(p, n)
36133613
of nkConstSection:
3614-
if useAliveDataFromDce in p.module.flags:
3614+
if delayedCodegen(p.module):
36153615
genConstStmt(p, n)
36163616
else: # enforce addressable consts for exportc
36173617
let m = p.module
@@ -3677,7 +3677,10 @@ proc expr(p: BProc, n: PNode, d: var TLoc) =
36773677
of nkProcDef, nkFuncDef, nkMethodDef, nkConverterDef:
36783678
if n[genericParamsPos].kind == nkEmpty:
36793679
var prc = n[namePos].sym
3680-
if useAliveDataFromDce in p.module.flags:
3680+
if optCompress in p.config.globalOptions:
3681+
if prc.magic in generatedMagics:
3682+
genProc(p.module, prc)
3683+
elif delayedCodegen(p.module):
36813684
if p.module.alive.contains(prc.itemId.item) and
36823685
prc.magic in generatedMagics:
36833686
genProc(p.module, prc)

compiler/ccgstmts.nim

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,7 @@ proc genSingleVar(p: BProc, v: PSym; vn, value: PNode) =
366366
if v.flags * {sfImportc, sfExportc} == {sfImportc} and
367367
value.kind == nkEmpty and
368368
v.loc.flags * {lfHeader, lfNoDecl} != {}:
369+
# IC XXX: this is bad, we should set v.loc regardless here
369370
return
370371
if sfPure in v.flags:
371372
# v.owner.kind != skModule:
@@ -461,7 +462,7 @@ proc genSingleVar(p: BProc, v: PSym; vn, value: PNode) =
461462
if value.kind != nkEmpty and valueAsRope.len == 0:
462463
genLineDir(targetProc, vn)
463464
if not isCppCtorCall:
464-
ensureMutable v
465+
backendEnsureMutable v
465466
loadInto(targetProc, vn, value, v.locImpl)
466467
if forHcr:
467468
endBlockWith(targetProc):

0 commit comments

Comments
 (0)