Skip to content

Commit 0b5aece

Browse files
author
Adam Hrbac
committed
Cleanup code
1 parent 2d41010 commit 0b5aece

File tree

3 files changed

+42
-21
lines changed

3 files changed

+42
-21
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/frame/FrameBuiltins.java

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@
4343
import com.oracle.graal.python.builtins.objects.object.ObjectBuiltinsFactory;
4444
import com.oracle.graal.python.builtins.objects.object.PythonObject;
4545
import com.oracle.graal.python.builtins.objects.str.StringUtils.SimpleTruffleStringFormatNode;
46+
import com.oracle.graal.python.lib.PyLongAsLongAndOverflowNode;
47+
import com.oracle.graal.python.lib.PyLongCheckExactNode;
4648
import com.oracle.graal.python.nodes.ErrorMessages;
4749
import com.oracle.graal.python.nodes.PRaiseNode;
4850
import com.oracle.graal.python.nodes.frame.GetFrameLocalsNode;
@@ -55,6 +57,7 @@
5557
import com.oracle.graal.python.nodes.util.CannotCastException;
5658
import com.oracle.graal.python.nodes.util.CastToJavaBooleanNode;
5759
import com.oracle.graal.python.runtime.object.PythonObjectFactory;
60+
import com.oracle.graal.python.util.OverflowException;
5861
import com.oracle.truffle.api.CompilerDirectives;
5962
import com.oracle.truffle.api.RootCallTarget;
6063
import com.oracle.truffle.api.dsl.Bind;
@@ -142,6 +145,7 @@ public static GetBuiltinsNode create() {
142145

143146
@GenerateNodeFactory
144147
public abstract static class GetLinenoNode extends PythonBuiltinNode {
148+
// Kept around since it is used by other nodes
145149
public abstract int executeInt(VirtualFrame frame, PFrame self);
146150

147151
@Specialization
@@ -168,14 +172,19 @@ public static GetLinenoNode create() {
168172

169173
@Builtin(name = "f_lineno", minNumOfPositionalArgs = 1, maxNumOfPositionalArgs = 2, isGetter = true, isSetter = true)
170174
@GenerateNodeFactory
171-
public abstract static class SetLinenoNode extends PythonBuiltinNode {
175+
public abstract static class LinenoNode extends PythonBuiltinNode {
172176
public abstract Object execute(VirtualFrame frame, PFrame self, Object newLineno);
173177

174178
@Specialization(guards = "isNoValue(newLineno)")
175179
int get(VirtualFrame frame, PFrame self, Object newLineno,
176180
@Bind("this") Node inliningTarget,
177181
@Cached.Shared("isCurrentFrame") @Cached InlinedConditionProfile isCurrentFrameProfile,
178182
@Cached.Shared("materialize") @Cached MaterializeFrameNode materializeNode) {
183+
syncLocationIfNeeded(frame, self, inliningTarget, isCurrentFrameProfile, materializeNode);
184+
return self.getLine();
185+
}
186+
187+
private void syncLocationIfNeeded(VirtualFrame frame, PFrame self, Node inliningTarget, InlinedConditionProfile isCurrentFrameProfile, MaterializeFrameNode materializeNode) {
179188
// Special case because this builtin can be called without going through an invoke node:
180189
// we need to sync the location of the frame if and only if 'self' represents the
181190
// current frame. If 'self' represents another frame on the stack, the location is
@@ -184,28 +193,31 @@ int get(VirtualFrame frame, PFrame self, Object newLineno,
184193
PFrame pyFrame = materializeNode.execute(frame, this, false, false);
185194
assert pyFrame == self;
186195
}
187-
return self.getLine();
188196
}
189197

190198
@Specialization(guards = "!isNoValue(newLineno)")
191199
PNone set(VirtualFrame frame, PFrame self, Object newLineno,
192200
@Bind("this") Node inliningTarget,
193201
@Cached.Shared("isCurrentFrame") @Cached InlinedConditionProfile isCurrentFrameProfile,
194202
@Cached.Shared("materialize") @Cached MaterializeFrameNode materializeNode,
195-
@Cached PRaiseNode.Lazy raise) {
196-
// Special case because this builtin can be called without going through an invoke node:
197-
// we need to sync the location of the frame if and only if 'self' represents the
198-
// current frame. If 'self' represents another frame on the stack, the location is
199-
// already set
200-
if (isCurrentFrameProfile.profile(inliningTarget, frame != null && PArguments.getCurrentFrameInfo(frame) == self.getRef())) {
201-
PFrame pyFrame = materializeNode.execute(frame, this, false, false);
202-
assert pyFrame == self;
203-
}
203+
@Cached PRaiseNode.Lazy raise,
204+
@Cached PyLongCheckExactNode isLong,
205+
@Cached PyLongAsLongAndOverflowNode toLong) {
206+
syncLocationIfNeeded(frame, self, inliningTarget, isCurrentFrameProfile, materializeNode);
204207
if (self.isTraceArgument()) {
205-
if (newLineno instanceof Integer x) {
206-
self.setJumpDestLine(x);
208+
if (isLong.execute(inliningTarget, newLineno)) {
209+
try {
210+
long lineno = toLong.execute(frame, inliningTarget, newLineno);
211+
if (lineno <= Integer.MAX_VALUE && lineno >= Integer.MIN_VALUE) {
212+
self.setJumpDestLine((int)lineno);
213+
} else {
214+
throw raise.get(inliningTarget).raise(PythonBuiltinClassType.ValueError, ErrorMessages.LINENO_OUT_OF_RANGE);
215+
}
216+
} catch (OverflowException e) {
217+
throw raise.get(inliningTarget).raise(PythonBuiltinClassType.ValueError, ErrorMessages.LINENO_OUT_OF_RANGE);
218+
}
207219
} else {
208-
throw raise.get(inliningTarget).raise(PythonBuiltinClassType.ValueError, ErrorMessages.EXPECTED_S_GOT_P, "int", newLineno);
220+
throw raise.get(inliningTarget).raise(PythonBuiltinClassType.ValueError, ErrorMessages.LINENO_MUST_BE_AN_INTEGER)
209221
}
210222
} else {
211223
throw raise.get(inliningTarget).raise(PythonBuiltinClassType.ValueError, ErrorMessages.CANT_JUMP_FROM_S_EVENT, getContext().getThreadState(getLanguage()).getTracingWhat().pythonName);

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/compiler/CodeUnit.java

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -629,7 +629,7 @@ public List<ArrayList<StackItem>> computeStackElems() {
629629
todo.addFirst(0);
630630
while (!todo.isEmpty()) {
631631
int i = todo.removeLast();
632-
assert blocks.get(i) != null : "TODO message here";
632+
assert blocks.get(i) != null : "Reached block without determining its stack state";
633633
opCodeAt(code, i, (bci, op, oparg, followingArgs) -> {
634634
ArrayList<StackItem> next = blocks.get(bci);
635635
for (int j = 0; j < exceptionHandlerRanges.length; j += 4) {
@@ -692,8 +692,19 @@ public List<ArrayList<StackItem>> computeStackElems() {
692692
}
693693
case LOAD_NONE:
694694
opCodeAt(code, op.getNextBci(bci, oparg, false), (ignored, nextOp, ignored2, ignored3) -> {
695-
// used instead of exceptions in non-error paths for these opcodes
696-
// the handler code will push the Except StackItem
695+
// Usually, when compiling bytecode around exception handlers, the code
696+
// is generated twice, once for the path with no exception, and
697+
// once for the path with the exception. However, when generating code
698+
// for a with statement exit, the code is generated as follows (and in a
699+
// similar manner for async with).
700+
// ...
701+
// LOAD_NONE
702+
// EXIT_WITH (exception handler starts here)
703+
// ...
704+
// This means that setting the stack at EXIT_WITH to have Object on top,
705+
// as LOAD_NONE usually would, would cause a conflict with the exception
706+
// handler starting at that position, which has the stack top be an
707+
// Exception.
697708
if (nextOp != OpCodes.GET_AEXIT_CORO && nextOp != OpCodes.EXIT_WITH) {
698709
setNextStack(todo, blocks, op.getNextBci(bci, oparg, false), StackItem.Object.push(blocks.get(bci)));
699710
}
@@ -722,16 +733,12 @@ public List<ArrayList<StackItem>> computeStackElems() {
722733
private void handleGeneralOp(List<ArrayList<StackItem>> blocks, ArrayDeque<Integer> todo, int bci, int next, int stackLost, int stackGain) {
723734
if (next >= 0) {
724735
ArrayList<StackItem> blocksHere = new ArrayList<>(blocks.get(bci));
725-
// System.out.println("Before: " + stackLost + "\t" + stackGain + "\t" + bci + "\t" +
726-
// blocksHere);
727736
for (int k = 0; k < stackLost; ++k) {
728737
blocksHere.remove(blocksHere.size() - 1);
729738
}
730739
for (int k = 0; k < stackGain; ++k) {
731740
blocksHere.add(StackItem.Object);
732741
}
733-
// System.out.println("Before stack: " + blocks.get(next));
734-
// System.out.println("After: " + blocksHere);
735742
setNextStack(todo, blocks, next, blocksHere);
736743
}
737744
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/ErrorMessages.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1620,5 +1620,7 @@ public abstract class ErrorMessages {
16201620
public static final TruffleString LINE_D_COMES_AFTER_THE_CURRENT_CODE_BLOCK = tsLiteral("line %d comes after the current code block");
16211621
public static final TruffleString LINE_D_COMES_BEFORE_THE_CURRENT_CODE_BLOCK = tsLiteral("line %d comes before the current code block");
16221622
public static final TruffleString CANT_JUMP_FROM_S_EVENT = tsLiteral("Can't jump from \"%s\" event.");
1623+
public static final TruffleString LINENO_MUST_BE_AN_INTEGER = tsLiteral("lineno must be an integer");
16231624

1625+
public static final TruffleString LINENO_OUT_OF_RANGE = tsLiteral("lineno out of range");
16241626
}

0 commit comments

Comments
 (0)