Skip to content

Commit 944d9b7

Browse files
authored
7903546: jdec,jdis: Enhance decompiler outputs flexibility and readability. (openjdk#64)
1 parent fa0906b commit 944d9b7

File tree

4 files changed

+70
-30
lines changed

4 files changed

+70
-30
lines changed

build/productinfo.properties

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (c) 2014, 2022, Oracle and/or its affiliates. All rights reserved.
1+
# Copyright (c) 2014, 2023, Oracle and/or its affiliates. All rights reserved.
22
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
33
#
44
# This code is free software; you can redistribute it and/or modify it
@@ -27,5 +27,5 @@ PRODUCT_NAME = asmtools
2727
PRODUCT_JAR_NAME = asmtools.jar
2828
PRODUCT_VERSION = 8.0
2929
PRODUCT_MILESTONE = ea
30-
PRODUCT_BUILDNUMBER = 05
30+
PRODUCT_BUILDNUMBER = 06
3131
PRODUCT_NAME_LONG = Java Assembler Tools

src/org/openjdk/asmtools/common/outputs/TextOutput.java

Lines changed: 45 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,16 @@
2929
import java.io.IOException;
3030
import java.util.ArrayList;
3131
import java.util.Optional;
32+
import java.util.function.BiFunction;
3233
import java.util.stream.Collectors;
3334

35+
import static java.lang.String.format;
36+
3437
public class TextOutput extends NamedToolOutput {
38+
39+
// decoration for text output
40+
private BiFunction<String, String, String> namedSourceOrnament = null;
41+
3542
private final ArrayList<NamedSource> outputs = new ArrayList<>();
3643
private StringBuilder currentClass;
3744

@@ -46,7 +53,7 @@ public String toString() {
4653

4754
@Override
4855
public DataOutputStream getDataOutputStream() throws FileNotFoundException {
49-
return null; //If you are here, you probably wanted ToolOutput.ByteOutput for assmbled binary output
56+
return null; //If you are here, you probably wanted ToolOutput.ByteOutput for assembled binary output
5057
}
5158

5259
@Override
@@ -56,14 +63,18 @@ public void startClass(String fullyQualifiedName, Optional<String> suffix, Envir
5663
}
5764

5865
@Override
59-
public void finishClass(String fqn) throws IOException {
60-
if (!getCurrentClassName().equals(fqn)) {
61-
throw new RuntimeException("Ended different class - " + fqn + " - then started - " + super.fqn);
66+
public void finishClass(String fullyQualifiedName) throws IOException {
67+
if (!getCurrentClassName().equals(fullyQualifiedName)) {
68+
throw new RuntimeException("Ended different class - " + fullyQualifiedName + " - then started - " + super.fqn);
6269
}
63-
outputs.add(new NamedSource(fqn, currentClass.toString()));
70+
outputs.add(new NamedSource(fullyQualifiedName, currentClass.toString(), namedSourceOrnament));
6471
super.fqn = null;
6572
currentClass = null;
73+
}
6674

75+
public TextOutput setNamedSourceOrnament(BiFunction<String, String, String> namedSourceOrnament) {
76+
this.namedSourceOrnament = namedSourceOrnament;
77+
return this;
6778
}
6879

6980
@Override
@@ -83,20 +94,42 @@ public void prints(char line) {
8394

8495
@Override
8596
public void flush() {
86-
8797
}
8898

8999
public class NamedSource {
90-
private final String fqn;
100+
// decoration for text output
101+
private BiFunction<String, String, String> ornament = (fname, body) ->
102+
format(
103+
"""
104+
/**
105+
%s
106+
**/
107+
%s
108+
/**
109+
%s
110+
**/
111+
""", fname, body, fname);
112+
private final String fullyQualifiedName;
91113
private final String body;
92114

93-
public NamedSource(String fqn, String body) {
94-
this.fqn = fqn;
115+
public NamedSource(String fullyQualifiedName, String body) {
116+
this.fullyQualifiedName = fullyQualifiedName;
95117
this.body = body;
96118
}
97119

98-
public String getFqn() {
99-
return fqn;
120+
public NamedSource(String fullyQualifiedName, String body, BiFunction<String, String, String> ornament) {
121+
this.fullyQualifiedName = fullyQualifiedName;
122+
this.body = body;
123+
this.ornament = ornament;
124+
}
125+
126+
public NamedSource setOrnament(BiFunction<String, String, String> ornament) {
127+
this.ornament = ornament;
128+
return this;
129+
}
130+
131+
public String getFullyQualifiedName() {
132+
return fullyQualifiedName;
100133
}
101134

102135
public String getBody() {
@@ -105,7 +138,7 @@ public String getBody() {
105138

106139
@Override
107140
public String toString() {
108-
return "/**********\n" + fqn + "\n**********/\n" + body + "\n/*end of " + fqn + "*/";
141+
return ornament == null ? body : ornament.apply(fullyQualifiedName, body);
109142
}
110143
}
111144
}

src/org/openjdk/asmtools/jdis/InstructionAttr.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1996, 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1996, 2023, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -213,13 +213,18 @@ public boolean printStackMap(int shift) {
213213
return false;
214214
}
215215
boolean printed = false;
216+
int mapShift = getCommentOffset() - STACKMAP_TYPE_PLACEHOLDER_LENGTH - getIndentStep();
216217
if (stackMapEntry.stackFrameType != null) {
217218
printPadRight(Opcode.opc_stack_frame_type.parseKey(), STACKMAP_TYPE_PLACEHOLDER_LENGTH + 1);
218-
println(stackMapEntry.stackFrameType.tagName() + ";");
219+
if( printCPIndex && !skipComments ) {
220+
print(PadRight(stackMapEntry.stackFrameType.tagName() + ";", mapShift)).
221+
println(" // frame_type " + stackMapEntry.stackFrameTypeValue);
222+
} else {
223+
println(stackMapEntry.stackFrameType.tagName() + ";");
224+
}
219225
printed = true;
220226
}
221227
int[] map = stackMapEntry.lockMap;
222-
int mapShift = getCommentOffset() - STACKMAP_TYPE_PLACEHOLDER_LENGTH - getIndentStep();
223228
if ((map != null) && (map.length > 0)) {
224229
Pair<String, String> line = getMapListAsString(map);
225230
if (stackMapEntry.type == STACKMAPTABLE) { // StackMapTable exists

src/org/openjdk/asmtools/jdis/StackMapData.java

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1996, 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1996, 2023, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -37,6 +37,8 @@
3737
class StackMapData extends MemberData<CodeData> {
3838
EAttributeType type;
3939
StackMap.FrameType stackFrameType = null;
40+
// stack frame type value
41+
int stackFrameTypeValue;
4042
int frame_pc;
4143
int offset;
4244
int[] lockMap;
@@ -61,52 +63,52 @@ public StackMapData(EAttributeType type, boolean firstStackMap, int prevFrame_pc
6163
environment.traceln(" stack_map_entry:pc=%d numloc=%s numstack=%s",
6264
frame_pc, mapToHexString(lockMap), mapToHexString(stackMap));
6365
} else { // if (type == EDataType.STACKMAPTABLE)
64-
int ft_val = in.readUnsignedByte();
65-
StackMap.FrameType frame_type = StackMap.stackMapFrameType(ft_val);
66+
stackFrameTypeValue = in.readUnsignedByte();
67+
StackMap.FrameType frame_type = StackMap.stackMapFrameType(stackFrameTypeValue);
6668
switch (frame_type) {
6769
case SAME_FRAME -> {
6870
// verificationType is same_frame;
69-
offset = ft_val;
70-
environment.traceln(" same_frame=%d", ft_val);
71+
offset = stackFrameTypeValue;
72+
environment.traceln(" same_frame=%d", stackFrameTypeValue);
7173
}
7274
case SAME_FRAME_EX -> {
7375
// verificationType is same_frame_extended;
7476
offset = in.readUnsignedShort();
75-
environment.traceln(" same_frame_extended=%d, offset=%d", ft_val, offset);
77+
environment.traceln(" same_frame_extended=%d, offset=%d", stackFrameTypeValue, offset);
7678
}
7779
case SAME_LOCALS_1_STACK_ITEM_FRAME -> {
7880
// verificationType is same_locals_1_stack_item_frame
79-
offset = ft_val - 64;
81+
offset = stackFrameTypeValue - 64;
8082
stackMap = readMapElements(in, 1);
8183
environment.traceln(" same_locals_1_stack_item_frame=%d, offset=%d, numstack=%s",
82-
ft_val, offset, mapToHexString(stackMap));
84+
stackFrameTypeValue, offset, mapToHexString(stackMap));
8385
}
8486
case SAME_LOCALS_1_STACK_ITEM_EXTENDED_FRAME -> {
8587
// verificationType is same_locals_1_stack_item_frame_extended
8688
offset = in.readUnsignedShort();
8789
stackMap = readMapElements(in, 1);
8890
environment.traceln(" same_locals_1_stack_item_frame_extended=%d, offset=%d, numstack=%s",
89-
ft_val, offset, mapToHexString(stackMap));
91+
stackFrameTypeValue, offset, mapToHexString(stackMap));
9092
}
9193
case CHOP_1_FRAME, CHOP_2_FRAME, CHOP_3_FRAME -> {
9294
// verificationType is chop_frame
9395
offset = in.readUnsignedShort();
94-
environment.traceln(" chop_frame=%d offset=%d", ft_val, offset);
96+
environment.traceln(" chop_frame=%d offset=%d", stackFrameTypeValue, offset);
9597
}
9698
case APPEND_FRAME -> {
9799
// verificationType is append_frame
98100
offset = in.readUnsignedShort();
99-
lockMap = readMapElements(in, ft_val - 251);
101+
lockMap = readMapElements(in, stackFrameTypeValue - 251);
100102
environment.traceln(" append_frame=%d offset=%d numlock=%s",
101-
ft_val, offset, mapToHexString(lockMap));
103+
stackFrameTypeValue, offset, mapToHexString(lockMap));
102104
}
103105
case FULL_FRAME -> {
104106
// verificationType is full_frame
105107
offset = in.readUnsignedShort();
106108
lockMap = readMap(in);
107109
stackMap = readMap(in);
108110
environment.traceln(" full_frame=%d offset=%d numloc=%s numstack=%s",
109-
ft_val, offset, mapToHexString(lockMap), mapToHexString(stackMap));
111+
stackFrameTypeValue, offset, mapToHexString(lockMap), mapToHexString(stackMap));
110112
}
111113
default -> environment.traceln("incorrect frame_type argument");
112114
}

0 commit comments

Comments
 (0)