Skip to content

Commit 8bec500

Browse files
author
Adam Hrbac
committed
Use extra indirection for TraceData
1 parent 79224ca commit 8bec500

File tree

1 file changed

+68
-24
lines changed

1 file changed

+68
-24
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/bytecode/PBytecodeRootNode.java

Lines changed: 68 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1011,13 +1011,60 @@ private InterpreterContinuation(int bci, int stackTop) {
10111011

10121012
@ValueType
10131013
private static final class MutableLoopData {
1014+
public int getPastBci() {
1015+
return getTraceData().pastBci;
1016+
}
1017+
1018+
public int setPastBci(int pastBci) {
1019+
return this.getTraceData().pastBci = pastBci;
1020+
}
1021+
1022+
public int getPastLine() {
1023+
return getTraceData().pastLine;
1024+
}
1025+
1026+
public int setPastLine(int pastLine) {
1027+
return this.getTraceData().pastLine = pastLine;
1028+
}
1029+
1030+
public int getReturnLine() {
1031+
return getTraceData().returnLine;
1032+
}
1033+
1034+
public int setReturnLine(int returnLine) {
1035+
return this.getTraceData().returnLine = returnLine;
1036+
}
1037+
1038+
public PFrame getPyFrame() {
1039+
return getTraceData().pyFrame;
1040+
}
1041+
1042+
public PFrame setPyFrame(PFrame pyFrame) {
1043+
return this.getTraceData().pyFrame = pyFrame;
1044+
}
1045+
1046+
private TraceData getTraceData() {
1047+
if (traceData == null) {
1048+
traceData = new TraceData();
1049+
}
1050+
return traceData;
1051+
}
1052+
10141053
/*
10151054
* data for tracing
10161055
*/
1017-
int pastBci;
1018-
int pastLine;
1019-
int returnLine;
1020-
PFrame pyFrame;
1056+
private static final class TraceData {
1057+
TraceData() {
1058+
pastBci = 0;
1059+
pastLine = returnLine = -1;
1060+
}
1061+
private int pastBci;
1062+
private int pastLine;
1063+
private int returnLine;
1064+
private PFrame pyFrame = null;
1065+
}
1066+
1067+
private TraceData traceData = null;
10211068

10221069
int loopCount;
10231070
/*
@@ -1087,7 +1134,6 @@ private Object bytecodeLoop(VirtualFrame virtualFrame, Frame localFrame, Bytecod
10871134
MutableLoopData mutableData = new MutableLoopData();
10881135
int stackTop = initialStackTop;
10891136
int bci = initialBci;
1090-
mutableData.pastLine = -1;
10911137

10921138
byte[] localBC = bytecode;
10931139
Object[] localConsts = consts;
@@ -1107,20 +1153,18 @@ private Object bytecodeLoop(VirtualFrame virtualFrame, Frame localFrame, Bytecod
11071153
// extra CALL event would be incorrect
11081154
if (!noTrace.isValid() && threadState.getTraceFun() != null && !fromOSR) {
11091155
invokeTraceFunction(virtualFrame, null, threadState, mutableData, PythonContext.TraceEvent.CALL,
1110-
initialBci == 0 ? getFirstLineno() : (mutableData.pastLine = bciToLine(initialBci)), false);
1156+
initialBci == 0 ? getFirstLineno() : (mutableData.setPastLine(bciToLine(initialBci))), false);
11111157
}
11121158

1113-
mutableData.returnLine = -1;
1114-
11151159
int oparg = 0;
11161160
while (true) {
11171161
final byte bc = localBC[bci];
11181162
final int beginBci = bci;
11191163
if (!noTrace.isValid() && threadState.getTraceFun() != null) {
11201164
int thisLine = bciToLine(bci);
1121-
boolean onANewLine = thisLine != mutableData.pastLine;
1122-
mutableData.pastLine = thisLine;
1123-
OpCodes c = OpCodes.fromOpCode(localBC[mutableData.pastBci]);
1165+
boolean onANewLine = thisLine != mutableData.getPastLine();
1166+
mutableData.setPastLine(thisLine);
1167+
OpCodes c = OpCodes.fromOpCode(localBC[mutableData.getPastBci()]);
11241168
/*
11251169
* normally, we trace a line every time the previous bytecode instruction was on a
11261170
* different line than the current one. There are a number of exceptions to this,
@@ -1138,23 +1182,23 @@ private Object bytecodeLoop(VirtualFrame virtualFrame, Frame localFrame, Bytecod
11381182
* https://github.com/python/cpython/blob/main/Objects/lnotab_notes.txt#L210-L215
11391183
* for more details
11401184
*/
1141-
boolean shouldTrace = mutableData.pastBci > bci; // is a backward jump
1185+
boolean shouldTrace = mutableData.getPastBci() > bci; // is a backward jump
11421186
if (!shouldTrace) {
11431187
shouldTrace = onANewLine &&
11441188
// is not a forward jump
1145-
(mutableData.pastBci + c.length() >= bci ||
1189+
(mutableData.getPastBci() + c.length() >= bci ||
11461190
// is a forward jump to the start of line
11471191
bciToLine(bci - 1) != thisLine);
11481192
}
11491193
if (shouldTrace) {
1150-
mutableData.returnLine = mutableData.pastLine;
1151-
mutableData.pyFrame = ensurePyFrame(virtualFrame, mutableData.pyFrame);
1152-
if (mutableData.pyFrame.getTraceLine()) {
1194+
mutableData.setReturnLine(mutableData.getPastLine());
1195+
mutableData.setPyFrame(ensurePyFrame(virtualFrame, mutableData.getPyFrame()));
1196+
if (mutableData.getPyFrame().getTraceLine()) {
11531197
invokeTraceFunction(virtualFrame, null, threadState, mutableData, PythonContext.TraceEvent.LINE,
1154-
mutableData.pastLine, true);
1198+
mutableData.getPastLine(), true);
11551199
}
11561200
}
1157-
mutableData.pastBci = bci;
1201+
mutableData.setPastBci(bci);
11581202
}
11591203

11601204
CompilerAsserts.partialEvaluationConstant(bc);
@@ -1612,7 +1656,7 @@ private Object bytecodeLoop(VirtualFrame virtualFrame, Frame localFrame, Bytecod
16121656
Object value = virtualFrame.getObject(stackTop);
16131657
if (!noTrace.isValid() && threadState.getTraceFun() != null) {
16141658
invokeTraceFunction(virtualFrame, value, threadState, mutableData, PythonContext.TraceEvent.RETURN,
1615-
mutableData.returnLine, true);
1659+
mutableData.getReturnLine(), true);
16161660
}
16171661
if (isGeneratorOrCoroutine) {
16181662
throw new GeneratorReturnException(value);
@@ -2054,7 +2098,7 @@ private Object bytecodeLoop(VirtualFrame virtualFrame, Frame localFrame, Bytecod
20542098
}
20552099
if (!noTrace.isValid() && threadState.getTraceFun() != null) {
20562100
invokeTraceFunction(virtualFrame, value, threadState, mutableData, PythonContext.TraceEvent.RETURN,
2057-
mutableData.returnLine, true);
2101+
mutableData.getReturnLine(), true);
20582102
}
20592103
return new GeneratorYieldResult(bci + 1, stackTop, value);
20602104
}
@@ -2144,8 +2188,8 @@ private Object bytecodeLoop(VirtualFrame virtualFrame, Frame localFrame, Bytecod
21442188
}
21452189

21462190
if (!noTrace.isValid() && threadState.getTraceFun() != null && !threadState.isTracing() && pe != null) {
2147-
mutableData.pyFrame = ensurePyFrame(virtualFrame, mutableData.pyFrame);
2148-
if (mutableData.pyFrame.getLocalTraceFun() != null) {
2191+
mutableData.setPyFrame(ensurePyFrame(virtualFrame, mutableData.getPyFrame()));
2192+
if (mutableData.getPyFrame().getLocalTraceFun() != null) {
21492193
Object traceback = GetExceptionTracebackNode.getUncached().execute(pe);
21502194
PBaseException peForPython = pe.setCatchingFrameAndGetEscapedException(virtualFrame, this);
21512195
Object peType = GetClassNode.getUncached().execute(peForPython);
@@ -2186,7 +2230,7 @@ private Object bytecodeLoop(VirtualFrame virtualFrame, Frame localFrame, Bytecod
21862230
}
21872231
if (!noTrace.isValid() && threadState.getTraceFun() != null) {
21882232
invokeTraceFunction(virtualFrame, PNone.NONE, threadState, mutableData, PythonContext.TraceEvent.RETURN,
2189-
mutableData.returnLine, true);
2233+
mutableData.getReturnLine(), true);
21902234
}
21912235
if (e == pe) {
21922236
throw pe;
@@ -2230,7 +2274,7 @@ private void invokeTraceFunction(VirtualFrame virtualFrame, Object arg, PythonCo
22302274
return;
22312275
}
22322276
threadState.tracingStart(event);
2233-
PFrame pyFrame = mutableData.pyFrame = ensurePyFrame(virtualFrame, mutableData.pyFrame);
2277+
PFrame pyFrame = mutableData.setPyFrame(ensurePyFrame(virtualFrame, mutableData.getPyFrame()));
22342278
Object traceFn = useLocalFn ? pyFrame.getLocalTraceFun() : threadState.getTraceFun();
22352279
if (traceFn == null) {
22362280
threadState.tracingStop();

0 commit comments

Comments
 (0)