-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathJavaClassInfo.java
More file actions
268 lines (232 loc) · 8.41 KB
/
JavaClassInfo.java
File metadata and controls
268 lines (232 loc) · 8.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
package org.perlonjava.codegen;
import org.objectweb.asm.Label;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;
import org.perlonjava.symbols.ScopedSymbolTable;
import java.util.ArrayDeque;
import java.util.Deque;
/**
* Represents information about a Java class being generated.
* This includes the class name, return label, stack level management,
* and a stack of loop labels for managing nested loops.
*/
public class JavaClassInfo {
/**
* The name of the Java class.
*/
public String javaClassName;
/**
* The label to return to after method execution.
*/
public Label returnLabel;
/**
* Local variable slot used to spill the value returned by `return`, `goto` markers,
* and other non-local control-flow paths before jumping to {@link #returnLabel}.
*
* The {@link #returnLabel} join point must be stack-neutral: no incoming edge may
* rely on operand stack contents.
*/
public int returnValueSlot;
/**
* Local variable slot for tail call trampoline - stores codeRef.
*/
public int tailCallCodeRefSlot;
/**
* Local variable slot for tail call trampoline - stores args.
*/
public int tailCallArgsSlot;
/**
* Local variable slot for temporarily storing marked RuntimeControlFlowList during call-site checks.
*/
public int controlFlowTempSlot;
public int controlFlowActionSlot;
public int[] spillSlots;
public int spillTop;
public static final class SpillRef {
public final int slot;
public final boolean pooled;
public SpillRef(int slot, boolean pooled) {
this.slot = slot;
this.pooled = pooled;
}
}
/**
* A stack of loop labels for managing nested loops.
*/
public Deque<LoopLabels> loopLabelStack;
public Deque<GotoLabels> gotoLabelStack;
/**
* Constructs a new JavaClassInfo object.
* Initializes the class name, stack level manager, and loop label stack.
*/
public JavaClassInfo() {
this.javaClassName = EmitterMethodCreator.generateClassName();
this.returnLabel = null;
this.returnValueSlot = -1;
this.loopLabelStack = new ArrayDeque<>();
this.gotoLabelStack = new ArrayDeque<>();
this.spillSlots = new int[0];
this.spillTop = 0;
}
public int acquireSpillSlot() {
if (spillTop >= spillSlots.length) {
return -1;
}
return spillSlots[spillTop++];
}
public void releaseSpillSlot() {
if (spillTop > 0) {
spillTop--;
}
}
public SpillRef tryAcquirePooledSpillRef() {
int slot = acquireSpillSlot();
if (slot < 0) {
return null;
}
return new SpillRef(slot, true);
}
public SpillRef acquireSpillRefOrAllocate(ScopedSymbolTable symbolTable) {
int slot = acquireSpillSlot();
if (slot >= 0) {
return new SpillRef(slot, true);
}
return new SpillRef(symbolTable.allocateLocalVariable(), false);
}
public void storeSpillRef(MethodVisitor mv, SpillRef ref) {
mv.visitVarInsn(Opcodes.ASTORE, ref.slot);
}
public void loadSpillRef(MethodVisitor mv, SpillRef ref) {
mv.visitVarInsn(Opcodes.ALOAD, ref.slot);
}
public void releaseSpillRef(SpillRef ref) {
if (ref.pooled) {
releaseSpillSlot();
}
}
/**
* Pushes a new set of loop labels onto the loop label stack.
*
* @param labelName the name of the loop label
* @param nextLabel the label for the next iteration
* @param redoLabel the label for redoing the current iteration
* @param lastLabel the label for exiting the loop
*/
public void pushLoopLabels(String labelName, Label nextLabel, Label redoLabel, Label lastLabel, int context) {
loopLabelStack.push(new LoopLabels(labelName, nextLabel, redoLabel, lastLabel, context));
}
/**
* Pushes a new set of loop labels with isTrueLoop flag.
*
* @param labelName the name of the loop label
* @param nextLabel the label for the next iteration
* @param redoLabel the label for redoing the current iteration
* @param lastLabel the label for exiting the loop
* @param context the context type
* @param isTrueLoop whether this is a true loop (for/while/until) or pseudo-loop (do-while/bare)
*/
public void pushLoopLabels(String labelName, Label nextLabel, Label redoLabel, Label lastLabel, int context, boolean isTrueLoop) {
loopLabelStack.push(new LoopLabels(labelName, nextLabel, redoLabel, lastLabel, context, isTrueLoop));
}
public void pushLoopLabels(String labelName, Label nextLabel, Label redoLabel, Label lastLabel, int context, boolean isTrueLoop, boolean isUnlabeledControlFlowTarget) {
loopLabelStack.push(new LoopLabels(labelName, nextLabel, redoLabel, lastLabel, context, isTrueLoop, isUnlabeledControlFlowTarget));
}
/**
* Pushes a LoopLabels object onto the loop label stack.
* This is useful when you've already constructed a LoopLabels object with a control flow handler.
*
* @param loopLabels the LoopLabels object to push
*/
public void pushLoopLabels(LoopLabels loopLabels) {
loopLabelStack.push(loopLabels);
}
/**
* Pops the top set of loop labels from the loop label stack and returns it.
*
* @return the popped LoopLabels object
*/
public LoopLabels popLoopLabels() {
return loopLabelStack.pop();
}
/**
* Gets the innermost (current) loop labels.
* Returns null if not currently inside a loop.
*
* @return the innermost LoopLabels object, or null if none
*/
public LoopLabels getInnermostLoopLabels() {
return loopLabelStack.peek();
}
/**
* Gets the parent loop labels (the loop containing the current loop).
* Returns null if there's no parent loop.
*
* @return the parent LoopLabels object, or null if none
*/
public LoopLabels getParentLoopLabels() {
if (loopLabelStack.size() < 2) {
return null;
}
// Convert deque to array to access second-to-top element
LoopLabels[] array = loopLabelStack.toArray(new LoopLabels[0]);
return array[array.length - 2];
}
/**
* Finds loop labels by their name.
*
* @param labelName the name of the loop label to find
* @return the LoopLabels object with the specified name, or the top of the stack if the name is null
*/
public LoopLabels findLoopLabelsByName(String labelName) {
if (labelName == null) {
return loopLabelStack.peek();
}
for (LoopLabels loopLabels : loopLabelStack) {
if (loopLabels.labelName != null && loopLabels.labelName.equals(labelName)) {
return loopLabels;
}
}
return null;
}
/**
* Finds the innermost LoopLabels that is a true loop (for/while/until).
*
* @return the innermost LoopLabels with isTrueLoop=true, or null if none
*/
public LoopLabels findInnermostTrueLoopLabels() {
for (LoopLabels loopLabels : loopLabelStack) {
if (loopLabels != null && loopLabels.isTrueLoop && loopLabels.isUnlabeledControlFlowTarget) {
return loopLabels;
}
}
return null;
}
public void pushGotoLabels(String labelName, Label gotoLabel) {
gotoLabelStack.push(new GotoLabels(labelName, gotoLabel));
}
public GotoLabels findGotoLabelsByName(String labelName) {
for (GotoLabels gotoLabels : gotoLabelStack) {
if (gotoLabels.labelName.equals(labelName)) {
return gotoLabels;
}
}
return null;
}
public void popGotoLabels() {
gotoLabelStack.pop();
}
/**
* Returns a string representation of the JavaClassInfo object.
*
* @return a string representation of the JavaClassInfo object
*/
@Override
public String toString() {
return "JavaClassInfo{\n" +
" javaClassName='" + javaClassName + "',\n" +
" returnLabel=" + (returnLabel != null ? returnLabel.toString() : "null") + ",\n" +
" loopLabelStack=" + loopLabelStack + "\n" +
" gotoLabelStack=" + gotoLabelStack + "\n" +
"}";
}
}