Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(#65): Decompilation of simple constructs #70

Merged
merged 16 commits into from
Jan 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ SOFTWARE.
<dependency>
<groupId>org.eolang</groupId>
<artifactId>jeo-maven-plugin</artifactId>
<version>0.2.15</version>
<version>0.2.16</version>
</dependency>
<dependency>
<groupId>com.jcabi</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@
</o>
</o>
</o>
<o base="tuple" name="trycatchblocks"/>
</o>
<o abstract="" name="j$foo">
<o base="int" data="bytes" name="access">00 00 00 00 00 00 00 01</o>
Expand Down Expand Up @@ -105,7 +104,6 @@
</o>
</o>
</o>
<o base="tuple" name="trycatchblocks"/>
</o>
</o>
</objects>
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/org/eolang/opeo/ast/Add.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
*/
package org.eolang.opeo.ast;

import java.util.ArrayList;
import java.util.List;
import org.objectweb.asm.Opcodes;
import org.xembly.Directive;
import org.xembly.Directives;

Expand Down Expand Up @@ -65,4 +68,13 @@ public Iterable<Directive> toXmir() {
.append(this.right.toXmir())
.up();
}

@Override
public List<AstNode> opcodes() {
final List<AstNode> res = new ArrayList<>(0);
res.addAll(this.left.opcodes());
res.addAll(this.right.opcodes());
res.add(new Opcode(Opcodes.IADD));
return res;
}
}
7 changes: 7 additions & 0 deletions src/main/java/org/eolang/opeo/ast/AstNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
*/
package org.eolang.opeo.ast;

import java.util.List;
import org.xembly.Directive;

/**
Expand All @@ -42,4 +43,10 @@ public interface AstNode {
* @return XMIR XML.
*/
Iterable<Directive> toXmir();

/**
* Bytecode instructions.
* @return List of opcodes.
*/
List<AstNode> opcodes();
}
5 changes: 5 additions & 0 deletions src/main/java/org/eolang/opeo/ast/Constructor.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ public Iterable<Directive> toXmir() {
return directives.up();
}

@Override
public List<AstNode> opcodes() {
throw new UnsupportedOperationException("Not implemented yet");
}

/**
* Print arguments.
* @return Arguments string.
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/org/eolang/opeo/ast/InstanceField.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
*/
package org.eolang.opeo.ast;

import java.util.List;
import org.xembly.Directive;
import org.xembly.Directives;

Expand Down Expand Up @@ -65,4 +66,9 @@ public Iterable<Directive> toXmir() {
.append(this.source.toXmir())
.up();
}

@Override
public List<AstNode> opcodes() {
throw new UnsupportedOperationException("Not implemented yet");
}
}
5 changes: 5 additions & 0 deletions src/main/java/org/eolang/opeo/ast/Invocation.java
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,11 @@ public Iterable<Directive> toXmir() {
return directives.up();
}

@Override
public List<AstNode> opcodes() {
throw new UnsupportedOperationException("Not implemented yet");
}

/**
* Print arguments.
* @return Arguments
Expand Down
68 changes: 68 additions & 0 deletions src/main/java/org/eolang/opeo/ast/Label.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2016-2023 Objectionary.com
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.eolang.opeo.ast;

import java.util.Collections;
import java.util.List;
import org.xembly.Directive;
import org.xembly.Directives;

/**
* Label ast node.
* @since 0.1
*/
public final class Label implements AstNode {

/**
* Label identifier.
*/
private final AstNode identifier;

/**
* Constructor.
* @param identifier Label identifier.
*/
public Label(final AstNode identifier) {
this.identifier = identifier;
}

@Override
public String print() {
return String.format(": %s", this.identifier.print());
}

@Override
public Iterable<Directive> toXmir() {
return new Directives()
.add("o")
.attr("base", "label")
.append(this.identifier.toXmir())
.up();
}

@Override
public List<AstNode> opcodes() {
return Collections.singletonList(this);
}
}
58 changes: 58 additions & 0 deletions src/main/java/org/eolang/opeo/ast/Literal.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@
*/
package org.eolang.opeo.ast;

