diff --git a/pkg/compiler/codegen.go b/pkg/compiler/codegen.go index 5fe02fbec..710d58de6 100644 --- a/pkg/compiler/codegen.go +++ b/pkg/compiler/codegen.go @@ -1455,6 +1455,14 @@ func (c *codegen) convertBuiltin(expr *ast.CallExpr) { emit.Int(c.prog.BinWriter, 3) emit.Opcode(c.prog.BinWriter, opcode.ROLL) emit.Opcode(c.prog.BinWriter, opcode.MIN) + if !c.scope.voidCalls[expr] { + // insert top item to the bottom of MEMCPY args, so that it is left on stack + emit.Opcode(c.prog.BinWriter, opcode.DUP) + emit.Int(c.prog.BinWriter, 6) + emit.Opcode(c.prog.BinWriter, opcode.REVERSEN) + emit.Int(c.prog.BinWriter, 5) + emit.Opcode(c.prog.BinWriter, opcode.REVERSEN) + } emit.Opcode(c.prog.BinWriter, opcode.MEMCPY) case "make": typ := c.typeOf(expr.Args[0]) diff --git a/pkg/compiler/slice_test.go b/pkg/compiler/slice_test.go index 6ceb967dc..ee776407f 100644 --- a/pkg/compiler/slice_test.go +++ b/pkg/compiler/slice_test.go @@ -427,4 +427,14 @@ func TestCopy(t *testing.T) { }` eval(t, src, []byte{0, 3}) }) + t.Run("AssignToVariable", func(t *testing.T) { + src := `package foo + func Main() int { + src := []byte{3, 2, 1} + dst := make([]byte, 2) + n := copy(dst, src) + return n + }` + eval(t, src, big.NewInt(2)) + }) }