Skip to content

Commit

Permalink
Increased TInstr field sizes: allow long jumps and 65535 VM registers (
Browse files Browse the repository at this point in the history
…#12777)

* Increased regBx size from 16 to 24 bits to increase jump range in the VM
from 32K to 8M instructions. Fixes #12727

* Increased VM TInst register field sizes to 16 bits to allow up to 65535 VM registers per proc

* Added test case for >255 VM registers
  • Loading branch information
zevv authored and Araq committed Dec 10, 2019
1 parent b8152b2 commit e61d702
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 5 deletions.
10 changes: 5 additions & 5 deletions compiler/vmdef.nim
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@

import ast, idents, options, modulegraphs, lineinfos

type TInstrType* = uint32
type TInstrType* = uint64

const
regOBits = 8 # Opcode
regABits = 8
regBBits = 8
regCBits = 8
regBxBits = 16
regABits = 16
regBBits = 16
regCBits = 16
regBxBits = 24

byteExcess* = 128 # we use excess-K for immediates

Expand Down
14 changes: 14 additions & 0 deletions tests/vm/tfarjump.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Test a VM relative jump with an offset larger then 32767 instructions.

import macros

static:
var a = 0
macro foo(): untyped =
let s = newStmtList()
for i in 1..6554:
s.add nnkCommand.newTree(ident("inc"), ident("a"))
quote do:
if true:
`s`
foo()
16 changes: 16 additions & 0 deletions tests/vm/tmanyregs.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import macros

# Generate a proc with more then 255 registers. Should not generate an error at
# compile time

static:
macro mkFoo() =
let ss = newStmtList()
for i in 1..256:
ss.add parseStmt "var x" & $i & " = " & $i
ss.add parseStmt "inc x" & $i
quote do:
proc foo() =
`ss`
mkFoo()
foo()

0 comments on commit e61d702

Please sign in to comment.