Skip to content

Commit

Permalink
feat(objectionary#540): use InstructionsFlow in MaxLocals
Browse files Browse the repository at this point in the history
  • Loading branch information
volodya-lombrozo committed Oct 10, 2024
1 parent a8312df commit 989be65
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 30 deletions.
49 changes: 25 additions & 24 deletions src/main/java/org/eolang/jeo/representation/bytecode/MaxLocals.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import java.util.TreeMap;
import java.util.stream.Collectors;
import lombok.ToString;
import org.eolang.jeo.representation.xmir.AllLabels;
import org.objectweb.asm.Label;
import org.objectweb.asm.Type;

Expand All @@ -43,17 +42,25 @@
final class MaxLocals {

private final BytecodeMethodProperties props;
private final List<BytecodeEntry> instructions;
private final InstructionsFlow instructions;
private final List<BytecodeTryCatchBlock> blocks;

MaxLocals(
final BytecodeMethodProperties props,
final List<BytecodeEntry> instructions,
final List<BytecodeTryCatchBlock> blocks
final List<BytecodeTryCatchBlock> catches
) {
this(props, new InstructionsFlow(instructions), catches);
}

private MaxLocals(
final BytecodeMethodProperties props,
final InstructionsFlow instructions,
final List<BytecodeTryCatchBlock> catches
) {
this.props = props;
this.instructions = instructions;
this.blocks = blocks;
this.blocks = catches;
}

public int value() {
Expand Down Expand Up @@ -93,18 +100,20 @@ public int value() {
if (var.isSwitchInstruction()) {
final List<Label> offsets = var.offsets();
for (Label offset : offsets) {
final int target = this.index(offset);
final int target = this.instructions.index(offset);
worklist.put(target, new Variables(currentVars));
}
break;
} else if (var.isConditionalBranchInstruction()) {
final int jump = this.index(var.offset());
final Label label = var.offset();
final int jump = this.instructions.index(label);
worklist.put(jump, new Variables(currentVars));
final int next = current + 1;
worklist.put(next, new Variables(currentVars));
break;
} else {
final int jump = this.index(var.offset());
final Label label = var.offset();
final int jump = this.instructions.index(label);
worklist.put(jump, new Variables(currentVars));
break;
}
Expand All @@ -129,25 +138,17 @@ public int value() {
private List<Integer> catches(final int instruction) {
return this.blocks.stream().map(BytecodeTryCatchBlock.class::cast)
.filter(
block -> index(block.start()) <= instruction
&& index(block.end()) >= instruction
block -> {
if (this.instructions.index(block.start()) > instruction) return false;
return this.instructions.index(block.end()) >= instruction;
}
).map(
block -> index(block.handler())
block -> {
return this.instructions.index(block.handler());
}
).collect(Collectors.toList());
}

private int index(final Label label) {
for (int index = 0; index < this.instructions.size(); index++) {
final BytecodeEntry entry = this.instructions.get(index);
final BytecodeLabel obj = new BytecodeLabel(label, new AllLabels());
final boolean equals = entry.equals(obj);
if (equals) {
return index;
}
}
throw new IllegalStateException("Label not found");
}

@ToString
private static class Variables {

Expand All @@ -173,11 +174,11 @@ int size() {
return (entry.getKey() + 1) + ((int) (entry.getValue() * 0.5));
}

public void put(BytecodeInstruction var) {
void put(BytecodeInstruction var) {
this.put(var.localIndex(), var.size());
}

public void put(int index, int value) {
void put(int index, int value) {
this.all.put(index, value);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,23 +51,26 @@ final class MaxStack {
/**
* Constructor.
* @param instructions Instructions.
* @param tryblocks Try-catch blocks.
* @param catches Try-catch blocks.
*/
MaxStack(
final List<? extends BytecodeEntry> instructions,
final List<BytecodeTryCatchBlock> tryblocks
final List<BytecodeTryCatchBlock> catches
) {
this(new InstructionsFlow(instructions), tryblocks);
this(new InstructionsFlow(instructions), catches);
}

/**
* Compute the maximum stack size.
* @param instructions Instructions.
* @param blocks Try-catch blocks.
* @param catches Try-catch blocks.
*/
public MaxStack(final InstructionsFlow instructions, final List<BytecodeTryCatchBlock> blocks) {
private MaxStack(
final InstructionsFlow instructions,
final List<BytecodeTryCatchBlock> catches
) {
this.instructions = instructions;
this.blocks = blocks;
this.blocks = catches;
}

/**
Expand Down

0 comments on commit 989be65

Please sign in to comment.