Skip to content

Commit

Permalink
also handle cast to/from pointer
Browse files Browse the repository at this point in the history
  • Loading branch information
timotheecour committed Nov 14, 2020
1 parent c21970f commit 8c1a686
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 28 deletions.
5 changes: 5 additions & 0 deletions compiler/vm.nim
Original file line number Diff line number Diff line change
Expand Up @@ -616,6 +616,9 @@ proc rawExecute(c: PCtx, start: int, tos: PStackFrame): TFullReg =
stackTrace(c, tos, pc, "opcCastPtrToInt: " & $regs[rb].node.kind)
of rkNodeAddr:
regs[ra].intVal = cast[int](regs[rb].nodeAddr)
of rkInt:
# could be tightened by checking type, eg `tyPointer`
regs[ra].intVal = regs[rb].intVal
else:
stackTrace(c, tos, pc, "opcCastPtrToInt: got " & $regs[rb].kind)
of 2: # tyRef
Expand Down Expand Up @@ -1011,6 +1014,8 @@ proc rawExecute(c: PCtx, start: int, tos: PStackFrame): TFullReg =
ret = ptrEquality(regs[rb].nodeAddr, regs[rc].node)
elif regs[rc].kind == rkNodeAddr:
ret = ptrEquality(regs[rc].nodeAddr, regs[rb].node)
elif regs[rc].kind == rkInt and regs[rb].kind == rkInt:
ret = regs[rb].intVal == regs[rc].intVal
else:
let nb = regs[rb].node
let nc = regs[rc].node
Expand Down
4 changes: 2 additions & 2 deletions compiler/vmgen.nim
Original file line number Diff line number Diff line change
Expand Up @@ -889,14 +889,14 @@ proc genCastIntFloat(c: PCtx; n: PNode; dest: var TDest) =
c.gABC(n, opcCastFloatToInt64, dest, tmp)
# narrowing for 64 bits not needed (no extended sign bits available).
c.freeTemp(tmp)
elif src.kind in PtrLikeKinds + {tyRef} and dst.kind == tyInt:
elif src.kind in PtrLikeKinds + {tyRef} and dst.kind in {tyInt, tyPointer}:
let tmp = c.genx(n[1])
if dest < 0: dest = c.getTemp(n[0].typ)
var imm: BiggestInt = if src.kind in PtrLikeKinds: 1 else: 2
c.gABI(n, opcCastPtrToInt, dest, tmp, imm)
c.freeTemp(tmp)
elif (src.kind in PtrLikeKinds and dst.kind in PtrLikeKinds) or
(src.kind == tyInt and dst.kind in PtrLikeKinds + {tyRef}):
(src.kind in {tyInt, tyPointer} and dst.kind in PtrLikeKinds + {tyRef}):
let tmp = c.genx(n[1])
if dest < 0: dest = c.getTemp(n[0].typ)
c.gABx(n, opcSetType, dest, c.genType(dst))
Expand Down
56 changes: 30 additions & 26 deletions tests/vm/tcastint.nim
Original file line number Diff line number Diff line change
Expand Up @@ -291,32 +291,36 @@ proc test_float32_castB() =
proc testCastRefOrPtr() =
type TFoo = object
f0: string

template fn(Foo, a) =
let pa = cast[int](a)
var a2 = cast[Foo](pa)
let pa2 = cast[int](pa)
doAssert a2[] == a[]
doAssert a2.f0 == a.f0
doAssert pa2 == pa

a2.f0 = "abc2"
doAssert a.f0 == "abc2"
let b = TFoo(f0: "abc3")
a2[] = b
doAssert a2.f0 == "abc3"
doAssert a2[] == b

block: # ref <=> int
type Foo = ref TFoo
var a = Foo(f0: "abc")
fn(Foo, a)

block: # ptr <=> int
type Foo = ptr TFoo
var a0 = TFoo(f0: "abc")
let a = a0.addr
fn(Foo, a)
template fnAux(Lhs) =
block:
template fn(Foo, a) =
let pa = cast[Lhs](a)
var a2 = cast[Foo](pa)
let pa2 = cast[Lhs](pa)
doAssert a2[] == a[]
doAssert a2.f0 == a.f0
doAssert pa2 == pa

a2.f0 = "abc2"
doAssert a.f0 == "abc2"
let b = TFoo(f0: "abc3")
a2[] = b
doAssert a2.f0 == "abc3"
doAssert a2[] == b

block: # ref <=> Lhs
type Foo = ref TFoo
var a = Foo(f0: "abc")
fn(Foo, a)

block: # ptr <=> Lhs
type Foo = ptr TFoo
var a0 = TFoo(f0: "abc")
let a = a0.addr
fn(Foo, a)

fnAux(int)
fnAux(pointer)

template main =
test()
Expand Down

0 comments on commit 8c1a686

Please sign in to comment.