fixes #5269
This commit is contained in:
parent
3978845266
commit
c4dd9dc77e
2 changed files with 40 additions and 6 deletions
|
|
@ -1259,6 +1259,13 @@ proc isTemp(c: PCtx; dest: TDest): bool =
|
||||||
template needsAdditionalCopy(n): untyped =
|
template needsAdditionalCopy(n): untyped =
|
||||||
not c.isTemp(dest) and not fitsRegister(n.typ)
|
not c.isTemp(dest) and not fitsRegister(n.typ)
|
||||||
|
|
||||||
|
proc genAdditionalCopy(c: PCtx; n: PNode; opc: TOpcode;
|
||||||
|
dest, idx, value: TRegister) =
|
||||||
|
var cc = c.getTemp(n.typ)
|
||||||
|
c.gABC(n, whichAsgnOpc(n), cc, value, 0)
|
||||||
|
c.gABC(n, opc, dest, idx, cc)
|
||||||
|
c.freeTemp(cc)
|
||||||
|
|
||||||
proc preventFalseAlias(c: PCtx; n: PNode; opc: TOpcode;
|
proc preventFalseAlias(c: PCtx; n: PNode; opc: TOpcode;
|
||||||
dest, idx, value: TRegister) =
|
dest, idx, value: TRegister) =
|
||||||
# opcLdObj et al really means "load address". We sometimes have to create a
|
# opcLdObj et al really means "load address". We sometimes have to create a
|
||||||
|
|
@ -1266,10 +1273,7 @@ proc preventFalseAlias(c: PCtx; n: PNode; opc: TOpcode;
|
||||||
# mylocal = a.b # needs a copy of the data!
|
# mylocal = a.b # needs a copy of the data!
|
||||||
assert n.typ != nil
|
assert n.typ != nil
|
||||||
if needsAdditionalCopy(n):
|
if needsAdditionalCopy(n):
|
||||||
var cc = c.getTemp(n.typ)
|
genAdditionalCopy(c, n, opc, dest, idx, value)
|
||||||
c.gABC(n, whichAsgnOpc(n), cc, value, 0)
|
|
||||||
c.gABC(n, opc, dest, idx, cc)
|
|
||||||
c.freeTemp(cc)
|
|
||||||
else:
|
else:
|
||||||
c.gABC(n, opc, dest, idx, value)
|
c.gABC(n, opc, dest, idx, value)
|
||||||
|
|
||||||
|
|
@ -1352,7 +1356,7 @@ proc genGlobalInit(c: PCtx; n: PNode; s: PSym) =
|
||||||
c.gABx(n, opcLdGlobal, dest, s.position)
|
c.gABx(n, opcLdGlobal, dest, s.position)
|
||||||
if s.ast != nil:
|
if s.ast != nil:
|
||||||
let tmp = c.genx(s.ast)
|
let tmp = c.genx(s.ast)
|
||||||
c.preventFalseAlias(n, opcWrDeref, dest, 0, tmp)
|
c.genAdditionalCopy(n, opcWrDeref, dest, 0, tmp)
|
||||||
c.freeTemp(dest)
|
c.freeTemp(dest)
|
||||||
c.freeTemp(tmp)
|
c.freeTemp(tmp)
|
||||||
|
|
||||||
|
|
@ -1525,7 +1529,7 @@ proc genVarSection(c: PCtx; n: PNode) =
|
||||||
if a.sons[2].kind != nkEmpty:
|
if a.sons[2].kind != nkEmpty:
|
||||||
let tmp = c.genx(a.sons[0], {gfAddrOf})
|
let tmp = c.genx(a.sons[0], {gfAddrOf})
|
||||||
let val = c.genx(a.sons[2])
|
let val = c.genx(a.sons[2])
|
||||||
c.preventFalseAlias(a.sons[2], opcWrDeref, tmp, 0, val)
|
c.genAdditionalCopy(a.sons[2], opcWrDeref, tmp, 0, val)
|
||||||
c.freeTemp(val)
|
c.freeTemp(val)
|
||||||
c.freeTemp(tmp)
|
c.freeTemp(tmp)
|
||||||
else:
|
else:
|
||||||
|
|
|
||||||
30
tests/vm/tcopy_global_var.nim
Normal file
30
tests/vm/tcopy_global_var.nim
Normal file
|
|
@ -0,0 +1,30 @@
|
||||||
|
discard """
|
||||||
|
nimout: "static done"
|
||||||
|
"""
|
||||||
|
|
||||||
|
# bug #5269
|
||||||
|
|
||||||
|
proc assertEq[T](arg0, arg1: T): void =
|
||||||
|
assert arg0 == arg1, $arg0 & " == " & $arg1
|
||||||
|
|
||||||
|
type
|
||||||
|
MyType = object
|
||||||
|
str: string
|
||||||
|
a: int
|
||||||
|
|
||||||
|
block:
|
||||||
|
var localValue = MyType(str: "Original strning, (OK)", a: 0)
|
||||||
|
var valueCopy = localValue
|
||||||
|
valueCopy.a = 123
|
||||||
|
valueCopy.str = "Modified strning, (not OK when in localValue)"
|
||||||
|
assertEq(localValue.str, "Original strning, (OK)")
|
||||||
|
assertEq(localValue.a, 0)
|
||||||
|
|
||||||
|
static:
|
||||||
|
var localValue = MyType(str: "Original strning, (OK)", a: 0)
|
||||||
|
var valueCopy = localValue
|
||||||
|
valueCopy.a = 123
|
||||||
|
valueCopy.str = "Modified strning, (not OK when in localValue)"
|
||||||
|
assertEq(localValue.str, "Original strning, (OK)")
|
||||||
|
assertEq(localValue.a, 0)
|
||||||
|
echo "static done"
|
||||||
Loading…
Add table
Add a link
Reference in a new issue