-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathInterpreterState.java
More file actions
117 lines (104 loc) · 4.08 KB
/
InterpreterState.java
File metadata and controls
117 lines (104 loc) · 4.08 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
package org.perlonjava.backend.bytecode;
import org.perlonjava.runtime.runtimetypes.RuntimeScalar;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.List;
/**
* Maintains minimal interpreter execution state for stack trace generation.
* Thread-safe via ThreadLocal. This enables proper Perl-level stack traces
* when exceptions occur in interpreted code.
* <p>
* Design: Minimal overhead approach matching codegen's zero-overhead strategy.
* Only tracks the call stack for stack trace generation, not PC updates.
*/
public class InterpreterState {
private static final ThreadLocal<Deque<InterpreterFrame>> frameStack =
ThreadLocal.withInitial(ArrayDeque::new);
private static final ThreadLocal<Deque<Integer>> pcStack =
ThreadLocal.withInitial(ArrayDeque::new);
/**
* Thread-local RuntimeScalar holding the runtime current package name.
*
* This is the single source of truth for the current package at runtime.
* It is used by:
* - caller() to return the correct calling package
* - eval STRING to compile code in the right package
* - SET_PACKAGE opcode (non-scoped: package Foo;) — sets it directly
* - PUSH_PACKAGE opcode (scoped: package Foo { }) — saves via DynamicVariableManager then sets
*
* Scoped package blocks are automatically restored when the scope exits via
* the existing POP_LOCAL_LEVEL opcode (DynamicVariableManager.popToLocalLevel).
*/
public static final ThreadLocal<RuntimeScalar> currentPackage =
ThreadLocal.withInitial(() -> new RuntimeScalar("main"));
/**
* Represents a single interpreter call frame.
* Contains minimal information needed for stack trace formatting.
*/
public static class InterpreterFrame {
public final InterpretedCode code;
public final String packageName;
public final String subroutineName;
public InterpreterFrame(InterpretedCode code, String packageName, String subroutineName) {
this.code = code;
this.packageName = packageName;
this.subroutineName = subroutineName;
}
}
/**
* Push a new interpreter frame onto the stack.
* Called at entry to BytecodeInterpreter.execute().
*
* @param code The InterpretedCode being executed
* @param packageName The package context (e.g., "main")
* @param subroutineName The subroutine name (or null for main code)
*/
public static void push(InterpretedCode code, String packageName, String subroutineName) {
frameStack.get().push(new InterpreterFrame(code, packageName, subroutineName));
pcStack.get().push(0);
}
/**
* Pop the current interpreter frame from the stack.
* Called at exit from BytecodeInterpreter.execute() (in finally block).
*/
public static void pop() {
Deque<InterpreterFrame> stack = frameStack.get();
if (!stack.isEmpty()) {
stack.pop();
}
Deque<Integer> pcs = pcStack.get();
if (!pcs.isEmpty()) {
pcs.pop();
}
}
public static void setCurrentPc(int pc) {
Deque<Integer> pcs = pcStack.get();
if (!pcs.isEmpty()) {
pcs.pop();
pcs.push(pc);
}
}
/**
* Get the current (topmost) interpreter frame.
* Used by ExceptionFormatter to detect interpreter execution.
*
* @return The current frame, or null if not executing interpreted code
*/
public static InterpreterFrame current() {
Deque<InterpreterFrame> stack = frameStack.get();
return stack.isEmpty() ? null : stack.peek();
}
/**
* Get the complete interpreter call stack.
* Used by caller() operator to introspect the call stack.
*
* @return A list of frames from most recent (index 0) to oldest
*/
public static List<InterpreterFrame> getStack() {
return new ArrayList<>(frameStack.get());
}
public static List<Integer> getPcStack() {
return new ArrayList<>(pcStack.get());
}
}