github.com/MetalBlockchain/subnet-evm@v0.4.9/core/vm/stack.go (about)

     1  // (c) 2019-2020, Ava Labs, Inc.
     2  //
     3  // This file is a derived work, based on the go-ethereum library whose original
     4  // notices appear below.
     5  //
     6  // It is distributed under a license compatible with the licensing terms of the
     7  // original code from which it is derived.
     8  //
     9  // Much love to the original authors for their work.
    10  // **********
    11  // Copyright 2014 The go-ethereum Authors
    12  // This file is part of the go-ethereum library.
    13  //
    14  // The go-ethereum library is free software: you can redistribute it and/or modify
    15  // it under the terms of the GNU Lesser General Public License as published by
    16  // the Free Software Foundation, either version 3 of the License, or
    17  // (at your option) any later version.
    18  //
    19  // The go-ethereum library is distributed in the hope that it will be useful,
    20  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    21  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    22  // GNU Lesser General Public License for more details.
    23  //
    24  // You should have received a copy of the GNU Lesser General Public License
    25  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    26  
    27  package vm
    28  
    29  import (
    30  	"sync"
    31  
    32  	"github.com/holiman/uint256"
    33  )
    34  
    35  var stackPool = sync.Pool{
    36  	New: func() interface{} {
    37  		return &Stack{data: make([]uint256.Int, 0, 16)}
    38  	},
    39  }
    40  
    41  // Stack is an object for basic stack operations. Items popped to the stack are
    42  // expected to be changed and modified. stack does not take care of adding newly
    43  // initialised objects.
    44  type Stack struct {
    45  	data []uint256.Int
    46  }
    47  
    48  func newstack() *Stack {
    49  	return stackPool.Get().(*Stack)
    50  }
    51  
    52  func returnStack(s *Stack) {
    53  	s.data = s.data[:0]
    54  	stackPool.Put(s)
    55  }
    56  
    57  // Data returns the underlying uint256.Int array.
    58  func (st *Stack) Data() []uint256.Int {
    59  	return st.data
    60  }
    61  
    62  func (st *Stack) push(d *uint256.Int) {
    63  	// NOTE push limit (1024) is checked in baseCheck
    64  	st.data = append(st.data, *d)
    65  }
    66  
    67  func (st *Stack) pop() (ret uint256.Int) {
    68  	ret = st.data[len(st.data)-1]
    69  	st.data = st.data[:len(st.data)-1]
    70  	return
    71  }
    72  
    73  func (st *Stack) len() int {
    74  	return len(st.data)
    75  }
    76  
    77  func (st *Stack) swap(n int) {
    78  	st.data[st.len()-n], st.data[st.len()-1] = st.data[st.len()-1], st.data[st.len()-n]
    79  }
    80  
    81  func (st *Stack) dup(n int) {
    82  	st.push(&st.data[st.len()-n])
    83  }
    84  
    85  func (st *Stack) peek() *uint256.Int {
    86  	return &st.data[st.len()-1]
    87  }
    88  
    89  // Back returns the n'th item in stack
    90  func (st *Stack) Back(n int) *uint256.Int {
    91  	return &st.data[st.len()-n-1]
    92  }