Debian Codegraph @

src/main/java/org/jboss/classfilewriter/code/StackState.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 java.util.ArrayList;
21 import java.util.Collections;
22 import java.util.List;
23
24 import org.jboss.classfilewriter.InvalidBytecodeException;
25 import org.jboss.classfilewriter.constpool.ConstPool;
26 import org.jboss.classfilewriter.util.DescriptorUtils;
27
28
29 /**
30 * In immutable stack state, which may be shared between frames
31 *
32 * @author Stuart Douglas
33 *
34 */
35 public class StackState {
36
37 /**
38 * The contents of the stack
39 * <p>
40 * This list may be shared between frames, so it must never be modified
41 * <p>
42 * The very first element represents the bottom of the stack, with the last element representing the top. Wide elements are
43 * stored as Wide, Top, with the {@link StackEntryType#TOP} on the top of the stack
44 */
45 private final List<StackEntry> contents;
46
47 private final ConstPool constPool;
48
49 public StackState(ConstPool constPool) {
50 this.contents = new ArrayList<StackEntry>(0);
51 this.constPool = constPool;
52 }
53
54 public StackState(String exceptionType, ConstPool constPool) {
55 this.contents = new ArrayList<StackEntry>(1);
56 this.contents.add(new StackEntry(StackEntryType.OBJECT, DescriptorUtils.makeDescriptor(exceptionType), constPool));
57 this.constPool = constPool;
58 }
59
60 private StackState(final List<StackEntry> contents, ConstPool constPool) {
61 this.contents = contents;
62 this.constPool = constPool;
63 }
64
65 /**
66 * checks that the appropriate object type is on top of the stack
67 */
68 public boolean isOnTop(String descriptor) {
69 if (contents.isEmpty()) {
70 return false;
71 }
72 StackEntry entry = StackEntry.of(descriptor, constPool);
73 StackEntry top = top();
74 if (entry.isWide()) {
75 if (contents.size() == 1) {
76 return false;
77 }
78 return top_1().getType() == entry.getType();
79 }
80 if (top.getType() == StackEntryType.NULL && entry.getType() == StackEntryType.OBJECT) {
81 return true;
82 }
83 return top.getType() == entry.getType();
84 }
85
86 public int size() {
87 return contents.size();
88 }
89
90 /**
91 * push a type on to the top of the stack
92 */
93 public StackState push(String type) {
94 StackEntry entry = StackEntry.of(type, constPool);
95 if (entry.getType() == StackEntryType.DOUBLE || entry.getType() == StackEntryType.LONG) {
96 return newStack(entry, new StackEntry(StackEntryType.TOP, type));
97 }
98 return newStack(entry);
99 }
100
101 /**
102 * push a type on to the top of the stack
103 */
104 public StackState push(StackEntry entry) {
105 if (entry.getType() == StackEntryType.DOUBLE || entry.getType() == StackEntryType.LONG) {
106 return newStack(entry, new StackEntry(StackEntryType.TOP, entry.getDescriptor()));
107 }
108 return newStack(entry);
109 }
110
111 public StackState aconstNull() {
112 StackEntry entry = new StackEntry(StackEntryType.NULL, null);
113 return newStack(entry);
114 }
115
116 /**
117 * pop a non-wide type from the top of the stack
118 */
119 public StackState pop(int no) {
120 if (no == 0) {
121 return this;
122 }
123 if (contents.size() < no) {
124 throw new InvalidBytecodeException("cannot pop" + no + ", only " + contents.size() + " on stack " + toString());
125 }
126 StackEntry type = contents.get(contents.size() - no);
127 if (type.getType() == StackEntryType.TOP) {
128 throw new InvalidBytecodeException("Pop" + no + " would split wide type " + toString());
129 }
130 return new StackState(contents.subList(0, contents.size() - no), constPool);
131 }
132
133 public StackState dup() {
134 if (contents.isEmpty()) {
135 throw new InvalidBytecodeException("cannot dup empty stack");
136 }
137 StackEntry type = top();
138 if (type.getType() == StackEntryType.TOP) {
139 throw new InvalidBytecodeException("Cannot dup wide type");
140 }
141 return newStack(type);
142 }
143
144 public StackState dupX1() {
145 if (contents.size() < 2) {
146 throw new InvalidBytecodeException("cannot dup_x1, stack does not have enough items");
147 }
148 StackEntry type = top();
149 if (type.getType() == StackEntryType.TOP) {
150 throw new InvalidBytecodeException("Cannot dup_x1 wide type");
151 }
152 ArrayList<StackEntry> ret = new ArrayList<StackEntry>(1 + contents.size());
153 for (int i = 0; i < contents.size(); ++i) {
154 if (i == contents.size() - 2) {
155 ret.add(top());
156 }
157 ret.add(contents.get(i));
158 }
159 return new StackState(ret, constPool);
160 }
161
162 public StackState dupX2() {
163 if (contents.size() < 3) {
164 throw new InvalidBytecodeException("cannot dup_x1, stack does not have enough items");
165 }
166 StackEntry type = top();
167 if (type.getType() == StackEntryType.TOP) {
168 throw new InvalidBytecodeException("Cannot dup_x1 wide type");
169 }
170 ArrayList<StackEntry> ret = new ArrayList<StackEntry>(1 + contents.size());
171 for (int i = 0; i < contents.size(); ++i) {
172 if (i == contents.size() - 3) {
173 ret.add(top());
174 }
175 ret.add(contents.get(i));
176 }
177 return new StackState(ret, constPool);
178 }
179
180 public StackState dup2() {
181 if (contents.size() <2) {
182 throw new InvalidBytecodeException("cannot dup2, stack size is " + contents.size() + " " + toString());
183 }
184 StackEntry t1 = top();
185 StackEntry t2 = top_1();
186 if (t2.getType() == StackEntryType.TOP) {
187 throw new InvalidBytecodeException("Cannot dup2 when second type on stack is wide: " + toString());
188 }
189 return newStack(t2, t1);
190 }
191
192 public StackState dup2X1() {
193 if (contents.size() < 3) {
194 throw new InvalidBytecodeException("cannot dup2X1, stack size is " + contents.size() + " " + toString());
195 }
196 StackEntry t1 = top();
197 StackEntry t2 = top_1();
198 StackEntry t3 = top_2();
199 if (t2.getType() == StackEntryType.TOP) {
200 throw new InvalidBytecodeException("Cannot dup2X1 when second type on stack is wide: " + toString());
201 }
202 if (t3.getType() == StackEntryType.TOP) {
203 throw new InvalidBytecodeException("Cannot dup2X2 when third type on stack is wide: " + toString());
204 }
205 ArrayList<StackEntry> ret = new ArrayList<StackEntry>(2 + contents.size());
206 for (int i = 0; i < contents.size(); ++i) {
207 if (i == contents.size() - 3) {
208 ret.add(t2);
209 ret.add(t1);
210 }
211 ret.add(contents.get(i));
212 }
213 return new StackState(ret, constPool);
214 }
215
216 public StackState dup2X2() {
217 if (contents.size() < 4) {
218 throw new InvalidBytecodeException("cannot dup2X2, stack size is " + contents.size() + " " + toString());
219 }
220 StackEntry t1 = top();
221 StackEntry t2 = top_1();
222 StackEntry t4 = top_3();
223 if (t2.getType() == StackEntryType.TOP) {
224 throw new InvalidBytecodeException("Cannot dup2X2 when second type on stack is wide: " + toString());
225 }
226 if (t4.getType() == StackEntryType.TOP) {
227 throw new InvalidBytecodeException("Cannot dup2X2 when fourth type on stack is wide: " + toString());
228 }
229 ArrayList<StackEntry> ret = new ArrayList<StackEntry>(2 + contents.size());
230 for (int i = 0; i < contents.size(); ++i) {
231 if (i == contents.size() - 4) {
232 ret.add(t2);
233 ret.add(t1);
234 }
235 ret.add(contents.get(i));
236 }
237 return new StackState(ret, constPool);
238 }
239
240 private StackState newStack(StackEntry... pushValues) {
241 ArrayList<StackEntry> ret = new ArrayList<StackEntry>(pushValues.length + contents.size());
242 ret.addAll(contents);
243 for (StackEntry s : pushValues) {
244 ret.add(s);
245 }
246 return new StackState(ret, constPool);
247 }
248
249 public StackEntry top() {
250 return contents.get(contents.size() - 1);
251 }
252
253 public StackEntry top_1() {
254 return contents.get(contents.size() - 2);
255 }
256
257 public StackEntry top_2() {
258 return contents.get(contents.size() - 3);
259 }
260
261 public StackEntry top_3() {
262 return contents.get(contents.size() - 4);
263 }
264 @Override
265 public String toString() {
266 return "Stack: " + contents.toString();
267 }
268
269 public List<StackEntry> getContents() {
270 return Collections.unmodifiableList(contents);
271 }
272
273 public StackState constructorCall(int initializedValueStackPosition, StackEntry entry) {
274 List<StackEntry> newContents = new ArrayList<StackEntry>(contents.size());
275 if (entry.getType() == StackEntryType.UNINITIALIZED_THIS) {
276 for (int i = 0; i < contents.size() - 1 - initializedValueStackPosition; ++i) {
277 StackEntry stackEntry = contents.get(i);
278 if (stackEntry.getType() == StackEntryType.UNINITIALIZED_THIS) {
279 newContents.add(StackEntry.of(stackEntry.getDescriptor(), constPool));
280 } else {
281 newContents.add(stackEntry);
282 }
283 }
284 return new StackState(newContents, constPool);
285 } else if (entry.getType() == StackEntryType.UNITITIALIZED_OBJECT) {
286 for (int i = 0; i < contents.size() - 1 - initializedValueStackPosition; ++i) {
287 StackEntry stackEntry = contents.get(i);
288 if (stackEntry.getType() == StackEntryType.UNITITIALIZED_OBJECT
289 && stackEntry.getNewInstructionLocation() == entry.getNewInstructionLocation()) {
290 newContents.add(StackEntry.of(stackEntry.getDescriptor(), constPool));
291 } else {
292 newContents.add(stackEntry);
293 }
294 }
295 return new StackState(newContents, constPool);
296 } else {
297 throw new InvalidBytecodeException("Object at position " + initializedValueStackPosition
298 + " is not an unitialized object. " + toString());
299 }
300 }
301
302 public StackState updateMerged(int pos, StackEntry frame) {
303 List<StackEntry> newContents = new ArrayList<StackEntry>(contents);
304 newContents.remove(pos);
305 newContents.add(pos, frame);
306 return new StackState(newContents, constPool);
307 }
308
309 public StackState swap() {
310 int size = contents.size();
311 List<StackEntry> newContents = new ArrayList<StackEntry>(contents.subList(0, size - 2));
312 newContents.add(contents.get(size - 1));
313 newContents.add(contents.get(size - 2));
314 return new StackState(newContents, constPool);
315 }
316
317 }