Skip to content

Commit e8d0b40

Browse files
committed
Move FrameInfo to upper level
1 parent 50a5a89 commit e8d0b40

File tree

6 files changed

+128
-102
lines changed

6 files changed

+128
-102
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/generator/GeneratorBuiltins.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
import com.oracle.graal.python.nodes.PGuards;
6262
import com.oracle.graal.python.nodes.PRaiseNode;
6363
import com.oracle.graal.python.nodes.SpecialMethodNames;
64-
import com.oracle.graal.python.nodes.bytecode.PBytecodeRootNode;
64+
import com.oracle.graal.python.nodes.bytecode.FrameInfo;
6565
import com.oracle.graal.python.nodes.call.CallTargetInvokeNode;
6666
import com.oracle.graal.python.nodes.call.GenericInvokeNode;
6767
import com.oracle.graal.python.nodes.call.special.LookupAndCallVarargsNode;
@@ -544,7 +544,7 @@ static Object getFrame(PGenerator self,
544544
Object[] arguments = PArguments.create();
545545
Node location;
546546
if (self.usesBytecode()) {
547-
location = ((PBytecodeRootNode.FrameInfo) generatorFrame.getFrameDescriptor().getInfo()).getRootNode();
547+
location = ((FrameInfo) generatorFrame.getFrameDescriptor().getInfo()).getRootNode();
548548
} else {
549549
location = self.getCurrentYieldNode();
550550
if (location == null) {
@@ -565,7 +565,7 @@ static Object getFrame(PGenerator self,
565565
frame.setLasti(10000);
566566
}
567567
} else {
568-
PBytecodeRootNode.FrameInfo info = (PBytecodeRootNode.FrameInfo) generatorFrame.getFrameDescriptor().getInfo();
568+
FrameInfo info = (FrameInfo) generatorFrame.getFrameDescriptor().getInfo();
569569
frame.setLasti(info.getBci(generatorFrame));
570570
frame.setLine(info.getLineno(generatorFrame));
571571
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/generator/PGenerator.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import com.oracle.graal.python.builtins.objects.function.PArguments;
3535
import com.oracle.graal.python.builtins.objects.iterator.PIntRangeIterator;
3636
import com.oracle.graal.python.builtins.objects.object.PythonBuiltinObject;
37+
import com.oracle.graal.python.nodes.bytecode.FrameInfo;
3738
import com.oracle.graal.python.nodes.bytecode.PBytecodeGeneratorRootNode;
3839
import com.oracle.graal.python.nodes.bytecode.PBytecodeRootNode;
3940
import com.oracle.graal.python.nodes.generator.AbstractYieldNode;
@@ -73,7 +74,7 @@ public final class PGenerator extends PythonBuiltinObject {
7374
private final boolean isPRangeIterator;
7475
private final GeneratorInfo generatorInfo;
7576
private final PBytecodeRootNode bytecodeRootNode;
76-
private final PBytecodeRootNode.FrameInfo frameInfo;
77+
private final FrameInfo frameInfo;
7778
// running means it is currently on the stack, not just started
7879
private boolean running;
7980

@@ -159,7 +160,7 @@ private PGenerator(PythonLanguage lang, String name, String qualname, PBytecodeR
159160
this.arguments = arguments;
160161
this.finished = false;
161162
this.bytecodeRootNode = rootNode;
162-
this.frameInfo = (PBytecodeRootNode.FrameInfo) rootNode.getFrameDescriptor().getInfo();
163+
this.frameInfo = (FrameInfo) rootNode.getFrameDescriptor().getInfo();
163164
this.iterator = null;
164165
this.isPRangeIterator = false;
165166
this.closure = null;
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/*
2+
* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* The Universal Permissive License (UPL), Version 1.0
6+
*
7+
* Subject to the condition set forth below, permission is hereby granted to any
8+
* person obtaining a copy of this software, associated documentation and/or
9+
* data (collectively the "Software"), free of charge and under any and all
10+
* copyright rights in the Software, and any and all patent rights owned or
11+
* freely licensable by each licensor hereunder covering either (i) the
12+
* unmodified Software as contributed to or provided by such licensor, or (ii)
13+
* the Larger Works (as defined below), to deal in both
14+
*
15+
* (a) the Software, and
16+
*
17+
* (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
18+
* one is included with the Software each a "Larger Work" to which the Software
19+
* is contributed by such licensors),
20+
*
21+
* without restriction, including without limitation the rights to copy, create
22+
* derivative works of, display, perform, and distribute the Software and make,
23+
* use, sell, offer for sale, import, export, have made, and have sold the
24+
* Software and the Larger Work(s), and to sublicense the foregoing rights on
25+
* either these or other terms.
26+
*
27+
* This license is subject to the following condition:
28+
*
29+
* The above copyright notice and either this complete permission notice or at a
30+
* minimum a reference to the UPL must be included in all copies or substantial
31+
* portions of the Software.
32+
*
33+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
34+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
35+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
36+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
37+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
38+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
39+
* SOFTWARE.
40+
*/
41+
package com.oracle.graal.python.nodes.bytecode;
42+
43+
import com.oracle.graal.python.compiler.CodeUnit;
44+
import com.oracle.graal.python.compiler.OpCodesConstants;
45+
import com.oracle.truffle.api.CompilerDirectives.CompilationFinal;
46+
import com.oracle.truffle.api.frame.Frame;
47+
48+
public final class FrameInfo {
49+
@CompilationFinal PBytecodeRootNode rootNode;
50+
51+
public PBytecodeRootNode getRootNode() {
52+
return rootNode;
53+
}
54+
55+
public int bciToLine(int bci) {
56+
return rootNode.bciToLine(bci);
57+
}
58+
59+
public int getBci(Frame frame) {
60+
if (frame.isInt(rootNode.bcioffset)) {
61+
return frame.getInt(rootNode.bcioffset);
62+
}
63+
return -1;
64+
}
65+
66+
public int getGeneratorStackTop(Frame frame) {
67+
return frame.getInt(rootNode.generatorStackTopOffset);
68+
}
69+
70+
public Object getYieldFrom(Frame generatorFrame) {
71+
int bci = getBci(generatorFrame);
72+
/* Match the `yield from` bytecode pattern and get the object from stack */
73+
if (bci > 3 && rootNode.bytecode[bci - 3] == OpCodesConstants.SEND && rootNode.bytecode[bci - 1] == OpCodesConstants.YIELD_VALUE &&
74+
rootNode.bytecode[bci] == OpCodesConstants.RESUME_YIELD) {
75+
int stackTop = generatorFrame.getInt(rootNode.generatorStackTopOffset);
76+
return generatorFrame.getObject(stackTop);
77+
}
78+
return null;
79+
}
80+
81+
public Object getGeneratorReturnValue(Frame frame) {
82+
return frame.getObject(rootNode.generatorReturnOffset);
83+
}
84+
85+
public int getLineno(Frame frame) {
86+
return bciToLine(getBci(frame));
87+
}
88+
89+
public int getVariableCount() {
90+
CodeUnit code = rootNode.getCodeUnit();
91+
return code.varnames.length + code.cellvars.length + code.freevars.length;
92+
}
93+
94+
public String getVariableName(int slot) {
95+
CodeUnit code = rootNode.getCodeUnit();
96+
if (slot < code.varnames.length) {
97+
return code.varnames[slot];
98+
} else if (slot < code.varnames.length + code.cellvars.length) {
99+
return code.cellvars[slot - code.varnames.length];
100+
} else {
101+
return code.freevars[slot - code.varnames.length - code.cellvars.length];
102+
}
103+
}
104+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ public Object execute(VirtualFrame frame) {
109109
}
110110
if (returnProfile.profile(result == null)) {
111111
// Null result indicates a generator return
112-
PBytecodeRootNode.FrameInfo info = (PBytecodeRootNode.FrameInfo) generatorFrame.getFrameDescriptor().getInfo();
112+
FrameInfo info = (FrameInfo) generatorFrame.getFrameDescriptor().getInfo();
113113
Object returnValue = info.getGeneratorReturnValue(generatorFrame);
114114
if (returnValue != PNone.NONE) {
115115
throw raise.raise(StopIteration, returnValue);

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

Lines changed: 9 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -356,20 +356,20 @@ public final class PBytecodeRootNode extends PRootNode implements BytecodeOSRNod
356356
private final String name;
357357
private boolean pythonInternal;
358358

359-
private final int celloffset;
360-
private final int freeoffset;
361-
private final int stackoffset;
362-
private final int bcioffset;
363-
private final int generatorStackTopOffset;
364-
private final int generatorReturnOffset;
365-
private final int selfIndex;
366-
private final int classcellIndex;
359+
final int celloffset;
360+
final int freeoffset;
361+
final int stackoffset;
362+
final int bcioffset;
363+
final int generatorStackTopOffset;
364+
final int generatorReturnOffset;
365+
final int selfIndex;
366+
final int classcellIndex;
367367

368368
private final CodeUnit co;
369369
private final Source source;
370370
private SourceSection sourceSection;
371371

372-
@CompilationFinal(dimensions = 1) private final byte[] bytecode;
372+
@CompilationFinal(dimensions = 1) final byte[] bytecode;
373373
@CompilationFinal(dimensions = 1) private final Object[] consts;
374374
@CompilationFinal(dimensions = 1) private final long[] longConsts;
375375
@CompilationFinal(dimensions = 1) private final String[] names;
@@ -395,85 +395,6 @@ public boolean isAdoptable() {
395395
}
396396
};
397397

398-
public static final class FrameInfo {
399-
@CompilationFinal PBytecodeRootNode rootNode;
400-
401-
public PBytecodeRootNode getRootNode() {
402-
return rootNode;
403-
}
404-
405-
public int bciToLine(int bci) {
406-
return rootNode.bciToLine(bci);
407-
}
408-
409-
public int getBci(Frame frame) {
410-
if (frame.isInt(rootNode.bcioffset)) {
411-
return frame.getInt(rootNode.bcioffset);
412-
}
413-
return -1;
414-
}
415-
416-
public int getGeneratorStackTop(Frame frame) {
417-
return frame.getInt(rootNode.generatorStackTopOffset);
418-
}
419-
420-
public Object getYieldFrom(Frame generatorFrame) {
421-
int bci = getBci(generatorFrame);
422-
/* Match the `yield from` bytecode pattern and get the object from stack */
423-
if (bci > 3 && rootNode.bytecode[bci - 3] == OpCodesConstants.SEND && rootNode.bytecode[bci - 1] == OpCodesConstants.YIELD_VALUE &&
424-
rootNode.bytecode[bci] == OpCodesConstants.RESUME_YIELD) {
425-
int stackTop = generatorFrame.getInt(rootNode.generatorStackTopOffset);
426-
return generatorFrame.getObject(stackTop);
427-
}
428-
return null;
429-
}
430-
431-
public Object getGeneratorReturnValue(Frame frame) {
432-
return frame.getObject(rootNode.generatorReturnOffset);
433-
}
434-
435-
public int getLineno(Frame frame) {
436-
return bciToLine(getBci(frame));
437-
}
438-
439-
public int getVariableCount() {
440-
CodeUnit code = rootNode.co;
441-
return code.varnames.length + code.cellvars.length + code.freevars.length;
442-
}
443-
444-
public String getVariableName(int slot) {
445-
CodeUnit code = rootNode.co;
446-
if (slot < code.varnames.length) {
447-
return code.varnames[slot];
448-
} else if (slot < code.varnames.length + code.cellvars.length) {
449-
return code.cellvars[slot - code.varnames.length];
450-
} else {
451-
return code.freevars[slot - code.varnames.length - code.cellvars.length];
452-
}
453-
}
454-
455-
@TruffleBoundary
456-
public int findVariable(String name) {
457-
CodeUnit code = rootNode.co;
458-
for (int i = 0; i < code.varnames.length; i++) {
459-
if (name.equals(code.varnames[i])) {
460-
return i;
461-
}
462-
}
463-
for (int i = 0; i < code.cellvars.length; i++) {
464-
if (name.equals(code.cellvars[i])) {
465-
return code.varnames.length + i;
466-
}
467-
}
468-
for (int i = 0; i < code.freevars.length; i++) {
469-
if (name.equals(code.freevars[i])) {
470-
return code.varnames.length + code.cellvars.length + i;
471-
}
472-
}
473-
return -1;
474-
}
475-
}
476-
477398
private static FrameDescriptor makeFrameDescriptor(CodeUnit co) {
478399
int capacity = co.varnames.length + co.cellvars.length + co.freevars.length + co.stacksize + 1;
479400
if (co.isGeneratorOrCoroutine()) {

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/frame/MaterializeFrameNode.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
import com.oracle.graal.python.nodes.ModuleRootNode;
5353
import com.oracle.graal.python.nodes.PRootNode;
5454
import com.oracle.graal.python.nodes.SpecialMethodNames;
55-
import com.oracle.graal.python.nodes.bytecode.PBytecodeRootNode;
55+
import com.oracle.graal.python.nodes.bytecode.FrameInfo;
5656
import com.oracle.graal.python.nodes.frame.MaterializeFrameNodeGen.SyncFrameValuesNodeGen;
5757
import com.oracle.graal.python.nodes.function.ClassBodyRootNode;
5858
import com.oracle.graal.python.nodes.object.GetClassNode;
@@ -170,12 +170,12 @@ static PFrame incompleteFrame(VirtualFrame frame, Node location, boolean markAsE
170170
}
171171

172172
public static boolean isBytecodeFrame(Frame frameToSync) {
173-
return frameToSync.getFrameDescriptor().getInfo() instanceof PBytecodeRootNode.FrameInfo;
173+
return frameToSync.getFrameDescriptor().getInfo() instanceof FrameInfo;
174174
}
175175

176176
private static void processBytecodeFrame(Frame frameToMaterialize, PFrame pyFrame) {
177177
if (isBytecodeFrame(frameToMaterialize)) {
178-
PBytecodeRootNode.FrameInfo info = (PBytecodeRootNode.FrameInfo) frameToMaterialize.getFrameDescriptor().getInfo();
178+
FrameInfo info = (FrameInfo) frameToMaterialize.getFrameDescriptor().getInfo();
179179
pyFrame.setLasti(info.getBci(frameToMaterialize));
180180
pyFrame.setLine(info.getLineno(frameToMaterialize));
181181
pyFrame.setLocation(info.getRootNode());
@@ -617,7 +617,7 @@ static void doGenericDictCached(VirtualFrame frame, PFrame pyFrame, Frame frameT
617617

618618
// The cast is guaranteed by the guard.
619619
PDict localsDict = (PDict) pyFrame.getLocalsDict();
620-
PBytecodeRootNode.FrameInfo info = (PBytecodeRootNode.FrameInfo) cachedFd.getInfo();
620+
FrameInfo info = (FrameInfo) cachedFd.getInfo();
621621
int slotCount = info.getVariableCount();
622622
for (int slot = 0; slot < slotCount; slot++) {
623623
ConditionProfile profile = profiles[slot];
@@ -636,16 +636,16 @@ static void doGenericDict(VirtualFrame frame, PFrame pyFrame, Frame frameToSync,
636636

637637
// The cast is guaranteed by the guard.
638638
PDict localsDict = (PDict) pyFrame.getLocalsDict();
639-
PBytecodeRootNode.FrameInfo info = (PBytecodeRootNode.FrameInfo) frameToSync.getFrameDescriptor().getInfo();
639+
FrameInfo info = (FrameInfo) frameToSync.getFrameDescriptor().getInfo();
640640
int slotCount = info.getVariableCount();
641641
for (int slot = 0; slot < slotCount; slot++) {
642642
ConditionProfile profile = ConditionProfile.getUncached();
643643
syncDict(frame, slot, info, frameToSync, localsDict, lib, hasFrame, updatedStorage, profile);
644644
}
645645
}
646646

647-
private static void syncDict(VirtualFrame frame, int slot, PBytecodeRootNode.FrameInfo info, Frame frameToSync, PDict localsDict, HashingStorageLibrary lib,
648-
ConditionProfile hasFrame, BranchProfile updatedStorage, ConditionProfile profile) {
647+
private static void syncDict(VirtualFrame frame, int slot, FrameInfo info, Frame frameToSync, PDict localsDict, HashingStorageLibrary lib,
648+
ConditionProfile hasFrame, BranchProfile updatedStorage, ConditionProfile profile) {
649649
HashingStorage storage = localsDict.getDictStorage();
650650
String identifier = info.getVariableName(slot);
651651
Object value = frameToSync.getValue(slot);
@@ -675,7 +675,7 @@ protected static boolean isBytecodeFrame(Frame frameToSync) {
675675
}
676676

677677
protected static int variableSlotCount(FrameDescriptor fd) {
678-
PBytecodeRootNode.FrameInfo info = (PBytecodeRootNode.FrameInfo) fd.getInfo();
678+
FrameInfo info = (FrameInfo) fd.getInfo();
679679
return info.getVariableCount();
680680
}
681681

0 commit comments

Comments
 (0)