github.com/ethereum/go-ethereum@v1.16.1/core/vm/stack.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 vm
    18  
    19  import (
    20  	"sync"
    21  
    22  	"github.com/holiman/uint256"
    23  )
    24  
    25  var stackPool = sync.Pool{
    26  	New: func() interface{} {
    27  		return &Stack{data: make([]uint256.Int, 0, 16)}
    28  	},
    29  }
    30  
    31  // Stack is an object for basic stack operations. Items popped to the stack are
    32  // expected to be changed and modified. stack does not take care of adding newly
    33  // initialized objects.
    34  type Stack struct {
    35  	data []uint256.Int
    36  }
    37  
    38  func newstack() *Stack {
    39  	return stackPool.Get().(*Stack)
    40  }
    41  
    42  func returnStack(s *Stack) {
    43  	s.data = s.data[:0]
    44  	stackPool.Put(s)
    45  }
    46  
    47  // Data returns the underlying uint256.Int array.
    48  func (st *Stack) Data() []uint256.Int {
    49  	return st.data
    50  }
    51  
    52  func (st *Stack) push(d *uint256.Int) {
    53  	// NOTE push limit (1024) is checked in baseCheck
    54  	st.data = append(st.data, *d)
    55  }
    56  
    57  func (st *Stack) pop() (ret uint256.Int) {
    58  	ret = st.data[len(st.data)-1]
    59  	st.data = st.data[:len(st.data)-1]
    60  	return
    61  }
    62  
    63  func (st *Stack) len() int {
    64  	return len(st.data)
    65  }
    66  
    67  func (st *Stack) swap1() {
    68  	st.data[st.len()-2], st.data[st.len()-1] = st.data[st.len()-1], st.data[st.len()-2]
    69  }
    70  func (st *Stack) swap2() {
    71  	st.data[st.len()-3], st.data[st.len()-1] = st.data[st.len()-1], st.data[st.len()-3]
    72  }
    73  func (st *Stack) swap3() {
    74  	st.data[st.len()-4], st.data[st.len()-1] = st.data[st.len()-1], st.data[st.len()-4]
    75  }
    76  func (st *Stack) swap4() {
    77  	st.data[st.len()-5], st.data[st.len()-1] = st.data[st.len()-1], st.data[st.len()-5]
    78  }
    79  func (st *Stack) swap5() {
    80  	st.data[st.len()-6], st.data[st.len()-1] = st.data[st.len()-1], st.data[st.len()-6]
    81  }
    82  func (st *Stack) swap6() {
    83  	st.data[st.len()-7], st.data[st.len()-1] = st.data[st.len()-1], st.data[st.len()-7]
    84  }
    85  func (st *Stack) swap7() {
    86  	st.data[st.len()-8], st.data[st.len()-1] = st.data[st.len()-1], st.data[st.len()-8]
    87  }
    88  func (st *Stack) swap8() {
    89  	st.data[st.len()-9], st.data[st.len()-1] = st.data[st.len()-1], st.data[st.len()-9]
    90  }
    91  func (st *Stack) swap9() {
    92  	st.data[st.len()-10], st.data[st.len()-1] = st.data[st.len()-1], st.data[st.len()-10]
    93  }
    94  func (st *Stack) swap10() {
    95  	st.data[st.len()-11], st.data[st.len()-1] = st.data[st.len()-1], st.data[st.len()-11]
    96  }
    97  func (st *Stack) swap11() {
    98  	st.data[st.len()-12], st.data[st.len()-1] = st.data[st.len()-1], st.data[st.len()-12]
    99  }
   100  func (st *Stack) swap12() {
   101  	st.data[st.len()-13], st.data[st.len()-1] = st.data[st.len()-1], st.data[st.len()-13]
   102  }
   103  func (st *Stack) swap13() {
   104  	st.data[st.len()-14], st.data[st.len()-1] = st.data[st.len()-1], st.data[st.len()-14]
   105  }
   106  func (st *Stack) swap14() {
   107  	st.data[st.len()-15], st.data[st.len()-1] = st.data[st.len()-1], st.data[st.len()-15]
   108  }
   109  func (st *Stack) swap15() {
   110  	st.data[st.len()-16], st.data[st.len()-1] = st.data[st.len()-1], st.data[st.len()-16]
   111  }
   112  func (st *Stack) swap16() {
   113  	st.data[st.len()-17], st.data[st.len()-1] = st.data[st.len()-1], st.data[st.len()-17]
   114  }
   115  
   116  func (st *Stack) dup(n int) {
   117  	st.push(&st.data[st.len()-n])
   118  }
   119  
   120  func (st *Stack) peek() *uint256.Int {
   121  	return &st.data[st.len()-1]
   122  }
   123  
   124  // Back returns the n'th item in stack
   125  func (st *Stack) Back(n int) *uint256.Int {
   126  	return &st.data[st.len()-n-1]
   127  }