This commit is contained in:
araq
2023-10-01 12:53:38 +02:00
parent 9d9d860797
commit f496c0e14c
8 changed files with 81 additions and 21 deletions

View File

@@ -936,7 +936,7 @@ type
# it won't cause problems
# for skModule the string literal to output for
# deprecated modules.
instantiatedFrom*: PSym # for instances, the generic symbol where it came from.
instantiatedFrom*: PSym # for instances, the generic symbol where it came from.
when defined(nimsuggest):
allUsages*: seq[TLineInfo]
@@ -2173,3 +2173,7 @@ const
nkFuncDef, nkConstSection, nkConstDef, nkIncludeStmt, nkImportStmt,
nkExportStmt, nkPragma, nkCommentStmt, nkBreakState,
nkTypeOfExpr, nkMixinStmt, nkBindStmt}
proc isTrue*(n: PNode): bool =
n.kind == nkSym and n.sym.kind == skEnumField and n.sym.position != 0 or
n.kind == nkIntLit and n.intVal != 0

View File

@@ -129,10 +129,6 @@ template withBlock(labl: PSym; body: untyped) =
body
popBlock(c, oldLen)
proc isTrue(n: PNode): bool =
n.kind == nkSym and n.sym.kind == skEnumField and n.sym.position != 0 or
n.kind == nkIntLit and n.intVal != 0
template forkT(body) =
let lab1 = c.forkI()
body

View File

@@ -20,8 +20,9 @@ type
lastFileVal: LitId
strings: BiTable[string]
man: LineInfoManager
labelGen: int
proc toLineInfo(i: TLineInfo; c: var Context): PackedLineInfo =
proc toLineInfo(c: var Context; i: TLineInfo): PackedLineInfo =
var val: LitId
if c.lastFileKey == i.fileIndex:
val = c.lastFileVal
@@ -32,6 +33,40 @@ proc toLineInfo(i: TLineInfo; c: var Context): PackedLineInfo =
c.lastFileVal = val
result = pack(c.man, val, int32 i.line, int32 i.col)
proc astToIr*(n: PNode; dest: var Tree; c: var Context) =
let info = toLineInfo(n.info, c)
proc gen*(c: var Context; dest: var Tree; n: PNode)
proc genx*(c: var Context; dest: var Tree; n: PNode): Tree
template withBlock(lab: LabelId; body: untyped) =
body
dest.addInstr(info, Label, lab)
proc genWhile(c: var Context; dest: var Tree; n: PNode) =
# LoopLabel lab1:
# cond, tmp
# select cond
# of false: goto lab2
# body
# GotoLoop lab1
# Label lab2:
let info = toLineInfo(c, n.info)
let loopLab = dest.addLabel(c.labelGen, info, LoopLabel)
let theEnd = newLabel(c.labelGen)
withBlock(theEnd):
if isTrue(n[0]):
c.gen(dest, n[1])
dest.gotoLabel info, GotoLoop, loopLab
else:
let x = c.genx(dest, n[0])
#dest.addSelect toLineInfo(c, n[0].kind), x
c.gen(dest, n[1])
dest.gotoLabel info, GotoLoop, loopLab
proc genx*(c: var Context; dest: var Tree; n: PNode): Tree =
quit "too implement"
proc gen*(c: var Context; dest: var Tree; n: PNode) =
case n.kind
of nkWhileStmt:
genWhile c, dest, n
else:
discard

View File

@@ -26,7 +26,8 @@ type
ModuleSymUse, # `module.x`
Label,
Goto,
GotoBack,
LoopLabel,
GotoLoop,
Typed, # with type ID
NilVal, # last atom
@@ -149,4 +150,35 @@ iterator sons*(tree: Tree; n: NodePos): NodePos =
template `[]`*(t: Tree; n: NodePos): Instr = t.nodes[n.int]
proc span(tree: Tree; pos: int): int {.inline.} =
if tree.nodes[pos].kind <= LastAtomicValue: 1 else: int(tree.nodes[pos].operand)
proc copyTree*(dest: var Tree; src: Tree) =
let pos = 0
let L = span(src, pos)
let d = dest.nodes.len
dest.nodes.setLen(d + L)
assert L > 0
for i in 0..<L:
dest.nodes[d+i] = src.nodes[pos+i]
type
LabelId* = distinct int
proc newLabel*(labelGen: var int): LabelId {.inline.} =
result = LabelId labelGen
inc labelGen
proc addLabel*(t: var Tree; labelGen: var int; info: PackedLineInfo; k: InstKind): LabelId =
assert k in {Label, LoopLabel}
result = LabelId labelGen
t.nodes.add Instr(x: toX(k, uint32(result)), info: info)
inc labelGen
proc gotoLabel*(t: var Tree; info: PackedLineInfo; k: InstKind; L: LabelId) =
assert k in {Goto, GotoLoop}
t.nodes.add Instr(x: toX(k, uint32(L)), info: info)
proc addInstr*(t: var Tree; info: PackedLineInfo; k: InstKind; L: LabelId) {.inline.} =
assert k in {Label, LoopLabel, Goto, GotoLoop}
t.nodes.add Instr(x: toX(k, uint32(L)), info: info)

View File

@@ -111,9 +111,10 @@ proc tupleToIr(c: var Context; t: PType): TypeId =
result = sealType(c.g, obj)
proc procToIr(c: var Context; t: PType; addEnv = false): TypeId =
var fieldTypes = newSeq[TypeId](t.len)
var fieldTypes = newSeq[TypeId](0)
for i in 0..<t.len:
fieldTypes[i] = typeToIr(c, t[i])
if not isCompileTimeOnly(t[i]):
fieldTypes.add typeToIr(c, t[i])
let obj = openType(c.g, ProcTy)
case t.callConv
@@ -127,7 +128,7 @@ proc procToIr(c: var Context; t: PType; addEnv = false): TypeId =
of ccThisCall: c.g.addAnnotation "__thiscall"
of ccNoConvention: c.g.addAnnotation ""
for i in 0..<t.len:
for i in 0..<fieldTypes.len:
c.g.addType fieldTypes[i]
if addEnv:

View File

@@ -25,7 +25,7 @@ import
ast, astalgo, idents, lowerings, magicsys, guards, msgs,
renderer, types, modulegraphs, options, spawn, lineinfos
from trees import getMagic, isTrue, getRoot
from trees import getMagic, getRoot
from strutils import `%`
discard """

View File

@@ -198,10 +198,6 @@ proc extractRange*(k: TNodeKind, n: PNode, a, b: int): PNode =
result = newNodeI(k, n.info, b-a+1)
for i in 0..b-a: result[i] = n[i+a]
proc isTrue*(n: PNode): bool =
n.kind == nkSym and n.sym.kind == skEnumField and n.sym.position != 0 or
n.kind == nkIntLit and n.intVal != 0
proc getRoot*(n: PNode): PSym =
## ``getRoot`` takes a *path* ``n``. A path is an lvalue expression
## like ``obj.x[i].y``. The *root* of a path is the symbol that can be

View File

@@ -321,10 +321,6 @@ proc isNotOpr(n: PNode): bool =
n.kind in nkCallKinds and n[0].kind == nkSym and
n[0].sym.magic == mNot
proc isTrue(n: PNode): bool =
n.kind == nkSym and n.sym.kind == skEnumField and n.sym.position != 0 or
n.kind == nkIntLit and n.intVal != 0
proc genWhile(c: PCtx; n: PNode) =
# lab1:
# cond, tmp