AS1/2 - Compound assignments detection

This commit is contained in:
Jindra Petřík
2021-03-07 23:35:04 +01:00
parent 23227caacd
commit 89ea68923c
12 changed files with 242 additions and 10 deletions

View File

@@ -156,4 +156,23 @@ public class DefineLocalActionItem extends ActionItem implements SetTypeActionIt
return true;
}
@Override
public GraphTargetItem getCompoundValue() {
throw new UnsupportedOperationException("Not supported.");
}
@Override
public void setCompoundValue(GraphTargetItem value) {
throw new UnsupportedOperationException("Not supported.");
}
@Override
public void setCompoundOperator(String operator) {
throw new UnsupportedOperationException("Not supported.");
}
@Override
public String getCompoundOperator() {
throw new UnsupportedOperationException("Not supported.");
}
}

View File

@@ -239,9 +239,10 @@ public class DirectValueActionItem extends ActionItem implements SimpleValue {
if (!Objects.equals(this.constants, other.constants)) {
return false;
}
if (other.pos != this.pos) {
//??
/*if (other.pos != this.pos) {
return false;
}
}*/
return true;
}

View File

@@ -174,4 +174,23 @@ public class PostDecrementActionItem extends ActionItem implements SetTypeAction
return true;
}
@Override
public GraphTargetItem getCompoundValue() {
throw new UnsupportedOperationException("Not supported.");
}
@Override
public void setCompoundValue(GraphTargetItem value) {
throw new UnsupportedOperationException("Not supported.");
}
@Override
public void setCompoundOperator(String operator) {
throw new UnsupportedOperationException("Not supported.");
}
@Override
public String getCompoundOperator() {
throw new UnsupportedOperationException("Not supported.");
}
}

View File

@@ -174,4 +174,23 @@ public class PostIncrementActionItem extends ActionItem implements SetTypeAction
return true;
}
@Override
public GraphTargetItem getCompoundValue() {
throw new UnsupportedOperationException("Not supported.");
}
@Override
public void setCompoundValue(GraphTargetItem value) {
throw new UnsupportedOperationException("Not supported.");
}
@Override
public void setCompoundOperator(String operator) {
throw new UnsupportedOperationException("Not supported.");
}
@Override
public String getCompoundOperator() {
throw new UnsupportedOperationException("Not supported.");
}
}

View File

@@ -47,6 +47,10 @@ public class SetMemberActionItem extends ActionItem implements SetTypeActionItem
private int tempRegister = -1;
public GraphTargetItem compoundValue;
public String compoundOperator;
@Override
public void visit(GraphTargetVisitorInterface visitor) {
visitor.visit(object);
@@ -97,6 +101,12 @@ public class SetMemberActionItem extends ActionItem implements SetTypeActionItem
writer.append(".");
stripQuotes(objectName, localData, writer);
}
if (compoundOperator != null) {
writer.append(" ");
writer.append(compoundOperator);
writer.append("= ");
return compoundValue.toString(writer, localData);
}
writer.append(" = ");
return value.toString(writer, localData);
@@ -174,4 +184,24 @@ public class SetMemberActionItem extends ActionItem implements SetTypeActionItem
return true;
}
@Override
public GraphTargetItem getCompoundValue() {
return compoundValue;
}
@Override
public void setCompoundValue(GraphTargetItem value) {
this.compoundValue = value;
}
@Override
public void setCompoundOperator(String operator) {
this.compoundOperator = operator;
}
@Override
public String getCompoundOperator() {
return compoundOperator;
}
}

View File

@@ -167,4 +167,24 @@ public class SetPropertyActionItem extends ActionItem implements SetTypeActionIt
return true;
}
@Override
public GraphTargetItem getCompoundValue() {
throw new UnsupportedOperationException("Not supported.");
}
@Override
public void setCompoundValue(GraphTargetItem value) {
throw new UnsupportedOperationException("Not supported.");
}
@Override
public void setCompoundOperator(String operator) {
throw new UnsupportedOperationException("Not supported.");
}
@Override
public String getCompoundOperator() {
throw new UnsupportedOperationException("Not supported.");
}
}

View File

