diff --git a/pkg/compiler/codegen.go b/pkg/compiler/codegen.go index 71baf838b..4de352221 100644 --- a/pkg/compiler/codegen.go +++ b/pkg/compiler/codegen.go @@ -702,6 +702,15 @@ func (c *codegen) Visit(node ast.Node) ast.Visitor { return nil } + if arg := c.getCompareWithNilArg(n); arg != nil { + ast.Walk(c, arg) + emit.Opcode(c.prog.BinWriter, opcode.ISNULL) + if n.Op == token.NEQ { + emit.Opcode(c.prog.BinWriter, opcode.NOT) + } + return nil + } + switch n.Op { case token.LAND: end := c.newLabel() @@ -724,27 +733,8 @@ func (c *codegen) Visit(node ast.Node) ast.Visitor { return nil default: - var checkForNull bool - - if isExprNil(n.X) { - checkForNull = true - } else { - ast.Walk(c, n.X) - } - if isExprNil(n.Y) { - checkForNull = true - } else { - ast.Walk(c, n.Y) - } - if checkForNull { - emit.Opcode(c.prog.BinWriter, opcode.ISNULL) - if n.Op == token.NEQ { - emit.Opcode(c.prog.BinWriter, opcode.NOT) - } - - return nil - } - + ast.Walk(c, n.X) + ast.Walk(c, n.Y) switch { case n.Op == token.ADD: // VM has separate opcodes for number and string concatenation @@ -1149,6 +1139,15 @@ func isFallthroughStmt(c ast.Node) bool { return ok && s.Tok == token.FALLTHROUGH } +func (c *codegen) getCompareWithNilArg(n *ast.BinaryExpr) ast.Expr { + if isExprNil(n.X) { + return n.Y + } else if isExprNil(n.Y) { + return n.X + } + return nil +} + func (c *codegen) pushStackLabel(name string, size int) { c.labelList = append(c.labelList, labelWithStackSize{ name: name,