gitlab.com/flarenetwork/coreth@v0.1.1/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 "fmt" 31 "sync" 32 33 "github.com/holiman/uint256" 34 ) 35 36 var stackPool = sync.Pool{ 37 New: func() interface{} { 38 return &Stack{data: make([]uint256.Int, 0, 16)} 39 }, 40 } 41 42 // Stack is an object for basic stack operations. Items popped to the stack are 43 // expected to be changed and modified. stack does not take care of adding newly 44 // initialised objects. 45 type Stack struct { 46 data []uint256.Int 47 } 48 49 func newstack() *Stack { 50 return stackPool.Get().(*Stack) 51 } 52 53 func returnStack(s *Stack) { 54 s.data = s.data[:0] 55 stackPool.Put(s) 56 } 57 58 // Data returns the underlying uint256.Int array. 59 func (st *Stack) Data() []uint256.Int { 60 return st.data 61 } 62 63 func (st *Stack) push(d *uint256.Int) { 64 // NOTE push limit (1024) is checked in baseCheck 65 st.data = append(st.data, *d) 66 } 67 func (st *Stack) pushN(ds ...uint256.Int) { 68 // FIXME: Is there a way to pass args by pointers. 69 st.data = append(st.data, ds...) 70 } 71 72 func (st *Stack) pop() (ret uint256.Int) { 73 ret = st.data[len(st.data)-1] 74 st.data = st.data[:len(st.data)-1] 75 return 76 } 77 78 func (st *Stack) len() int { 79 return len(st.data) 80 } 81 82 func (st *Stack) swap(n int) { 83 st.data[st.len()-n], st.data[st.len()-1] = st.data[st.len()-1], st.data[st.len()-n] 84 } 85 86 func (st *Stack) dup(n int) { 87 st.push(&st.data[st.len()-n]) 88 } 89 90 func (st *Stack) peek() *uint256.Int { 91 return &st.data[st.len()-1] 92 } 93 94 // Back returns the n'th item in stack 95 func (st *Stack) Back(n int) *uint256.Int { 96 return &st.data[st.len()-n-1] 97 } 98 99 // Print dumps the content of the stack 100 func (st *Stack) Print() { 101 fmt.Println("### stack ###") 102 if len(st.data) > 0 { 103 for i, val := range st.data { 104 fmt.Printf("%-3d %v\n", i, val) 105 } 106 } else { 107 fmt.Println("-- empty --") 108 } 109 fmt.Println("#############") 110 }