github.com/klaytn/klaytn@v1.12.1/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  	"github.com/holiman/uint256"
    25  )
    26  
    27  // Stack is an object for basic stack operations. Items popped to the stack are
    28  // expected to be changed and modified. stack does not take care of adding newly
    29  // initialised objects.
    30  type Stack struct {
    31  	data []uint256.Int
    32  }
    33  
    34  func newstack() *Stack {
    35  	return &Stack{data: make([]uint256.Int, 0, 16)}
    36  }
    37  
    38  // Data returns the underlying uint256 array.
    39  func (st *Stack) Data() []uint256.Int {
    40  	return st.data
    41  }
    42  
    43  func (st *Stack) push(d *uint256.Int) {
    44  	// NOTE push limit (1024) is checked in baseCheck
    45  	st.data = append(st.data, *d)
    46  }
    47  
    48  func (st *Stack) pushN(ds ...uint256.Int) {
    49  	st.data = append(st.data, ds...)
    50  }
    51  
    52  func (st *Stack) pop() (ret uint256.Int) {
    53  	ret = st.data[len(st.data)-1]
    54  	st.data = st.data[:len(st.data)-1]
    55  	return
    56  }
    57  
    58  func (st *Stack) len() int {
    59  	return len(st.data)
    60  }
    61  
    62  func (st *Stack) swap(n int) {
    63  	st.data[st.len()-n], st.data[st.len()-1] = st.data[st.len()-1], st.data[st.len()-n]
    64  }
    65  
    66  func (st *Stack) dup(n int) {
    67  	st.push(&st.data[st.len()-n])
    68  }
    69  
    70  func (st *Stack) peek() *uint256.Int {
    71  	return &st.data[st.len()-1]
    72  }
    73  
    74  // Back returns the n'th item in stack
    75  func (st *Stack) Back(n int) *uint256.Int {
    76  	return &st.data[st.len()-n-1]
    77  }