stack.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. // 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-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. // 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-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. package vm
  17. import (
  18. "fmt"
  19. "sync"
  20. "github.com/holiman/uint256"
  21. )
  22. var stackPool = sync.Pool{
  23. New: func() interface{} {
  24. return &Stack{data: make([]uint256.Int, 0, 16)}
  25. },
  26. }
  27. // Stack is an object for basic stack operations. Items popped to the stack are
  28. // expected to be changed and modified. stack does not take care of adding newly
  29. // initialised objects.
  30. type Stack struct {
  31. data []uint256.Int
  32. }
  33. func newstack() *Stack {
  34. return stackPool.Get().(*Stack)
  35. }
  36. func returnStack(s *Stack) {
  37. s.data = s.data[:0]
  38. stackPool.Put(s)
  39. }
  40. // Data returns the underlying uint256.Int array.
  41. func (st *Stack) Data() []uint256.Int {
  42. return st.data
  43. }
  44. func (st *Stack) push(d *uint256.Int) {
  45. // NOTE push limit (1024) is checked in baseCheck
  46. st.data = append(st.data, *d)
  47. }
  48. func (st *Stack) pushN(ds ...uint256.Int) {
  49. // FIXME: Is there a way to pass args by pointers.
  50. st.data = append(st.data, ds...)
  51. }
  52. func (st *Stack) pop() (ret uint256.Int) {
  53. ret = st.data[len(st.data)-1]
  54. st.data = st.data[:len(st.data)-1]
  55. return
  56. }
  57. func (st *Stack) len() int {
  58. return len(st.data)
  59. }
  60. func (st *Stack) swap(n int) {
  61. st.data[st.len()-n], st.data[st.len()-1] = st.data[st.len()-1], st.data[st.len()-n]
  62. }
  63. func (st *Stack) dup(n int) {
  64. st.push(&st.data[st.len()-n])
  65. }
  66. func (st *Stack) peek() *uint256.Int {
  67. return &st.data[st.len()-1]
  68. }
  69. // Back returns the n'th item in stack
  70. func (st *Stack) Back(n int) *uint256.Int {
  71. return &st.data[st.len()-n-1]
  72. }
  73. // Print dumps the content of the stack
  74. func (st *Stack) Print() {
  75. fmt.Println("### stack ###")
  76. if len(st.data) > 0 {
  77. for i, val := range st.data {
  78. fmt.Printf("%-3d %v\n", i, val)
  79. }
  80. } else {
  81. fmt.Println("-- empty --")
  82. }
  83. fmt.Println("#############")
  84. }