@@ -12,7 +12,8 @@
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library.
* License along with this library.
*/
package com.jpexs.decompiler.flash.action.model;
import com.jpexs.decompiler.graph.GraphTargetItem;
@@ -32,4 +33,12 @@ public interface SetTypeActionItem {
public int getTempRegister();
public void setValue(GraphTargetItem value);
public GraphTargetItem getCompoundValue();
public void setCompoundValue(GraphTargetItem value);
public void setCompoundOperator(String operator);
public String getCompoundOperator();
}

View File

@@ -46,6 +46,10 @@ public class SetVariableActionItem extends ActionItem implements SetTypeActionIt
private int tempRegister = -1;
public GraphTargetItem compoundValue;
public String compoundOperator;
@Override
public GraphPart getFirstPart() {
return value.getFirstPart();
@@ -84,6 +88,12 @@ public class SetVariableActionItem extends ActionItem implements SetTypeActionIt
HighlightData srcData = getSrcData();
srcData.localName = name.toStringNoQuotes(localData);
stripQuotes(name, localData, writer);
if (compoundOperator != null) {
writer.append(" ");
writer.append(compoundOperator);
writer.append("= ");
return compoundValue.toString(writer, localData);
}
writer.append(" = ");
return value.toString(writer, localData);
} else {
@@ -174,4 +184,23 @@ public class SetVariableActionItem extends ActionItem implements SetTypeActionIt
return true;
}
@Override
public GraphTargetItem getCompoundValue() {
return compoundValue;
}
@Override
public void setCompoundValue(GraphTargetItem value) {
this.compoundValue = value;
}
@Override
public void setCompoundOperator(String operator) {
this.compoundOperator = operator;
}
@Override
public String getCompoundOperator() {
return compoundOperator;
}
}

View File

@@ -45,6 +45,10 @@ public class StoreRegisterActionItem extends ActionItem implements SetTypeAction
public boolean temporary = false;
public GraphTargetItem compoundValue;
public String compoundOperator;
@Override
public GraphPart getFirstPart() {
return value.getFirstPart();
@@ -92,7 +96,15 @@ public class StoreRegisterActionItem extends ActionItem implements SetTypeAction
srcData.declaredType = DottedChain.ALL;
writer.append("var ");
}
writer.append(register.translate()).append(" = ");
writer.append(register.translate());
if (compoundOperator != null) {
writer.append(" ");
writer.append(compoundOperator);
writer.append("= ");
return compoundValue.toString(writer, localData);
}
writer.append(" = ");
value.toString(writer, localData);
}
return writer;
@@ -166,4 +178,24 @@ public class StoreRegisterActionItem extends ActionItem implements SetTypeAction
public boolean hasSideEffect() {
return true;
}
@Override
public GraphTargetItem getCompoundValue() {
return compoundValue;
}
@Override
public void setCompoundValue(GraphTargetItem value) {
this.compoundValue = value;
}
@Override
public void setCompoundOperator(String operator) {
this.compoundOperator = operator;
}
@Override
public String getCompoundOperator() {
return compoundOperator;
}
}

View File

