Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

preparations for IC+ORC #17814

Closed
wants to merge 13 commits into from
Closed
8 changes: 6 additions & 2 deletions compiler/ast.nim
Original file line number Diff line number Diff line change
Expand Up @@ -866,7 +866,9 @@ type
of routineKinds:
#procInstCache*: seq[PInstantiation]
gcUnsafetyReason*: PSym # for better error messages regarding gcsafe
transformedBody*: PNode # cached body after transf pass
semcheckedBody*: PNode # proc body after semantic checking
when not defined(nimOrcic):
transformedBody*: PNode # cached body after transf pass
of skLet, skVar, skField, skForVar:
guard*: PSym
bitsize*: int
Expand Down Expand Up @@ -1717,7 +1719,9 @@ proc transitionGenericParamToType*(s: PSym) =
proc transitionRoutineSymKind*(s: PSym, kind: range[skProc..skTemplate]) =
transitionSymKindCommon(kind)
s.gcUnsafetyReason = obj.gcUnsafetyReason
s.transformedBody = obj.transformedBody
s.semcheckedBody = obj.semcheckedBody
when not defined(nimOrcic):
s.transformedBody = obj.transformedBody

proc transitionToLet*(s: PSym) =
transitionSymKindCommon(skLet)
Expand Down
9 changes: 6 additions & 3 deletions compiler/cgen.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1034,9 +1034,12 @@ proc genProcAux(m: BModule, prc: PSym) =
var returnStmt: Rope = nil
assert(prc.ast != nil)

var procBody = transformBody(m.g.graph, m.idgen, prc, cache = false)
if sfInjectDestructors in prc.flags:
procBody = injectDestructorCalls(m.g.graph, m.idgen, prc, procBody)
when not defined(nimOrcic):
var procBody = transformBody(m.g.graph, m.idgen, prc, dontUseCache)
if sfInjectDestructors in prc.flags:
procBody = injectDestructorCalls(m.g.graph, m.idgen, prc, procBody)
else:
let procBody = getBody(m.g.graph, prc)

if sfPure notin prc.flags and prc.typ[0] != nil:
if resultPos >= prc.ast.len:
Expand Down
46 changes: 1 addition & 45 deletions compiler/cgmeth.nim
Original file line number Diff line number Diff line change
Expand Up @@ -11,51 +11,7 @@

import
intsets, options, ast, msgs, idents, renderer, types, magicsys,
sempass2, strutils, modulegraphs, lineinfos

when defined(nimPreviewSlimSystem):
import std/assertions


proc genConv(n: PNode, d: PType, downcast: bool; conf: ConfigRef): PNode =
var dest = skipTypes(d, abstractPtrs)
var source = skipTypes(n.typ, abstractPtrs)
if (source.kind == tyObject) and (dest.kind == tyObject):
var diff = inheritanceDiff(dest, source)
if diff == high(int):
# no subtype relation, nothing to do
result = n
elif diff < 0:
result = newNodeIT(nkObjUpConv, n.info, d)
result.add n
if downcast: internalError(conf, n.info, "cgmeth.genConv: no upcast allowed")
elif diff > 0:
result = newNodeIT(nkObjDownConv, n.info, d)
result.add n
if not downcast:
internalError(conf, n.info, "cgmeth.genConv: no downcast allowed")
else:
result = n
else:
result = n

proc getDispatcher*(s: PSym): PSym =
## can return nil if is has no dispatcher.
if dispatcherPos < s.ast.len:
result = s.ast[dispatcherPos].sym
doAssert sfDispatcher in result.flags

proc methodCall*(n: PNode; conf: ConfigRef): PNode =
result = n
# replace ordinary method by dispatcher method:
let disp = getDispatcher(result[0].sym)
if disp != nil:
result[0].sym = disp
# change the arguments to up/downcasts to fit the dispatcher's parameters:
for i in 1..<result.len:
result[i] = genConv(result[i], disp.typ[i], true, conf)
else:
localError(conf, n.info, "'" & $result[0] & "' lacks a dispatcher")
sempass2, strutils, modulegraphs, lineinfos, transf

