Skip to content

Commit

Permalink
Merge branch 'devel' into pr_iterator_array_lent
Browse files Browse the repository at this point in the history
  • Loading branch information
ringabout authored Nov 22, 2023
2 parents 3bffd3a + 8c56e80 commit de06778
Show file tree
Hide file tree
Showing 63 changed files with 640 additions and 436 deletions.
34 changes: 21 additions & 13 deletions compiler/ast.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1166,7 +1166,7 @@ proc idGeneratorFromModule*(m: PSym): IdGenerator =
proc idGeneratorForPackage*(nextIdWillBe: int32): IdGenerator =
result = IdGenerator(module: PackageModuleId, symId: nextIdWillBe - 1'i32, typeId: 0, disambTable: initCountTable[PIdent]())

proc nextSymId*(x: IdGenerator): ItemId {.inline.} =
proc nextSymId(x: IdGenerator): ItemId {.inline.} =
assert(not x.sealed)
inc x.symId
result = ItemId(module: x.module, item: x.symId)
Expand Down Expand Up @@ -1547,7 +1547,8 @@ iterator items*(t: PType): PType =
iterator pairs*(n: PType): tuple[i: int, n: PType] =
for i in 0..<n.sons.len: yield (i, n.sons[i])

proc newType*(kind: TTypeKind, id: ItemId; owner: PSym, sons: seq[PType] = @[]): PType =
proc newType*(kind: TTypeKind, idgen: IdGenerator; owner: PSym, sons: seq[PType] = @[]): PType =
let id = nextTypeId idgen
result = PType(kind: kind, owner: owner, size: defaultSize,
align: defaultAlignment, itemId: id,
uniqueId: id, sons: sons)
Expand All @@ -1556,12 +1557,15 @@ proc newType*(kind: TTypeKind, id: ItemId; owner: PSym, sons: seq[PType] = @[]):
echo "KNID ", kind
writeStackTrace()

template newType*(kind: TTypeKind, id: ItemId; owner: PSym, parent: PType): PType =
template newType*(kind: TTypeKind, id: IdGenerator; owner: PSym, parent: PType): PType =
newType(kind, id, owner, parent.sons)

proc newType*(prev: PType, sons: seq[PType]): PType =
result = prev
result.sons = sons
proc setSons*(dest: PType; sons: seq[PType]) {.inline.} = dest.sons = sons

when false:
proc newType*(prev: PType, sons: seq[PType]): PType =
result = prev
result.sons = sons

proc addSon*(father, son: PType) =
# todo fixme: in IC, `son` might be nil
Expand Down Expand Up @@ -1595,13 +1599,17 @@ proc assignType*(dest, src: PType) =
newSons(dest, src.len)
for i in 0..<src.len: dest[i] = src[i]

proc copyType*(t: PType, id: ItemId, owner: PSym): PType =
result = newType(t.kind, id, owner)
proc copyType*(t: PType, idgen: IdGenerator, owner: PSym): PType =
result = newType(t.kind, idgen, owner)
assignType(result, t)
result.sym = t.sym # backend-info should not be copied

proc exactReplica*(t: PType): PType =
result = copyType(t, t.itemId, t.owner)
result = PType(kind: t.kind, owner: t.owner, size: defaultSize,
align: defaultAlignment, itemId: t.itemId,
uniqueId: t.uniqueId)
assignType(result, t)
result.sym = t.sym # backend-info should not be copied

proc copySym*(s: PSym; idgen: IdGenerator): PSym =
result = newSym(s.kind, s.name, idgen, s.owner, s.info, s.options)
Expand Down Expand Up @@ -1992,15 +2000,15 @@ proc toVar*(typ: PType; kind: TTypeKind; idgen: IdGenerator): PType =
## returned. Otherwise ``typ`` is simply returned as-is.
result = typ
if typ.kind != kind:
result = newType(kind, nextTypeId(idgen), typ.owner)
result = newType(kind, idgen, typ.owner)
rawAddSon(result, typ)

proc toRef*(typ: PType; idgen: IdGenerator): PType =
## If ``typ`` is a tyObject then it is converted into a `ref <typ>` and
## returned. Otherwise ``typ`` is simply returned as-is.
result = typ
if typ.skipTypes({tyAlias, tyGenericInst}).kind == tyObject:
result = newType(tyRef, nextTypeId(idgen), typ.owner)
result = newType(tyRef, idgen, typ.owner)
rawAddSon(result, typ)

proc toObject*(typ: PType): PType =
Expand Down Expand Up @@ -2108,8 +2116,8 @@ proc isSinkParam*(s: PSym): bool {.inline.} =
proc isSinkType*(t: PType): bool {.inline.} =
t.kind == tySink or tfHasOwned in t.flags

proc newProcType*(info: TLineInfo; id: ItemId; owner: PSym): PType =
result = newType(tyProc, id, owner)
proc newProcType*(info: TLineInfo; idgen: IdGenerator; owner: PSym): PType =
result = newType(tyProc, idgen, owner)
result.n = newNodeI(nkFormalParams, info)
rawAddSon(result, nil) # return type
# result.n[0] used to be `nkType`, but now it's `nkEffectList` because
Expand Down
18 changes: 9 additions & 9 deletions compiler/ccgtypes.nim
Original file line number Diff line number Diff line change
Expand Up @@ -663,13 +663,13 @@ proc genCppInitializer(m: BModule, prc: BProc; typ: PType): string =
result = "{}"
if typ.itemId in m.g.graph.initializersPerType:
let call = m.g.graph.initializersPerType[typ.itemId]
if call != nil:
if call != nil:
var p = prc
if p == nil:
p = BProc(module: m)
result = "{" & genCppParamsForCtor(p, call) & "}"
if prc == nil:
assert p.blocks.len == 0, "BProc belongs to a struct doesnt have blocks"
assert p.blocks.len == 0, "BProc belongs to a struct doesnt have blocks"

proc genRecordFieldsAux(m: BModule; n: PNode,
rectype: PType,
Expand Down Expand Up @@ -1530,9 +1530,9 @@ proc genArrayInfo(m: BModule; typ: PType, name: Rope; info: TLineInfo) =

proc fakeClosureType(m: BModule; owner: PSym): PType =
# we generate the same RTTI as for a tuple[pointer, ref tuple[]]
result = newType(tyTuple, nextTypeId m.idgen, owner)
result.rawAddSon(newType(tyPointer, nextTypeId m.idgen, owner))
var r = newType(tyRef, nextTypeId m.idgen, owner)
result = newType(tyTuple, m.idgen, owner)
result.rawAddSon(newType(tyPointer, m.idgen, owner))
var r = newType(tyRef, m.idgen, owner)
let obj = createObj(m.g.graph, m.idgen, owner, owner.info, final=false)
r.rawAddSon(obj)
result.rawAddSon(r)
Expand Down Expand Up @@ -1586,7 +1586,7 @@ proc generateRttiDestructor(g: ModuleGraph; typ: PType; owner: PSym; kind: TType

dest.typ = getSysType(g, info, tyPointer)

result.typ = newProcType(info, nextTypeId(idgen), owner)
result.typ = newProcType(info, idgen, owner)
result.typ.addParam dest

var n = newNodeI(nkProcDef, info, bodyPos+1)
Expand Down Expand Up @@ -1800,9 +1800,9 @@ proc genTypeInfoV2(m: BModule; t: PType; info: TLineInfo): Rope =
result = prefixTI.rope & result & ")".rope

proc openArrayToTuple(m: BModule; t: PType): PType =
result = newType(tyTuple, nextTypeId m.idgen, t.owner)
let p = newType(tyPtr, nextTypeId m.idgen, t.owner)
let a = newType(tyUncheckedArray, nextTypeId m.idgen, t.owner)
result = newType(tyTuple, m.idgen, t.owner)
let p = newType(tyPtr, m.idgen, t.owner)
let a = newType(tyUncheckedArray, m.idgen, t.owner)
a.add t.lastSon
p.add a
result.add p
Expand Down
2 changes: 1 addition & 1 deletion compiler/cgmeth.nim
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ proc createDispatcher(s: PSym; g: ModuleGraph; idgen: IdGenerator): PSym =
incl(disp.flags, sfDispatcher)
excl(disp.flags, sfExported)
let old = disp.typ
disp.typ = copyType(disp.typ, nextTypeId(idgen), disp.typ.owner)
disp.typ = copyType(disp.typ, idgen, disp.typ.owner)
copyTypeProps(g, idgen.module, disp.typ, old)

# we can't inline the dispatcher itself (for now):
Expand Down
4 changes: 2 additions & 2 deletions compiler/closureiters.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1160,9 +1160,9 @@ proc skipThroughEmptyStates(ctx: var Ctx, n: PNode): PNode=
n[i] = ctx.skipThroughEmptyStates(n[i])

proc newArrayType(g: ModuleGraph; n: int, t: PType; idgen: IdGenerator; owner: PSym): PType =
result = newType(tyArray, nextTypeId(idgen), owner)
result = newType(tyArray, idgen, owner)

let rng = newType(tyRange, nextTypeId(idgen), owner)
let rng = newType(tyRange, idgen, owner)
rng.n = newTree(nkRange, g.newIntLit(owner.info, 0), g.newIntLit(owner.info, n - 1))
rng.rawAddSon(t)

Expand Down
1 change: 1 addition & 0 deletions compiler/commands.nim
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,7 @@ proc parseCommand*(command: string): Command =
of "objc", "compiletooc": cmdCompileToOC
of "js", "compiletojs": cmdCompileToJS
of "r": cmdCrun
of "m": cmdM
of "run": cmdTcc
of "check": cmdCheck
of "e": cmdNimscript
Expand Down
4 changes: 2 additions & 2 deletions compiler/concepts.nim
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ proc declareSelf(c: PContext; info: TLineInfo) =
## Adds the magical 'Self' symbols to the current scope.
let ow = getCurrOwner(c)
let s = newSym(skType, getIdent(c.cache, "Self"), c.idgen, ow, info)
s.typ = newType(tyTypeDesc, nextTypeId(c.idgen), ow)
s.typ = newType(tyTypeDesc, c.idgen, ow)
s.typ.flags.incl {tfUnresolved, tfPacked}
s.typ.add newType(tyEmpty, nextTypeId(c.idgen), ow)
s.typ.add newType(tyEmpty, c.idgen, ow)
addDecl(c, s, info)

proc semConceptDecl(c: PContext; n: PNode): PNode =
Expand Down
4 changes: 2 additions & 2 deletions compiler/enumtostr.nim
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ proc genEnumToStrProc*(t: PType; info: TLineInfo; g: ModuleGraph; idgen: IdGener
let res = newSym(skResult, getIdent(g.cache, "result"), idgen, result, info)
res.typ = getSysType(g, info, tyString)

result.typ = newType(tyProc, nextTypeId idgen, t.owner)
result.typ = newType(tyProc, idgen, t.owner)
result.typ.n = newNodeI(nkFormalParams, info)
rawAddSon(result.typ, res.typ)
result.typ.n.add newNodeI(nkEffectList, info)
Expand Down Expand Up @@ -76,7 +76,7 @@ proc genCaseObjDiscMapping*(t: PType; field: PSym; info: TLineInfo; g: ModuleGra
let res = newSym(skResult, getIdent(g.cache, "result"), idgen, result, info)
res.typ = getSysType(g, info, tyUInt8)

result.typ = newType(tyProc, nextTypeId idgen, t.owner)
result.typ = newType(tyProc, idgen, t.owner)
result.typ.n = newNodeI(nkFormalParams, info)
rawAddSon(result.typ, res.typ)
result.typ.n.add newNodeI(nkEffectList, info)
Expand Down
6 changes: 4 additions & 2 deletions compiler/errorhandling.nim
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ proc newError*(wrongNode: PNode; k: ErrorKind; args: varargs[PNode]): PNode =
let innerError = errorSubNode(wrongNode)
if innerError != nil:
return innerError
result = newNodeIT(nkError, wrongNode.info, newType(tyError, ItemId(module: -1, item: -1), nil))
var idgen = idGeneratorForPackage(-1'i32)
result = newNodeIT(nkError, wrongNode.info, newType(tyError, idgen, nil))
result.add wrongNode
result.add newIntNode(nkIntLit, ord(k))
for a in args: result.add a
Expand All @@ -51,7 +52,8 @@ proc newError*(wrongNode: PNode; msg: string): PNode =
let innerError = errorSubNode(wrongNode)
if innerError != nil:
return innerError
result = newNodeIT(nkError, wrongNode.info, newType(tyError, ItemId(module: -1, item: -1), nil))
var idgen = idGeneratorForPackage(-1'i32)
result = newNodeIT(nkError, wrongNode.info, newType(tyError, idgen, nil))
result.add wrongNode
result.add newIntNode(nkIntLit, ord(CustomError))
result.add newStrNode(msg, wrongNode.info)
Expand Down
4 changes: 2 additions & 2 deletions compiler/guards.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1098,8 +1098,8 @@ proc addFactLt*(m: var TModel; a, b: PNode) =
addFactLe(m, a, bb)

proc settype(n: PNode): PType =
result = newType(tySet, ItemId(module: -1, item: -1), n.typ.owner)
var idgen: IdGenerator = nil
var idgen = idGeneratorForPackage(-1'i32)
result = newType(tySet, idgen, n.typ.owner)
addSonSkipIntLit(result, n.typ, idgen)

proc buildOf(it, loc: PNode; o: Operators): PNode =
Expand Down
6 changes: 6 additions & 0 deletions compiler/ic/bitabs.nim
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,12 @@ proc load*[T](f: var RodFile; t: var BiTable[T]) =
loadSeq(f, t.vals)
loadSeq(f, t.keys)

proc sizeOnDisc*(t: BiTable[string]): int =
result = 4
for x in t.vals:
result += x.len + 4
result += t.keys.len * sizeof(LitId)

when isMainModule:

var t: BiTable[string]
Expand Down
6 changes: 3 additions & 3 deletions compiler/ic/cbackend.nim
Original file line number Diff line number Diff line change
Expand Up @@ -147,12 +147,12 @@ proc generateCode*(g: ModuleGraph) =
var alive = computeAliveSyms(g.packed, g.config)

when false:
for i in 0..high(g.packed):
for i in 0..<len(g.packed):
echo i, " is of status ", g.packed[i].status, " ", toFullPath(g.config, FileIndex(i))

# First pass: Setup all the backend modules for all the modules that have
# changed:
for i in 0..high(g.packed):
for i in 0..<len(g.packed):
# case statement here to enforce exhaustive checks.
case g.packed[i].status
of undefined:
Expand All @@ -174,7 +174,7 @@ proc generateCode*(g: ModuleGraph) =
let mainModuleIdx = g.config.projectMainIdx2.int
# We need to generate the main module last, because only then
# all init procs have been registered:
for i in 0..high(g.packed):
for i in 0..<len(g.packed):
if i != mainModuleIdx:
genPackedModule(g, i, alive)
if mainModuleIdx >= 0:
Expand Down
8 changes: 4 additions & 4 deletions compiler/ic/dce.nim
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,13 @@ proc aliveCode(c: var AliveContext; g: PackedModuleGraph; tree: PackedTree; n: N
discard "ignore non-sym atoms"
of nkSym:
# This symbol is alive and everything its body references.
followLater(c, g, c.thisModule, n.operand)
followLater(c, g, c.thisModule, tree[n].soperand)
of nkModuleRef:
let (n1, n2) = sons2(tree, n)
assert n1.kind == nkNone
assert n2.kind == nkNone
let m = n1.litId
let item = n2.operand
let item = tree[n2].soperand
let otherModule = toFileIndexCached(c.decoder, g, c.thisModule, m).int
followLater(c, g, otherModule, item)
of nkMacroDef, nkTemplateDef, nkTypeSection, nkTypeOfExpr,
Expand All @@ -131,7 +131,7 @@ proc aliveCode(c: var AliveContext; g: PackedModuleGraph; tree: PackedTree; n: N
rangeCheckAnalysis(c, g, tree, n)
of nkProcDef, nkConverterDef, nkMethodDef, nkFuncDef, nkIteratorDef:
if n.firstSon.kind == nkSym and isNotGeneric(n):
let item = n.firstSon.operand
let item = tree[n.firstSon].soperand
if isExportedToC(c, g, item):
# This symbol is alive and everything its body references.
followLater(c, g, c.thisModule, item)
Expand All @@ -153,7 +153,7 @@ proc computeAliveSyms*(g: PackedModuleGraph; conf: ConfigRef): AliveSyms =
var c = AliveContext(stack: @[], decoder: PackedDecoder(config: conf),
thisModule: -1, alive: newSeq[IntSet](g.len),
options: conf.options)
for i in countdown(high(g), 0):
for i in countdown(len(g)-1, 0):
if g[i].status != undefined:
c.thisModule = i
for p in allNodes(g[i].fromDisk.topLevel):
Expand Down
Loading

0 comments on commit de06778

Please sign in to comment.