-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathRuntimeControlFlowRegistry.java
More file actions
222 lines (199 loc) · 7.28 KB
/
RuntimeControlFlowRegistry.java
File metadata and controls
222 lines (199 loc) · 7.28 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
package org.perlonjava.runtime;
/**
* Thread-local registry for non-local control flow markers.
* This provides an alternative to call-site checks that works around ASM frame computation issues.
*
* When a control flow statement (last/next/redo/goto) executes inside a subroutine,
* it registers the marker here. Loop boundaries check the registry and handle the marker.
*/
public class RuntimeControlFlowRegistry {
// Thread-local storage for control flow markers
private static final ThreadLocal<ControlFlowMarker> currentMarker = new ThreadLocal<>();
/**
* Register a control flow marker.
* This is called by last/next/redo/goto when they need to propagate across subroutine boundaries.
*
* @param marker The control flow marker to register
*/
public static void register(ControlFlowMarker marker) {
currentMarker.set(marker);
}
/**
* Check if there's a pending control flow marker.
*
* @return true if a marker is registered
*/
public static boolean hasMarker() {
return currentMarker.get() != null;
}
/**
* Get the current control flow marker.
*
* @return The marker, or null if none
*/
public static ControlFlowMarker getMarker() {
return currentMarker.get();
}
/**
* Clear the current marker.
* This is called after the marker has been handled by a loop.
*/
public static void clear() {
currentMarker.remove();
}
/**
* Check if the current marker matches a given label and type.
* If it matches, clears the marker and returns true.
*
* @param type The control flow type to check (LAST, NEXT, REDO, GOTO)
* @param label The label to match (null for unlabeled)
* @return true if the marker matches and was cleared
*/
public static boolean checkAndClear(ControlFlowType type, String label) {
ControlFlowMarker marker = currentMarker.get();
if (marker == null) {
return false;
}
// Check if type matches
if (marker.type != type) {
return false;
}
// Check if label matches (Perl semantics)
if (marker.label == null) {
// Unlabeled control flow matches any loop
clear();
return true;
}
// Labeled control flow must match exactly
if (marker.label.equals(label)) {
clear();
return true;
}
return false;
}
/**
* Check if the current marker is a GOTO that matches a specific label.
* Used for goto LABEL (not last/next/redo).
*
* @param label The goto label to check
* @return true if there's a GOTO marker for this label
*/
public static boolean checkGoto(String label) {
ControlFlowMarker marker = currentMarker.get();
if (marker == null || marker.type != ControlFlowType.GOTO) {
return false;
}
if (marker.label != null && marker.label.equals(label)) {
clear();
return true;
}
return false;
}
/**
* Check if the current marker (if any) matches the given label.
* Does NOT clear the marker - just checks if it matches.
*
* @param labelName The label to check against
* @return true if there's a marker and it matches this label
*/
public static boolean markerMatchesLabel(String labelName) {
ControlFlowMarker marker = currentMarker.get();
if (marker == null) {
return false;
}
// Check if marker's label matches (Perl semantics)
if (marker.label == null) {
// Unlabeled control flow matches any loop
return true;
} else if (labelName == null) {
// Labeled control flow doesn't match unlabeled loop
return false;
} else {
// Both labeled - must match exactly
return marker.label.equals(labelName);
}
}
/**
* Check if there's a pending control flow marker for a specific loop label.
* If the marker matches, clear it and return the action code.
* If it doesn't match, leave it for an outer loop.
*
* This is called at loop boundaries to check for non-local control flow.
*
* @param labelName The label of the current loop (null for unlabeled loops)
* @return Action code: 0=no match, 1=LAST, 2=NEXT, 3=REDO
*/
public static int checkLoopAndGetAction(String labelName) {
ControlFlowMarker marker = currentMarker.get();
if (marker == null) {
return 0; // No marker
}
// Check if marker's label matches (Perl semantics)
boolean labelMatches;
if (marker.label == null) {
// Unlabeled control flow matches any loop
labelMatches = true;
} else if (labelName == null) {
// Labeled control flow doesn't match unlabeled loop
labelMatches = false;
} else {
// Both labeled - must match exactly
labelMatches = marker.label.equals(labelName);
}
if (!labelMatches) {
return 0; // Label doesn't match, leave marker for outer loop
}
// Label matches - return action and clear marker
clear();
switch (marker.type) {
case LAST:
return 1;
case NEXT:
return 2;
case REDO:
return 3;
case GOTO:
// GOTO markers are handled separately (not by loops)
// Put it back and return 0
register(marker);
return 0;
case TAILCALL:
// Shouldn't reach here in a loop
register(marker);
return 0;
default:
return 0;
}
}
/**
* If there's an uncaught marker at the top level, throw an error.
* This is called at program exit or when returning from a subroutine to the top level.
*/
public static void throwIfUncaught() {
ControlFlowMarker marker = currentMarker.get();
if (marker != null) {
clear();
marker.throwError();
}
}
/**
* Check the registry and wrap the result if needed.
* This is the SIMPLEST approach to avoid ASM frame computation issues:
* - No branching in bytecode (no TABLESWITCH, no IF chains, no GOTOs)
* - Just a single method call that returns either the original or a marked list
* - All the logic is in Java code, not bytecode
*
* @param result The original result from the subroutine call
* @param labelName The loop's label (null for unlabeled)
* @return Either the original result or a marked RuntimeControlFlowList
*/
public static RuntimeList checkAndWrapIfNeeded(RuntimeList result, String labelName) {
int action = checkLoopAndGetAction(labelName);
if (action == 0) {
// No action - return original result
return result;
}
// Action detected - return a marked RuntimeControlFlowList
return RuntimeControlFlowList.createFromAction(action, labelName);
}
}