Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
b0c3aed
Add ControlFlowType enum, ControlFlowMarker class, and RuntimeControl…
fglock Nov 6, 2025
e7b78e9
Modify EmitControlFlow to return marked RuntimeList for non-local con…
fglock Nov 6, 2025
48d5cd5
Add control flow check infrastructure (disabled) - WIP Phase 3
fglock Nov 6, 2025
10f631b
Phase 2 complete: Non-local control flow via tagged returns (99.8%)
fglock Nov 6, 2025
937c0b3
Fix plan: Skip Phase 3 (call-site checks), renumber to make straightf…
fglock Nov 6, 2025
86daaee
Phase 3 partial: Add TAILCALL trampoline at returnLabel (99.8% mainta…
fglock Nov 6, 2025
c03f76f
Phase 3: Tail call trampoline working, call-site checks deferred
fglock Nov 6, 2025
28f39a0
feat: Add feature flags and Phase 3 loop handler infrastructure
fglock Nov 6, 2025
0d1cccb
feat: Implement Phase 4 (Top-Level Safety)
fglock Nov 6, 2025
4a8a369
feat: Propagate isMainProgram flag to code generation
fglock Nov 6, 2025
1f5bc8b
feat: Add source location tracking to control flow markers
fglock Nov 6, 2025
5544bbc
feat: Pass source location to RuntimeControlFlowList
fglock Nov 6, 2025
065bc87
docs: Streamline design doc to action-oriented plan
fglock Nov 6, 2025
c4db263
feat: Dynamic slot allocation for control flow temp storage
fglock Nov 6, 2025
a183087
docs: Update plan with ASM frame computation findings
fglock Nov 6, 2025
e8c5a85
docs: Add critical decision document for control flow architecture
fglock Nov 6, 2025
e3bcbad
docs: Document existing SKIP workarounds to be removed
fglock Nov 6, 2025
b9ec4d0
fix: Remove top-level safety check - restored 99.8% pass rate
fglock Nov 6, 2025
0405780
docs: Update plan - Phases 5 & 6 complete (99.9% pass rate)
fglock Nov 6, 2025
c5c895d
test: Add comprehensive control flow unit tests
fglock Nov 6, 2025
fe226a3
Fix goto __SUB__ tail call by detecting it in handleGotoLabel
fglock Nov 6, 2025
326a856
Update MILESTONES.md: goto __SUB__ is now working
fglock Nov 6, 2025
bb0e5fe
Update design document: Phase 7 (Tail Call Trampoline) COMPLETE
fglock Nov 6, 2025
8c674dd
Add comprehensive comments explaining disabled features and next steps
fglock Nov 6, 2025
d5477dd
Update MILESTONES.md: Mark Phase 7 (Non-local control flow) as COMPLETE
fglock Nov 6, 2025
84084ac
Update FEATURE_MATRIX.md: Add tail call features, simplify control fl…
fglock Nov 6, 2025
055630b
docs
fglock Nov 6, 2025
cb28263
fix: Prevent RuntimeControlFlowList from being corrupted as data
fglock Nov 6, 2025
7cc0bf1
docs: Document RuntimeControlFlowList data corruption fix
fglock Nov 6, 2025
2d03783
docs: Comprehensive analysis of ASM frame computation blocker
fglock Nov 6, 2025
ee4cb82
feat: Implement runtime control flow registry for 'last SKIP' support
fglock Nov 6, 2025
b2e23ef
docs: Add comprehensive documentation for control flow registry solution
fglock Nov 6, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
feat: Propagate isMainProgram flag to code generation
- Added isMainProgram flag to CompilerOptions
- Set flag in PerlLanguageProvider.executePerlCode()
- Modified loop handler to throw error directly when in main program's
  outermost loop instead of propagating via returnLabel
- Added RuntimeCode.throwControlFlowError() helper method for consistent
  error messages
- Main program loops now throw errors immediately, while subroutine loops
  still propagate to caller (Phase 4 catches it)

This optimizes error detection: main program errors are caught at the
loop handler level, while subroutine errors are caught at the boundary.
  • Loading branch information
fglock committed Nov 6, 2025
commit 4a8a3695a6cc8b09a1e5c54ad2fceef165bb19ba
1 change: 1 addition & 0 deletions src/main/java/org/perlonjava/CompilerOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ public class CompilerOptions implements Cloneable {
public String debugFlags = ""; // For -D
// Unicode/encoding flags for -C switches
public boolean unicodeStdin = false; // -CS or -CI
public boolean isMainProgram = false; // True if this is the top-level main script
public boolean unicodeStdout = false; // -CO
public boolean unicodeStderr = false; // -CE
public boolean unicodeInput = false; // -CI (same as stdin)
Expand Down
48 changes: 45 additions & 3 deletions src/main/java/org/perlonjava/codegen/EmitForeach.java
Original file line number Diff line number Diff line change
Expand Up @@ -254,13 +254,18 @@ public static void emitFor1(EmitterVisitor emitterVisitor, For1Node node) {
// Get goto labels from current scope (TODO: implement getGotoLabels in EmitterContext)
java.util.Map<String, org.objectweb.asm.Label> gotoLabels = null; // For now

// Check if this is the outermost loop in the main program
boolean isMainProgramOutermostLoop = emitterVisitor.ctx.compilerOptions.isMainProgram
&& parentLoopLabels == null;

// Emit the handler
emitControlFlowHandler(
mv,
currentLoopLabels,
parentLoopLabels,
emitterVisitor.ctx.javaClassInfo.returnLabel,
gotoLabels);
gotoLabels,
isMainProgramOutermostLoop);
}

// Restore dynamic variable stack for our localization
Expand Down Expand Up @@ -307,13 +312,15 @@ public static void emitFor1(EmitterVisitor emitterVisitor, For1Node node) {
* @param parentLoopLabels The parent loop's labels (null if this is the outermost loop)
* @param returnLabel The subroutine's return label
* @param gotoLabels Map of goto label names to ASM Labels in current scope
* @param isMainProgramOutermostLoop True if this is the outermost loop in main program (should throw error instead of returning)
*/
private static void emitControlFlowHandler(
MethodVisitor mv,
LoopLabels loopLabels,
LoopLabels parentLoopLabels,
org.objectweb.asm.Label returnLabel,
java.util.Map<String, org.objectweb.asm.Label> gotoLabels) {
java.util.Map<String, org.objectweb.asm.Label> gotoLabels,
boolean isMainProgramOutermostLoop) {

if (!ENABLE_LOOP_HANDLERS) {
return; // Feature not enabled yet
Expand Down Expand Up @@ -409,8 +416,43 @@ private static void emitControlFlowHandler(
if (parentLoopLabels != null && parentLoopLabels.controlFlowHandler != null) {
// Chain to parent loop's handler
mv.visitJumpInsn(Opcodes.GOTO, parentLoopLabels.controlFlowHandler);
} else if (isMainProgramOutermostLoop) {
// Outermost loop in main program - throw error immediately
// Stack: [RuntimeControlFlowList]

// Cast to RuntimeControlFlowList
mv.visitTypeInsn(Opcodes.CHECKCAST, "org/perlonjava/runtime/RuntimeControlFlowList");
mv.visitInsn(Opcodes.DUP);

// Get control flow type
mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL,
"org/perlonjava/runtime/RuntimeControlFlowList",
"getControlFlowType",
"()Lorg/perlonjava/runtime/ControlFlowType;",
false);
mv.visitInsn(Opcodes.DUP);

// Get label
mv.visitInsn(Opcodes.SWAP);
mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL,
"org/perlonjava/runtime/RuntimeControlFlowList",
"getControlFlowLabel",
"()Ljava/lang/String;",
false);

// Call helper method to throw appropriate error
// Stack: [ControlFlowType, String label]
mv.visitMethodInsn(Opcodes.INVOKESTATIC,
"org/perlonjava/runtime/RuntimeCode",
"throwControlFlowError",
"(Lorg/perlonjava/runtime/ControlFlowType;Ljava/lang/String;)V",
false);

// Should never reach here (method throws), but add RETURN for verifier
mv.visitInsn(Opcodes.ACONST_NULL);
mv.visitInsn(Opcodes.ARETURN);
} else {
// Outermost loop - return to caller
// Outermost loop in subroutine - return to caller (will be caught by Phase 4 or parent)
mv.visitJumpInsn(Opcodes.GOTO, returnLabel);
}
}
Expand Down
29 changes: 29 additions & 0 deletions src/main/java/org/perlonjava/runtime/RuntimeCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -1003,4 +1003,33 @@ public void dynamicRestoreState() {
throw new PerlCompilerException("Can't modify anonymous subroutine");
}

/**
* Helper method to throw appropriate control flow error.
* Called from loop handlers in main program when control flow escapes.
*
* @param cfType The control flow type
* @param label The label (may be null)
* @throws PerlCompilerException Always throws with appropriate message
*/
public static void throwControlFlowError(ControlFlowType cfType, String label) {
if (cfType == ControlFlowType.TAILCALL) {
// Tail call should have been handled by trampoline at returnLabel
throw new PerlCompilerException("Tail call escaped to top level (internal error) at ");
} else if (cfType == ControlFlowType.GOTO) {
if (label != null) {
throw new PerlCompilerException("Can't find label " + label + " at ");
} else {
throw new PerlCompilerException("goto must have a label at ");
}
} else {
// last/next/redo
String operation = cfType.name().toLowerCase();
if (label != null) {
throw new PerlCompilerException("Label not found for \"" + operation + " " + label + "\" at ");
} else {
throw new PerlCompilerException("Can't \"" + operation + "\" outside a loop block at ");
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ public static RuntimeList executePerlCode(CompilerOptions compilerOptions,
boolean isTopLevelScript,
int callerContext) throws Exception {

// Store the isMainProgram flag in CompilerOptions for use during code generation
compilerOptions.isMainProgram = isTopLevelScript;

ScopedSymbolTable globalSymbolTable = new ScopedSymbolTable();
// Enter a new scope in the symbol table and add special Perl variables
globalSymbolTable.enterScope();
Expand Down