forked from sgodbillon/BytecodeParser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack.java
More file actions
231 lines (209 loc) · 6.41 KB
/
Copy pathStack.java
File metadata and controls
231 lines (209 loc) · 6.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/*
* Copyright (C) 2011 Stephane Godbillon
*
* This file is part of BytecodeParser. See the README file in the root
* directory of this project.
*
* BytecodeParser is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* BytecodeParser is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public License
* along with BytecodeParser. If not, see <http://www.gnu.org/licenses/>.
*/
package bytecodeparser.analysis.stack;
import static bytecodeparser.analysis.stack.Stack.StackElementLength.DOUBLE;
import java.util.LinkedList;
import org.apache.log4j.Logger;
/**
* Represents the current stack's state.
*
* @author Stephane Godbillon
*
*/
public class Stack {
private static final Logger LOGGER = Logger.getLogger(Stack.class);
/**
* Length of a stack element. Can be one or two words.
* @author Stephane Godbillon
*
*/
public static enum StackElementLength {
/**
* One word length
*/
ONE, // one word
/**
* Two words length
*/
DOUBLE; // two words
/**
* Computes the total length of the stackElementLengths.
* @param stackElementLengths
* @return The total length of the stackElementLengths.
*/
public static int add(StackElementLength... stackElementLengths) {
int result = 0;
for(StackElementLength sel : stackElementLengths) {
result += sel == ONE ? 1 : 2;
}
return result;
}
}
/**
* Linked List holding the references of this stack's elements.
* It is preferable to use the methods of this class instead of accessing this field.
*/
public LinkedList<StackElement> stack = new LinkedList<StackElement>();
/**
* States if this stack is empty.
* @return true if this stack is empty, false if not.
*/
public boolean isEmpty() {
return stack.size() == 0;
}
/**
* Removes the top stackElement from this stack.
* The stackElement must be one-word length.
* @throws java.util.NoSuchElementException if this stack is empty.
* @throws RuntimeException if the stackElement is a part of a two-words element.
* @return the removed stackElement.
*/
public StackElement pop() {
StackElement se = stack.pop();
if(se instanceof TOP)
throw new RuntimeException("WARN: popped a TOP!");
return se;
}
/**
* Removes the top stackElement from this stack.
* The stackElement must be two-words length.
* @throws java.util.NoSuchElementException if this stack is empty.
* @throws RuntimeException if the stackElement is not a two-words element.
* @return the removed stackElement.
*/
public StackElement pop2() {
StackElement se = stack.pop();
if( !(se instanceof TOP) )
throw new RuntimeException("WARN: popped2 top is not a TOP! (is instanceof " + se.getClass() + ")");
se = stack.pop();
if(se instanceof TOP)
throw new RuntimeException("WARN: popped2 a TOP!");
return se;
}
/**
* Removes the top stackElement of the given length from this stack.
* @throws java.util.NoSuchElementException if this stack is empty.
* @throws RuntimeException if the top stackElement is not of the given length.
* @return the removed stackElement.
*/
public StackElement pop(StackElementLength length) {
if(length == StackElementLength.DOUBLE)
return pop2();
return pop();
}
/**
* Returns the top stackElement from this stack.
* The stackElement might be a a TOP (part of a two-words element).
* @throws java.util.NoSuchElementException if this stack is empty.
* @return the stackElement.
*/
public StackElement peek() {
StackElement se = stack.peek();
if(se instanceof TOP)
LOGGER.warn("WARN: popped a TOP!");
return se;
}
/**
* Returns the top-1 stackElement from this stack.
* The stackElement might be a a TOP (part of a two-words element).
* @throws java.util.NoSuchElementException if this stack is empty.
* @return the stackElement.
*/
public StackElement peek2() {
StackElement se = stack.get(stack.size() - 2);
if(se instanceof TOP)
LOGGER.warn("WARN: peek2 a TOP!");
return se;
}
/**
* Returns the top or top-1 stackElement from this stack, depending on the given length.
* The stackElement might be a a TOP (part of a two-words element).
* @throws java.util.NoSuchElementException if this stack is empty.
* @return the stackElement.
*/
public StackElement peek(StackElementLength length) {
if(length == StackElementLength.DOUBLE)
return peek2();
return peek();
}
/**
* Pushes the given one-word stackElement on the stack.
* @param se The element to push on.
* @return the current Stack instance for chaining.
*/
public Stack push(StackElement se) {
stack.push(se);
return this;
}
/**
* Pushes the given two-words stackElement on the stack.
* @param se The element to push on.
* @return the current Stack instance for chaining.
*/
public Stack push2(StackElement se) {
stack.push(se);
stack.push(new TOP());
return this;
}
/**
* Get the n-ith element from the stack.
* @param i
* @return the stackElement. Can be a TOP (part of a two-words element).
*/
public StackElement getFromTop(int i) {
return stack.get(i);
}
/**
* Makes a copy of this Stack instance.
* @return a new Stack instance containing the same elements.
*/
public Stack copy() {
Stack copy = new Stack();
copy.stack = new LinkedList<StackElement>(this.stack);
return copy;
}
@Override
public String toString() {
StringBuffer sb = new StringBuffer("stack: [");
for(int i = 0; i < stack.size(); i++) {
if(i > 0)
sb.append(", ");
sb.append(stack.get(i));
}
return sb.append("]").toString();
}
/**
* Pops some elements from the given stack, then pushes some Whatever elements onto it.
* @param stack
* @param pops
* @param pushes
*/
// TODO
public static void processBasicAlteration(Stack stack, StackElementLength[] pops, StackElementLength[] pushes) {
for(int i = 0; i < pops.length; i++) {
if(pops[i] == DOUBLE)
stack.pop2();
else stack.pop();
}
for(int i = 0; i < pushes.length; i++) {
if(pushes[i] == DOUBLE)
stack.push2(new Whatever());
else stack.push(new Whatever());
}
}
}