compiler: abstract out emitReverse

Extract logic of reversing top n items of the stack
in a separate function.
This commit is contained in:
Evgenii Stratonikov 2020-02-10 10:43:29 +03:00
parent 268cad5c06
commit 1fc64d515f

View file

@ -540,17 +540,7 @@ func (c *codegen) Visit(node ast.Node) ast.Visitor {
} }
// Do not swap for builtin functions. // Do not swap for builtin functions.
if !isBuiltin { if !isBuiltin {
if numArgs == 2 { c.emitReverse(numArgs)
emit.Opcode(c.prog.BinWriter, opcode.SWAP)
} else if numArgs == 3 {
emit.Int(c.prog.BinWriter, 2)
emit.Opcode(c.prog.BinWriter, opcode.XSWAP)
} else {
for i := 1; i < numArgs; i++ {
emit.Int(c.prog.BinWriter, int64(i))
emit.Opcode(c.prog.BinWriter, opcode.ROLL)
}
}
} }
// Check builtin first to avoid nil pointer on funcScope! // Check builtin first to avoid nil pointer on funcScope!
@ -680,6 +670,22 @@ func (c *codegen) Visit(node ast.Node) ast.Visitor {
return c return c
} }
// emitReverse reverses top num items of the stack.
func (c *codegen) emitReverse(num int) {
switch num {
case 2:
emit.Opcode(c.prog.BinWriter, opcode.SWAP)
case 3:
emit.Int(c.prog.BinWriter, 2)
emit.Opcode(c.prog.BinWriter, opcode.XSWAP)
default:
for i := 1; i < num; i++ {
emit.Int(c.prog.BinWriter, int64(i))
emit.Opcode(c.prog.BinWriter, opcode.ROLL)
}
}
}
func (c *codegen) getEqualityOpcode(expr ast.Expr) opcode.Opcode { func (c *codegen) getEqualityOpcode(expr ast.Expr) opcode.Opcode {
t, ok := c.typeInfo.Types[expr].Type.Underlying().(*types.Basic) t, ok := c.typeInfo.Types[expr].Type.Underlying().(*types.Basic)
if ok && t.Info()&types.IsNumeric != 0 { if ok && t.Info()&types.IsNumeric != 0 {