@@ -12,7 +12,8 @@
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library.
* License along with this library.
*/
package com.jpexs.decompiler.flash.action.swf4;
import com.jpexs.decompiler.flash.BaseLocalData;
@@ -36,9 +37,11 @@ import com.jpexs.decompiler.flash.types.annotations.SWFVersion;
import com.jpexs.decompiler.graph.GraphSourceItem;
import com.jpexs.decompiler.graph.GraphTargetItem;
import com.jpexs.decompiler.graph.TranslateStack;
import com.jpexs.decompiler.graph.model.CompoundableBinaryOp;
import com.jpexs.decompiler.graph.model.LocalData;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;
/**
*
@@ -111,7 +114,21 @@ public class ActionSetVariable extends Action implements StoreTypeAction {
}
}
SetVariableActionItem setVar = new SetVariableActionItem(this, lineStartAction, name, value);
GraphTargetItem ret = setVar;
if (value.getNotCoercedNoDup() instanceof CompoundableBinaryOp) {
if (!name.hasSideEffect()) {
CompoundableBinaryOp binaryOp = (CompoundableBinaryOp) value.getNotCoercedNoDup();
if (binaryOp.getLeftSide() instanceof GetVariableActionItem) {
GetVariableActionItem getVar = (GetVariableActionItem) binaryOp.getLeftSide();
if (Objects.equals(name, getVar.name)) {
setVar.setCompoundValue(binaryOp.getRightSide());
setVar.setCompoundOperator(binaryOp.getOperator());
}
}
}
}
if (value instanceof StoreRegisterActionItem) {
StoreRegisterActionItem sr = (StoreRegisterActionItem) value;

View File

@@ -12,7 +12,8 @@
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library.
* License along with this library.
*/
package com.jpexs.decompiler.flash.action.swf5;
import com.jpexs.decompiler.flash.BaseLocalData;
@@ -33,8 +34,10 @@ import com.jpexs.decompiler.flash.types.annotations.SWFVersion;
import com.jpexs.decompiler.graph.GraphSourceItem;
import com.jpexs.decompiler.graph.GraphTargetItem;
import com.jpexs.decompiler.graph.TranslateStack;
import com.jpexs.decompiler.graph.model.CompoundableBinaryOp;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;
/**
*
@@ -110,7 +113,23 @@ public class ActionSetMember extends Action {
}
}
}
}
SetMemberActionItem setMem = new SetMemberActionItem(this, lineStartAction, object, memberName, value);
if (value.getNotCoercedNoDup() instanceof CompoundableBinaryOp) {
if (!object.hasSideEffect() && !memberName.hasSideEffect()) {
CompoundableBinaryOp binaryOp = (CompoundableBinaryOp) value.getNotCoercedNoDup();
if (binaryOp.getLeftSide() instanceof GetMemberActionItem) {
GetMemberActionItem getMember = (GetMemberActionItem) binaryOp.getLeftSide();
if (Objects.equals(object, getMember.object.getThroughDuplicate()) && Objects.equals(memberName, getMember.memberName)) {
setMem.setCompoundValue(binaryOp.getRightSide());
setMem.setCompoundOperator(binaryOp.getOperator());
}
}
}
}
GraphTargetItem ret = setMem;
if (value instanceof StoreRegisterActionItem) {
StoreRegisterActionItem sr = (StoreRegisterActionItem) value;
if (sr.define) {

View File

@@ -12,7 +12,8 @@
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library.
* License along with this library.
*/
package com.jpexs.decompiler.flash.action.swf5;
import com.jpexs.decompiler.flash.BaseLocalData;
@@ -26,6 +27,7 @@ import com.jpexs.decompiler.flash.action.model.DecrementActionItem;
import com.jpexs.decompiler.flash.action.model.DirectValueActionItem;
import com.jpexs.decompiler.flash.action.model.EnumeratedValueActionItem;
import com.jpexs.decompiler.flash.action.model.EnumerationAssignmentValueActionItem;
import com.jpexs.decompiler.flash.action.model.GetMemberActionItem;
import com.jpexs.decompiler.flash.action.model.IncrementActionItem;
import com.jpexs.decompiler.flash.action.model.PostDecrementActionItem;
import com.jpexs.decompiler.flash.action.model.PostIncrementActionItem;
@@ -39,9 +41,11 @@ import com.jpexs.decompiler.graph.GraphSourceItem;
import com.jpexs.decompiler.graph.GraphSourceItemPos;
import com.jpexs.decompiler.graph.GraphTargetItem;
import com.jpexs.decompiler.graph.TranslateStack;
import com.jpexs.decompiler.graph.model.CompoundableBinaryOp;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;
/**
*
@@ -160,7 +164,21 @@ public class ActionStoreRegister extends Action implements StoreTypeAction {
if ((value instanceof EnumeratedValueActionItem)) {
variables.put("__register" + registerNumber, new TemporaryRegister(registerNumber, new EnumerationAssignmentValueActionItem()));
}
}
StoreRegisterActionItem ret = new StoreRegisterActionItem(this, lineStartAction, rn, value, define);
if (value.getNotCoercedNoDup() instanceof CompoundableBinaryOp) {
CompoundableBinaryOp binaryOp = (CompoundableBinaryOp) value.getNotCoercedNoDup();
if (binaryOp.getLeftSide() instanceof DirectValueActionItem) {
DirectValueActionItem directValue = (DirectValueActionItem) binaryOp.getLeftSide();
if (directValue.value instanceof RegisterNumber) {
if (((RegisterNumber) directValue.value).number == registerNumber) {
ret.setCompoundValue(binaryOp.getRightSide());
ret.setCompoundOperator(binaryOp.getOperator());
}
}
}
}
stack.push(ret);
}
@Override