import java.util.Collections;
import java.util.List;
import org.eolang.jeo.representation.directives.DirectivesData;
import org.objectweb.asm.Opcodes;
import org.xembly.Directive;

/**
Expand All @@ -50,6 +53,19 @@ public Iterable<Directive> toXmir() {
return new DirectivesData(this.object);
}

@Override
public List<AstNode> opcodes() {
final List<AstNode> result;
if (this.object instanceof Integer) {
result = Collections.singletonList(Literal.opcode((Integer) this.object));
} else if (this.object instanceof String) {
result = Collections.singletonList(Literal.opcode((String) this.object));
} else {
throw new UnsupportedOperationException("Not implemented yet");
}
return result;
}

@Override
public String print() {
final String result;
Expand All @@ -60,4 +76,46 @@ public String print() {
}
return result;
}

/**
* Convert string into an opcode.
* @param value String value.
* @return Opcode.
*/
private static Opcode opcode(final String value) {
return new Opcode(Opcodes.LDC, value);
}

/**
* Convert integer into an opcode.
* @param value Integer value.
* @return Opcode.
*/
private static Opcode opcode(final int value) {
final Opcode res;
switch (value) {
case 0:
res = new Opcode(Opcodes.ICONST_0);
break;
case 1:
res = new Opcode(Opcodes.ICONST_1);
break;
case 2:
res = new Opcode(Opcodes.ICONST_2);
break;
case 3:
res = new Opcode(Opcodes.ICONST_3);
break;
case 4:
res = new Opcode(Opcodes.ICONST_4);
break;
case 5:
res = new Opcode(Opcodes.ICONST_5);
break;
default:
res = new Opcode(Opcodes.BIPUSH, value);
break;
}
return res;
}
}
6 changes: 6 additions & 0 deletions src/main/java/org/eolang/opeo/ast/Mul.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
*/
package org.eolang.opeo.ast;

import java.util.List;
import org.xembly.Directive;
import org.xembly.Directives;

Expand Down Expand Up @@ -65,4 +66,9 @@ public Iterable<Directive> toXmir() {
.append(this.right.toXmir())
.up();
}

@Override
public List<AstNode> opcodes() {
throw new UnsupportedOperationException("Not implemented yet");
}
}
26 changes: 25 additions & 1 deletion src/main/java/org/eolang/opeo/ast/Opcode.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
import org.eolang.jeo.representation.directives.DirectivesInstruction;
import org.eolang.parser.XMIR;
import org.xembly.Directive;
Expand All @@ -38,6 +39,11 @@
*/
public final class Opcode implements AstNode {

/**
* Opcodes counting.
*/
private static final AtomicBoolean COUNTING = new AtomicBoolean(true);

/**
* Opcode.
*/
Expand Down Expand Up @@ -82,7 +88,7 @@ public Opcode(final int opcode, final boolean counting) {
* @param operands Opcode operands
*/
public Opcode(final int opcode, final List<Object> operands) {
this(opcode, operands, true);
this(opcode, operands, Opcode.COUNTING.get());
}

/**
Expand All @@ -106,4 +112,22 @@ public String print() {
public Iterable<Directive> toXmir() {
return new DirectivesInstruction(this.bytecode, this.counting, this.operands.toArray());
}

@Override
public List<AstNode> opcodes() {
return List.of(this);
}

/**
* Disable opcodes counting.
* It is useful for tests.
* @todo #65:30min Remove public static method 'disableCounting()' from Opcode.
* Currently it is used in tests. We should find another
* way to disable opcodes counting in tests. When we find
* the way to do it, we should remove this method.
*/
@SuppressWarnings("PMD.ProhibitPublicStaticMethods")
public static void disableCounting() {
Opcode.COUNTING.set(false);
}
}
Loading