From 2b73508561e1d37457a79369a74940f737755bc3 Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Wed, 19 Aug 2020 13:51:30 +0300 Subject: [PATCH] compiler: emit short jumps while short-circuiting Unless there is some `ast.Walk` between current instruction and jump target, we can calculate the offset precisely. --- pkg/compiler/codegen.go | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/pkg/compiler/codegen.go b/pkg/compiler/codegen.go index 79a9ef5b8..8efdb57e4 100644 --- a/pkg/compiler/codegen.go +++ b/pkg/compiler/codegen.go @@ -691,25 +691,21 @@ func (c *codegen) Visit(node ast.Node) ast.Visitor { case *ast.BinaryExpr: switch n.Op { case token.LAND: - next := c.newLabel() end := c.newLabel() ast.Walk(c, n.X) - emit.Jmp(c.prog.BinWriter, opcode.JMPIFL, next) + emit.Instruction(c.prog.BinWriter, opcode.JMPIF, []byte{2 + 1 + 5}) emit.Opcode(c.prog.BinWriter, opcode.PUSHF) emit.Jmp(c.prog.BinWriter, opcode.JMPL, end) - c.setLabel(next) ast.Walk(c, n.Y) c.setLabel(end) return nil case token.LOR: - next := c.newLabel() end := c.newLabel() ast.Walk(c, n.X) - emit.Jmp(c.prog.BinWriter, opcode.JMPIFNOTL, next) + emit.Instruction(c.prog.BinWriter, opcode.JMPIFNOT, []byte{2 + 1 + 5}) emit.Opcode(c.prog.BinWriter, opcode.PUSHT) emit.Jmp(c.prog.BinWriter, opcode.JMPL, end) - c.setLabel(next) ast.Walk(c, n.Y) c.setLabel(end) return nil