Commit 08c9d122 authored by Skylot's avatar Skylot

core: inline 'cmp' instruction

parent 11d8b28f
......@@ -24,6 +24,10 @@ public final class Compare {
return insn.getArg(1);
}
public IfNode getInsn() {
return insn;
}
public Compare invert() {
insn.invertCondition();
return this;
......
......@@ -2,10 +2,13 @@ package jadx.core.dex.regions;
import jadx.core.dex.instructions.IfNode;
import jadx.core.dex.instructions.IfOp;
import jadx.core.dex.instructions.InsnType;
import jadx.core.dex.instructions.args.InsnArg;
import jadx.core.dex.instructions.args.InsnWrapArg;
import jadx.core.dex.instructions.args.LiteralArg;
import jadx.core.dex.instructions.args.RegisterArg;
import jadx.core.dex.nodes.BlockNode;
import jadx.core.dex.nodes.InsnNode;
import jadx.core.utils.exceptions.JadxRuntimeException;
import java.util.ArrayList;
......@@ -132,6 +135,7 @@ public final class IfCondition {
public static IfCondition simplify(IfCondition cond) {
if (cond.isCompare()) {
Compare c = cond.getCompare();
simplifyCmpOp(c);
if (c.getOp() == IfOp.EQ && c.getB().isLiteral() && c.getB().equals(LiteralArg.FALSE)) {
return not(new IfCondition(c.invert()));
} else {
......@@ -177,6 +181,22 @@ public final class IfCondition {
return cond;
}
private static void simplifyCmpOp(Compare c) {
if (!c.getA().isInsnWrap()) {
return;
}
if (!c.getB().isLiteral() || ((LiteralArg) c.getB()).getLiteral() != 0) {
return;
}
InsnNode wrapInsn = ((InsnWrapArg) c.getA()).getWrapInsn();
InsnType type = wrapInsn.getType();
if (type != InsnType.CMP_L && type != InsnType.CMP_G) {
return;
}
IfNode insn = c.getInsn();
insn.changeCondition(insn.getOp(), wrapInsn.getArg(0), wrapInsn.getArg(1));
}
public List<RegisterArg> getRegisterArgs() {
List<RegisterArg> list = new LinkedList<RegisterArg>();
if (mode == Mode.COMPARE) {
......
package jadx.tests.internal.conditions;
import jadx.api.InternalJadxTest;
import jadx.core.dex.nodes.ClassNode;
import org.junit.Test;
import static org.hamcrest.CoreMatchers.containsString;
import static org.junit.Assert.assertThat;
public class TestCmpOp extends InternalJadxTest {
public static class TestCls {
public boolean testGT(float a) {
return a > 3.0f;
}
public boolean testLT(float b) {
return b < 2.0f;
}
public boolean testEQ(float c) {
return c == 1.0f;
}
public boolean testNE(float d) {
return d != 0.0f;
}
public boolean testGE(float e) {
return e >= -1.0f;
}
public boolean testLE(float f) {
return f <= -2.0f;
}
public boolean testGT2(float g) {
return 4.0f > g;
}
public boolean testLT2(long h) {
return 5 < h;
}
public boolean testGE2(double i) {
return 6.5d < i;
}
}
@Test
public void test() {
ClassNode cls = getClassNode(TestCls.class);
String code = cls.getCode().toString();
System.out.println(code);
assertThat(code, containsString("return a > 3.0f;"));
assertThat(code, containsString("return b < 2.0f;"));
assertThat(code, containsString("return c == 1.0f;"));
assertThat(code, containsString("return d != 0.0f;"));
assertThat(code, containsString("return e >= -1.0f;"));
assertThat(code, containsString("return f <= -2.0f;"));
assertThat(code, containsString("return 4.0f > g;"));
assertThat(code, containsString("return 5 < h;"));
assertThat(code, containsString("return 6.5d < i;"));
}
}
package jadx.tests.internal.conditions;
import jadx.api.InternalJadxTest;
import jadx.core.dex.nodes.ClassNode;
import org.junit.Test;
import static org.hamcrest.CoreMatchers.containsString;
import static org.junit.Assert.assertThat;
public class TestCmpOp2 extends InternalJadxTest {
public static class TestCls {
public boolean testGT(float a, float b) {
return a > b;
}
public boolean testLT(float c, double d) {
return c < d;
}
}
@Test
public void test() {
ClassNode cls = getClassNode(TestCls.class);
String code = cls.getCode().toString();
System.out.println(code);
assertThat(code, containsString("return a > b;"));
assertThat(code, containsString("return ((double) c) < d;"));
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment