From 2925a47ae6aaa8e93117a29b018582540bf55856 Mon Sep 17 00:00:00 2001 From: Clyybber Date: Thu, 26 Mar 2020 16:18:45 +0100 Subject: [PATCH] Fix vm.nim for --gc:arc (#13741) * 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 --- compiler/vm.nim | 16 ++-------------- compiler/vmdef.nim | 14 +++++++++++++- compiler/vmhooks.nim | 41 +++++++++++++++-------------------------- 3 files changed, 30 insertions(+), 41 deletions(-) diff --git a/compiler/vm.nim b/compiler/vm.nim index 8e05c5618ff61..a2c6e686d145b 100644 --- a/compiler/vm.nim +++ b/compiler/vm.nim @@ -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 @@ -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): @@ -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) diff --git a/compiler/vmdef.nim b/compiler/vmdef.nim index 176558eaca1a3..7b51c626d5154 100644 --- a/compiler/vmdef.nim +++ b/compiler/vmdef.nim @@ -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 @@ -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.} diff --git a/compiler/vmhooks.nim b/compiler/vmhooks.nim index 412c93a7740f1..d211d834370f8 100644 --- a/compiler/vmhooks.nim +++ b/compiler/vmhooks.nim @@ -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) @@ -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