From f496c0e14cfe18575f49279b6dc6ca88daa998df Mon Sep 17 00:00:00 2001 From: araq Date: Sun, 1 Oct 2023 12:53:38 +0200 Subject: [PATCH] progress --- compiler/ast.nim | 6 +++++- compiler/dfa.nim | 4 ---- compiler/nir/ast2ir.nim | 41 ++++++++++++++++++++++++++++++++++++--- compiler/nir/nirinsts.nim | 34 +++++++++++++++++++++++++++++++- compiler/nir/types2ir.nim | 7 ++++--- compiler/semparallel.nim | 2 +- compiler/trees.nim | 4 ---- compiler/vmgen.nim | 4 ---- 8 files changed, 81 insertions(+), 21 deletions(-) diff --git a/compiler/ast.nim b/compiler/ast.nim index 8658251e52..bfebf45abe 100644 --- a/compiler/ast.nim +++ b/compiler/ast.nim @@ -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 diff --git a/compiler/dfa.nim b/compiler/dfa.nim index 1459bde457..4cae9ec42b 100644 --- a/compiler/dfa.nim +++ b/compiler/dfa.nim @@ -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 diff --git a/compiler/nir/ast2ir.nim b/compiler/nir/ast2ir.nim index 0fa145264f..67fd210c35 100644 --- a/compiler/nir/ast2ir.nim +++ b/compiler/nir/ast2ir.nim @@ -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 diff --git a/compiler/nir/nirinsts.nim b/compiler/nir/nirinsts.nim index e12973c450..192cf752e3 100644 --- a/compiler/nir/nirinsts.nim +++ b/compiler/nir/nirinsts.nim @@ -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..