stack.go 3.06 KB
Newer Older
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 6 7 8
// 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.
//
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 13 14
// GNU Lesser General Public License for more details.
//
// 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

obscuren's avatar
obscuren committed
17
package vm
18 19 20

import (
	"fmt"
21
	"sync"
22 23

	"github.com/holiman/uint256"
24 25
)

26 27 28 29 30 31
var stackPool = sync.Pool{
	New: func() interface{} {
		return &Stack{data: make([]uint256.Int, 0, 16)}
	},
}

32
// Stack is an object for basic stack operations. Items popped to the stack are
33 34
// expected to be changed and modified. stack does not take care of adding newly
// initialised objects.
35
type Stack struct {
36
	data []uint256.Int
37 38
}

39
func newstack() *Stack {
40 41 42 43 44 45
	return stackPool.Get().(*Stack)
}

func returnStack(s *Stack) {
	s.data = s.data[:0]
	stackPool.Put(s)
46 47
}

48 49
// Data returns the underlying uint256.Int array.
func (st *Stack) Data() []uint256.Int {
50
	return st.data
51 52
}

53
func (st *Stack) push(d *uint256.Int) {
obscuren's avatar
obscuren committed
54
	// NOTE push limit (1024) is checked in baseCheck
55
	st.data = append(st.data, *d)
56
}
57 58
func (st *Stack) pushN(ds ...uint256.Int) {
	// FIXME: Is there a way to pass args by pointers.
59 60
	st.data = append(st.data, ds...)
}
61

62
func (st *Stack) pop() (ret uint256.Int) {
63 64
	ret = st.data[len(st.data)-1]
	st.data = st.data[:len(st.data)-1]
obscuren's avatar
obscuren committed
65
	return
66 67
}

68
func (st *Stack) len() int {
69
	return len(st.data)
obscuren's avatar
obscuren committed
70 71
}

72
func (st *Stack) swap(n int) {
obscuren's avatar
obscuren committed
73
	st.data[st.len()-n], st.data[st.len()-1] = st.data[st.len()-1], st.data[st.len()-n]
obscuren's avatar
obscuren committed
74 75
}

76 77
func (st *Stack) dup(n int) {
	st.push(&st.data[st.len()-n])
78 79
}

80 81
func (st *Stack) peek() *uint256.Int {
	return &st.data[st.len()-1]
82 83
}

84
// Back returns the n'th item in stack
85 86
func (st *Stack) Back(n int) *uint256.Int {
	return &st.data[st.len()-n-1]
87 88
}

89
// Print dumps the content of the stack
90
func (st *Stack) Print() {
91 92 93 94 95 96 97 98 99 100
	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("#############")
}
101

102 103 104 105 106 107
var rStackPool = sync.Pool{
	New: func() interface{} {
		return &ReturnStack{data: make([]uint32, 0, 10)}
	},
}

108 109
// ReturnStack is an object for basic return stack operations.
type ReturnStack struct {
110
	data []uint32
111 112 113
}

func newReturnStack() *ReturnStack {
114 115 116 117 118 119
	return rStackPool.Get().(*ReturnStack)
}

func returnRStack(rs *ReturnStack) {
	rs.data = rs.data[:0]
	rStackPool.Put(rs)
120 121
}

122
func (st *ReturnStack) push(d uint32) {
123 124 125
	st.data = append(st.data, d)
}

126 127
// A uint32 is sufficient as for code below 4.2G
func (st *ReturnStack) pop() (ret uint32) {
128 129 130 131
	ret = st.data[len(st.data)-1]
	st.data = st.data[:len(st.data)-1]
	return
}