Skip to content

Commit

Permalink
Fix vm.nim for --gc:arc (#13741)
Browse files Browse the repository at this point in the history
* koch boot --gc:arc now passes the nim stage

... but generates invalid C code

* Move it closer to where its used

* Try something else

* Poor mans var

* Use UncheckedArray instead
  • Loading branch information
Clyybber authored Mar 26, 2020
1 parent 6162da8 commit 2925a47
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 41 deletions.
16 changes: 2 additions & 14 deletions compiler/vm.nim
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,6 @@ when hasFFI:
import evalffi

type
TRegisterKind = enum
rkNone, rkNode, rkInt, rkFloat, rkRegisterAddr, rkNodeAddr
TFullReg = object # with a custom mark proc, we could use the same
# data representation as LuaJit (tagged NaNs).
case kind: TRegisterKind
of rkNone: nil
of rkInt: intVal: BiggestInt
of rkFloat: floatVal: BiggestFloat
of rkNode: node: PNode
of rkRegisterAddr: regAddr: ptr TFullReg
of rkNodeAddr: nodeAddr: ptr PNode

PStackFrame* = ref TStackFrame
TStackFrame* = object
prc: PSym # current prc; proc that is evaluated
Expand Down Expand Up @@ -1206,7 +1194,7 @@ proc rawExecute(c: PCtx, start: int, tos: PStackFrame): TFullReg =
if prc.offset < -1:
# it's a callback:
c.callbacks[-prc.offset-2].value(
VmArgs(ra: ra, rb: rb, rc: rc, slots: cast[pointer](regs),
VmArgs(ra: ra, rb: rb, rc: rc, slots: cast[ptr UncheckedArray[TFullReg]](addr regs[0]),
currentException: c.currentExceptionA,
currentLineInfo: c.debug[pc]))
elif importcCond(prc):
Expand Down Expand Up @@ -1525,7 +1513,7 @@ proc rawExecute(c: PCtx, start: int, tos: PStackFrame): TFullReg =
rc = instr.regC
idx = int(regs[rb+rc-1].intVal)
callback = c.callbacks[idx].value
args = VmArgs(ra: ra, rb: rb, rc: rc, slots: cast[pointer](regs),
args = VmArgs(ra: ra, rb: rb, rc: rc, slots: cast[ptr UncheckedArray[TFullReg]](addr regs[0]),
currentException: c.currentExceptionA,
currentLineInfo: c.debug[pc])
callback(args)
Expand Down
14 changes: 13 additions & 1 deletion compiler/vmdef.nim
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,18 @@ type
slotTempComplex, # some complex temporary (s.node field is used)
slotTempPerm # slot is temporary but permanent (hack)

TRegisterKind* = enum
rkNone, rkNode, rkInt, rkFloat, rkRegisterAddr, rkNodeAddr
TFullReg* = object # with a custom mark proc, we could use the same
# data representation as LuaJit (tagged NaNs).
case kind*: TRegisterKind
of rkNone: nil
of rkInt: intVal*: BiggestInt
of rkFloat: floatVal*: BiggestFloat
of rkNode: node*: PNode
of rkRegisterAddr: regAddr*: ptr TFullReg
of rkNodeAddr: nodeAddr*: ptr PNode

PProc* = ref object
blocks*: seq[TBlock] # blocks; temp data structure
sym*: PSym
Expand All @@ -220,7 +232,7 @@ type

VmArgs* = object
ra*, rb*, rc*: Natural
slots*: pointer
slots*: ptr UncheckedArray[TFullReg]
currentException*: PNode
currentLineInfo*: TLineInfo
VmCallback* = proc (args: VmArgs) {.closure.}
Expand Down
41 changes: 15 additions & 26 deletions compiler/vmhooks.nim
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,8 @@
import pathutils

template setX(k, field) {.dirty.} =
var s: seq[TFullReg]
move(s, cast[seq[TFullReg]](a.slots))
s[a.ra].ensureKind(k)
s[a.ra].field = v
a.slots[a.ra].ensureKind(k)
a.slots[a.ra].field = v

proc setResult*(a: VmArgs; v: BiggestInt) = setX(rkInt, intVal)
proc setResult*(a: VmArgs; v: BiggestFloat) = setX(rkFloat, floatVal)
Expand All @@ -22,45 +20,36 @@ proc setResult*(a: VmArgs; v: bool) =
setX(rkInt, intVal)

proc setResult*(a: VmArgs; v: string) =
var s: seq[TFullReg]
move(s, cast[seq[TFullReg]](a.slots))
s[a.ra].ensureKind(rkNode)
s[a.ra].node = newNode(nkStrLit)
s[a.ra].node.strVal = v
a.slots[a.ra].ensureKind(rkNode)
a.slots[a.ra].node = newNode(nkStrLit)
a.slots[a.ra].node.strVal = v

proc setResult*(a: VmArgs; n: PNode) =
var s: seq[TFullReg]
move(s, cast[seq[TFullReg]](a.slots))
s[a.ra].ensureKind(rkNode)
s[a.ra].node = n
a.slots[a.ra].ensureKind(rkNode)
a.slots[a.ra].node = n

proc setResult*(a: VmArgs; v: AbsoluteDir) = setResult(a, v.string)

proc setResult*(a: VmArgs; v: seq[string]) =
var s: seq[TFullReg]
move(s, cast[seq[TFullReg]](a.slots))
s[a.ra].ensureKind(rkNode)
a.slots[a.ra].ensureKind(rkNode)
var n = newNode(nkBracket)
for x in v: n.add newStrNode(nkStrLit, x)
s[a.ra].node = n
a.slots[a.ra].node = n

template getX(k, field) {.dirty.} =
doAssert i < a.rc-1
let s = cast[seq[TFullReg]](a.slots)
doAssert s[i+a.rb+1].kind == k
result = s[i+a.rb+1].field
doAssert a.slots[i+a.rb+1].kind == k
result = a.slots[i+a.rb+1].field

proc getInt*(a: VmArgs; i: Natural): BiggestInt = getX(rkInt, intVal)
proc getBool*(a: VmArgs; i: Natural): bool = getInt(a, i) != 0
proc getFloat*(a: VmArgs; i: Natural): BiggestFloat = getX(rkFloat, floatVal)
proc getString*(a: VmArgs; i: Natural): string =
doAssert i < a.rc-1
let s = cast[seq[TFullReg]](a.slots)
doAssert s[i+a.rb+1].kind == rkNode
result = s[i+a.rb+1].node.strVal
doAssert a.slots[i+a.rb+1].kind == rkNode
result = a.slots[i+a.rb+1].node.strVal

proc getNode*(a: VmArgs; i: Natural): PNode =
doAssert i < a.rc-1
let s = cast[seq[TFullReg]](a.slots)
doAssert s[i+a.rb+1].kind == rkNode
result = s[i+a.rb+1].node
doAssert a.slots[i+a.rb+1].kind == rkNode
result = a.slots[i+a.rb+1].node

0 comments on commit 2925a47

Please sign in to comment.