Debian Codegraph @

src/main/java/org/jboss/classfilewriter/code/StackFrame.java

raw
1 /*
2 * JBoss, Home of Professional Open Source.
3 *
4 * Copyright 2012 Red Hat, Inc.
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18 package org.jboss.classfilewriter.code;
19
20 import org.jboss.classfilewriter.ClassMethod;
21
22 /**
23 * Represents a stack frame in the virtual machine. Holds the state of the local variable array and the stack
24 *
25 * @author Stuart Douglas
26 *
27 */
28 public class StackFrame {
29
30 /**
31 * The current state of the stack
32 */
33 private final StackState stackState;
34
35 /**
36 * The local variable state
37 */
38 private final LocalVariableState localVariableState;
39
40 private final StackFrameType type;
41
42 /**
43 * Creates the initial stack frame
44 */
45 public StackFrame(ClassMethod method) {
46 this.stackState = new StackState(method.getClassFile().getConstPool());
47 this.localVariableState = new LocalVariableState(method);
48 this.type = StackFrameType.FULL_FRAME;
49 }
50
51 public StackFrame(StackState stackState, LocalVariableState localVariableState, StackFrameType type) {
52 this.stackState = stackState;
53 this.localVariableState = localVariableState;
54 this.type = type;
55 }
56
57 public StackState getStackState() {
58 return stackState;
59 }
60
61 public LocalVariableState getLocalVariableState() {
62 return localVariableState;
63 }
64
65 /**
66 * push an operand of the given type onto the stack
67 * <p>
68 * If the entry is wide then a corresponding TOP type will be created
69 */
70 public StackFrame push(String type) {
71 StackState ns = stackState.push(type);
72 return new StackFrame(ns, localVariableState, typeNoLocalChange(ns));
73 }
74
75 /**
76 * push an operand of the given type onto the stack.
77 * <p>
78 * If the entry is wide then a corresponding TOP type will be created
79 */
80 public StackFrame push(StackEntry entry) {
81 StackState ns = stackState.push(entry);
82 return new StackFrame(ns, localVariableState, typeNoLocalChange(ns));
83 }
84
85 /**
86 * pushes a null type onto the stack
87 *
88 * @return
89 */
90 public StackFrame aconstNull() {
91 StackState ns = stackState.aconstNull();
92 return new StackFrame(ns, localVariableState, typeNoLocalChange(ns));
93
94 }
95
96 public StackFrame pop(int no) {
97 StackState ns = stackState.pop(no);
98 return new StackFrame(ns, localVariableState, typeNoLocalChange(ns));
99 }
100
101 /**
102 * pops an operand from the stack
103 */
104 public StackFrame pop() {
105 StackState ns = stackState.pop(1);
106 return new StackFrame(ns, localVariableState, typeNoLocalChange(ns));
107 }
108
109 /**
110 * pops 2 operands from the stack
111 */
112 public StackFrame pop2() {
113 StackState ns = stackState.pop(2);
114 return new StackFrame(ns, localVariableState, typeNoLocalChange(ns));
115 }
116
117 /**
118 * pops 3 operands from the stack
119 */
120 public StackFrame pop3() {
121 StackState ns = stackState.pop(3);
122 return new StackFrame(ns, localVariableState, typeNoLocalChange(ns));
123 }
124
125 /**
126 * pops 4 operands from the stack
127 */
128 public StackFrame pop4() {
129 StackState ns = stackState.pop(4);
130 return new StackFrame(ns, localVariableState, typeNoLocalChange(ns));
131 }
132
133 /**
134 * replace the operand at the top of the stack with the given operand
135 */
136 public StackFrame replace(String type) {
137 // TODO: inefficinet
138 StackState ns = stackState.pop(1).push(type);
139 return new StackFrame(ns, localVariableState, typeNoLocalChange(ns));
140 }
141
142 public StackFrame dup() {
143 StackState ns = stackState.dup();
144 return new StackFrame(ns, localVariableState, StackFrameType.FULL_FRAME);
145 }
146
147 public StackFrame dupX1() {
148 StackState ns = stackState.dupX1();
149 return new StackFrame(ns, localVariableState, StackFrameType.FULL_FRAME);
150 }
151
152 public StackFrame dupX2() {
153 StackState ns = stackState.dupX2();
154 return new StackFrame(ns, localVariableState, StackFrameType.FULL_FRAME);
155 }
156
157 public StackFrame dup2() {
158 StackState ns = stackState.dup2();
159 return new StackFrame(ns, localVariableState, StackFrameType.FULL_FRAME);
160 }
161
162 public StackFrame dup2X1() {
163 StackState ns = stackState.dup2X1();
164 return new StackFrame(ns, localVariableState, StackFrameType.FULL_FRAME);
165 }
166
167 public StackFrame dup2X2() {
168 StackState ns = stackState.dup2X2();
169 return new StackFrame(ns, localVariableState, StackFrameType.FULL_FRAME);
170 }
171
172 /**
173 * Store the variable on top of the stack into a local variable, poping the variable from the stack. Wide types are handled
174 * automatically
175 */
176 public StackFrame store(int no) {
177 StackEntry top = stackState.top();
178 StackState ns;
179 LocalVariableState ls;
180 if(top.getType() == StackEntryType.TOP) { //wide type
181 StackEntry type = stackState.top_1();
182 ns = stackState.pop(2);
183 ls = localVariableState.storeWide(no, type);
184 } else {
185 StackEntry type = stackState.top();
186 ns = stackState.pop(1);
187 ls = localVariableState.store(no, type);
188 }
189 return new StackFrame(ns, ls, StackFrameType.FULL_FRAME);
190 }
191
192 /**
193 * remote the top two operands and replace them with an different operand
194 *
195 */
196 public StackFrame pop2push1(String type) {
197 StackState ns = stackState.pop(2).push(type);
198 return new StackFrame(ns, localVariableState, typeNoLocalChange(ns));
199 }
200
201 /**
202 * remote the top two operands and replace them with an different operand
203 *
204 */
205 public StackFrame pop4push1(String type) {
206 StackState ns = stackState.pop(4).push(type);
207 return new StackFrame(ns, localVariableState, typeNoLocalChange(ns));
208 }
209
210 @Override
211 public String toString() {
212 return "StackFrame [localVariableState=" + localVariableState + ", stackState=" + stackState + "]";
213 }
214
215 /**
216 * marks the value in potition initializedValueStackPosition as initialized. This also pops this value and everything above
217 * it
218 */
219 public StackFrame constructorCall(int initializedValueStackPosition) {
220 StackEntry entry = stackState.getContents().get(stackState.getContents().size() - 1 - initializedValueStackPosition);
221 StackState ns = stackState.constructorCall(initializedValueStackPosition, entry);
222 LocalVariableState locals = localVariableState.constructorCall(entry);
223 return new StackFrame(ns, locals, StackFrameType.FULL_FRAME);
224
225 }
226
227 public StackFrame swap() {
228 StackState ns = stackState.swap();
229 return new StackFrame(ns, localVariableState, StackFrameType.FULL_FRAME);
230 }
231
232 /**
233 * determins the type of stack frame this is when no local variables have changed
234 */
235 private StackFrameType typeNoLocalChange(StackState ns) {
236 int size = ns.getContents().size();
237 if (size == 0) {
238 return StackFrameType.SAME_FRAME;
239 } else if (size == 1) {
240 return StackFrameType.SAME_LOCALS_1_STACK;
241 }
242 return StackFrameType.FULL_FRAME;
243 }
244
245 public StackFrame mergeStack(int pos, StackEntry frame) {
246 StackState stack = stackState.updateMerged(pos, frame);
247 return new StackFrame(stack, localVariableState, StackFrameType.FULL_FRAME);
248 }
249
250 public StackFrame mergeLocals(int pos, StackEntry frame) {
251 LocalVariableState locals = localVariableState.updateMerged(pos, frame);
252 return new StackFrame(stackState, locals, StackFrameType.FULL_FRAME);
253 }
254 public StackFrameType getType() {
255 return type;
256 }
257
258 public StackFrame createFull() {
259 return new StackFrame(stackState, localVariableState, StackFrameType.FULL_FRAME);
260 }
261 }