asm.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. // Copyright 2014 The go-ethereum Authors
  2. // This file is part of go-ethereum.
  3. //
  4. // go-ethereum is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // go-ethereum is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
  16. package core
  17. import (
  18. "fmt"
  19. "math/big"
  20. "github.com/ethereum/go-ethereum/common"
  21. "github.com/ethereum/go-ethereum/core/vm"
  22. )
  23. func Disassemble(script []byte) (asm []string) {
  24. pc := new(big.Int)
  25. for {
  26. if pc.Cmp(big.NewInt(int64(len(script)))) >= 0 {
  27. return
  28. }
  29. // Get the memory location of pc
  30. val := script[pc.Int64()]
  31. // Get the opcode (it must be an opcode!)
  32. op := vm.OpCode(val)
  33. asm = append(asm, fmt.Sprintf("%04v: %v", pc, op))
  34. switch op {
  35. case vm.PUSH1, vm.PUSH2, vm.PUSH3, vm.PUSH4, vm.PUSH5, vm.PUSH6, vm.PUSH7, vm.PUSH8,
  36. vm.PUSH9, vm.PUSH10, vm.PUSH11, vm.PUSH12, vm.PUSH13, vm.PUSH14, vm.PUSH15,
  37. vm.PUSH16, vm.PUSH17, vm.PUSH18, vm.PUSH19, vm.PUSH20, vm.PUSH21, vm.PUSH22,
  38. vm.PUSH23, vm.PUSH24, vm.PUSH25, vm.PUSH26, vm.PUSH27, vm.PUSH28, vm.PUSH29,
  39. vm.PUSH30, vm.PUSH31, vm.PUSH32:
  40. pc.Add(pc, common.Big1)
  41. a := int64(op) - int64(vm.PUSH1) + 1
  42. if int(pc.Int64()+a) > len(script) {
  43. return
  44. }
  45. data := script[pc.Int64() : pc.Int64()+a]
  46. if len(data) == 0 {
  47. data = []byte{0}
  48. }
  49. asm = append(asm, fmt.Sprintf("%04v: 0x%x", pc, data))
  50. pc.Add(pc, big.NewInt(a-1))
  51. }
  52. pc.Add(pc, common.Big1)
  53. }
  54. return asm
  55. }