Commit 3ccab60f authored by Skylot's avatar Skylot

core: remove redundant parenthesis for arithmetic operations

parent ed64b8c1
...@@ -720,22 +720,19 @@ public class InsnGen { ...@@ -720,22 +720,19 @@ public class InsnGen {
} }
private void makeArith(ArithNode insn, CodeWriter code, EnumSet<Flags> state) throws CodegenException { private void makeArith(ArithNode insn, CodeWriter code, EnumSet<Flags> state) throws CodegenException {
ArithOp op = insn.getOp(); // wrap insn in brackets for save correct operation order
if (state.contains(Flags.BODY_ONLY)) { boolean wrap = state.contains(Flags.BODY_ONLY)
// wrap insn in brackets for save correct operation order && !insn.getAttributes().contains(AttributeFlag.DONT_WRAP);
if (wrap) {
code.add('('); code.add('(');
addArg(code, insn.getArg(0)); }
code.add(' '); addArg(code, insn.getArg(0));
code.add(op.getSymbol()); code.add(' ');
code.add(' '); code.add(insn.getOp().getSymbol());
addArg(code, insn.getArg(1)); code.add(' ');
addArg(code, insn.getArg(1));
if (wrap) {
code.add(')'); code.add(')');
} else {
addArg(code, insn.getArg(0));
code.add(' ');
code.add(op.getSymbol());
code.add(' ');
addArg(code, insn.getArg(1));
} }
} }
......
...@@ -13,6 +13,7 @@ public enum AttributeFlag { ...@@ -13,6 +13,7 @@ public enum AttributeFlag {
RETURN, // block contains only return instruction RETURN, // block contains only return instruction
DECLARE_VAR, DECLARE_VAR,
DONT_WRAP,
DONT_SHRINK, DONT_SHRINK,
DONT_GENERATE, DONT_GENERATE,
......
package jadx.core.dex.visitors; package jadx.core.dex.visitors;
import jadx.core.dex.attributes.AttributeFlag;
import jadx.core.dex.instructions.ArithNode; import jadx.core.dex.instructions.ArithNode;
import jadx.core.dex.instructions.ArithOp;
import jadx.core.dex.instructions.InsnType; import jadx.core.dex.instructions.InsnType;
import jadx.core.dex.instructions.args.InsnArg; import jadx.core.dex.instructions.args.InsnArg;
import jadx.core.dex.instructions.args.InsnWrapArg;
import jadx.core.dex.instructions.args.RegisterArg; import jadx.core.dex.instructions.args.RegisterArg;
import jadx.core.dex.instructions.mods.ConstructorInsn; import jadx.core.dex.instructions.mods.ConstructorInsn;
import jadx.core.dex.nodes.BlockNode; import jadx.core.dex.nodes.BlockNode;
...@@ -23,6 +26,7 @@ public class PrepareForCodeGen extends AbstractVisitor { ...@@ -23,6 +26,7 @@ public class PrepareForCodeGen extends AbstractVisitor {
} }
for (BlockNode block : blocks) { for (BlockNode block : blocks) {
removeInstructions(block); removeInstructions(block);
removeBrackets(block);
modifyArith(block); modifyArith(block);
} }
} }
...@@ -48,6 +52,36 @@ public class PrepareForCodeGen extends AbstractVisitor { ...@@ -48,6 +52,36 @@ public class PrepareForCodeGen extends AbstractVisitor {
} }
} }
private static void removeBrackets(BlockNode block) {
for (InsnNode insn : block.getInstructions()) {
checkInsn(insn);
}
}
private static void checkInsn(InsnNode insn) {
if (insn.getType() == InsnType.ARITH) {
ArithNode arith = (ArithNode) insn;
ArithOp op = arith.getOp();
if (op == ArithOp.ADD || op == ArithOp.SUB) {
for (int i = 0; i < 2; i++) {
InsnArg arg = arith.getArg(i);
if (arg.isInsnWrap()) {
InsnNode wrapInsn = ((InsnWrapArg) arg).getWrapInsn();
wrapInsn.getAttributes().add(AttributeFlag.DONT_WRAP);
checkInsn(wrapInsn);
}
}
}
} else {
for (InsnArg arg : insn.getArguments()) {
if (arg.isInsnWrap()) {
InsnNode wrapInsn = ((InsnWrapArg) arg).getWrapInsn();
checkInsn(wrapInsn);
}
}
}
}
private static void modifyArith(BlockNode block) { private static void modifyArith(BlockNode block) {
List<InsnNode> list = block.getInstructions(); List<InsnNode> list = block.getInstructions();
for (int i = 0; i < list.size(); i++) { for (int i = 0; i < list.size(); i++) {
......
...@@ -16,6 +16,10 @@ public class TestArith2 extends InternalJadxTest { ...@@ -16,6 +16,10 @@ public class TestArith2 extends InternalJadxTest {
public int test1(int a) { public int test1(int a) {
return (a + 2) * 3; return (a + 2) * 3;
} }
public int test2(int a, int b, int c) {
return a + b + c;
}
} }
@Test @Test
...@@ -26,5 +30,8 @@ public class TestArith2 extends InternalJadxTest { ...@@ -26,5 +30,8 @@ public class TestArith2 extends InternalJadxTest {
assertThat(code, containsString("return (a + 2) * 3;")); assertThat(code, containsString("return (a + 2) * 3;"));
assertThat(code, not(containsString("a + 2 * 3"))); assertThat(code, not(containsString("a + 2 * 3")));
assertThat(code, containsString("return a + b + c;"));
assertThat(code, not(containsString("return (a + b) + c;")));
} }
} }
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