Skip to content

Commit

Permalink
revert #12217 since the root problem seems to have been fixed; fix #1…
Browse files Browse the repository at this point in the history
  • Loading branch information
ringabout committed Jun 16, 2022
1 parent d33e112 commit 2bffb86
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 3 deletions.
3 changes: 0 additions & 3 deletions compiler/vmgen.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1382,9 +1382,6 @@ proc unneededIndirection(n: PNode): bool =
n.typ.skipTypes(abstractInstOwned-{tyTypeDesc}).kind == tyRef

proc canElimAddr(n: PNode): PNode =
if n[0].typ.skipTypes(abstractInst).kind in {tyObject, tyTuple, tyArray}:
# objects are reference types in the VM
return n[0]
case n[0].kind
of nkObjUpConv, nkObjDownConv, nkChckRange, nkChckRangeF, nkChckRange64:
var m = n[0][0]
Expand Down
120 changes: 120 additions & 0 deletions tests/vm/tmisc_vm.nim
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@ foo4
(a: 0, b: 0)
(a: 0, b: 0)
(a: 0, b: 0)
z1 m: (lo: 12)
z2 a: (lo: 3)
x1 a: (lo: 3)
x2 a: (lo: 6)
x3 a: (lo: 0)
z3 a: (lo: 3)
x1 a: (lo: 3)
x2 a: (lo: 6)
x3 a: (lo: 0)
'''
"""
import std/sets
Expand Down Expand Up @@ -249,3 +258,114 @@ static:
echo x2[]
let x3 = new(ref MyObject) # cannot generate VM code for ref MyObject
echo x3[]

# bug #19464
type
Wrapper = object
inner: int

proc assign(r: var Wrapper, a: Wrapper) =
r = a

proc myEcho(a: Wrapper) =
var tmp = a
assign(tmp, Wrapper(inner: 0)) # this shouldn't modify `a`
doAssert a.inner == 1

static:
var result: Wrapper
assign(result, Wrapper(inner: 1))
myEcho(result)

when true:
# bug #15974
type Foo = object
f0: int

proc fn(a: var Foo) =
var s: Foo
a = Foo(f0: 2)
s = a
doAssert s.f0 == 2
a = Foo(f0: 3)
doAssert s.f0 == 2

proc test2()=
var a = Foo(f0: 1)
fn(a)

static: test2()
test2()

# bug #12551
type
StUint = object
lo: uint64

func `+=`(x: var Stuint, y: Stuint) =
x.lo += y.lo

func `-`(x, y: Stuint): Stuint =
result.lo = x.lo - y.lo

func `+`(x, y: Stuint): Stuint =
result.lo = x.lo + y.lo

func `-=`(x: var Stuint, y: Stuint) =
x = x - y

func `<`(x, y: Stuint): bool=
x.lo < y.lo

func `==`(x, y: Stuint): bool =
x.lo == y.lo

func `<=`(x, y: Stuint): bool =
x.lo <= y.lo

proc div3n2n(r: var Stuint, b: Stuint) =
var d: Stuint
r = d
r += b

func div2n1n(r: var Stuint, b: Stuint) =
div3n2n(r, b)

func divmodBZ(x, y: Stuint, r: var Stuint)=
div2n1n(r, y)
r.lo = 3

func `mod`(x, y: Stuint): Stuint =
divmodBZ(x, y, result)

func doublemod_internal(a, m: Stuint): Stuint =
result = a
if a >= m - a:
result -= m
result += a

func mulmod_internal(a, b, m: Stuint): Stuint =
var (a, b) = (a, b)
swap(a, b)
debugEcho "x1 a: ", a
a = doublemod_internal(a, m)
debugEcho "x2 a: ", a
a = doublemod_internal(a, m)
debugEcho "x3 a: ", a

func powmod_internal(a, m: Stuint): Stuint =
var a = a
debugEcho "z1 m: ", m
debugEcho "z2 a: ", a
result = mulmod_internal(result, a, m)
debugEcho "z3 a: ", a
a = mulmod_internal(a, a, m)

func powmod*(a, m: Stuint) =
discard powmod_internal(a mod m, m)

static:
var x = Stuint(lo: high(uint64))
var y = Stuint(lo: 12)

powmod(x, y)

0 comments on commit 2bffb86

Please sign in to comment.