Commit 6951d0e6 authored by Skylot's avatar Skylot

core: use NotNull and Nullable annotations

parent 73dd55ea
...@@ -5,6 +5,7 @@ dependencies { ...@@ -5,6 +5,7 @@ dependencies {
compile files('lib/dx-1.8.jar') compile files('lib/dx-1.8.jar')
compile 'org.ow2.asm:asm:5.0.3' compile 'org.ow2.asm:asm:5.0.3'
compile 'com.intellij:annotations:12.0'
testCompile 'org.smali:smali:2.0.3' testCompile 'org.smali:smali:2.0.3'
} }
......
...@@ -19,7 +19,8 @@ public class RegisterArg extends InsnArg implements Named { ...@@ -19,7 +19,8 @@ public class RegisterArg extends InsnArg implements Named {
private static final Logger LOG = LoggerFactory.getLogger(RegisterArg.class); private static final Logger LOG = LoggerFactory.getLogger(RegisterArg.class);
protected final int regNum; protected final int regNum;
protected SSAVar sVar; // not null after SSATransform pass
private SSAVar sVar;
public RegisterArg(int rn) { public RegisterArg(int rn) {
this.regNum = rn; this.regNum = rn;
...@@ -139,11 +140,7 @@ public class RegisterArg extends InsnArg implements Named { ...@@ -139,11 +140,7 @@ public class RegisterArg extends InsnArg implements Named {
if (sVar == null) { if (sVar == null) {
return null; return null;
} }
RegisterArg assign = sVar.getAssign(); return sVar.getAssign().getParentInsn();
if (assign != null) {
return assign.getParentInsn();
}
return null;
} }
public InsnNode getPhiAssignInsn() { public InsnNode getPhiAssignInsn() {
...@@ -151,12 +148,9 @@ public class RegisterArg extends InsnArg implements Named { ...@@ -151,12 +148,9 @@ public class RegisterArg extends InsnArg implements Named {
if (usePhi != null) { if (usePhi != null) {
return usePhi; return usePhi;
} }
RegisterArg assign = sVar.getAssign(); InsnNode parent = sVar.getAssign().getParentInsn();
if (assign != null) { if (parent != null && parent.getType() == InsnType.PHI) {
InsnNode parent = assign.getParentInsn(); return parent;
if (parent != null && parent.getType() == InsnType.PHI) {
return parent;
}
} }
return null; return null;
} }
......
...@@ -5,6 +5,9 @@ import jadx.core.dex.instructions.PhiInsn; ...@@ -5,6 +5,9 @@ import jadx.core.dex.instructions.PhiInsn;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public class SSAVar { public class SSAVar {
private final int regNum; private final int regNum;
...@@ -14,21 +17,21 @@ public class SSAVar { ...@@ -14,21 +17,21 @@ public class SSAVar {
private int startUseAddr; private int startUseAddr;
private int endUseAddr; private int endUseAddr;
@NotNull
private RegisterArg assign; private RegisterArg assign;
private final List<RegisterArg> useList = new ArrayList<RegisterArg>(2); private final List<RegisterArg> useList = new ArrayList<RegisterArg>(2);
@Nullable
private PhiInsn usedInPhi; private PhiInsn usedInPhi;
private ArgType type; private ArgType type;
private boolean typeImmutable; private boolean typeImmutable;
public SSAVar(int regNum, int v, RegisterArg assign) { public SSAVar(int regNum, int v, @NotNull RegisterArg assign) {
this.regNum = regNum; this.regNum = regNum;
this.version = v; this.version = v;
this.assign = assign; this.assign = assign;
if (assign != null) { assign.setSVar(this);
assign.setSVar(this);
}
startUseAddr = -1; startUseAddr = -1;
endUseAddr = -1; endUseAddr = -1;
} }
...@@ -51,7 +54,7 @@ public class SSAVar { ...@@ -51,7 +54,7 @@ public class SSAVar {
int start = Integer.MAX_VALUE; int start = Integer.MAX_VALUE;
int end = Integer.MIN_VALUE; int end = Integer.MIN_VALUE;
if (assign != null && assign.getParentInsn() != null) { if (assign.getParentInsn() != null) {
int insnAddr = assign.getParentInsn().getOffset(); int insnAddr = assign.getParentInsn().getOffset();
if (insnAddr >= 0) { if (insnAddr >= 0) {
start = Math.min(insnAddr, start); start = Math.min(insnAddr, start);
...@@ -81,11 +84,12 @@ public class SSAVar { ...@@ -81,11 +84,12 @@ public class SSAVar {
return version; return version;
} }
@NotNull
public RegisterArg getAssign() { public RegisterArg getAssign() {
return assign; return assign;
} }
public void setAssign(RegisterArg assign) { public void setAssign(@NotNull RegisterArg assign) {
this.assign = assign; this.assign = assign;
} }
...@@ -114,10 +118,11 @@ public class SSAVar { ...@@ -114,10 +118,11 @@ public class SSAVar {
} }
} }
public void setUsedInPhi(PhiInsn usedInPhi) { public void setUsedInPhi(@Nullable PhiInsn usedInPhi) {
this.usedInPhi = usedInPhi; this.usedInPhi = usedInPhi;
} }
@Nullable
public PhiInsn getUsedInPhi() { public PhiInsn getUsedInPhi() {
return usedInPhi; return usedInPhi;
} }
...@@ -127,7 +132,7 @@ public class SSAVar { ...@@ -127,7 +132,7 @@ public class SSAVar {
} }
public int getVariableUseCount() { public int getVariableUseCount() {
if (!isUsedInPhi()) { if (usedInPhi == null) {
return useList.size(); return useList.size();
} }
return useList.size() + usedInPhi.getResult().getSVar().getUseCount(); return useList.size() + usedInPhi.getResult().getSVar().getUseCount();
...@@ -142,9 +147,7 @@ public class SSAVar { ...@@ -142,9 +147,7 @@ public class SSAVar {
acceptedType = type; acceptedType = type;
this.type = acceptedType; this.type = acceptedType;
} }
if (assign != null) { assign.type = acceptedType;
assign.type = acceptedType;
}
for (int i = 0, useListSize = useList.size(); i < useListSize; i++) { for (int i = 0, useListSize = useList.size(); i < useListSize; i++) {
useList.get(i).type = acceptedType; useList.get(i).type = acceptedType;
} }
......
...@@ -60,8 +60,8 @@ public class TernaryMod { ...@@ -60,8 +60,8 @@ public class TernaryMod {
} }
if (t.getResult() != null && e.getResult() != null) { if (t.getResult() != null && e.getResult() != null) {
if (!t.getResult().equalRegisterAndType(e.getResult()) PhiInsn phi = t.getResult().getSVar().getUsedInPhi();
|| !t.getResult().getSVar().isUsedInPhi()) { if (phi == null || !t.getResult().equalRegisterAndType(e.getResult())) {
return false; return false;
} }
if (!ifRegion.getParent().replaceSubBlock(ifRegion, header)) { if (!ifRegion.getParent().replaceSubBlock(ifRegion, header)) {
...@@ -71,7 +71,6 @@ public class TernaryMod { ...@@ -71,7 +71,6 @@ public class TernaryMod {
InsnList.remove(eb, e); InsnList.remove(eb, e);
RegisterArg resArg; RegisterArg resArg;
PhiInsn phi = t.getResult().getSVar().getUsedInPhi();
if (phi.getArgsCount() == 2) { if (phi.getArgsCount() == 2) {
resArg = phi.getResult(); resArg = phi.getResult();
} else { } else {
......
...@@ -28,15 +28,14 @@ public class TypeInference extends AbstractVisitor { ...@@ -28,15 +28,14 @@ public class TypeInference extends AbstractVisitor {
// search variable name // search variable name
String name = processVarName(var); String name = processVarName(var);
if (name != null) { var.setName(name);
var.setName(name);
}
} }
// fix type for vars used only in Phi nodes // fix type for vars used only in Phi nodes
for (SSAVar sVar : mth.getSVars()) { for (SSAVar sVar : mth.getSVars()) {
if (sVar.isUsedInPhi()) { PhiInsn phi = sVar.getUsedInPhi();
processPhiNode(sVar.getUsedInPhi()); if (phi != null) {
processPhiNode(phi);
} }
} }
} }
...@@ -44,10 +43,10 @@ public class TypeInference extends AbstractVisitor { ...@@ -44,10 +43,10 @@ public class TypeInference extends AbstractVisitor {
private static ArgType processType(SSAVar var) { private static ArgType processType(SSAVar var) {
RegisterArg assign = var.getAssign(); RegisterArg assign = var.getAssign();
List<RegisterArg> useList = var.getUseList(); List<RegisterArg> useList = var.getUseList();
if (assign != null && (useList.isEmpty() || var.isTypeImmutable())) { if (useList.isEmpty() || var.isTypeImmutable()) {
return assign.getType(); return assign.getType();
} }
ArgType type = assign != null ? assign.getType() : ArgType.UNKNOWN; ArgType type = assign.getType();
for (RegisterArg arg : useList) { for (RegisterArg arg : useList) {
ArgType useType = arg.getType(); ArgType useType = arg.getType();
ArgType newType = ArgType.merge(type, useType); ArgType newType = ArgType.merge(type, useType);
...@@ -77,19 +76,16 @@ public class TypeInference extends AbstractVisitor { ...@@ -77,19 +76,16 @@ public class TypeInference extends AbstractVisitor {
} }
private static String processVarName(SSAVar var) { private static String processVarName(SSAVar var) {
String name = null; String name = var.getAssign().getName();
if (var.getAssign() != null) {
name = var.getAssign().getName();
}
if (name != null) { if (name != null) {
return name; return name;
} }
for (RegisterArg arg : var.getUseList()) { for (RegisterArg arg : var.getUseList()) {
String vName = arg.getName(); String vName = arg.getName();
if (vName != null) { if (vName != null) {
name = vName; return vName;
} }
} }
return name; return null;
} }
} }
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