-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathJavaClassInfo.java
More file actions
382 lines (334 loc) · 12.9 KB
/
JavaClassInfo.java
File metadata and controls
382 lines (334 loc) · 12.9 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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
package org.perlonjava.backend.jvm;
import org.objectweb.asm.Label;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;
import org.perlonjava.frontend.semantic.ScopedSymbolTable;
import org.perlonjava.runtime.runtimetypes.RuntimeBase;
import java.util.*;
/**
* 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}.
* <p>
* 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;
/**
* Local variable slot that stores the saved dynamic variable level from localSetup().
* Used by goto &sub to call popToLocalLevel() before tail calls.
*/
public int dynamicLevelSlot;
/**
* Flag indicating if this subroutine uses 'local' variables.
* Used to optimize return statements - if true, return values must be cloned
* to prevent aliasing issues with local variable teardown.
*/
public boolean usesLocal;
/**
* Flag indicating if this subroutine is a defer block.
* Control flow statements (last, next, redo, return, goto) are prohibited in defer blocks.
*/
public boolean isInDeferBlock;
/**
* Flag indicating if this method is an eval block (eval { ... }).
* goto &sub from eval blocks is prohibited ("Can't goto subroutine from an eval-block").
*/
public boolean isInEvalBlock;
/**
* Flag indicating if this method is compiled for an eval string (eval 'string').
* goto &sub from eval strings is prohibited ("Can't goto subroutine from an eval-string").
*/
public boolean isInEvalString;
/**
* Flag indicating if this subroutine is a map/grep/all/any block.
* When true, 'return' creates a non-local RETURN marker instead of a normal return,
* so the return value propagates to the enclosing subroutine.
*/
public boolean isMapGrepBlock;
/**
* Counter tracking nesting depth inside finally blocks.
* Control flow statements (last, next, redo, return, goto) are prohibited in finally blocks.
* This is a counter rather than a boolean to handle nested finally blocks.
*/
public int finallyBlockDepth;
public int[] spillSlots;
public int spillTop;
/**
* True iff this subroutine's scope exits need the full cleanup
* emission (scopeExitCleanup on scalars/hashes/arrays,
* MyVarCleanupStack.unregister, MortalList flush).
* <p>
* Default true (safe). Flipped to false by
* {@link org.perlonjava.frontend.analysis.CleanupNeededVisitor} when
* the sub body is statically proven to have no bless / weaken /
* local / nested-sub / defer activity — in which case scope-exit
* emissions can be dropped to just the null-store sequence,
* matching the fast path master uses for simple numeric loops.
* <p>
* {@link EmitStatement#emitScopeExitNullStores} honours this flag.
* The env var {@code JPERL_FORCE_CLEANUP=1} forces this to true
* globally for debugging suspected correctness regressions.
*/
public boolean cleanupNeeded = true;
/**
* JVM local variable indices of my-variables (scalar, hash, array) allocated
* inside the eval body. Used by the eval catch handler to emit scope-exit
* cleanup when die unwinds through eval. Populated during compilation by
* {@link EmitStatement#emitScopeExitNullStores} when recording is active.
*/
public List<Integer> evalCleanupLocals;
/**
* A stack of loop labels for managing nested loops.
*/
public Deque<LoopLabels> loopLabelStack;
public Deque<GotoLabels> gotoLabelStack;
/**
* Map of loop state signature to block-level dispatcher label.
* Allows multiple call sites with the same visible loops to share one dispatcher.
*/
public Map<String, Label> blockDispatcherLabels;
/**
* Constants referenced via backslash (e.g., \"yay") inside this subroutine.
* When the CODE slot of a glob is replaced, weak references to these constants
* are cleared to emulate Perl 5's "optree reaping" behavior.
*/
public List<RuntimeBase> padConstants;
/**
* 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.dynamicLevelSlot = -1;
this.loopLabelStack = new ArrayDeque<>();
this.gotoLabelStack = new ArrayDeque<>();
this.blockDispatcherLabels = new HashMap<>();
this.spillSlots = new int[0];
this.spillTop = 0;
}
/**
* Records a cached constant that was referenced via backslash in this subroutine.
*/
public void addPadConstant(RuntimeBase constant) {
if (padConstants == null) {
padConstants = new ArrayList<>();
}
padConstants.add(constant);
}
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();
}
/**
* Computes a unique signature for the current loop state.
* This signature identifies which loops are visible at the current point.
* Call sites with the same signature can share a block-level dispatcher.
*
* @return a string signature representing the current loop state
*/
public String getLoopStateSignature() {
if (loopLabelStack.isEmpty()) {
return "NO_LOOPS";
}
StringBuilder sb = new StringBuilder();
boolean first = true;
// Iterate from innermost to outermost (stack order)
for (LoopLabels loop : loopLabelStack) {
if (!first) {
sb.append("|");
}
first = false;
sb.append(loop.labelName != null ? loop.labelName : "UNLABELED");
sb.append("@").append(System.identityHashCode(loop));
}
return sb.toString();
}
/**
* 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" +
"}";
}
public record SpillRef(int slot, boolean pooled) {
}
}