Skip to content

Commit

Permalink
Continue on previous work to improve offset tracking
Browse files Browse the repository at this point in the history
  • Loading branch information
Kroppeb committed Jun 7, 2024
1 parent cbc4296 commit 78efdc7
Show file tree
Hide file tree
Showing 82 changed files with 660 additions and 292 deletions.
18 changes: 7 additions & 11 deletions src/org/jetbrains/java/decompiler/code/cfg/BasicBlock.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public class BasicBlock implements IGraphNode {

public final int id;
public int mark = 0;
public Instruction oldGotoInstruction = null;

// *****************************************************************************
// private fields
Expand All @@ -27,7 +28,6 @@ public class BasicBlock implements IGraphNode {

private final List<BasicBlock> preds = new ArrayList<>();
private final List<BasicBlock> succs = new ArrayList<>();
private final List<Integer> instrOldOffsets = new ArrayList<>();
private final List<BasicBlock> predExceptions = new ArrayList<>();
private final List<BasicBlock> succExceptions = new ArrayList<>();

Expand All @@ -53,8 +53,8 @@ public Instruction getLastInstruction() {
}

public Integer getOldOffset(int index) {
if(index < instrOldOffsets.size()) {
return instrOldOffsets.get(index);
if(index < seq.length()) {
return getInstruction(index).startOffset;
} else {
return -1;
}
Expand Down Expand Up @@ -153,7 +153,7 @@ BasicBlock cloneBlock(int id) {
BasicBlock block = new BasicBlock(id);

block.setSeq(this.seq.clone());
block.instrOldOffsets.addAll(this.instrOldOffsets);
block.oldGotoInstruction = this.oldGotoInstruction;

return block;
}
Expand All @@ -162,10 +162,6 @@ BasicBlock cloneBlock(int id) {
// getter and setter methods
// *****************************************************************************

public List<Integer> getInstrOldOffsets() {
return instrOldOffsets;
}

/**
* Only used for printing debugging strings
*/
Expand Down Expand Up @@ -208,14 +204,14 @@ public int getStartInstruction() {
if (seq.isEmpty()) {
return 0;
}
return instrOldOffsets.get(0);
return seq.getInstr(0).startOffset;
}

public int getEndInstruction() {
if (seq.isEmpty()) {
return 0;
}
int end = seq.getLastInstr().length;
return end + instrOldOffsets.get(size() -1);
Instruction last = seq.getLastInstr();
return last.startOffset + last.length;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,6 @@ private Map<Integer, BasicBlock> createBasicBlocks(
this.blocks = new VBStyleCollection<>();

InstructionSequence currseq = null;
List<Integer> lstOffs = null;

int len = startblock.length;
short counter = 0;
Expand All @@ -294,7 +293,6 @@ private Map<Integer, BasicBlock> createBasicBlocks(
currentBlock = new BasicBlock(++counter);

currseq = currentBlock.getSeq();
lstOffs = currentBlock.getInstrOldOffsets();

// index: $i, key: $i+1, value BasicBlock(id = $i + 1)
this.blocks.addWithKey(currentBlock, currentBlock.id);
Expand All @@ -304,10 +302,9 @@ private Map<Integer, BasicBlock> createBasicBlocks(
mapInstrBlocks.put(i, currentBlock);

// can't throw npe cause startblock[0] == 1 is always true
assert currseq != null && lstOffs != null;
assert currseq != null;
Instruction instr = instrSeq.getInstr(i);
currseq.addInstruction(instr);
lstOffs.add(instr.startOffset);
}

this.first = this.blocks.get(0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ public static void removeGotos(ControlFlowGraph graph) {
}

block.getSeq().removeLast();
block.oldGotoInstruction = instr;
}
}

Expand Down Expand Up @@ -445,6 +446,7 @@ public static void extendSynchronizedRangeToMonitorexit(ControlFlowGraph graph)
}

// copy instructions (handler successor block)
handlerBlock.oldGotoInstruction = null; // clear old goto instruction information
InstructionSequence handlerSeq = handlerBlock.getSeq();
for(int counter = 0; counter < handler_monitorexit_index; counter++) {
handlerSeq.addInstruction(succHandlerSeq.getInstr(0));
Expand Down Expand Up @@ -673,7 +675,6 @@ public static void mergeBasicBlocks(ControlFlowGraph graph) {

if (sameRanges) {
seq.addSequence(next.getSeq());
block.getInstrOldOffsets().addAll(next.getInstrOldOffsets());
next.getSeq().clear();

removeEmptyBlock(graph, next, true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -815,12 +815,10 @@ public static boolean endsWithSemicolon(Exprent expr) {
}

private static void addDeletedGotoInstructionMapping(Statement stat, TextBuffer buffer) {
if (stat instanceof BasicBlockStatement) {
BasicBlock block = ((BasicBlockStatement)stat).getBlock();
List<Integer> offsets = block.getInstrOldOffsets();
if (!offsets.isEmpty() &&
offsets.size() > block.getSeq().length()) { // some instructions have been deleted, but we still have offsets
buffer.addBytecodeMapping(offsets.get(offsets.size() - 1)); // add the last offset
if (stat instanceof BasicBlockStatement blockStat) {
BasicBlock block = blockStat.getBlock();
if (block.oldGotoInstruction != null) {
buffer.addBytecodeMapping(block.oldGotoInstruction.startOffset, block.oldGotoInstruction.length);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -795,7 +795,6 @@ private boolean compareBasicBlocksEx(ControlFlowGraph graph,
List<int[]> lstStoreVars) {
InstructionSequence seqPattern = pattern.getSeq();
InstructionSequence seqSample = sample.getSeq();
List<Integer> instrOldOffsetsSample = sample.getInstrOldOffsets();

if (type != 0) {
seqPattern = seqPattern.clone();
Expand Down Expand Up @@ -833,19 +832,13 @@ private boolean compareBasicBlocksEx(ControlFlowGraph graph,

if (seqPattern.length() < seqSample.length()) { // split in two blocks
InstructionSequence seq = new InstructionSequence();
LinkedList<Integer> oldOffsets = new LinkedList<>();
for (int i = seqSample.length() - 1; i >= seqPattern.length(); i--) {
seq.addInstruction(0, seqSample.getInstr(i));
oldOffsets.addFirst(sample.getOldOffset(i));
seqSample.removeInstruction(i);
if (i < instrOldOffsetsSample.size()) {
instrOldOffsetsSample.remove(i);
}
}

BasicBlock newblock = new BasicBlock(++graph.last_id);
newblock.setSeq(seq);
newblock.getInstrOldOffsets().addAll(oldOffsets);

List<BasicBlock> lstTemp = new ArrayList<>(sample.getSuccs());

Expand Down Expand Up @@ -1027,30 +1020,25 @@ private static void deleteArea(ControlFlowGraph graph, Area area) {

private static void removeExceptionInstructionsEx(BasicBlock block, int blocktype, int finallytype) {
InstructionSequence seq = block.getSeq();
List<Integer> instrOldOffsets = block.getInstrOldOffsets();

if (finallytype == 3) { // empty finally handler
for (int i = seq.length() - 1; i >= 0; i--) {
seq.removeInstruction(i);
instrOldOffsets.remove(i);
}
} else {
if ((blocktype & 1) > 0) { // first
if (finallytype == 2 || finallytype == 1) { // astore or pop
seq.removeInstruction(0);
instrOldOffsets.remove(0);
}
}

if ((blocktype & 2) > 0) { // last
if (finallytype == 2 || finallytype == 0) {
seq.removeLast();
instrOldOffsets.remove(instrOldOffsets.size() - 1);
}

if (finallytype == 2) { // astore
seq.removeLast();
instrOldOffsets.remove(instrOldOffsets.size() - 1);
}
}
}
Expand Down
13 changes: 13 additions & 0 deletions src/org/jetbrains/java/decompiler/util/TextBuffer.java
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,19 @@ public void addBytecodeMapping(int bytecodeOffset) {
myBytecodeOffsetMapping.putIfAbsent(new BytecodeMappingKey(bytecodeOffset, null, null), myStringBuilder.length());
}

public void addBytecodeMapping(int bytecodeOffset, int length) {
if (myDebugTrace != null) {
myDebugTrace.myPreventDeletion = true;
}

for (int i = 0; i < length; i++) {
myBytecodeOffsetMapping.putIfAbsent(
new BytecodeMappingKey(bytecodeOffset + i, null, null),
myStringBuilder.length()
);
}
}

public void addStartBytecodeMapping(int bytecodeOffset) {
if (myDebugTrace != null) {
myDebugTrace.myPreventDeletion = true;
Expand Down
2 changes: 1 addition & 1 deletion testData/results/JsHurt.dec
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ class 'JsHurt' {
method 'main ([Ljava/lang/String;)V' {
0 2
1 2
5 10
6 10
7 10
8 10
9 10
a 10
b 10
c 5
d 5
e 5
Expand Down
4 changes: 4 additions & 0 deletions testData/results/TestEclipseSwitchEnum.dec
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ class 'pkg/TestEclipseSwitchEnum' {
2a 10
2b 10
2c 11
2d 11
2e 11
2f 13
30 13
31 13
Expand All @@ -69,6 +71,8 @@ class 'pkg/TestEclipseSwitchEnum' {
35 13
36 13
37 14
38 14
39 14
3a 16
3b 16
3c 16
Expand Down
Loading

0 comments on commit 78efdc7

Please sign in to comment.