Skip to content

Commit 0c1791a

Browse files
committed
[GR-38700] Minor Bytecode DSL improvements.
PullRequest: graalpython/4136
2 parents abcdc8d + ba4d71a commit 0c1791a

File tree

2 files changed

+40
-29
lines changed

2 files changed

+40
-29
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/bytecode_dsl/PBytecodeDSLRootNode.java

Lines changed: 14 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -970,17 +970,17 @@ protected byte[] extractCode() {
970970
return MarshalModuleBuiltins.serializeCodeUnit(null, PythonContext.get(this), co);
971971
}
972972

973-
private static Object checkUnboundCell(PCell cell, int index, PBytecodeDSLRootNode rootNode, Node inliningTarget, PRaiseNode raiseNode) {
973+
private static Object checkUnboundCell(PCell cell, int index, BytecodeNode bytecodeNode) {
974974
Object result = cell.getRef();
975975
if (result == null) {
976976
CompilerDirectives.transferToInterpreterAndInvalidate();
977-
CodeUnit codeUnit = rootNode.getCodeUnit();
977+
CodeUnit codeUnit = ((PBytecodeDSLRootNode) bytecodeNode.getRootNode()).getCodeUnit();
978978
if (index < codeUnit.cellvars.length) {
979979
TruffleString localName = codeUnit.cellvars[index];
980-
throw raiseNode.raise(inliningTarget, PythonBuiltinClassType.UnboundLocalError, ErrorMessages.LOCAL_VAR_REFERENCED_BEFORE_ASSIGMENT, localName);
980+
throw PRaiseNode.raiseStatic(bytecodeNode, PythonBuiltinClassType.UnboundLocalError, ErrorMessages.LOCAL_VAR_REFERENCED_BEFORE_ASSIGMENT, localName);
981981
} else {
982982
TruffleString localName = codeUnit.freevars[index - codeUnit.cellvars.length];
983-
throw raiseNode.raise(inliningTarget, PythonBuiltinClassType.NameError, ErrorMessages.UNBOUNDFREEVAR, localName);
983+
throw PRaiseNode.raiseStatic(bytecodeNode, PythonBuiltinClassType.NameError, ErrorMessages.UNBOUNDFREEVAR, localName);
984984
}
985985
}
986986
return result;
@@ -2512,10 +2512,8 @@ public static void doAssertFailed(VirtualFrame frame, Object assertionMessage,
25122512
public static final class LoadCell {
25132513
@Specialization
25142514
public static Object doLoadCell(int index, PCell cell,
2515-
@Bind PBytecodeDSLRootNode rootNode,
2516-
@Bind Node inliningTarget,
2517-
@Cached PRaiseNode raiseNode) {
2518-
return checkUnboundCell(cell, index, rootNode, inliningTarget, raiseNode);
2515+
@Bind BytecodeNode bytecodeNode) {
2516+
return checkUnboundCell(cell, index, bytecodeNode);
25192517
}
25202518
}
25212519

@@ -2529,11 +2527,10 @@ public static Object doLoadCell(int index, PCell cell,
25292527
public static final class LoadFromDictOrCell {
25302528
@Specialization
25312529
public static Object doLoadCell(VirtualFrame frame, int index, Object locals, PCell cell,
2532-
@Bind PBytecodeDSLRootNode rootNode,
2530+
@Bind BytecodeNode bytecodeNode,
25332531
@Bind Node inliningTarget,
2534-
@Cached ReadFromLocalsNode readLocalsNode,
2535-
@Cached PRaiseNode raiseNode) {
2536-
CodeUnit co = rootNode.getCodeUnit();
2532+
@Cached ReadFromLocalsNode readLocalsNode) {
2533+
CodeUnit co = ((PBytecodeDSLRootNode) bytecodeNode.getRootNode()).getCodeUnit();
25372534
TruffleString name;
25382535
if (index < co.cellvars.length) {
25392536
name = co.cellvars[index];
@@ -2544,7 +2541,7 @@ public static Object doLoadCell(VirtualFrame frame, int index, Object locals, PC
25442541
if (value != PNone.NO_VALUE) {
25452542
return value;
25462543
} else {
2547-
return checkUnboundCell(cell, index, rootNode, inliningTarget, raiseNode);
2544+
return checkUnboundCell(cell, index, bytecodeNode);
25482545
}
25492546
}
25502547
}
@@ -2609,10 +2606,8 @@ public static void doCreateCells(VirtualFrame frame, LocalRangeAccessor locals,
26092606
public static final class ClearCell {
26102607
@Specialization
26112608
public static void doClearCell(int index, PCell cell,
2612-
@Bind PBytecodeDSLRootNode rootNode,
2613-
@Bind Node inliningTarget,
2614-
@Cached PRaiseNode raiseNode) {
2615-
checkUnboundCell(cell, index, rootNode, inliningTarget, raiseNode);
2609+
@Bind BytecodeNode bytecodeNode) {
2610+
checkUnboundCell(cell, index, bytecodeNode);
26162611
cell.clearRef();
26172612
}
26182613
}
@@ -3591,16 +3586,6 @@ private static PException raiseUnbound(BytecodeNode bytecodeNode, int index) {
35913586
throw PRaiseNode.raiseStatic(bytecodeNode, PythonBuiltinClassType.UnboundLocalError, ErrorMessages.LOCAL_VAR_REFERENCED_BEFORE_ASSIGMENT, localName);
35923587
}
35933588

3594-
@Operation(storeBytecodeIndex = true)
3595-
public static final class RaiseNotImplementedError {
3596-
@Specialization
3597-
public static void doRaise(VirtualFrame frame, TruffleString name,
3598-
@Bind BytecodeNode node) {
3599-
throw PRaiseNode.raiseStatic(node, PythonBuiltinClassType.NotImplementedError, name);
3600-
3601-
}
3602-
}
3603-
36043589
/**
36053590
* Creates a TypeVar, TypeVarTuple or ParamSpec object. The constant argument determines
36063591
* (defined in {@link MakeTypeParamKind}) which and whether it will need to pop bound or
@@ -3642,8 +3627,8 @@ public static Object doObject(int kind, TruffleString name, Object boundOrConstr
36423627
public static final class MakeTypeAliasType {
36433628
@Specialization
36443629
public static PTypeAliasType doObject(TruffleString name, Object typeParams, Object computeValue,
3645-
@Bind PBytecodeDSLRootNode rootNode) {
3646-
PythonLanguage language = PythonLanguage.get(rootNode);
3630+
@Bind BytecodeNode bytecodeNode) {
3631+
PythonLanguage language = PythonLanguage.get(bytecodeNode);
36473632
// bytecode compiler should ensure that typeParams are either PTuple or null
36483633
return PFactory.createTypeAliasType(language, name, (PTuple) typeParams, computeValue, null, null);
36493634
}

mx.graalpython/mx_graalpython.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1993,6 +1993,7 @@ def python_style_checks(args):
19931993
"Check (and fix where possible) copyrights, eclipse formatting, and spotbugs"
19941994
warn_about_old_hardcoded_version()
19951995
python_run_mx_filetests(args)
1996+
check_unused_operations()
19961997
python_checkcopyrights(["--fix"] if "--fix" in args else [])
19971998
if not os.environ.get("ECLIPSE_EXE"):
19981999
find_eclipse()
@@ -2054,6 +2055,31 @@ def python_run_mx_filetests(_):
20542055
mx.run([sys.executable, test, "-v"])
20552056

20562057

2058+
def check_unused_operations():
2059+
root_node = "graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/bytecode_dsl/PBytecodeDSLRootNode.java"
2060+
in_operation = False
2061+
operations = []
2062+
with open(os.path.join(SUITE.dir, root_node)) as f:
2063+
while line := f.readline():
2064+
if not in_operation and ('@Operation\n' in line or '@Operation(' in line):
2065+
in_operation = True
2066+
elif in_operation:
2067+
if names := re.findall(r'public static final class (\S+)', line):
2068+
in_operation = False
2069+
operations += names
2070+
compiler = "graalpython/com.oracle.graal.python/src/com/oracle/graal/python/compiler/bytecode_dsl/RootNodeCompiler.java"
2071+
with open(os.path.join(SUITE.dir, compiler)) as f:
2072+
contents = f.read().lower()
2073+
result = True
2074+
for n in operations:
2075+
if not n.lower() in contents:
2076+
mx.log_error(f"ERROR: unused @Operation {n}")
2077+
result = False
2078+
if not result:
2079+
mx.abort("Found unused @Operation, see above")
2080+
2081+
2082+
20572083
def _python_checkpatchfiles():
20582084
env = os.environ.copy()
20592085
mx_dir = Path(__file__).parent

0 commit comments

Comments
 (0)