github.com/klaytn/klaytn@v1.10.2/blockchain/vm/stack.go (about)

     1  // Modifications Copyright 2018 The klaytn Authors
     2  // Copyright 2015 The go-ethereum Authors
     3  // This file is part of the go-ethereum library.
     4  //
     5  // The go-ethereum library is free software: you can redistribute it and/or modify
     6  // it under the terms of the GNU Lesser General Public License as published by
     7  // the Free Software Foundation, either version 3 of the License, or
     8  // (at your option) any later version.
     9  //
    10  // The go-ethereum library is distributed in the hope that it will be useful,
    11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    13  // GNU Lesser General Public License for more details.
    14  //
    15  // You should have received a copy of the GNU Lesser General Public License
    16  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    17  //
    18  // This file is derived from core/vm/stack.go (2018/06/04).
    19  // Modified and improved for the klaytn development.
    20  
    21  package vm
    22  
    23  import (
    24  	"fmt"
    25  	"math/big"
    26  )
    27  
    28  // Stack is an object for basic stack operations. Items popped to the stack are
    29  // expected to be changed and modified. stack does not take care of adding newly
    30  // initialised objects.
    31  type Stack struct {
    32  	data []*big.Int
    33  }
    34  
    35  func newstack() *Stack {
    36  	return &Stack{data: make([]*big.Int, 0, 1024)}
    37  }
    38  
    39  // Data returns the underlying big.Int array.
    40  func (st *Stack) Data() []*big.Int {
    41  	return st.data
    42  }
    43  
    44  func (st *Stack) push(d *big.Int) {
    45  	// NOTE push limit (1024) is checked in baseCheck
    46  	// stackItem := new(big.Int).Set(d)
    47  	// st.data = append(st.data, stackItem)
    48  	st.data = append(st.data, d)
    49  }
    50  
    51  func (st *Stack) pushN(ds ...*big.Int) {
    52  	st.data = append(st.data, ds...)
    53  }
    54  
    55  func (st *Stack) pop() (ret *big.Int) {
    56  	ret = st.data[len(st.data)-1]
    57  	st.data = st.data[:len(st.data)-1]
    58  	return
    59  }
    60  
    61  func (st *Stack) len() int {
    62  	return len(st.data)
    63  }
    64  
    65  func (st *Stack) swap(n int) {
    66  	st.data[st.len()-n], st.data[st.len()-1] = st.data[st.len()-1], st.data[st.len()-n]
    67  }
    68  
    69  func (st *Stack) dup(pool *intPool, n int) {
    70  	st.push(pool.get().Set(st.data[st.len()-n]))
    71  }
    72  
    73  func (st *Stack) Peek() *big.Int {
    74  	return st.data[st.len()-1]
    75  }
    76  
    77  // Back returns the n'th item in stack
    78  func (st *Stack) Back(n int) *big.Int {
    79  	return st.data[st.len()-n-1]
    80  }
    81  
    82  func (st *Stack) require(n int) error {
    83  	if st.len() < n {
    84  		return fmt.Errorf("stack underflow (%d <=> %d)", len(st.data), n) // TODO-Klaytn-Issue615
    85  	}
    86  	return nil
    87  }
    88  
    89  // Print dumps the content of the stack
    90  func (st *Stack) Print() {
    91  	fmt.Println("### stack ###")
    92  	if len(st.data) > 0 {
    93  		for i, val := range st.data {
    94  			fmt.Printf("%-3d  %v\n", i, val)
    95  		}
    96  	} else {
    97  		fmt.Println("-- empty --")
    98  	}
    99  	fmt.Println("#############")
   100  }