github.com/ethereumproject/go-ethereum@v5.5.2+incompatible/core/asm.go (about)

     1  // Copyright 2014 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library 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  // The go-ethereum library 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 the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package core
    18  
    19  import (
    20  	"fmt"
    21  	"math/big"
    22  
    23  	"github.com/ethereumproject/go-ethereum/common"
    24  	"github.com/ethereumproject/go-ethereum/core/vm"
    25  )
    26  
    27  func Disassemble(script []byte) (asm []string) {
    28  	pc := new(big.Int)
    29  	for {
    30  		if pc.Cmp(big.NewInt(int64(len(script)))) >= 0 {
    31  			return
    32  		}
    33  
    34  		// Get the memory location of pc
    35  		val := script[pc.Int64()]
    36  		// Get the opcode (it must be an opcode!)
    37  		op := vm.OpCode(val)
    38  
    39  		asm = append(asm, fmt.Sprintf("%04v: %v", pc, op))
    40  
    41  		switch op {
    42  		case vm.PUSH1, vm.PUSH2, vm.PUSH3, vm.PUSH4, vm.PUSH5, vm.PUSH6, vm.PUSH7, vm.PUSH8,
    43  			vm.PUSH9, vm.PUSH10, vm.PUSH11, vm.PUSH12, vm.PUSH13, vm.PUSH14, vm.PUSH15,
    44  			vm.PUSH16, vm.PUSH17, vm.PUSH18, vm.PUSH19, vm.PUSH20, vm.PUSH21, vm.PUSH22,
    45  			vm.PUSH23, vm.PUSH24, vm.PUSH25, vm.PUSH26, vm.PUSH27, vm.PUSH28, vm.PUSH29,
    46  			vm.PUSH30, vm.PUSH31, vm.PUSH32:
    47  			pc.Add(pc, common.Big1)
    48  			a := int64(op) - int64(vm.PUSH1) + 1
    49  			if int(pc.Int64()+a) > len(script) {
    50  				return
    51  			}
    52  
    53  			data := script[pc.Int64() : pc.Int64()+a]
    54  			if len(data) == 0 {
    55  				data = []byte{0}
    56  			}
    57  			asm = append(asm, fmt.Sprintf("%04v: 0x%x", pc, data))
    58  
    59  			pc.Add(pc, big.NewInt(a-1))
    60  		}
    61  
    62  		pc.Add(pc, common.Big1)
    63  	}
    64  }