stack.go 2.7 KB

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