summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorAraq <rumpf_a@web.de>2013-07-26 12:17:36 +0200
committerAraq <rumpf_a@web.de>2013-07-26 12:17:36 +0200
commit698eac2a94f9b2ef687735cf3e49d93e7cdb2121 (patch)
treec38ba8e6e037b0597d9b2578d4c1baf4b4480d00
parent05108cf81c3758ce8fbe9026470de296c9b264a1 (diff)
downloadNim-698eac2a94f9b2ef687735cf3e49d93e7cdb2121.tar.gz
new vm: can execute simple programs
-rw-r--r--compiler/vm.nim7
-rw-r--r--compiler/vmgen.nim12
2 files changed, 10 insertions, 9 deletions
diff --git a/compiler/vm.nim b/compiler/vm.nim
index 5f10c4e2c..7ae5b7878 100644
--- a/compiler/vm.nim
+++ b/compiler/vm.nim
@@ -81,7 +81,7 @@ template decodeBx(k: expr) {.immediate, dirty.} =
 
 proc compile(c: PCtx, s: PSym): int = 
   result = vmgen.genProc(c, s)
-  c.echoCode
+  #c.echoCode
 
 proc myreset(n: PNode) =
   when defined(system.reset): 
@@ -183,7 +183,7 @@ proc execute(c: PCtx, start: int) =
     {.interpreterLoop.}
     let instr = c.code[pc]
     let ra = instr.regA
-    echo "PC ", pc, " ", c.code[pc].opcode, " ra ", ra
+    #echo "PC ", pc, " ", c.code[pc].opcode, " ra ", ra
     case instr.opcode
     of opcEof: break
     of opcRet:
@@ -462,7 +462,8 @@ proc execute(c: PCtx, start: int) =
         newFrame.slots[i] = newNode(nkEmpty)
       tos = newFrame
       move(regs, newFrame.slots)
-      pc = newPc
+      # -1 for the following 'inc pc'
+      pc = newPc-1
     of opcTJmp:
       # jump Bx if A != 0
       let rbx = instr.regBx - wordExcess - 1 # -1 for the following 'inc pc'
diff --git a/compiler/vmgen.nim b/compiler/vmgen.nim
index 9e276f86c..00acbd48d 100644
--- a/compiler/vmgen.nim
+++ b/compiler/vmgen.nim
@@ -25,7 +25,7 @@ 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-wordExcess)
+      result.addf("\t$#\tr$#, $#", ($opc).substr(3), x.regA, x.regBx-wordExcess)
     result.add("\t#")
     result.add(toFileLine(c.debug[i]))
     result.add("\n")
@@ -66,7 +66,7 @@ proc genLabel(c: PCtx): TPosition =
   result = TPosition(c.code.len)
   c.jumpTargets.incl(c.code.len)
 
-proc jmp(c: PCtx, n: PNode, opc: TOpcode, p = TPosition(0)) =
+proc jmpBack(c: PCtx, n: PNode, opc: TOpcode, p = TPosition(0)) =
   let dist = p.int - c.code.len
   InternalAssert(-0x7fff < dist and dist < 0x7fff)
   gABx(c, n, opc, 0, dist)
@@ -79,8 +79,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.uint32 and 0xffff'u32) or
-               uint32(diff+wordExcess) shr 16'u32).TInstr
+  c.code[p] = ((oldInstr.uint32 and 0xffff'u32).uint32 or
+               uint32(diff+wordExcess) shl 16'u32).TInstr
 
 proc getSlotKind(t: PType): TSlotKind =
   case t.skipTypes(abstractRange).kind
@@ -185,7 +185,7 @@ proc genWhile(c: PCtx; n: PNode) =
     let L2 = c.xjmp(n, opcFJmp, tmp)
     c.freeTemp(tmp)
     c.gen(n.sons[1])
-    c.jmp(n, opcJmp, L1)
+    c.jmpBack(n, opcJmp, L1)
     c.patch(L2)
 
 proc genBlock(c: PCtx; n: PNode; dest: var TDest) =
@@ -222,7 +222,7 @@ proc genIf(c: PCtx, n: PNode; dest: var TDest) =
       withTemp(tmp, it.sons[0].typ):
         c.gen(it.sons[0], tmp)
         let elsePos = c.xjmp(it.sons[0], opcFJmp, tmp) # if false
-      c.gen(n.sons[1], dest) # then part
+      c.gen(it.sons[1], dest) # then part
       if i < sonsLen(n)-1:
         endings.add(c.xjmp(it.sons[1], opcJmp, 0))
       c.patch(elsePos)