Commit 6951d0e6 authored by Skylot's avatar Skylot

core: use NotNull and Nullable annotations

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