-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathEmitBlock.java
More file actions
338 lines (297 loc) · 14.2 KB
/
EmitBlock.java
File metadata and controls
338 lines (297 loc) · 14.2 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
package org.perlonjava.codegen;
import org.objectweb.asm.Label;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;
import org.perlonjava.astnode.*;
import org.perlonjava.astrefactor.LargeBlockRefactorer;import org.perlonjava.astvisitor.EmitterVisitor;
import org.perlonjava.runtime.RuntimeContextType;
import java.util.List;
import java.util.Set;
import java.util.LinkedHashSet;
import java.util.ArrayList;
public class EmitBlock {
private static void collectStateDeclSigilNodes(Node node, Set<OperatorNode> out) {
if (node == null) {
return;
}
if (node instanceof OperatorNode op) {
if ("state".equals(op.operator) && op.operand instanceof OperatorNode sigilNode) {
if (sigilNode.operand instanceof IdentifierNode && "$@%".contains(sigilNode.operator)) {
out.add(sigilNode);
}
}
collectStateDeclSigilNodes(op.operand, out);
return;
}
if (node instanceof BinaryOperatorNode bin) {
collectStateDeclSigilNodes(bin.left, out);
collectStateDeclSigilNodes(bin.right, out);
return;
}
if (node instanceof ListNode list) {
for (Node child : list.elements) {
collectStateDeclSigilNodes(child, out);
}
return;
}
if (node instanceof BlockNode block) {
for (Node child : block.elements) {
collectStateDeclSigilNodes(child, out);
}
}
if (node instanceof For1Node for1) {
collectStateDeclSigilNodes(for1.variable, out);
collectStateDeclSigilNodes(for1.list, out);
collectStateDeclSigilNodes(for1.body, out);
collectStateDeclSigilNodes(for1.continueBlock, out);
return;
}
if (node instanceof For3Node for3) {
collectStateDeclSigilNodes(for3.initialization, out);
collectStateDeclSigilNodes(for3.condition, out);
collectStateDeclSigilNodes(for3.increment, out);
collectStateDeclSigilNodes(for3.body, out);
collectStateDeclSigilNodes(for3.continueBlock, out);
return;
}
if (node instanceof IfNode ifNode) {
collectStateDeclSigilNodes(ifNode.condition, out);
collectStateDeclSigilNodes(ifNode.thenBranch, out);
collectStateDeclSigilNodes(ifNode.elseBranch, out);
return;
}
if (node instanceof TryNode tryNode) {
collectStateDeclSigilNodes(tryNode.tryBlock, out);
collectStateDeclSigilNodes(tryNode.catchBlock, out);
collectStateDeclSigilNodes(tryNode.finallyBlock, out);
}
}
private static void collectStatementLabelNames(List<Node> elements, List<String> out) {
for (Node element : elements) {
if (element instanceof LabelNode labelNode) {
out.add(labelNode.label);
}
}
}
/**
* Emits bytecode for a block of statements.
*
* @param emitterVisitor The visitor used for code emission.
* @param node The block node representing the block of statements.
*/
public static void emitBlock(EmitterVisitor emitterVisitor, BlockNode node) {
MethodVisitor mv = emitterVisitor.ctx.mv;
// Try to refactor large blocks using the helper class
if (LargeBlockRefactorer.processBlock(emitterVisitor, node)) {
// Block was refactored and emitted by the helper
return;
}
emitterVisitor.ctx.logDebug("generateCodeBlock start context:" + emitterVisitor.ctx.contextType);
int scopeIndex = emitterVisitor.ctx.symbolTable.enterScope();
EmitterVisitor voidVisitor =
emitterVisitor.with(RuntimeContextType.VOID); // statements in the middle of the block have context VOID
List<Node> list = node.elements;
// Hoist `state` declarations to the beginning of the block scope so that JVM local slots
// are initialized even if a `goto` skips the original declaration statement.
// This prevents NPEs when later code evaluates e.g. `defined $state_var`.
Set<OperatorNode> stateDeclSigilNodes = new LinkedHashSet<>();
for (Node element : list) {
collectStateDeclSigilNodes(element, stateDeclSigilNodes);
}
for (OperatorNode sigilNode : stateDeclSigilNodes) {
new OperatorNode("state", sigilNode, sigilNode.tokenIndex)
.accept(voidVisitor);
}
int lastNonNullIndex = -1;
for (int i = list.size() - 1; i >= 0; i--) {
if (list.get(i) != null) {
lastNonNullIndex = i;
break;
}
}
// Create labels for the block as a loop, like `L1: {...}`
Label redoLabel = new Label();
Label nextLabel = new Label();
// Pre-register statement labels (e.g. `NEXT:`) in this block so `goto NEXT` can resolve
// even when the goto appears before the label (forward goto).
//
// We intentionally only register labels at this block's top level. Nested blocks get
// their own EmitBlock invocation and maintain proper scoping/shadowing via the stack.
List<String> statementLabelNames = new ArrayList<>();
collectStatementLabelNames(list, statementLabelNames);
for (String labelName : statementLabelNames) {
emitterVisitor.ctx.javaClassInfo.pushGotoLabels(labelName, new Label());
}
// Create labels used inside the block, like `{ L1: ... }`
for (int i = 0; i < node.labels.size(); i++) {
emitterVisitor.ctx.javaClassInfo.pushGotoLabels(node.labels.get(i), new Label());
}
// Setup 'local' environment if needed
Local.localRecord localRecord = Local.localSetup(emitterVisitor.ctx, node, mv);
// Add redo label
mv.visitLabel(redoLabel);
// Restore 'local' environment if 'redo' was called
Local.localTeardown(localRecord, mv);
if (node.isLoop) {
// A labeled/bare block used as a loop target (e.g. SKIP: { ... }) is a
// pseudo-loop: it supports labeled next/last/redo (e.g. next SKIP), but
// an unlabeled next/last/redo must target the nearest enclosing true loop.
//
// However, a *bare* block with loop control (e.g. `{ ...; redo }` or
// `{ ... } continue { ... }`) is itself a valid target for *unlabeled*
// last/next/redo, matching Perl semantics.
boolean isBareBlock = node.labelName == null;
emitterVisitor.ctx.javaClassInfo.pushLoopLabels(
node.labelName,
nextLabel,
redoLabel,
nextLabel,
emitterVisitor.ctx.contextType,
isBareBlock,
isBareBlock);
}
// Special case: detect pattern of `local $_` followed by `For1Node` with needsArrayOfAlias
// In this case, we need to evaluate the For1Node's list before emitting the local operator
For1Node preEvalForNode = null;
int savedPreEvaluatedArrayIndex = -1;
if (list.size() >= 2 &&
list.get(0) instanceof OperatorNode localOp && localOp.operator.equals("local") &&
list.get(1) instanceof For1Node forNode && forNode.needsArrayOfAlias) {
// Pre-evaluate the For1Node's list to array of aliases before localizing $_
int tempArrayIndex = emitterVisitor.ctx.symbolTable.allocateLocalVariable();
forNode.list.accept(emitterVisitor.with(RuntimeContextType.LIST));
mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "org/perlonjava/runtime/RuntimeBase", "getArrayOfAlias", "()Lorg/perlonjava/runtime/RuntimeArray;", false);
mv.visitVarInsn(Opcodes.ASTORE, tempArrayIndex);
// Mark the For1Node to use the pre-evaluated array
preEvalForNode = forNode;
savedPreEvaluatedArrayIndex = forNode.preEvaluatedArrayIndex;
forNode.preEvaluatedArrayIndex = tempArrayIndex;
}
try {
for (int i = 0; i < list.size(); i++) {
Node element = list.get(i);
// Skip null elements - these occur when parseStatement returns null to signal
// "not a statement, continue parsing" (e.g., AUTOLOAD without {}, try without feature enabled)
// ParseBlock.parseBlock() adds these null results to the statements list
if (element == null) {
emitterVisitor.ctx.logDebug("Skipping null element in block at index " + i);
continue;
}
ByteCodeSourceMapper.setDebugInfoLineNumber(emitterVisitor.ctx, element.getIndex());
// Emit the statement with current context
if (i == lastNonNullIndex) {
// Special case for the last element
emitterVisitor.ctx.logDebug("Last element: " + element);
element.accept(emitterVisitor);
} else {
// General case for all other elements
emitterVisitor.ctx.logDebug("Element: " + element);
element.accept(voidVisitor);
}
// NOTE: Registry checks are DISABLED in EmitBlock because:
// 1. They cause ASM frame computation errors in nested/refactored code
// 2. Bare labeled blocks (like TODO:) don't need non-local control flow
// 3. Real loops (for/while/foreach) have their own registry checks in
// EmitForeach.java and EmitStatement.java that work correctly
//
// This means non-local control flow (next LABEL from closures) works for
// actual loop constructs but NOT for bare labeled blocks, which is correct
// Perl behavior anyway.
}
} finally {
if (preEvalForNode != null) {
preEvalForNode.preEvaluatedArrayIndex = savedPreEvaluatedArrayIndex;
}
}
if (node.isLoop) {
emitterVisitor.ctx.javaClassInfo.popLoopLabels();
}
// Pop labels used inside the block
for (int i = 0; i < node.labels.size(); i++) {
emitterVisitor.ctx.javaClassInfo.popGotoLabels();
}
// Pop statement labels registered for this block
for (int i = 0; i < statementLabelNames.size(); i++) {
emitterVisitor.ctx.javaClassInfo.popGotoLabels();
}
// Add 'next', 'last' label
mv.visitLabel(nextLabel);
Local.localTeardown(localRecord, mv);
emitterVisitor.ctx.symbolTable.exitScope(scopeIndex);
emitterVisitor.ctx.logDebug("generateCodeBlock end");
}
/**
* Emit bytecode to check RuntimeControlFlowRegistry and handle any registered control flow.
* This is called after loop body execution to catch non-local control flow markers.
*
* @param mv The MethodVisitor
* @param loopLabels The current loop's labels
* @param redoLabel The redo target
* @param nextLabel The next/continue target
* @param lastLabel The last/exit target
*/
private static void emitRegistryCheck(MethodVisitor mv, LoopLabels loopLabels,
Label redoLabel, Label nextLabel, Label lastLabel) {
// Check RuntimeControlFlowRegistry for non-local control flow
// Use simple IF comparisons instead of TABLESWITCH to avoid ASM frame computation issues
String labelName = loopLabels.labelName;
if (labelName != null) {
mv.visitLdcInsn(labelName);
} else {
mv.visitInsn(Opcodes.ACONST_NULL);
}
// Call: int action = RuntimeControlFlowRegistry.checkLoopAndGetAction(String labelName)
// Returns: 0=none, 1=last, 2=next, 3=redo
mv.visitMethodInsn(Opcodes.INVOKESTATIC,
"org/perlonjava/runtime/RuntimeControlFlowRegistry",
"checkLoopAndGetAction",
"(Ljava/lang/String;)I",
false);
// Store action in a local variable to avoid stack issues
// Then use simple IF comparisons instead of TABLESWITCH
Label continueLabel = new Label();
Label checkNext = new Label();
Label checkRedo = new Label();
// Check if action == 1 (LAST)
mv.visitInsn(Opcodes.DUP);
mv.visitInsn(Opcodes.ICONST_1);
mv.visitJumpInsn(Opcodes.IF_ICMPNE, checkNext);
mv.visitInsn(Opcodes.POP); // Pop the duplicate
mv.visitJumpInsn(Opcodes.GOTO, lastLabel);
// Check if action == 2 (NEXT)
mv.visitLabel(checkNext);
mv.visitInsn(Opcodes.DUP);
mv.visitInsn(Opcodes.ICONST_2);
mv.visitJumpInsn(Opcodes.IF_ICMPNE, checkRedo);
mv.visitInsn(Opcodes.POP); // Pop the duplicate
mv.visitJumpInsn(Opcodes.GOTO, nextLabel);
// Check if action == 3 (REDO)
mv.visitLabel(checkRedo);
mv.visitInsn(Opcodes.DUP);
mv.visitInsn(Opcodes.ICONST_3);
mv.visitJumpInsn(Opcodes.IF_ICMPNE, continueLabel);
mv.visitInsn(Opcodes.POP); // Pop the duplicate
mv.visitJumpInsn(Opcodes.GOTO, redoLabel);
// Default: action == 0 (none) or other - continue normally
mv.visitLabel(continueLabel);
mv.visitInsn(Opcodes.POP); // Pop the action value
}
private static BinaryOperatorNode refactorBlockToSub(BlockNode node) {
// Create sub {...}->(@_)
int index = node.tokenIndex;
ListNode args = new ListNode(index);
args.elements.add(new OperatorNode("@", new IdentifierNode("_", index), index));
BinaryOperatorNode subr = new BinaryOperatorNode(
"->",
new SubroutineNode(
null, null, null,
new BlockNode(List.of(node), index),
false,
index
),
args,
index
);
return subr;
}
}