github.com/core-coin/go-core@v1.1.7/core/vm/stack.go (about)

     1  // Copyright 2014 by the Authors
     2  // This file is part of the go-core library.
     3  //
     4  // The go-core 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-core 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-core library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package vm
    18  
    19  import (
    20  	"fmt"
    21  	"github.com/core-coin/uint256"
    22  	"sync"
    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  // initialised 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 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  func (st *Stack) pushN(ds ...uint256.Int) {
    57  	// FIXME: Is there a way to pass args by pointers.
    58  	st.data = append(st.data, ds...)
    59  }
    60  
    61  func (st *Stack) pop() (ret uint256.Int) {
    62  	ret = st.data[len(st.data)-1]
    63  	st.data = st.data[:len(st.data)-1]
    64  	return
    65  }
    66  
    67  func (st *Stack) len() int {
    68  	return len(st.data)
    69  }
    70  
    71  func (st *Stack) swap(n int) {
    72  	st.data[st.len()-n], st.data[st.len()-1] = st.data[st.len()-1], st.data[st.len()-n]
    73  }
    74  
    75  func (st *Stack) dup(n int) {
    76  	st.push(&st.data[st.len()-n])
    77  }
    78  
    79  func (st *Stack) peek() *uint256.Int {
    80  	return &st.data[st.len()-1]
    81  }
    82  
    83  // Back returns the n'th item in stack
    84  func (st *Stack) Back(n int) *uint256.Int {
    85  	return &st.data[st.len()-n-1]
    86  }
    87  
    88  // Print dumps the content of the stack
    89  func (st *Stack) Print() {
    90  	fmt.Println("### stack ###")
    91  	if len(st.data) > 0 {
    92  		for i, val := range st.data {
    93  			fmt.Printf("%-3d  %v\n", i, val)
    94  		}
    95  	} else {
    96  		fmt.Println("-- empty --")
    97  	}
    98  	fmt.Println("#############")
    99  }
   100  
   101  var rStackPool = sync.Pool{
   102  	New: func() interface{} {
   103  		return &ReturnStack{data: make([]uint32, 0, 10)}
   104  	},
   105  }
   106  
   107  // ReturnStack is an object for basic return stack operations.
   108  type ReturnStack struct {
   109  	data []uint32
   110  }
   111  
   112  func newReturnStack() *ReturnStack {
   113  	return rStackPool.Get().(*ReturnStack)
   114  }
   115  
   116  func returnRStack(rs *ReturnStack) {
   117  	rs.data = rs.data[:0]
   118  	rStackPool.Put(rs)
   119  }
   120  
   121  func (st *ReturnStack) push(d uint32) {
   122  	st.data = append(st.data, d)
   123  }
   124  
   125  // A uint32 is sufficient as for code below 4.2G
   126  func (st *ReturnStack) pop() (ret uint32) {
   127  	ret = st.data[len(st.data)-1]
   128  	st.data = st.data[:len(st.data)-1]
   129  	return
   130  }