Official Go implementation of the Ethereum protocol
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
go-ethereum/core/vm/stack.go

70 lines
1.1 KiB

10 years ago
package vm
import (
"fmt"
"math/big"
)
10 years ago
const maxStack = 1024
10 years ago
func newStack() *stack {
return &stack{}
}
10 years ago
type stack struct {
data []*big.Int
ptr int
}
10 years ago
func (st *stack) push(d *big.Int) {
// NOTE push limit (1024) is checked in baseCheck
stackItem := new(big.Int).Set(d)
10 years ago
if len(st.data) > st.ptr {
st.data[st.ptr] = stackItem
10 years ago
} else {
st.data = append(st.data, stackItem)
10 years ago
}
st.ptr++
}
10 years ago
func (st *stack) pop() (ret *big.Int) {
st.ptr--
ret = st.data[st.ptr]
return
}
10 years ago
func (st *stack) len() int {
return st.ptr
10 years ago
}
10 years ago
func (st *stack) swap(n int) {
st.data[st.len()-n], st.data[st.len()-1] = st.data[st.len()-1], st.data[st.len()-n]
10 years ago
}
10 years ago
func (st *stack) dup(n int) {
st.push(st.data[st.len()-n])
}
10 years ago
func (st *stack) peek() *big.Int {
return st.data[st.len()-1]
}
func (st *stack) require(n int) error {
10 years ago
if st.len() < n {
return fmt.Errorf("stack underflow (%d <=> %d)", len(st.data), n)
}
return nil
}
10 years ago
func (st *stack) Print() {
fmt.Println("### stack ###")
if len(st.data) > 0 {
for i, val := range st.data {
fmt.Printf("%-3d %v\n", i, val)
}
} else {
fmt.Println("-- empty --")
}
fmt.Println("#############")
}