From 4eda92569a03199b1d210f9e5654b8bf60ff08ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Valentin=20W=C3=BCstholz?= Date: Sun, 8 Jan 2017 15:43:22 +0100 Subject: [PATCH] core/asm: address feedback from code review. --- cmd/disasm/main.go | 2 +- core/asm/asm.go | 11 +++++------ core/asm/asm_test.go | 2 +- 3 files changed, 7 insertions(+), 8 deletions(-) diff --git a/cmd/disasm/main.go b/cmd/disasm/main.go index d9ae20e86d..4ea2dfcba6 100644 --- a/cmd/disasm/main.go +++ b/cmd/disasm/main.go @@ -34,7 +34,7 @@ func main() { } code := strings.TrimSpace(string(in[:])) fmt.Printf("%v\n", code) - err = asm.PrettyPrintDisassembledInstructions(code) + err = asm.PrintDisassembled(code) if err != nil { fmt.Printf("Error: %v\n", err) return diff --git a/core/asm/asm.go b/core/asm/asm.go index 9250a5fab7..2c3417aeb1 100644 --- a/core/asm/asm.go +++ b/core/asm/asm.go @@ -1,4 +1,4 @@ -// Copyright 2014, 2017 The go-ethereum Authors +// Copyright 2017 The go-ethereum Authors // This file is part of the go-ethereum library. // // The go-ethereum library is free software: you can redistribute it and/or modify @@ -28,8 +28,7 @@ import ( func ForEachDisassembledInstruction(script []byte, fun func(uint64, vm.OpCode, []byte)) error { for pc := uint64(0); pc < uint64(len(script)); pc++ { op := vm.OpCode(script[pc]) - switch op { - case vm.PUSH1, vm.PUSH2, vm.PUSH3, vm.PUSH4, vm.PUSH5, vm.PUSH6, vm.PUSH7, vm.PUSH8, vm.PUSH9, vm.PUSH10, vm.PUSH11, vm.PUSH12, vm.PUSH13, vm.PUSH14, vm.PUSH15, vm.PUSH16, vm.PUSH17, vm.PUSH18, vm.PUSH19, vm.PUSH20, vm.PUSH21, vm.PUSH22, vm.PUSH23, vm.PUSH24, vm.PUSH25, vm.PUSH26, vm.PUSH27, vm.PUSH28, vm.PUSH29, vm.PUSH30, vm.PUSH31, vm.PUSH32: + if op.IsPush() { a := uint64(op) - uint64(vm.PUSH1) + 1 u := pc + 1 + a if uint64(len(script)) <= pc || uint64(len(script)) < u { @@ -37,7 +36,7 @@ func ForEachDisassembledInstruction(script []byte, fun func(uint64, vm.OpCode, [ } fun(pc, op, script[pc+1:u]) pc += a - default: + } else { fun(pc, op, make([]byte, 0)) } } @@ -45,7 +44,7 @@ func ForEachDisassembledInstruction(script []byte, fun func(uint64, vm.OpCode, [ } // Pretty-print all disassembled EVM instructions to stdout. -func PrettyPrintDisassembledInstructions(code string) error { +func PrintDisassembled(code string) error { script, err := hex.DecodeString(code) if err != nil { return err @@ -61,7 +60,7 @@ func PrettyPrintDisassembledInstructions(code string) error { } // Return all disassembled EVM instructions in human-readable format. -func PrettyPrintedDisassembledInstructions(script []byte) ([]string, error) { +func Disassemble(script []byte) ([]string, error) { instrs := make([]string, 0) err := ForEachDisassembledInstruction(script, func(pc uint64, op vm.OpCode, args []byte) { if args != nil && 0 < len(args) { diff --git a/core/asm/asm_test.go b/core/asm/asm_test.go index fc0cd1b25b..738fd62550 100644 --- a/core/asm/asm_test.go +++ b/core/asm/asm_test.go @@ -1,4 +1,4 @@ -// Copyright 2016 The go-ethereum Authors +// Copyright 2017 The go-ethereum Authors // This file is part of the go-ethereum library. // // The go-ethereum library is free software: you can redistribute it and/or modify