diff --git a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/abc/AVM2LocalData.java b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/abc/AVM2LocalData.java index aaebe4a1a..879c317d7 100644 --- a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/abc/AVM2LocalData.java +++ b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/abc/AVM2LocalData.java @@ -29,6 +29,7 @@ import com.jpexs.decompiler.graph.GraphTargetItem; import com.jpexs.decompiler.graph.ScopeStack; import java.util.ArrayList; import java.util.HashMap; +import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; @@ -81,6 +82,16 @@ public class AVM2LocalData extends BaseLocalData { } + public Set getSetLocalUsages(int setLocalPos) { + if (setLocalPosToGetLocalPos == null) { + return new HashSet<>(); + } + if (!setLocalPosToGetLocalPos.containsKey(setLocalPos)) { + return new HashSet<>(); + } + return setLocalPosToGetLocalPos.get(setLocalPos); + } + public AVM2LocalData(AVM2LocalData localData) { isStatic = localData.isStatic; classIndex = localData.classIndex; diff --git a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/abc/avm2/AVM2Code.java b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/abc/avm2/AVM2Code.java index c95d45579..62eb0cc44 100644 --- a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/abc/avm2/AVM2Code.java +++ b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/abc/avm2/AVM2Code.java @@ -1602,7 +1602,7 @@ public class AVM2Code implements Cloneable { }*/ if ((ins.definition instanceof SetLocalTypeIns) && (ip + 1 <= end)) { // set_local_x,get_local_x.. no other local_x get AVM2Instruction insAfter = code.get(ip + 1); - Set usages = setLocalPosToGetLocalPos.get(ip); + Set usages = setLocalPosToGetLocalPos.containsKey(ip) ? setLocalPosToGetLocalPos.get(ip) : new HashSet<>(); if (usages.size() == 1 && (usages.iterator().next().equals(ip + 1)) && (insAfter.definition instanceof GetLocalTypeIns) && (((GetLocalTypeIns) insAfter.definition).getRegisterId(insAfter) == ((SetLocalTypeIns) ins.definition).getRegisterId(ins))) { ip += 2; diff --git a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/abc/avm2/graph/AVM2Graph.java b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/abc/avm2/graph/AVM2Graph.java index 5325b622d..b4883c4d4 100644 --- a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/abc/avm2/graph/AVM2Graph.java +++ b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/abc/avm2/graph/AVM2Graph.java @@ -1055,7 +1055,7 @@ public class AVM2Graph extends Graph { SetLocalAVM2Item setLocal = (SetLocalAVM2Item) output.get(i); if (setLocal.regIndex == objRegIndex) { int setLocalIp = aLocalData.code.adr2pos(setLocal.getSrc().getAddress()); - Set objUsages = new HashSet<>(aLocalData.setLocalPosToGetLocalPos.get(setLocalIp)); + Set objUsages = new HashSet<>(aLocalData.getSetLocalUsages(setLocalIp)); int hnUsageIp = aLocalData.code.adr2pos(hn.getSrc().getAddress()); objUsages.remove(hnUsageIp); @@ -1097,7 +1097,7 @@ public class AVM2Graph extends Graph { SetLocalAVM2Item setLocal = (SetLocalAVM2Item) output.get(i); if (setLocal.regIndex == indexRegIndex) { int setLocalIp = aLocalData.code.adr2pos(setLocal.getSrc().getAddress()); - Set objUsages = new HashSet<>(aLocalData.setLocalPosToGetLocalPos.get(setLocalIp)); + Set objUsages = new HashSet<>(aLocalData.getSetLocalUsages(setLocalIp)); int hnUsageIp = aLocalData.code.adr2pos(hn.getSrc().getAddress()); objUsages.remove(hnUsageIp); @@ -1191,7 +1191,29 @@ public class AVM2Graph extends Graph { continue; } } - if (usages.size() <= 1) { + //if (usages.size() <= 1) + { + if (i + 1 < list.size()) { + if ((list.get(i + 1) instanceof ReturnValueAVM2Item) + && (list.get(i + 1).value instanceof LocalRegAVM2Item) + && (((LocalRegAVM2Item) list.get(i + 1).value).regIndex == ri.regIndex)) { + ReturnValueAVM2Item r = (ReturnValueAVM2Item) list.get(i + 1); + r.value = ri.value; + list.remove(i); + i--; + continue; + } + if ((list.get(i + 1) instanceof ThrowAVM2Item) + && (list.get(i + 1).value instanceof LocalRegAVM2Item) + && (((LocalRegAVM2Item) list.get(i + 1).value).regIndex == ri.regIndex)) { + ThrowAVM2Item t = (ThrowAVM2Item) list.get(i + 2); + t.value = ri.value; + list.remove(i); + i--; + continue; + } + } + if (i + 2 < list.size()) { if ((list.get(i + 1) instanceof IntegerValueAVM2Item) && (list.get(i + 2) instanceof ReturnValueAVM2Item) && (list.get(i + 2).value instanceof LocalRegAVM2Item) @@ -1211,7 +1233,7 @@ public class AVM2Graph extends Graph { list.remove(i + 1); list.remove(i); i--; - //continue; + continue; } } else if (i + 1 < list.size() && usages.isEmpty()) { if (list.get(i + 1) instanceof IntegerValueAVM2Item) { @@ -1304,7 +1326,7 @@ public class AVM2Graph extends Graph { AVM2LocalData avm2LocalData = (AVM2LocalData) localData; SetLocalAVM2Item setLocal = (SetLocalAVM2Item) output.get(output.size() - 1); int setLocalIp = InstructionDefinition.getItemIp(avm2LocalData, setLocal);; - Set allUsages = new HashSet<>(avm2LocalData.setLocalPosToGetLocalPos.get(setLocalIp)); + Set allUsages = new HashSet<>(avm2LocalData.getSetLocalUsages(setLocalIp)); for (GraphTargetItem otherSide : otherSides.values()) { if (otherSide instanceof LocalRegAVM2Item) { LocalRegAVM2Item otherLog = (LocalRegAVM2Item) otherSide; diff --git a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/abc/avm2/instructions/InstructionDefinition.java b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/abc/avm2/instructions/InstructionDefinition.java index 723f40d07..49d79b4b8 100644 --- a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/abc/avm2/instructions/InstructionDefinition.java +++ b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/abc/avm2/instructions/InstructionDefinition.java @@ -328,7 +328,7 @@ public abstract class InstructionDefinition implements Serializable { SetLocalAVM2Item setLocal = (SetLocalAVM2Item) output.get(i); if (setLocal.regIndex == reg.regIndex) { int setLocalIp = getItemIp(localData, setLocal); - Set usages = localData.setLocalPosToGetLocalPos.get(setLocalIp); + Set usages = localData.getSetLocalUsages(setLocalIp); int usageIp = getItemIp(localData, reg); if (usages.size() == 1 && usages.iterator().next().equals(usageIp)) { output.remove(i); diff --git a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/abc/avm2/instructions/localregs/GetLocalTypeIns.java b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/abc/avm2/instructions/localregs/GetLocalTypeIns.java index f9f6abba9..7cba66b9f 100644 --- a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/abc/avm2/instructions/localregs/GetLocalTypeIns.java +++ b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/abc/avm2/instructions/localregs/GetLocalTypeIns.java @@ -96,7 +96,7 @@ public abstract class GetLocalTypeIns extends InstructionDefinition { int setLocalIp = getItemIp(localData, setLocal); int getLocalIp = localData.code.adr2pos(ins.getAddress()); - Set usages = localData.setLocalPosToGetLocalPos.get(setLocalIp); + Set usages = localData.getSetLocalUsages(setLocalIp); if (usages.size() == 1 && usages.iterator().next().equals(getLocalIp)) { output.remove(output.size() - 1); output.remove(output.size() - 1); diff --git a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/abc/avm2/instructions/localregs/SetLocalTypeIns.java b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/abc/avm2/instructions/localregs/SetLocalTypeIns.java index 8514ba51b..78ddfe132 100644 --- a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/abc/avm2/instructions/localregs/SetLocalTypeIns.java +++ b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/abc/avm2/instructions/localregs/SetLocalTypeIns.java @@ -37,6 +37,7 @@ import com.jpexs.decompiler.flash.abc.types.MethodBody; import com.jpexs.decompiler.graph.DottedChain; import com.jpexs.decompiler.graph.GraphTargetItem; import com.jpexs.decompiler.graph.TranslateStack; +import com.jpexs.decompiler.graph.model.DuplicateItem; import com.jpexs.decompiler.graph.model.PopItem; import java.util.HashMap; import java.util.List; @@ -128,6 +129,9 @@ public abstract class SetLocalTypeIns extends InstructionDefinition implements S } } + if (localData.getSetLocalUsages(localData.code.adr2pos(ins.getAddress())).isEmpty() && (value instanceof DuplicateItem)) { + return; + } GraphTargetItem result = new SetLocalAVM2Item(ins, localData.lineStartInstruction, regId, value); output.add(result); } diff --git a/libsrc/ffdec_lib/src/com/jpexs/decompiler/graph/model/DuplicateItem.java b/libsrc/ffdec_lib/src/com/jpexs/decompiler/graph/model/DuplicateItem.java index 81be012b7..3843e4885 100644 --- a/libsrc/ffdec_lib/src/com/jpexs/decompiler/graph/model/DuplicateItem.java +++ b/libsrc/ffdec_lib/src/com/jpexs/decompiler/graph/model/DuplicateItem.java @@ -49,10 +49,8 @@ public class DuplicateItem extends GraphTargetItem implements SimpleValue { @Override public GraphTextWriter appendTo(GraphTextWriter writer, LocalData localData) throws InterruptedException { - if (!Configuration.displayDupInstructions.get()) { - if (((value instanceof SimpleValue) && (((SimpleValue) value).isSimpleValue())) || !Configuration.displayDupInstructions.get()) { - return value.appendTry(writer, localData); - } + if (((value instanceof SimpleValue) && (((SimpleValue) value).isSimpleValue())) || !Configuration.displayDupInstructions.get()) { + return value.appendTry(writer, localData); } writer.append("§§dup("); value.appendTry(writer, localData); diff --git a/libsrc/ffdec_lib/testdata/flashdevelop/bin/flashdevelop.swf b/libsrc/ffdec_lib/testdata/flashdevelop/bin/flashdevelop.swf index 95d9333cd..fb526fa78 100644 Binary files a/libsrc/ffdec_lib/testdata/flashdevelop/bin/flashdevelop.swf and b/libsrc/ffdec_lib/testdata/flashdevelop/bin/flashdevelop.swf differ diff --git a/libsrc/ffdec_lib/testdata/flashdevelop/src/Main.as b/libsrc/ffdec_lib/testdata/flashdevelop/src/Main.as index 0d65a2786..ba8a5c618 100644 --- a/libsrc/ffdec_lib/testdata/flashdevelop/src/Main.as +++ b/libsrc/ffdec_lib/testdata/flashdevelop/src/Main.as @@ -76,6 +76,7 @@ package TestTernarOperator; TestTry; TestTryReturn; + TestTryReturn2; TestVector; TestVector2; TestWhileAnd; diff --git a/libsrc/ffdec_lib/testdata/flashdevelop/src/tests/TestTryReturn2.as b/libsrc/ffdec_lib/testdata/flashdevelop/src/tests/TestTryReturn2.as new file mode 100644 index 000000000..708a22ba9 --- /dev/null +++ b/libsrc/ffdec_lib/testdata/flashdevelop/src/tests/TestTryReturn2.as @@ -0,0 +1,42 @@ +package tests +{ + public class TestTryReturn2 + { + + public function run() : String + { + trace("before"); + var a:Boolean = true; + var b:Boolean = false; + var c:Boolean = true; + var d:Boolean = false; + try + { + if (a) + { + return "A"; + } + if (b) + { + return "B"; + } + } + catch (e:Error) + { + if (d){ + return "D"; + } + } + finally + { + if (c) { + return "C"; + } + } + trace("after"); + return "X"; + } + + } + +} \ No newline at end of file