Skip to content

Commit

Permalink
compiler: remove jumps to the next instruction
Browse files Browse the repository at this point in the history
In case there are no returns in the inlined function, jumps point to the
next instruction and can be omitted. This optimization can be extended
to handle other cases, here we just make sure that already existing code
stays the same.

Signed-off-by: Evgeniy Stratonikov <[email protected]>
  • Loading branch information
fyrchik committed Jul 12, 2022
1 parent 05efc57 commit 17329ee
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
9 changes: 7 additions & 2 deletions pkg/compiler/codegen.go
Original file line number Diff line number Diff line change
Expand Up @@ -2244,8 +2244,13 @@ func (c *codegen) writeJumps(b []byte) ([]byte, error) {
return nil, err
}
if op != opcode.PUSHA && math.MinInt8 <= offset && offset <= math.MaxInt8 {
copy(b[ctx.IP():], []byte{byte(toShortForm(op)), byte(offset), byte(opcode.NOP), byte(opcode.NOP), byte(opcode.NOP)})
nopOffsets = append(nopOffsets, ctx.IP()+2, ctx.IP()+3, ctx.IP()+4)
if op == opcode.JMPL && offset == 5 {
copy(b[ctx.IP():], []byte{byte(opcode.NOP), byte(opcode.NOP), byte(opcode.NOP), byte(opcode.NOP), byte(opcode.NOP)})
nopOffsets = append(nopOffsets, ctx.IP(), ctx.IP()+1, ctx.IP()+2, ctx.IP()+3, ctx.IP()+4)
} else {
copy(b[ctx.IP():], []byte{byte(toShortForm(op)), byte(offset), byte(opcode.NOP), byte(opcode.NOP), byte(opcode.NOP)})
nopOffsets = append(nopOffsets, ctx.IP()+2, ctx.IP()+3, ctx.IP()+4)
}
}
case opcode.INITSLOT:
nextIP := ctx.NextIP()
Expand Down
2 changes: 0 additions & 2 deletions pkg/compiler/inline_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,6 @@ func TestInlineVariadicInInlinedCall(t *testing.T) {
}

func TestInlineConversion(t *testing.T) {
t.Skip()
src1 := `package foo
import "github.com/nspcc-dev/neo-go/pkg/compiler/testdata/inline"
var _ = inline.A
Expand All @@ -307,7 +306,6 @@ func TestInlineConversion(t *testing.T) {
}

func TestInlineConversionQualified(t *testing.T) {
t.Skip()
src1 := `package foo
import "github.com/nspcc-dev/neo-go/pkg/compiler/testdata/inline"
var A = 1
Expand Down

0 comments on commit 17329ee

Please sign in to comment.