go-ethereum/core/vm/stack.go
2026-03-31 16:59:27 +05:30

87 lines
2 KiB
Go

// Copyright 2014 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
package vm
import (
"sync"
"github.com/holiman/uint256"
)
const stackLimit = 1024
var stackPool = sync.Pool{
New: func() any {
return &Stack{}
},
}
// Stack is an object for basic stack operations. Items popped to the stack are
// expected to be changed and modified. stack does not take care of adding newly
// initialized objects.
type Stack struct {
data [stackLimit]uint256.Int
top int
}
func newstack() *Stack {
return stackPool.Get().(*Stack)
}
func returnStack(s *Stack) {
s.top = 0
stackPool.Put(s)
}
// Data returns the underlying uint256.Int array.
func (st *Stack) Data() []uint256.Int {
return st.data[:st.top]
}
func (st *Stack) push(d *uint256.Int) {
// NOTE push limit (1024) is checked in baseCheck
st.data[st.top] = *d
st.top++
}
func (st *Stack) pop() (ret uint256.Int) {
st.top--
ret = st.data[st.top]
return
}
func (st *Stack) len() int {
return st.top
}
func (st *Stack) swap(n int) {
st.data[st.top-n-1], st.data[st.top-1] = st.data[st.top-1], st.data[st.top-n-1]
}
func (st *Stack) dup(n int) {
st.data[st.top] = st.data[st.top-n]
st.top++
}
func (st *Stack) peek() *uint256.Int {
return &st.data[st.top-1]
}
// Back returns the n'th item in stack
func (st *Stack) Back(n int) *uint256.Int {
return &st.data[st.top-n-1]
}