Skip to content

Commit

Permalink
feat(objectionary#376): try to modify all agents to be independent
Browse files Browse the repository at this point in the history
  • Loading branch information
volodya-lombrozo committed Aug 12, 2024
1 parent cc7e05b commit 71c0739
Show file tree
Hide file tree
Showing 30 changed files with 627 additions and 367 deletions.
39 changes: 39 additions & 0 deletions src/main/java/org/eolang/opeo/ast/Opcode.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.eolang.jeo.representation.xmir.XmlInstruction;
import org.eolang.jeo.representation.xmir.XmlNode;
import org.eolang.jeo.representation.xmir.XmlOperand;
import org.eolang.opeo.Instruction;
import org.xembly.Directive;

/**
Expand Down Expand Up @@ -119,6 +120,10 @@ public Opcode(final XmlInstruction instruction) {
);
}

public Opcode(final Instruction instruction) {
this(instruction.opcode(), instruction.operands());
}

/**
* Constructor.
* @param bytecode Bytecode
Expand All @@ -141,6 +146,40 @@ public List<AstNode> opcodes() {
return Arrays.asList(this);
}

/**
* Opcode number.
* @return Opcode number.
*/
public int opcode() {
return this.bytecode;
}

/**
* Opcode operands.
* @return Opcode operands.
*/
public List<Object> params() {
return this.operands;
}

/**
* Instruction operand.
* @param index Operand index.
* @return Instruction operand.
*/
public Object operand(final int index) {
if (this.operands.size() <= index) {
throw new IllegalStateException(
String.format(
"Instruction '%s' doesn't have operand at index '%d'",
this,
index
)
);
}
return this.operands.get(index);
}

/**
* Disable opcodes counting.
* It is useful for tests.
Expand Down
18 changes: 14 additions & 4 deletions src/main/java/org/eolang/opeo/decompilation/DecompilerMachine.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,16 @@
package org.eolang.opeo.decompilation;

import java.util.Arrays;
import java.util.Deque;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.cactoos.list.ListOf;
import org.eolang.opeo.Instruction;
import org.eolang.opeo.ast.AstNode;
import org.eolang.opeo.ast.Opcode;
import org.eolang.opeo.ast.Root;
import org.eolang.opeo.decompilation.agents.AllAgents;
import org.xembly.Directive;
Expand Down Expand Up @@ -85,10 +91,14 @@ public DecompilerMachine(final LocalVariables locals, final Map<String, String>
* @return Decompiled instructions.
*/
public Iterable<Directive> decompile(final Instruction... instructions) {
final DecompilerState state = new DecompilerState(this.locals);
Arrays.stream(instructions)
.forEach(inst -> this.agents.handle(state.next(inst)));
return new Root(new ListOf<>(state.stack().descendingIterator())).toXmir();
final DecompilerState initial = new DecompilerState(
new OperandStack(Arrays.stream(instructions)
.map(Opcode::new)
.collect(Collectors.toCollection(LinkedList::new))),
this.locals
);
this.agents.handle(initial);
return new Root(new ListOf<>(initial.stack().descendingIterator())).toXmir();
}
}

58 changes: 20 additions & 38 deletions src/main/java/org/eolang/opeo/decompilation/DecompilerState.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import lombok.ToString;
import org.eolang.opeo.Instruction;
import org.eolang.opeo.ast.AstNode;
import org.eolang.opeo.ast.Opcode;
import org.objectweb.asm.Type;

/**
Expand All @@ -36,11 +37,6 @@
@ToString
public final class DecompilerState {

/**
* Current instruction.
*/
private final Instruction current;

/**
* Current operand stack.
* It might have some values inside.
Expand All @@ -57,31 +53,25 @@ public final class DecompilerState {
* @param vars Method local variables.
*/
public DecompilerState(final LocalVariables vars) {
this(new Instruction.Nop(), new OperandStack(), vars);
this(new OperandStack(), vars);
}

/**
* Constructor.
* @param current Current instruction.
* @param stack Operand stack.
* @param variables Method local variables.
* @param operands Operand stack.
* @param vars Method local variables.
*/
private DecompilerState(
final Instruction current,
final OperandStack stack,
final LocalVariables variables
) {
this.current = current;
this.operands = stack;
this.vars = variables;
public DecompilerState(final OperandStack operands, final LocalVariables vars) {
this.operands = operands;
this.vars = vars;
}

/**
* Retrieve current bytecode instruction.
* @return Current bytecode instruction.
*/
public Instruction instruction() {
return this.current;
public Opcode instruction() {
return (Opcode) this.stack().peek();
}

/**
Expand All @@ -90,16 +80,17 @@ public Instruction instruction() {
* @return Instruction operand.
*/
public Object operand(final int index) {
if (this.current.operands().size() <= index) {
throw new IllegalStateException(
String.format(
"Instruction '%s' doesn't have operand at index '%d'",
this.current,
index
)
);
}
return this.current.operand(index);
return this.instruction().operand(index);
// if (this.current.operands().size() <= index) {
// throw new IllegalStateException(
// String.format(
// "Instruction '%s' doesn't have operand at index '%d'",
// this.current,
// index
// )
// );
// }
// return this.current.operand(index);
}

/**
Expand All @@ -119,13 +110,4 @@ public AstNode variable(final int index, final Type type) {
public OperandStack stack() {
return this.operands;
}

/**
* Move the state to the next instruction.
* @param instruction Next instruction.
* @return New decompiler state with the next instruction.
*/
DecompilerState next(final Instruction instruction) {
return new DecompilerState(instruction, this.operands, this.vars);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public final class OperandStack {
* Constructor.
* @param original Initial stack collection.
*/
private OperandStack(final Deque<AstNode> original) {
OperandStack(final Deque<AstNode> original) {
this.stack = original;
}

Expand Down
22 changes: 19 additions & 3 deletions src/main/java/org/eolang/opeo/decompilation/agents/AddAgent.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,38 @@
*/
package org.eolang.opeo.decompilation.agents;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import org.eolang.opeo.ast.Addition;
import org.eolang.opeo.ast.AstNode;
import org.eolang.opeo.decompilation.DecompilerState;
import org.eolang.opeo.decompilation.DecompilationAgent;
import org.objectweb.asm.Opcodes;

/**
* Add instruction handler.
* @since 0.1
*/
public final class AddAgent implements DecompilationAgent {

private static final Set<Integer> SUPPORTED = new HashSet<>(
Arrays.asList(
Opcodes.IADD,
Opcodes.LADD,
Opcodes.FADD,
Opcodes.DADD
)
);

@Override
public void handle(final DecompilerState state) {
final AstNode right = state.stack().pop();
final AstNode left = state.stack().pop();
state.stack().push(new Addition(left, right));
final int opcode = state.instruction().opcode();
if (AddAgent.SUPPORTED.contains(opcode)) {
final AstNode right = state.stack().pop();
final AstNode left = state.stack().pop();
state.stack().push(new Addition(left, right));
}
}

}
Loading

0 comments on commit 71c0739

Please sign in to comment.