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)
9'>269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494