diff --git a/src/main/java/org/eolang/jeo/representation/bytecode/BytecodeMethod.java b/src/main/java/org/eolang/jeo/representation/bytecode/BytecodeMethod.java index e8a470e53..166baeab1 100644 --- a/src/main/java/org/eolang/jeo/representation/bytecode/BytecodeMethod.java +++ b/src/main/java/org/eolang/jeo/representation/bytecode/BytecodeMethod.java @@ -29,10 +29,8 @@ import java.util.Arrays; import java.util.Deque; import java.util.HashMap; -import java.util.HashSet; import java.util.List; import java.util.Map; -import java.util.Set; import java.util.TreeMap; import java.util.stream.Collectors; import lombok.EqualsAndHashCode; @@ -322,8 +320,8 @@ void write(final CustomClassWriter visitor) { try { final MethodVisitor mvisitor = this.properties.writeMethod( visitor, -// this.maxs.compute() false +// this.maxs.compute() ); this.annotations.write(mvisitor); this.defvalues.forEach(defvalue -> defvalue.writeTo(mvisitor)); @@ -331,12 +329,7 @@ void write(final CustomClassWriter visitor) { mvisitor.visitCode(); this.tryblocks.forEach(block -> block.writeTo(mvisitor)); this.instructions.forEach(instruction -> instruction.writeTo(mvisitor)); -// mvisitor.visitMaxs(this.maxs.stack(), this.maxs.locals()); - final int stack = this.computeStack(); -// final int locals = this.computeLocals(); -// final int locals = this.computeLocalsWithCFG(); - final int locals = this.computeLocalsWithCFG2(); - mvisitor.visitMaxs(stack, locals); + mvisitor.visitMaxs(this.computeStack(), this.computeLocals()); } mvisitor.visitEnd(); } catch (final NegativeArraySizeException exception) { @@ -381,7 +374,7 @@ void write(final CustomClassWriter visitor) { * @return Maxs. */ BytecodeMaxs computeMaxs() { - return new BytecodeMaxs(this.computeStack(), this.computeLocalsWithCFG2()); + return new BytecodeMaxs(this.computeStack(), this.computeLocals()); } /** @@ -511,159 +504,11 @@ private int computeStack() { return max; } - private String trackStack(final Map visited) { - String res = ""; - for (int i = 0; i < this.instructions.size(); i++) { - res += String.format("%s: %d\n", this.instructions.get(i).testCode(), visited.get(i)); - } - return res; - } - - 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"); - } - /** * Compute max local variables. * @return Max local variables. */ private int computeLocals() { - Map variables = new HashMap<>(0); - int first = 0; - if (!this.properties.isStatic()) { - variables.put(0, 1); - first = 1; - } - final Type[] types = Type.getArgumentTypes(this.properties.descriptor()); - for (int index = 0; index < types.length; index++) { - final Type type = types[index]; - variables.put(index * type.getSize() + first, type.getSize()); - } - for (final BytecodeEntry instruction : this.instructions) { - if (instruction instanceof BytecodeInstruction) { - final BytecodeInstruction var = BytecodeInstruction.class.cast(instruction); - if (var.isVarInstruction()) { - if (var.size() == 2) { - final int key = var.localIndex(); - variables.put(key, 2); - } else { - final int key = var.localIndex(); - variables.putIfAbsent(key, 1); - variables.compute(key, (k, v) -> v == 2 ? 2 : 1); - } - } - } - } - int max = variables.values().stream().mapToInt(Integer::intValue).sum(); - return max; - } - - - private int computeLocalsWithCFG() { - Logger.info(this, "Computing locals for %s", this.properties); - Map variables = new HashMap<>(0); - int first = 0; - if (!this.properties.isStatic()) { - variables.put(0, 1); - first = 1; - } - final Type[] types = Type.getArgumentTypes(this.properties.descriptor()); - for (int index = 0; index < types.length; index++) { - final Type type = types[index]; - variables.put(index * type.getSize() + first, type.getSize()); - } - Map> allVariables = new HashMap<>(1); - allVariables.put(0, variables); - Deque worklist = new ArrayDeque<>(1); - worklist.add(0); - this.tryblocks.stream() - .map(BytecodeTryCatchBlock.class::cast) - .map(BytecodeTryCatchBlock::handler) - .map(this::index) - .forEach(worklist::add); - int MAX_RESULT = 0; - Map> visited = new HashMap<>(0); - while (!worklist.isEmpty()) { - int current = worklist.pop(); - Map vars = new HashMap<>( - allVariables.getOrDefault(current, new HashMap<>(0))); - while (current < this.instructions.size()) { - BytecodeEntry entry = this.instructions.get(current); - final String aopcode = entry.testCode(); - System.out.println(aopcode); - if (entry instanceof BytecodeInstruction) { - final BytecodeInstruction var = BytecodeInstruction.class.cast(entry); - if (var.isBranchInstruction()) { - if (var.isSwitchInstruction()) { - final List