From 58d2efb8e51db3f5cec63256030db3d3f98789e7 Mon Sep 17 00:00:00 2001 From: Jeff Smith Date: Thu, 14 Nov 2013 15:42:35 -0600 Subject: [PATCH 1/3] baksmali: Write comments about possible floating-point values --- .../Adaptors/Format/ArrayDataMethodItem.java | 6 +- .../Format/InstructionMethodItem.java | 59 ++++++++++++++++++- 2 files changed, 63 insertions(+), 2 deletions(-) diff --git a/baksmali/src/main/java/org/jf/baksmali/Adaptors/Format/ArrayDataMethodItem.java b/baksmali/src/main/java/org/jf/baksmali/Adaptors/Format/ArrayDataMethodItem.java index 1d4957ec..0ea28eab 100644 --- a/baksmali/src/main/java/org/jf/baksmali/Adaptors/Format/ArrayDataMethodItem.java +++ b/baksmali/src/main/java/org/jf/baksmali/Adaptors/Format/ArrayDataMethodItem.java @@ -65,8 +65,12 @@ public class ArrayDataMethodItem extends InstructionMethodItem { for (Number number: elements) { LongRenderer.writeSignedIntOrLongTo(writer, number.longValue()); writer.write(suffix); - if (elementWidth == 4) + if (elementWidth == 8) { + writeDouble(writer, number.longValue()); + } else if (elementWidth == 4) { writeResourceId(writer, number.intValue()); + writeFloat(writer, number.intValue()); + } writer.write("\n"); } writer.deindent(4); diff --git a/baksmali/src/main/java/org/jf/baksmali/Adaptors/Format/InstructionMethodItem.java b/baksmali/src/main/java/org/jf/baksmali/Adaptors/Format/InstructionMethodItem.java index e0a969cc..b81b0194 100644 --- a/baksmali/src/main/java/org/jf/baksmali/Adaptors/Format/InstructionMethodItem.java +++ b/baksmali/src/main/java/org/jf/baksmali/Adaptors/Format/InstructionMethodItem.java @@ -40,6 +40,7 @@ import org.jf.dexlib2.iface.instruction.formats.Instruction20bc; import org.jf.dexlib2.iface.instruction.formats.UnknownInstruction; import org.jf.dexlib2.iface.reference.Reference; import org.jf.util.IndentingWriter; +import org.jf.util.NumberUtils; import javax.annotation.Nonnull; import java.io.IOException; @@ -141,8 +142,12 @@ public class InstructionMethodItem extends MethodItem { writeFirstRegister(writer); writer.write(", "); writeLiteral(writer); - if (instruction.getOpcode().setsWideRegister() == false) + if (instruction.getOpcode().setsWideRegister()) { + writeDouble(writer); + } else { writeResourceId(writer); + writeFloat(writer); + } return true; case Format21t: case Format31t: @@ -340,6 +345,58 @@ public class InstructionMethodItem extends MethodItem { LongRenderer.writeSignedIntOrLongTo(writer, ((WideLiteralInstruction)instruction).getWideLiteral()); } + protected void writeFloat(IndentingWriter writer) throws IOException { + writeFloat(writer, ((NarrowLiteralInstruction)instruction).getNarrowLiteral()); + } + + protected void writeFloat(IndentingWriter writer, int val) throws IOException { + if (NumberUtils.isLikelyFloat(val)) { + writer.write(" # "); + float fval = Float.intBitsToFloat(val); + if (fval == Float.POSITIVE_INFINITY) + writer.write("Float.POSITIVE_INFINITY"); + else if (fval == Float.NEGATIVE_INFINITY) + writer.write("Float.NEGATIVE_INFINITY"); + else if (fval == Float.NaN) + writer.write("Float.NaN"); + else if (fval == Float.MAX_VALUE) + writer.write("Float.MAX_VALUE"); + else if (fval == (float)Math.PI) + writer.write("(float)Math.PI"); + else if (fval == (float)Math.E) + writer.write("(float)Math.E"); + else { + writer.write(Float.toString(fval)); + writer.write('f'); + } + } + } + + protected void writeDouble(IndentingWriter writer) throws IOException { + writeDouble(writer, ((WideLiteralInstruction)instruction).getWideLiteral()); + } + + protected void writeDouble(IndentingWriter writer, long val) throws IOException { + if (NumberUtils.isLikelyDouble(val)) { + writer.write(" # "); + double dval = Double.longBitsToDouble(val); + if (dval == Double.POSITIVE_INFINITY) + writer.write("Double.POSITIVE_INFINITY"); + else if (dval == Double.NEGATIVE_INFINITY) + writer.write("Double.NEGATIVE_INFINITY"); + else if (dval == Double.NaN) + writer.write("Double.NaN"); + else if (dval == Double.MAX_VALUE) + writer.write("Double.MAX_VALUE"); + else if (dval == Math.PI) + writer.write("Math.PI"); + else if (dval == Math.E) + writer.write("Math.E"); + else + writer.write(Double.toString(dval)); + } + } + protected void writeResourceId(IndentingWriter writer) throws IOException { writeResourceId(writer, ((NarrowLiteralInstruction)instruction).getNarrowLiteral()); } From 0b836342e21b4de21d1d452d5b43b54a364a35c6 Mon Sep 17 00:00:00 2001 From: Jeff Smith Date: Mon, 17 Feb 2014 00:08:50 -0600 Subject: [PATCH 2/3] Give comment-generating functions more precise naming --- .../Adaptors/Format/ArrayDataMethodItem.java | 7 +++--- .../Format/InstructionMethodItem.java | 25 ++++++++++--------- .../Format/PackedSwitchMethodItem.java | 2 +- .../Format/SparseSwitchMethodItem.java | 2 +- 4 files changed, 19 insertions(+), 17 deletions(-) diff --git a/baksmali/src/main/java/org/jf/baksmali/Adaptors/Format/ArrayDataMethodItem.java b/baksmali/src/main/java/org/jf/baksmali/Adaptors/Format/ArrayDataMethodItem.java index 0ea28eab..9c7b658c 100644 --- a/baksmali/src/main/java/org/jf/baksmali/Adaptors/Format/ArrayDataMethodItem.java +++ b/baksmali/src/main/java/org/jf/baksmali/Adaptors/Format/ArrayDataMethodItem.java @@ -66,10 +66,11 @@ public class ArrayDataMethodItem extends InstructionMethodItem { LongRenderer.writeSignedIntOrLongTo(writer, number.longValue()); writer.write(suffix); if (elementWidth == 8) { - writeDouble(writer, number.longValue()); + writeCommentIfLikelyDouble(writer, number.longValue()); } else if (elementWidth == 4) { - writeResourceId(writer, number.intValue()); - writeFloat(writer, number.intValue()); + int value = number.intValue(); + boolean isResourceId = writeCommentIfResourceId(writer, value); + if (!isResourceId) writeCommentIfLikelyFloat(writer, value); } writer.write("\n"); } diff --git a/baksmali/src/main/java/org/jf/baksmali/Adaptors/Format/InstructionMethodItem.java b/baksmali/src/main/java/org/jf/baksmali/Adaptors/Format/InstructionMethodItem.java index 9554f25c..556e7c2c 100644 --- a/baksmali/src/main/java/org/jf/baksmali/Adaptors/Format/InstructionMethodItem.java +++ b/baksmali/src/main/java/org/jf/baksmali/Adaptors/Format/InstructionMethodItem.java @@ -224,10 +224,10 @@ public class InstructionMethodItem extends MethodItem { writer.write(", "); writeLiteral(writer); if (instruction.getOpcode().setsWideRegister()) { - writeDouble(writer); + writeCommentIfLikelyDouble(writer); } else { - writeResourceId(writer); - writeFloat(writer); + boolean isResourceId = writeCommentIfResourceId(writer); + if (!isResourceId) writeCommentIfLikelyFloat(writer); } break; case Format21t: @@ -433,11 +433,11 @@ public class InstructionMethodItem extends MethodItem { LongRenderer.writeSignedIntOrLongTo(writer, ((WideLiteralInstruction)instruction).getWideLiteral()); } - protected void writeFloat(IndentingWriter writer) throws IOException { - writeFloat(writer, ((NarrowLiteralInstruction)instruction).getNarrowLiteral()); + protected void writeCommentIfLikelyFloat(IndentingWriter writer) throws IOException { + writeCommentIfLikelyFloat(writer, ((NarrowLiteralInstruction)instruction).getNarrowLiteral()); } - protected void writeFloat(IndentingWriter writer, int val) throws IOException { + protected void writeCommentIfLikelyFloat(IndentingWriter writer, int val) throws IOException { if (NumberUtils.isLikelyFloat(val)) { writer.write(" # "); float fval = Float.intBitsToFloat(val); @@ -460,11 +460,11 @@ public class InstructionMethodItem extends MethodItem { } } - protected void writeDouble(IndentingWriter writer) throws IOException { - writeDouble(writer, ((WideLiteralInstruction)instruction).getWideLiteral()); + protected void writeCommentIfLikelyDouble(IndentingWriter writer) throws IOException { + writeCommentIfLikelyDouble(writer, ((WideLiteralInstruction)instruction).getWideLiteral()); } - protected void writeDouble(IndentingWriter writer, long val) throws IOException { + protected void writeCommentIfLikelyDouble(IndentingWriter writer, long val) throws IOException { if (NumberUtils.isLikelyDouble(val)) { writer.write(" # "); double dval = Double.longBitsToDouble(val); @@ -485,17 +485,18 @@ public class InstructionMethodItem extends MethodItem { } } - protected void writeResourceId(IndentingWriter writer) throws IOException { - writeResourceId(writer, ((NarrowLiteralInstruction)instruction).getNarrowLiteral()); + protected boolean writeCommentIfResourceId(IndentingWriter writer) throws IOException { + return writeCommentIfResourceId(writer, ((NarrowLiteralInstruction)instruction).getNarrowLiteral()); } - protected void writeResourceId(IndentingWriter writer, int val) throws IOException { + protected boolean writeCommentIfResourceId(IndentingWriter writer, int val) throws IOException { Map resourceIds = methodDef.classDef.options.resourceIds; String resource = resourceIds.get(Integer.valueOf(val)); if (resource != null) { writer.write(" # "); writer.write(resource); } + return (resource != null); } protected void writeFieldOffset(IndentingWriter writer) throws IOException { diff --git a/baksmali/src/main/java/org/jf/baksmali/Adaptors/Format/PackedSwitchMethodItem.java b/baksmali/src/main/java/org/jf/baksmali/Adaptors/Format/PackedSwitchMethodItem.java index 5d7dea7e..f0dd656b 100644 --- a/baksmali/src/main/java/org/jf/baksmali/Adaptors/Format/PackedSwitchMethodItem.java +++ b/baksmali/src/main/java/org/jf/baksmali/Adaptors/Format/PackedSwitchMethodItem.java @@ -85,7 +85,7 @@ public class PackedSwitchMethodItem extends InstructionMethodItem "); target.writeTargetTo(writer); - writeResourceId(writer, target.getKey()); + writeCommentIfResourceId(writer, target.getKey()); writer.write('\n'); } writer.deindent(4); From 9cc35b2d05c6d97cc753b9cc956ea93a8b176c19 Mon Sep 17 00:00:00 2001 From: Jeff Smith Date: Fri, 15 Aug 2014 14:25:38 -0500 Subject: [PATCH 3/3] No need to reevaluate 'resource != null' --- .../org/jf/baksmali/Adaptors/Format/InstructionMethodItem.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/baksmali/src/main/java/org/jf/baksmali/Adaptors/Format/InstructionMethodItem.java b/baksmali/src/main/java/org/jf/baksmali/Adaptors/Format/InstructionMethodItem.java index 556e7c2c..cee478b6 100644 --- a/baksmali/src/main/java/org/jf/baksmali/Adaptors/Format/InstructionMethodItem.java +++ b/baksmali/src/main/java/org/jf/baksmali/Adaptors/Format/InstructionMethodItem.java @@ -495,8 +495,9 @@ public class InstructionMethodItem extends MethodItem { if (resource != null) { writer.write(" # "); writer.write(resource); + return true; } - return (resource != null); + return false; } protected void writeFieldOffset(IndentingWriter writer) throws IOException {