Skip to content

Commit 9340707

Browse files
committed
Improve Bytecode DSL code units dumping
1 parent 9592e53 commit 9340707

File tree

4 files changed

+58
-38
lines changed

4 files changed

+58
-38
lines changed

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -685,10 +685,8 @@ public String toString() {
685685
public String toDisassembledString(boolean quickened) {
686686
RootNode rootNode = getRootCallTarget().getRootNode();
687687
if (PythonOptions.ENABLE_BYTECODE_DSL_INTERPRETER && rootNode instanceof PBytecodeDSLRootNode dslRoot) {
688-
return dslRoot.dump();
689-
}
690-
691-
if (rootNode instanceof PBytecodeGeneratorRootNode r) {
688+
return dslRoot.getCodeUnit().toString(quickened);
689+
} else if (rootNode instanceof PBytecodeGeneratorRootNode r) {
692690
rootNode = r.getBytecodeRootNode();
693691
} else if (rootNode instanceof PBytecodeGeneratorFunctionRootNode r) {
694692
rootNode = r.getBytecodeRootNode();

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

Lines changed: 2 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -125,34 +125,11 @@ public int bciToColumn(int bci) {
125125
}
126126

127127
@Override
128-
public String toString() {
129-
return toString(false);
130-
}
131-
132-
public String toString(boolean quickened) {
133-
StringBuilder sb = new StringBuilder();
134-
135-
HashMap<Integer, String[]> lines = new HashMap<>();
136-
137-
sb.append("Disassembly of ").append(qualname).append(":\n");
138-
139-
List<String> flagNames = new ArrayList<>();
140-
if (isGenerator()) {
141-
flagNames.add("CO_GENERATOR");
142-
}
143-
if (isCoroutine()) {
144-
flagNames.add("CO_COROUTINE");
145-
}
146-
if (isAsyncGenerator()) {
147-
flagNames.add("CO_ASYNC_GENERATOR");
148-
}
149-
if (!flagNames.isEmpty()) {
150-
sb.append("Flags: ").append(String.join(" | ", flagNames)).append("\n");
151-
}
152-
128+
protected void dumpBytecode(StringBuilder sb, boolean quickened) {
153129
int bci = 0;
154130
int oparg = 0;
155131
SourceMap map = getSourceMap();
132+
HashMap<Integer, String[]> lines = new HashMap<>();
156133
while (bci < code.length) {
157134
int bcBCI = bci;
158135
OpCodes opcode = OpCodes.fromOpCode(code[bci++]);
@@ -393,15 +370,6 @@ public String toString(boolean quickened) {
393370
sb.append('\n');
394371
}
395372
}
396-
397-
for (Object c : constants) {
398-
if (c instanceof CodeUnit) {
399-
sb.append('\n');
400-
sb.append(c);
401-
}
402-
}
403-
404-
return sb.toString();
405373
}
406374

407375
private static String collectionKindToString(int oparg) {

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

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@
4040
*/
4141
package com.oracle.graal.python.compiler;
4242

43+
import java.util.ArrayList;
4344
import java.util.Arrays;
45+
import java.util.List;
4446

4547
import com.oracle.graal.python.builtins.objects.code.PCode;
4648
import com.oracle.graal.python.builtins.objects.function.Signature;
@@ -167,4 +169,46 @@ public final Signature computeSignature() {
167169
parameterNames,
168170
kwOnlyNames);
169171
}
172+
173+
@Override
174+
public String toString() {
175+
return toString(false);
176+
}
177+
178+
protected abstract void dumpBytecode(StringBuilder sb, boolean optimized);
179+
180+
/**
181+
* @param optimized Whether to print the initial state of the bytecode or current state, if
182+
* available, where some instructions may be transformed, e.g., quickened.
183+
*/
184+
public String toString(boolean optimized) {
185+
StringBuilder sb = new StringBuilder();
186+
187+
sb.append("Disassembly of ").append(qualname).append(":\n");
188+
189+
List<String> flagNames = new ArrayList<>();
190+
if (isGenerator()) {
191+
flagNames.add("CO_GENERATOR");
192+
}
193+
if (isCoroutine()) {
194+
flagNames.add("CO_COROUTINE");
195+
}
196+
if (isAsyncGenerator()) {
197+
flagNames.add("CO_ASYNC_GENERATOR");
198+
}
199+
if (!flagNames.isEmpty()) {
200+
sb.append("Flags: ").append(String.join(" | ", flagNames)).append("\n");
201+
}
202+
203+
dumpBytecode(sb, optimized);
204+
205+
for (Object c : constants) {
206+
if (c instanceof CodeUnit cd) {
207+
sb.append('\n');
208+
sb.append(cd.toString(optimized));
209+
}
210+
}
211+
212+
return sb.toString();
213+
}
170214
}

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,4 +129,14 @@ public TruffleString getDocstring() {
129129
return null;
130130
}
131131

132+
@Override
133+
protected void dumpBytecode(StringBuilder sb, boolean optimized) {
134+
for (int i = 0; i < nodes.count(); i++) {
135+
if (i != 0) {
136+
sb.append('\n');
137+
}
138+
sb.append(nodes.getNode(i).dump());
139+
sb.append('\n'); // dump does not print newline at the end
140+
}
141+
}
132142
}

0 commit comments

Comments
 (0)