Skip to content

Commit

Permalink
feat(objectionary#750): consider only join nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
volodya-lombrozo committed Nov 7, 2024
1 parent 53acd66 commit 729040f
Showing 1 changed file with 34 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,10 @@
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import org.objectweb.asm.Label;
Expand Down Expand Up @@ -99,14 +97,14 @@ List<BytecodeFrame> frames() {
final BytecodeInstruction instr = (BytecodeInstruction) entry;
final Entry updated;
if (res.containsKey(index)) {
updated = current.join(res.get(index));
updated = current.append(res.get(index));
} else {
updated = Entry.fromInstruction(index, instr, current);
}
if (instr.isIf()) {
worklist.push(updated.copy(this.index(instr.jumps().get(0))));
worklist.push(updated.join(this.index(instr.jumps().get(0))));
} else if (instr.isJump()) {
worklist.push(updated.copy(this.index(instr.jumps().get(0))));
worklist.push(updated.join(this.index(instr.jumps().get(0))));
res.put(index, updated);
break;
} else if (instr.isReturn() || instr.isThrow()) {
Expand All @@ -117,11 +115,17 @@ List<BytecodeFrame> frames() {
current = updated;
} else {
res.put(index, current);
current = current.copy(index);
}
}
}
this.logEntires(res);
return this.computeFrames(new ArrayList<>(res.values()));
return this.computeFrames(
res.values()
.stream()
.filter(Entry::joined)
.collect(Collectors.toList())
);
}

private void logEntires(final Map<Integer, Entry> res) {
Expand Down Expand Up @@ -173,7 +177,7 @@ private Entry initial() {
}
indx += type.getSize();
}
return new Entry(0, locals, new LinkedHashMap<>(0));
return new Entry(0, locals, new LinkedHashMap<>(0), true);
}


Expand Down Expand Up @@ -228,6 +232,7 @@ private static class Entry {

private final Map<Integer, Object> stack;

@EqualsAndHashCode.Exclude
private final boolean join;

public Entry(final int indx) {
Expand All @@ -236,11 +241,18 @@ public Entry(final int indx) {

private Entry(
final int indx, final Map<Integer, Object> locals, final Map<Integer, Object> stack
) {
this(indx, locals, stack, false);
}

public Entry(
final int indx, final Map<Integer, Object> locals, final Map<Integer, Object> stack,
final boolean join
) {
this.indx = indx;
this.locals = locals;
this.stack = stack;
this.join = false;
this.join = join;
}

static Entry fromInstruction(
Expand All @@ -266,14 +278,6 @@ static Entry fromInstruction(
}
}

Entry copy(final int indx) {
return new Entry(
indx,
this.locals,
this.stack
);
}

int indx() {
return this.indx;
}
Expand All @@ -286,7 +290,15 @@ int nstack() {
return this.stack.size();
}

Entry join(final Entry next) {
Entry join(final int indx) {
return new Entry(indx, this.locals, this.stack, true);
}

Entry copy(final int indx) {
return new Entry(indx, this.locals, this.stack, false);
}

Entry append(final Entry next) {
final int max = Math.max(
this.locals.keySet().stream().mapToInt(Integer::intValue).max().orElse(0),
next.locals.keySet().stream().mapToInt(Integer::intValue).max().orElse(0)
Expand All @@ -301,49 +313,9 @@ Entry join(final Entry next) {
map.put(i, a);
}
}
// final Map<Integer, Object> map = Stream.concat(
// this.locals.entrySet().stream(),
// next.locals.entrySet().stream()
// ).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (a, b) -> {
// if (a.equals(b)) {
// return a;
// } else {
// return Opcodes.TOP;
// }
// }));
return new Entry(
next.indx(),
map,
next.stack
);
return new Entry(next.indx(), map, next.stack, false);
}

// Entry append(final int indx, final BytecodeInstruction instruction) {
// if (instruction.isVarInstruction()) {
// final LinkedHashMap<Integer, Object> copy = new LinkedHashMap<>(this.locals);
// if (copy.containsKey(instruction.localIndex())) {
// final Object current = copy.get(instruction.localIndex());
// final Object instr = instruction.elementType();
// if (!current.equals(instr)) {
// copy.put(instruction.localIndex(), Opcodes.TOP);
// }
// } else {
// copy.put(instruction.localIndex(), instruction.elementType());
// }
// return new Entry(
// indx,
// copy,
// this.stack
// );
// } else {
// return new Entry(
// indx,
// this.locals,
// this.stack
// );
// }
// }

BytecodeFrame toFrame() {
return new BytecodeFrame(
Opcodes.F_NEW,
Expand All @@ -353,6 +325,10 @@ BytecodeFrame toFrame() {
this.stack.values().toArray()
);
}

public boolean joined() {
return this.join;
}
}

/**
Expand Down

0 comments on commit 729040f

Please sign in to comment.