Skip to content

Commit

Permalink
feat(objectionary#376): each agent decides when to move to the next p…
Browse files Browse the repository at this point in the history
…osition
  • Loading branch information
volodya-lombrozo committed Aug 12, 2024
1 parent 83318e7 commit f4f5df7
Show file tree
Hide file tree
Showing 28 changed files with 78 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,13 @@
import java.util.Deque;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Optional;
import javax.swing.text.html.Option;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import org.eolang.opeo.ast.AstNode;
import org.eolang.opeo.ast.Opcode;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.Type;

/**
Expand Down Expand Up @@ -86,15 +89,17 @@ public boolean hasInstructions() {
}

public void move() {
this.opcodes.pop();
if (!this.opcodes.isEmpty()) {
this.opcodes.pop();
}
}

/**
* Retrieve current bytecode instruction.
* @return Current bytecode instruction.
*/
public Opcode instruction() {
return this.opcodes.peek();
return Optional.ofNullable(this.opcodes.peek()).orElse(new Opcode(-100));
// final Iterator<AstNode> iterator = this.stack().iterator();
// while (iterator.hasNext()) {
// final AstNode next = iterator.next();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ 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));
state.move();
}
}

Expand Down
90 changes: 45 additions & 45 deletions src/main/java/org/eolang/opeo/decompilation/agents/AllAgents.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,54 +59,55 @@ public AllAgents(final boolean counting) {
this(
new MapOf<Integer, DecompilationAgent>(
new MapEntry<>(Opcodes.ICONST_M1, new ConstAgent()),
// new MapEntry<>(Opcodes.ICONST_0, new ConstAgent()),
// new MapEntry<>(Opcodes.ICONST_1, new ConstAgent()),
// new MapEntry<>(Opcodes.ICONST_2, new ConstAgent()),
// new MapEntry<>(Opcodes.ICONST_3, new ConstAgent()),
// new MapEntry<>(Opcodes.ICONST_4, new ConstAgent()),
// new MapEntry<>(Opcodes.ICONST_5, new ConstAgent()),
// new MapEntry<>(Opcodes.LCONST_0, new ConstAgent()),
// new MapEntry<>(Opcodes.LCONST_1, new ConstAgent()),
// new MapEntry<>(Opcodes.FCONST_0, new ConstAgent()),
// new MapEntry<>(Opcodes.FCONST_1, new ConstAgent()),
// new MapEntry<>(Opcodes.FCONST_2, new ConstAgent()),
// new MapEntry<>(Opcodes.DCONST_0, new ConstAgent()),
// new MapEntry<>(Opcodes.DCONST_1, new ConstAgent()),
new MapEntry<>(Opcodes.ICONST_0, new ConstAgent()),
new MapEntry<>(Opcodes.ICONST_1, new ConstAgent()),
new MapEntry<>(Opcodes.ICONST_2, new ConstAgent()),
new MapEntry<>(Opcodes.ICONST_3, new ConstAgent()),
new MapEntry<>(Opcodes.ICONST_4, new ConstAgent()),
new MapEntry<>(Opcodes.ICONST_5, new ConstAgent()),
new MapEntry<>(Opcodes.LCONST_0, new ConstAgent()),
new MapEntry<>(Opcodes.LCONST_1, new ConstAgent()),
new MapEntry<>(Opcodes.FCONST_0, new ConstAgent()),
new MapEntry<>(Opcodes.FCONST_1, new ConstAgent()),
new MapEntry<>(Opcodes.FCONST_2, new ConstAgent()),
new MapEntry<>(Opcodes.DCONST_0, new ConstAgent()),
new MapEntry<>(Opcodes.DCONST_1, new ConstAgent()),
//
new MapEntry<>(Opcodes.IADD, new AddAgent()),
// new MapEntry<>(Opcodes.LADD, new AddAgent()),
// new MapEntry<>(Opcodes.FADD, new AddAgent()),
// new MapEntry<>(Opcodes.DADD, new AddAgent()),
new MapEntry<>(Opcodes.LADD, new AddAgent()),
new MapEntry<>(Opcodes.FADD, new AddAgent()),
new MapEntry<>(Opcodes.DADD, new AddAgent()),
new MapEntry<>(Opcodes.ISUB, new SubstractionAgent()),
// new MapEntry<>(Opcodes.LSUB, new SubstractionAgent()),
// new MapEntry<>(Opcodes.FSUB, new SubstractionAgent()),
// new MapEntry<>(Opcodes.DSUB, new SubstractionAgent()),
new MapEntry<>(Opcodes.LSUB, new SubstractionAgent()),
new MapEntry<>(Opcodes.FSUB, new SubstractionAgent()),
new MapEntry<>(Opcodes.DSUB, new SubstractionAgent()),
new MapEntry<>(Opcodes.IMUL, new MulAgent()),
new MapEntry<>(Opcodes.IF_ICMPGT, new IfAgent()),
new MapEntry<>(Opcodes.I2B, new CastAgent()),
// new MapEntry<>(Opcodes.I2C, new CastAgent()),
// new MapEntry<>(Opcodes.I2S, new CastAgent()),
// new MapEntry<>(Opcodes.I2L, new CastAgent()),
// new MapEntry<>(Opcodes.I2F, new CastAgent()),
// new MapEntry<>(Opcodes.I2D, new CastAgent()),
// new MapEntry<>(Opcodes.L2I, new CastAgent()),
// new MapEntry<>(Opcodes.L2F, new CastAgent()),
// new MapEntry<>(Opcodes.L2D, new CastAgent()),
// new MapEntry<>(Opcodes.F2I, new CastAgent()),
// new MapEntry<>(Opcodes.F2L, new CastAgent()),
// new MapEntry<>(Opcodes.F2D, new CastAgent()),
// new MapEntry<>(Opcodes.D2I, new CastAgent()),
// new MapEntry<>(Opcodes.D2L, new CastAgent()),
// new MapEntry<>(Opcodes.D2F, new CastAgent()),
new MapEntry<>(Opcodes.I2C, new CastAgent()),
new MapEntry<>(Opcodes.I2S, new CastAgent()),
new MapEntry<>(Opcodes.I2L, new CastAgent()),
new MapEntry<>(Opcodes.I2F, new CastAgent()),
new MapEntry<>(Opcodes.I2D, new CastAgent()),
new MapEntry<>(Opcodes.L2I, new CastAgent()),
new MapEntry<>(Opcodes.L2F, new CastAgent()),
new MapEntry<>(Opcodes.L2D, new CastAgent()),
new MapEntry<>(Opcodes.F2I, new CastAgent()),
new MapEntry<>(Opcodes.F2L, new CastAgent()),
new MapEntry<>(Opcodes.F2D, new CastAgent()),
new MapEntry<>(Opcodes.D2I, new CastAgent()),
new MapEntry<>(Opcodes.D2L, new CastAgent()),
new MapEntry<>(Opcodes.D2F, new CastAgent()),
new MapEntry<>(Opcodes.ILOAD, new LoadAgent()),
// new MapEntry<>(Opcodes.LLOAD, new LoadAgent()),
// new MapEntry<>(Opcodes.FLOAD, new LoadAgent()),
// new MapEntry<>(Opcodes.DLOAD, new LoadAgent()),
// new MapEntry<>(Opcodes.ALOAD, new LoadAgent()),
new MapEntry<>(Opcodes.LLOAD, new LoadAgent()),
new MapEntry<>(Opcodes.FLOAD, new LoadAgent()),
new MapEntry<>(Opcodes.DLOAD, new LoadAgent()),
new MapEntry<>(Opcodes.ALOAD, new LoadAgent()),
new MapEntry<>(Opcodes.ISTORE, new StoreAgent()),
// new MapEntry<>(Opcodes.LSTORE, new StoreAgent()),
// new MapEntry<>(Opcodes.FSTORE, new StoreAgent()),
// new MapEntry<>(Opcodes.DSTORE, new StoreAgent()),
// new MapEntry<>(Opcodes.ASTORE, new StoreAgent()),
new MapEntry<>(Opcodes.LSTORE, new StoreAgent()),
new MapEntry<>(Opcodes.FSTORE, new StoreAgent()),
new MapEntry<>(Opcodes.DSTORE, new StoreAgent()),
new MapEntry<>(Opcodes.ASTORE, new StoreAgent()),
new MapEntry<>(Opcodes.AASTORE, new StoreToArrayAgent()),
new MapEntry<>(Opcodes.ANEWARRAY, new NewArrayAgent()),
new MapEntry<>(Opcodes.CHECKCAST, new CheckCastAgent()),
Expand All @@ -124,8 +125,8 @@ public AllAgents(final boolean counting) {
new MapEntry<>(Opcodes.LDC, new LdcAgent()),
new MapEntry<>(Opcodes.POP, new PopAgent()),
new MapEntry<>(Opcodes.RETURN, new ReturnAgent()),
// new MapEntry<>(Opcodes.IRETURN, new ReturnAgent()),
// new MapEntry<>(Opcodes.ARETURN, new ReturnAgent()),
new MapEntry<>(Opcodes.IRETURN, new ReturnAgent()),
new MapEntry<>(Opcodes.ARETURN, new ReturnAgent()),
new MapEntry<>(LabelInstruction.LABEL_OPCODE, new LabelAgent())
// , new MapEntry<>(AllAgents.UNIMPLEMENTED, new UnimplementedAgent(counting))
)
Expand All @@ -143,10 +144,9 @@ private AllAgents(final Map<Integer, DecompilationAgent> agents) {
@Override
public void handle(final DecompilerState state) {
//todo: until?
int hash;
while (state.hasInstructions()) {
this.agents.values().forEach(agent -> agent.handle(state));
state.move();
// state.move();
}
// do {
// hash = state.hashCode();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public final class BipushAgent implements DecompilationAgent {
public void handle(final DecompilerState state) {
if (BipushAgent.SUPPORTED.contains(state.instruction().opcode())) {
state.stack().push(new Literal(state.operand(0)));
state.move();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ public void handle(final DecompilerState state) {
state.stack().pop()
)
);
state.move();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public void handle(final DecompilerState state) {
final AstNode value = state.stack().pop();
final Object type = state.operand(0);
state.stack().push(new CheckCast(Type.getObjectType((String) type), value));
state.move();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ public void handle(final DecompilerState state) {
state.stack().push(res);
break;
}
state.move();
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public void handle(final DecompilerState state) {
if (DupAgent.SUPPORTED.contains(state.instruction().opcode())) {
final OperandStack stack = state.stack();
stack.push(new Duplicate(stack.pop()));
state.move();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public void handle(final DecompilerState state) {
.type("field")
)
);
state.move();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public void handle(final DecompilerState state) {
final String method = (String) state.operand(1);
final String descriptor = (String) state.operand(2);
state.stack().push(new ClassField(klass, method, descriptor));
state.move();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public void handle(final DecompilerState state) {
final AstNode first = stack.pop();
final Label operand = (Label) state.operand(0);
stack.push(new If(first, second, operand));
state.move();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public void handle(final DecompilerState state) {
args
);
state.stack().push(node);
state.move();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ public void handle(final DecompilerState state) {
args
)
);
state.move();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ public void handle(final DecompilerState state) {
new Super(target, args, descriptor, type, name)
);
}
state.move();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public void handle(final DecompilerState state) {
args
)
);
state.move();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ public void handle(final DecompilerState state) {
args
)
);
state.move();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public void handle(final DecompilerState state) {
new Label(String.class.cast(state.operand(0)))
);
state.stack().push(node);
state.move();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public void handle(final DecompilerState state) {
if (state.instruction().opcode() == Opcodes.LDC) {
final Object operand = state.operand(0);
state.stack().push(new Constant(operand));
state.move();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public void handle(final DecompilerState state) {
state.stack().push(
state.variable(index, LoadAgent.type(opcode))
);
state.move();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public void handle(final DecompilerState state) {
final AstNode right = state.stack().pop();
final AstNode left = state.stack().pop();
state.stack().push(new Multiplication(left, right));

state.move();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public final class NewAgent implements DecompilationAgent {
public void handle(final DecompilerState state) {
if (NewAgent.SUPPORTED.contains(state.instruction().opcode())) {
state.stack().push(new NewAddress(state.operand(0).toString()));
state.move();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public void handle(final DecompilerState state) {
final OperandStack stack = state.stack();
final AstNode size = stack.pop();
stack.push(new Reference(new ArrayConstructor(size, type)));
state.move();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public void handle(final DecompilerState state) {
if (state.instruction().opcode() == Opcodes.POP) {
final OperandStack stack = state.stack();
stack.push(new Popped(stack.pop()));
state.move();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public void handle(final DecompilerState state) {
value
)
);
state.move();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ public void handle(final DecompilerState state) {
String.format("Unexpected opcode: %d", opcode)
);
}
state.move();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ public void handle(final DecompilerState state) {
value
)
);
state.move();
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ public void handle(final DecompilerState state) {
} catch (final IllegalStateException exception) {
state.stack().push(new StoreArray(array, index, value));
}
state.move();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public void handle(final DecompilerState state) {
final AstNode right = state.stack().pop();
final AstNode left = state.stack().pop();
state.stack().push(new Substraction(left, right));
state.move();
}
}
}

0 comments on commit f4f5df7

Please sign in to comment.