type
MethodResult = enum No, Invalid, Yes
Expand Down
2 changes: 1 addition & 1 deletion compiler/enumtostr.nim
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ proc genEnumToStrProc*(t: PType; info: TLineInfo; g: ModuleGraph; idgen: IdGener
assert(t.n[i].kind == nkSym)
var field = t.n[i].sym
let val = if field.ast == nil: field.name.s else: field.ast.strVal
caseStmt.add newTree(nkOfBranch, newSymNode(field),
caseStmt.add newTree(nkOfBranch, newIntTypeNode(field.position, t),
newTree(nkStmtList, newTree(nkFastAsgn, newSymNode(res), newStrNode(val, info))))
#newIntTypeNode(nkIntLit, field.position, t)

Expand Down
22 changes: 22 additions & 0 deletions compiler/finalast.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#
#
# The Nim Compiler
# (c) Copyright 2021 Andreas Rumpf
#
# See the file "copying.txt", included in this
# distribution, for details about the copyright.
#

## This module implements a tiny intermediate layer.

import
ast, transf, injectdestructors, modulegraphs

proc finalProcBody*(g: ModuleGraph; idgen: IdGenerator; prc: PSym; n: PNode): PNode =
## Transformations after sem'checking that we need to do.
result = transformBody(g, idgen, prc, dontUseCache)
if sfInjectDestructors in prc.flags:
result = injectDestructorCalls(g, idgen, prc, result)

proc finalToplevelStmt*(g: ModuleGraph; idgen: IdGenerator; module: PSym; n: PNode): PNode =
result = transformStmt(g, idgen, module, n)
21 changes: 10 additions & 11 deletions compiler/ic/ic.nim
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,15 @@ type
toReplay*: PackedTree # pragmas and VM specific state to replay.
topLevel*: PackedTree # top level statements
bodies*: PackedTree # other trees. Referenced from typ.n and sym.ast by their position.
#producedGenerics*: Table[GenericKey, SymId]
exports*: seq[(LitId, int32)]
hidden*: seq[(LitId, int32)]
reexports*: seq[(LitId, PackedItemId)]
compilerProcs*: seq[(LitId, int32)]
converters*, methods*, trmacros*, pureEnums*: seq[int32]
macroUsages*: seq[(PackedItemId, PackedLineInfo)]

typeInstCache*: seq[(PackedItemId, PackedItemId)]
procInstCache*: seq[PackedInstantiation]
attachedOps*: seq[(TTypeAttachedOp, PackedItemId, PackedItemId)]
attachedOps*: seq[(PackedItemId, TTypeAttachedOp, PackedItemId)]
methodsPerType*: seq[(PackedItemId, int, PackedItemId)]
enumToStringProcs*: seq[(PackedItemId, PackedItemId)]

Expand Down Expand Up @@ -529,6 +527,15 @@ proc toPackedGeneratedProcDef*(s: PSym, encoder: var PackedEncoder; m: var Packe
toPackedProcDef(s.ast, m.topLevel, encoder, m)
#flush encoder, m

proc storeAttachedProcDef*(t: PType; op: TTypeAttachedOp; s: PSym,
encoder: var PackedEncoder; m: var PackedModule) =
assert s.kind in routineKinds
assert isActive(encoder)
let tid = storeTypeLater(t, encoder, m)
let sid = storeSymLater(s, encoder, m)
m.attachedOps.add (tid, op, sid)
toPackedGeneratedProcDef(s, encoder, m)

proc storeInstantiation*(c: var PackedEncoder; m: var PackedModule; s: PSym; i: PInstantiation) =
var t = newSeq[PackedItemId](i.concreteTypes.len)
for j in 0..high(i.concreteTypes):
Expand Down Expand Up @@ -593,7 +600,6 @@ proc loadRodFile*(filename: AbsoluteFile; m: var PackedModule; config: ConfigRef
loadSeqSection convertersSection, m.converters
loadSeqSection methodsSection, m.methods
loadSeqSection pureEnumsSection, m.pureEnums
loadSeqSection macroUsagesSection, m.macroUsages

loadSeqSection toReplaySection, m.toReplay.nodes
loadSeqSection topLevelSection, m.topLevel.nodes
Expand Down Expand Up @@ -657,7 +663,6 @@ proc saveRodFile*(filename: AbsoluteFile; encoder: var PackedEncoder; m: var Pac
storeSeqSection convertersSection, m.converters
storeSeqSection methodsSection, m.methods
storeSeqSection pureEnumsSection, m.pureEnums
storeSeqSection macroUsagesSection, m.macroUsages

storeSeqSection toReplaySection, m.toReplay.nodes
storeSeqSection topLevelSection, m.topLevel.nodes
Expand Down Expand Up @@ -898,16 +903,10 @@ proc typeBodyFromPacked(c: var PackedDecoder; g: var PackedModuleGraph;
t: PackedType; si, item: int32; result: PType) =
result.sym = loadSym(c, g, si, t.sym)
result.owner = loadSym(c, g, si, t.owner)
when false:
for op, item in pairs t.attachedOps:
result.attachedOps[op] = loadSym(c, g, si, item)
result.typeInst = loadType(c, g, si, t.typeInst)
for son in items t.types:
result.sons.add loadType(c, g, si, son)
loadAstBody(t, n)
when false:
for gen, id in items t.methods:
result.methods.add((gen, loadSym(c, g, si, id)))

proc loadType(c: var PackedDecoder; g: var PackedModuleGraph; thisModule: int; t: PackedItemId): PType =
if t == nilItemId:
Expand Down
20 changes: 15 additions & 5 deletions compiler/ic/replayer.nim
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,20 @@ proc replayStateChanges*(module: PSym; g: ModuleGraph) =
else:
internalAssert g.config, false

proc replayBackendProcs*(g: ModuleGraph; module: int) =
for it in mitems(g.packed[module].fromDisk.attachedOps):
let key = translateId(it[0], g.packed, module, g.config)
let op = it[1]
let tmp = translateId(it[2], g.packed, module, g.config)
let symId = FullId(module: tmp.module, packed: it[2])
g.attachedOps[op][key] = LazySym(id: symId, sym: nil)

for it in mitems(g.packed[module].fromDisk.enumToStringProcs):
let key = translateId(it[0], g.packed, module, g.config)
let tmp = translateId(it[1], g.packed, module, g.config)
let symId = FullId(module: tmp.module, packed: it[1])
g.enumToStringProcs[key] = LazySym(id: symId, sym: nil)

proc replayGenericCacheInformation*(g: ModuleGraph; module: int) =
## We remember the generic instantiations a module performed
## in order to to avoid the code bloat that generic code tends
Expand Down Expand Up @@ -120,11 +134,7 @@ proc replayGenericCacheInformation*(g: ModuleGraph; module: int) =
let symId = FullId(module: tmp.module, packed: it[2])
g.methodsPerType.mgetOrPut(key, @[]).add (col, LazySym(id: symId, sym: nil))

for it in mitems(g.packed[module].fromDisk.enumToStringProcs):
let key = translateId(it[0], g.packed, module, g.config)
let tmp = translateId(it[1], g.packed, module, g.config)
let symId = FullId(module: tmp.module, packed: it[1])
g.enumToStringProcs[key] = LazySym(id: symId, sym: nil)
replayBackendProcs(g, module)

for it in mitems(g.packed[module].fromDisk.methods):
let sym = loadSymFromId(g.config, g.cache, g.packed, module,
Expand Down
1 change: 0 additions & 1 deletion compiler/ic/rodfiles.nim
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ type
convertersSection
methodsSection
pureEnumsSection
macroUsagesSection
toReplaySection
topLevelSection
bodiesSection
Expand Down
2 changes: 1 addition & 1 deletion compiler/injectdestructors.nim
Original file line number Diff line number Diff line change
Expand Up @@ -990,7 +990,7 @@ proc p(n: PNode; c: var Con; s: var Scope; mode: ProcessMode): PNode =
result[0] = p(n[0], c, s, mode)
for i in 1..<n.len:
result[i] = n[i]
of nkGotoState, nkState, nkAsmStmt:
of nkGotoState, nkState, nkAsmStmt, nkOpenSymChoice, nkClosedSymChoice:
result = n
else:
internalError(c.graph.config, n.info, "cannot inject destructors to node kind: " & $n.kind)
Expand Down
12 changes: 0 additions & 12 deletions compiler/int128.nim
Original file line number Diff line number Diff line change
Expand Up @@ -357,18 +357,6 @@ proc low64(a: Int128): uint64 =
bitconcat(a.udata[1], a.udata[0])

proc `*`*(lhs, rhs: Int128): Int128 =
let
a = cast[uint64](lhs.udata[0])
b = cast[uint64](lhs.udata[1])
c = cast[uint64](lhs.udata[2])
d = cast[uint64](lhs.udata[3])

e = cast[uint64](rhs.udata[0])
f = cast[uint64](rhs.udata[1])
g = cast[uint64](rhs.udata[2])
h = cast[uint64](rhs.udata[3])


let a32 = cast[uint64](lhs.udata[1])
let a00 = cast[uint64](lhs.udata[0])
let b32 = cast[uint64](rhs.udata[1])
Expand Down
9 changes: 6 additions & 3 deletions compiler/jsgen.nim
Original file line number Diff line number Diff line change
Expand Up @@ -2421,9 +2421,12 @@ proc genProc(oldProc: PProc, prc: PSym): Rope =
else:
returnStmt = "return $#;$n" % [a.res]

var transformedBody = transformBody(p.module.graph, p.module.idgen, prc, cache = false)
if sfInjectDestructors in prc.flags:
transformedBody = injectDestructorCalls(p.module.graph, p.module.idgen, prc, transformedBody)
when not defined(nimOrcic):
var transformedBody = transformBody(p.module.graph, p.module.idgen, prc, dontUseCache)
if sfInjectDestructors in prc.flags:
transformedBody = injectDestructorCalls(p.module.graph, p.module.idgen, prc, transformedBody)
else:
let transformedBody = getBody(p.module.graph, prc)

p.nested: genStmt(p, transformedBody)

Expand Down
30 changes: 22 additions & 8 deletions compiler/lambdalifting.nim
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@
import
intsets, strutils, options, ast, astalgo, msgs,
idents, renderer, types, magicsys, lowerings, tables, modulegraphs, lineinfos,
transf, liftdestructors, typeallowed
liftdestructors, typeallowed

when not defined(nimOrcic):
import transf

when defined(nimPreviewSlimSystem):
import std/assertions
Expand Down Expand Up @@ -437,7 +440,10 @@ proc detectCapturedVars(n: PNode; owner: PSym; c: var DetectionPass) =
if innerProc:
if s.isIterator: c.somethingToDo = true
if not c.processed.containsOrIncl(s.id):
let body = transformBody(c.graph, c.idgen, s, cache = true)
when not defined(nimOrcic):
let body = transformBody(c.graph, c.idgen, s, useCache)
else:
let body = getBody(c.graph, s)
detectCapturedVars(body, s, c)
let ow = s.skipGenericOwner
if ow == owner:
Expand Down Expand Up @@ -733,13 +739,21 @@ proc liftCapturedVars(n: PNode; owner: PSym; d: var DetectionPass;
# echo renderTree(s.getBody, {renderIds})
let oldInContainer = c.inContainer
c.inContainer = 0
var body = transformBody(d.graph, d.idgen, s, cache = false)
body = liftCapturedVars(body, s, d, c)
if c.envVars.getOrDefault(s.id).isNil:
s.transformedBody = body
when not defined(nimOrcic):
var body = transformBody(d.graph, d.idgen, s, dontUseCache)
body = liftCapturedVars(body, s, d, c)
if c.envVars.getOrDefault(s.id).isNil:
s.transformedBody = body
else:
s.transformedBody = newTree(nkStmtList, rawClosureCreation(s, d, c, n.info), body)
finishClosureCreation(s, d, c, n.info, s.transformedBody)
else:
s.transformedBody = newTree(nkStmtList, rawClosureCreation(s, d, c, n.info), body)
finishClosureCreation(s, d, c, n.info, s.transformedBody)
var body = getBody(d.graph, s) #transformBody(d.graph, d.idgen, s, dontUseCache)
body = liftCapturedVars(body, s, d, c)
if not c.envVars.getOrDefault(s.id).isNil:
body = newTree(nkStmtList, rawClosureCreation(s, d, c, n.info), body)
finishClosureCreation(s, d, c, n.info, body)
setRoutineBody(d.graph, s, body)
c.inContainer = oldInContainer

if s.typ.callConv == ccClosure:
Expand Down
7 changes: 4 additions & 3 deletions compiler/main.nim
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ import
llstream, ast, lexer, syntaxes, options, msgs,
condsyms,
sem, idents, passes, extccomp,
cgen, nversion,
platform, nimconf, passaux, depends, vm,
cgen, json, nversion,
nimconf, passaux, depends, vm,
modules,
modulegraphs, lineinfos, pathutils, vmprofiler
modulegraphs, tables, lineinfos, pathutils, vmprofiler,
platform


when defined(nimPreviewSlimSystem):
Expand Down
Loading