1
2
3
4
5
6
7
8
9
10 package nl.tudelft.simulation.dsol.interpreter;
11
12
13 /***
14 * Each frame (§3.6) contains a last-in-first-out (LIFO) stack known as its
15 * operand stack. The maximum depth of the operand stack of a frame is
16 * determined at compile time and is supplied along with the code for the method
17 * associated with the frame (§4.7.3). Where it is clear by context, we will
18 * sometimes refer to the operand stack of the current frame as simply the
19 * operand stack.
20 * <p>
21 * The operand stack is empty when the frame that contains it is created. The
22 * Java virtual machine supplies instructions to load constants or values from
23 * local variables or fields onto the operand stack. Other Java virtual machine
24 * instructions take operands from the operand stack, operate on them, and push
25 * the result back onto the operand stack. The operand stack is also used to
26 * prepare parameters to be passed to methods and to receive method results.
27 * <p>
28 * For example, the <link>IADD </link> instruction adds two int values together.
29 * It requires that the int values to be added be the top two values of the
30 * operand stack, pushed there by previous instructions. Both of the int values
31 * are popped from the operand stack. They are added, and their sum is pushed
32 * back onto the operand stack. Subcomputations may be nested on the operand
33 * stack, resulting in values that can be used by the encompassing computation.
34 * <p>
35 * (c) copyright 2003 <a href="http://www.simulation.tudelft.nl">Delft
36 * University of Technology </a>, the Netherlands. <br>
37 * See for project information <a
38 * href="http://www.simulation.tudelft.nl">www.simulation.tudelft.nl </a> <br>
39 * License of use: <a href="http://www.gnu.org/copyleft/gpl.html">General Public
40 * License (GPL) </a>, no warranty <br>
41 *
42 * @author <a href="http://www.simulation.tudelft.nl/people/jacobs.html">Peter
43 * Jacobs </a>
44 * @version 1.2 Apr 1, 2004
45 * @since 1.4
46 */
47 public class OperandStack
48 {
49 /*** the actual pointer */
50 private int pointer = 0;
51
52 /*** the stackContent */
53 private Object[] stack = null;
54
55 /***
56 * constructs a new OperandStack
57 *
58 * @param initialSize the stackSize
59 */
60 public OperandStack(final int initialSize)
61 {
62 super();
63 this.stack = new Object[initialSize];
64 }
65
66 /***
67 * clears the operand stack
68 */
69 public void clear()
70 {
71 this.pointer = 0;
72 }
73
74 /***
75 * pops the first object from the operandstack.
76 *
77 * @return the top object from the stack
78 */
79 public Object pop()
80 {
81 synchronized (this.stack)
82 {
83 return this.stack[--this.pointer];
84 }
85 }
86
87 /***
88 * peeks the first object from the stack. This is a pop without remove
89 *
90 * @return Object the first object
91 */
92 public Object peek()
93 {
94 synchronized (this.stack)
95 {
96 return this.stack[this.pointer - 1];
97 }
98 }
99
100 /***
101 * peeks the depth-object from the stack.
102 *
103 * @param depth the depth-object
104 * @return the depth object.
105 */
106 public Object peek(final int depth)
107 {
108 synchronized (this.stack)
109 {
110 return this.stack[this.pointer - (depth + 1)];
111 }
112 }
113
114 /***
115 * pushes object on the stack
116 *
117 * @param object the object to be pushed to the stack
118 */
119 public void push(final Object object)
120 {
121 synchronized (this.stack)
122 {
123 this.stack[this.pointer++] = object;
124 }
125 }
126
127 /***
128 * replaces all instances of oldObject by newObject
129 *
130 * @param oldObject the oldObject
131 * @param newObject the newObject
132 */
133 public void replace(final Object oldObject, final Object newObject)
134 {
135 synchronized (this.stack)
136 {
137 for (int i = 0; i < this.pointer; i++)
138 {
139 if (this.stack[i] != null)
140 {
141 if (this.stack[i].equals(oldObject))
142 {
143 this.stack[i] = newObject;
144 }
145 }
146 }
147 }
148 }
149
150 /***
151 * @see java.lang.Object#toString()
152 */
153 public String toString()
154 {
155 String result = "";
156 for (int i = 0; i < this.pointer; i++)
157 {
158 String value = "null";
159 if (this.stack[i] != null)
160 {
161 if (this.stack[i] instanceof StringBuffer)
162 {
163 value = StringBuffer.class.getName();
164 } else
165 {
166 value = this.stack[i].toString();
167 }
168 }
169 result = result + "(" + i + ")" + value + "|";
170 }
171 return result;
172 }
173 }