From 3e0899359be8bd962fd673f2530ba4d285cc28ae Mon Sep 17 00:00:00 2001 From: Araq Date: Thu, 25 Jul 2013 08:37:37 +0200 Subject: [PATCH 1/3] new evaluation engine: next steps --- compiler/types.nim | 4 ++-- compiler/vm.nim | 4 ---- compiler/vmgen.nim | 15 +++++++++------ 3 files changed, 11 insertions(+), 12 deletions(-) diff --git a/compiler/types.nim b/compiler/types.nim index 9dad958b8..6c4249b83 100644 --- a/compiler/types.nim +++ b/compiler/types.nim @@ -1028,9 +1028,9 @@ proc typeAllowedAux(marker: var TIntSet, typ: PType, kind: TSymKind): bool = result = kind == skParam of tyGenericInst, tyDistinct: result = typeAllowedAux(marker, lastSon(t), kind) - of tyRange: + of tyRange: result = skipTypes(t.sons[0], abstractInst-{tyTypeDesc}).kind in - {tyChar, tyEnum, tyInt..tyFloat128} + {tyChar, tyEnum, tyInt..tyUInt64} of tyOpenArray, tyVarargs: result = (kind == skParam) and typeAllowedAux(marker, t.sons[0], skVar) of tySequence: diff --git a/compiler/vm.nim b/compiler/vm.nim index 72e0cb5b1..97c870051 100644 --- a/compiler/vm.nim +++ b/compiler/vm.nim @@ -197,14 +197,10 @@ proc execute(c: PCtx, start: int) = of opcYldYoid: assert false of opcYldVal: assert false of opcAsgnInt: - echo ra, " ", instr.regB, " ", regs.len, tos.prc.name.s decodeB(nkIntLit) regs[ra].intVal = regs[rb].intVal of opcAsgnStr: decodeB(nkStrLit) - debug regs[rb] - echo rb - Message(c.debug[pc], warnUser, " here") regs[ra].strVal = regs[rb].strVal of opcAsgnFloat: decodeB(nkFloatLit) diff --git a/compiler/vmgen.nim b/compiler/vmgen.nim index 048a89b8c..baa38e05a 100644 --- a/compiler/vmgen.nim +++ b/compiler/vmgen.nim @@ -40,19 +40,22 @@ proc gABC(ctx: PCtx; n: PNode; opc: TOpcode; a, b, c: TRegister = 0) = ctx.debug.add(n.info) proc gABI(c: PCtx; n: PNode; opc: TOpcode; a, b: TRegister; imm: biggestInt) = - let ins = (opc.ord or a shl 8 or b shl 16 or (imm+byteExcess) shl 24).TInstr + let ins = (opc.uint32 or (a.uint32 shl 8'u32) or + (b.uint32 shl 16'u32) or + (imm+byteExcess).uint32 shl 24'u32).TInstr c.code.add(ins) c.debug.add(n.info) proc gABx(c: PCtx; n: PNode; opc: TOpcode; a: TRegister = 0; bx: int) = - let ins = (opc.ord or a shl 8 or (bx+wordExcess) shl 16).TInstr + let ins = (opc.uint32 or a.uint32 shl 8'u32 or + (bx+wordExcess).uint32 shl 16'u32).TInstr c.code.add(ins) c.debug.add(n.info) proc xjmp(c: PCtx; n: PNode; opc: TOpcode; a: TRegister = 0): TPosition = #assert opc in {opcJmp, opcFJmp, opcTJmp} - gABx(c, n, opc, a, 0) result = TPosition(c.code.len) + gABx(c, n, opc, a, 0) proc genLabel(c: PCtx): TPosition = result = TPosition(c.code.len) @@ -71,7 +74,8 @@ proc patch(c: PCtx, p: TPosition) = InternalAssert(-0x7fff < diff and diff < 0x7fff) let oldInstr = c.code[p] # opcode and regA stay the same: - c.code[p] = ((oldInstr.int and 0xffff) or (diff+wordExcess)).TInstr + c.code[p] = ((oldInstr.uint32 and 0xffff'u32) or + uint32(diff+wordExcess) shr 16'u32).TInstr proc getSlotKind(t: PType): TSlotKind = case t.skipTypes(abstractRange).kind @@ -704,7 +708,7 @@ proc genAsgn(c: PCtx; le, ri: PNode; requiresCopy: bool) = let dest = c.genx(le.sons[0]) let idx = c.genx(le.sons[1]) let tmp = c.genx(ri) - if le.typ.skipTypes(abstractVarRange).kind in {tyString, tyCString}: + if le.sons[0].typ.skipTypes(abstractVarRange).kind in {tyString, tyCString}: c.gABC(le, opcWrStrIdx, dest, idx, tmp) else: c.gABC(le, whichAsgnOpc(le, opcWrArr), dest, idx, tmp) @@ -820,7 +824,6 @@ proc setSlot(c: PCtx; v: PSym) = v.position = c.prc.maxSlots c.prc.slots[v.position] = (inUse: true, kind: slotFixed) inc c.prc.maxSlots - echo v.name.s, " has position ", v.position proc genVarSection(c: PCtx; n: PNode) = for a in n: From 02c86567eedb16ebc6680096412983d4d1fb2496 Mon Sep 17 00:00:00 2001 From: Araq Date: Thu, 25 Jul 2013 22:40:25 +0200 Subject: [PATCH 2/3] new vm: next steps --- compiler/vm.nim | 4 +++- compiler/vmgen.nim | 9 +++++++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/compiler/vm.nim b/compiler/vm.nim index 97c870051..5f10c4e2c 100644 --- a/compiler/vm.nim +++ b/compiler/vm.nim @@ -79,7 +79,9 @@ template decodeBx(k: expr) {.immediate, dirty.} = let rbx = instr.regBx - wordExcess ensureKind(k) -proc compile(c: PCtx, s: PSym): int = vmgen.genProc(c, s) +proc compile(c: PCtx, s: PSym): int = + result = vmgen.genProc(c, s) + c.echoCode proc myreset(n: PNode) = when defined(system.reset): diff --git a/compiler/vmgen.nim b/compiler/vmgen.nim index baa38e05a..9e276f86c 100644 --- a/compiler/vmgen.nim +++ b/compiler/vmgen.nim @@ -25,12 +25,17 @@ proc codeListing(c: PCtx, result: var string) = result.addf("\t$#\tr$#, r$#, r$#", ($opc).substr(3), x.regA, x.regB, x.regC) else: - result.addf("\t$#\tr$#, r$#", ($opc).substr(3), x.regA, x.regBx) - result.add(" #") + result.addf("\t$#\tr$#, r$#", ($opc).substr(3), x.regA, x.regBx-wordExcess) + result.add("\t#") result.add(toFileLine(c.debug[i])) result.add("\n") inc i +proc echoCode*(c: PCtx) = + var buf = "" + codeListing(c, buf) + echo buf + proc gABC(ctx: PCtx; n: PNode; opc: TOpcode; a, b, c: TRegister = 0) = assert opc.ord < 255 let ins = (opc.uint32 or (a.uint32 shl 8'u32) or From e360d048bab2af6d7542d4ea8549778a657bad0d Mon Sep 17 00:00:00 2001 From: Dominik Picheta Date: Thu, 25 Jul 2013 22:15:07 +0100 Subject: [PATCH 3/3] Added macros.newIfStmt and macros.newVarStmt. --- lib/core/macros.nim | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/lib/core/macros.nim b/lib/core/macros.nim index 9130c3ffb..856bc0929 100644 --- a/lib/core/macros.nim +++ b/lib/core/macros.nim @@ -429,11 +429,17 @@ proc newStmtList*(stmts: varargs[PNimrodNode]): PNimrodNode {.compileTime.}= proc newBlockStmt*(label, body: PNimrodNode): PNimrodNode {.compileTime.} = ## Create a new block statement with label return newNimNode(nnkBlockStmt).add(label, body) + proc newBlockStmt*(body: PNimrodNode): PNimrodNode {.compiletime.} = ## Create a new block: stmt return newNimNode(nnkBlockStmt).add(newEmptyNode(), body) -proc newLetStmt*(name, value: PNimrodNode): PNimrodNode{.compiletime.} = +proc newVarStmt*(name, value: PNimrodNode): PNimrodNode {.compiletime.} = + ## Create a new var stmt + return newNimNode(nnkVarSection).add( + newNimNode(nnkIdentDefs).add(name, newNimNode(nnkEmpty), value)) + +proc newLetStmt*(name, value: PNimrodNode): PNimrodNode {.compiletime.} = ## Create a new let stmt return newNimNode(nnkLetSection).add( newNimNode(nnkIdentDefs).add(name, newNimNode(nnkEmpty), value)) @@ -484,6 +490,22 @@ proc newProc*(name = newEmptyNode(); params: openarray[PNimrodNode] = []; newEmptyNode(), body) +proc newIfStmt*(branches: varargs[tuple[cond, body: PNimrodNode]]): + PNimrodNode {.compiletime.} = + ## Constructor for ``if`` statements. + ## + ## .. code-block:: nimrod + ## + ## newIfStmt( + ## (Ident, StmtList), + ## ... + ## ) + ## + result = newNimNode(nnkIfStmt) + for i in branches: + result.add(newNimNode(nnkElifBranch).add(i.cond, i.body)) + + proc copyChildrenTo*(src, dest: PNimrodNode) {.compileTime.}= ## Copy all children from `src` to `dest` for i in 0 .. < src.len: