diff --git a/pkg/vm/compiler/codegen.go b/pkg/vm/compiler/codegen.go index 0071bf131..8a1ceb622 100644 --- a/pkg/vm/compiler/codegen.go +++ b/pkg/vm/compiler/codegen.go @@ -430,10 +430,29 @@ func (c *codegen) Visit(node ast.Node) ast.Visitor { if !isBuiltin { if numArgs == 2 { emitOpcode(c.prog, vm.SWAP) - } - if numArgs == 3 { + } else if numArgs == 3 { emitInt(c.prog, 2) emitOpcode(c.prog, vm.XSWAP) + } else { + half := int(numArgs / 2) + + for i := 0; i < half; i++ { + to := numArgs - 1 - i + + emitInt(c.prog, int64(to)) + emitOpcode(c.prog, vm.PICK) + + emitInt(c.prog, int64(i+1)) + emitOpcode(c.prog, vm.PICK) + + emitInt(c.prog, int64(to+2)) + emitOpcode(c.prog, vm.XSWAP) + emitOpcode(c.prog, vm.DROP) + + emitInt(c.prog, int64(i+1)) + emitOpcode(c.prog, vm.XSWAP) + emitOpcode(c.prog, vm.DROP) + } } }