Skip to content

Commit f3f5ec4

Browse files
committed
Allow dis to show quickened bytecode
1 parent 0b89f88 commit f3f5ec4

File tree

3 files changed

+28
-11
lines changed

3 files changed

+28
-11
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/GraalPythonModuleBuiltins.java

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
import org.graalvm.polyglot.io.ByteSequence;
7272

7373
import com.oracle.graal.python.PythonLanguage;
74+
import com.oracle.graal.python.annotations.ArgumentClinic;
7475
import com.oracle.graal.python.builtins.Builtin;
7576
import com.oracle.graal.python.builtins.CoreFunctions;
7677
import com.oracle.graal.python.builtins.Python3Core;
@@ -112,8 +113,10 @@
112113
import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode;
113114
import com.oracle.graal.python.nodes.function.PythonBuiltinNode;
114115
import com.oracle.graal.python.nodes.function.builtins.PythonBinaryBuiltinNode;
116+
import com.oracle.graal.python.nodes.function.builtins.PythonBinaryClinicBuiltinNode;
115117
import com.oracle.graal.python.nodes.function.builtins.PythonTernaryBuiltinNode;
116118
import com.oracle.graal.python.nodes.function.builtins.PythonUnaryBuiltinNode;
119+
import com.oracle.graal.python.nodes.function.builtins.clinic.ArgumentClinicProvider;
117120
import com.oracle.graal.python.nodes.object.GetClassNode;
118121
import com.oracle.graal.python.nodes.statement.AbstractImportNode;
119122
import com.oracle.graal.python.nodes.subscript.GetItemNode;
@@ -650,33 +653,40 @@ protected static boolean isType(Object obj, Env env, InteropLibrary lib) {
650653

651654
}
652655

653-
@Builtin(name = "dis", minNumOfPositionalArgs = 1, doc = "Helper to disassemble code objects if running with the bytecode interpreter")
656+
@Builtin(name = "dis", minNumOfPositionalArgs = 1, parameterNames = {"obj", "quickened"}, doc = "Helper to disassemble code objects if running with the bytecode interpreter")
657+
@ArgumentClinic(name = "quickened", conversion = ArgumentClinic.ClinicConversion.Boolean, defaultValue = "false")
654658
@GenerateNodeFactory
655-
public abstract static class BCIDisNode extends PythonUnaryBuiltinNode {
659+
public abstract static class BCIDisNode extends PythonBinaryClinicBuiltinNode {
656660
@Specialization
657-
Object doMethod(PMethod method) {
661+
Object doMethod(PMethod method, boolean quickened) {
658662
final Object function = method.getFunction();
659663
if (function instanceof PFunction) {
660-
return doFunction((PFunction) function);
664+
return doFunction((PFunction) function, quickened);
661665
}
662666
return PNone.NONE;
663667
}
664668

665669
@Specialization
666-
Object doFunction(PFunction function) {
667-
return doCode(function.getCode());
670+
Object doFunction(PFunction function, boolean quickened) {
671+
return doCode(function.getCode(), quickened);
668672
}
669673

670674
@Specialization
671675
@TruffleBoundary
672-
Object doCode(PCode code) {
673-
return toTruffleStringUncached(code.toDisassembledString());
676+
Object doCode(PCode code, boolean quickened) {
677+
return toTruffleStringUncached(code.toDisassembledString(quickened));
674678
}
675679

676-
@Specialization(guards = {"!isCode(value)", "!isPFunction(value)", "!isMethod(value)"})
677-
Object doObject(@SuppressWarnings("unused") Object value) {
680+
@Fallback
681+
@SuppressWarnings("unused")
682+
Object doObject(Object value, Object quickened) {
678683
return PNone.NONE;
679684
}
685+
686+
@Override
687+
protected ArgumentClinicProvider getArgumentClinic() {
688+
return GraalPythonModuleBuiltinsClinicProviders.BCIDisNodeClinicProviderGen.INSTANCE;
689+
}
680690
}
681691

682692
@Builtin(name = "super", minNumOfPositionalArgs = 1, doc = "Returns HostAdapter instance of the object or None")

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/code/PCode.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -759,10 +759,13 @@ public String toString() {
759759
}
760760

761761
@TruffleBoundary
762-
public String toDisassembledString() {
762+
public String toDisassembledString(boolean quickened) {
763763
final RootNode rootNode = getRootCallTarget().getRootNode();
764764
if (rootNode instanceof PBytecodeRootNode) {
765765
CodeUnit code = ((PBytecodeRootNode) rootNode).getCodeUnit();
766+
if (quickened) {
767+
return code.toString(((PBytecodeRootNode) rootNode).getBytecode());
768+
}
766769
return code.toString();
767770
}
768771
return J_EMPTY_STRING;

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -631,6 +631,10 @@ public CodeUnit getCodeUnit() {
631631
return co;
632632
}
633633

634+
public byte[] getBytecode() {
635+
return bytecode;
636+
}
637+
634638
@FunctionalInterface
635639
private interface NodeSupplier<T> {
636640

0 commit comments

Comments
 (0)