Commit 0e04dc72 authored by Skylot's avatar Skylot

Avoid variable names clash

parent 484e07df
...@@ -101,7 +101,7 @@ public class InsnGen { ...@@ -101,7 +101,7 @@ public class InsnGen {
} }
public String declareVar(RegisterArg arg) throws CodegenException { public String declareVar(RegisterArg arg) throws CodegenException {
return useType(arg.getType()) + " " + arg(arg); return useType(arg.getType()) + " " + mgen.assignArg(arg);
} }
private String lit(LiteralArg arg) { private String lit(LiteralArg arg) {
......
...@@ -20,12 +20,9 @@ import jadx.utils.Utils; ...@@ -20,12 +20,9 @@ import jadx.utils.Utils;
import jadx.utils.exceptions.CodegenException; import jadx.utils.exceptions.CodegenException;
import jadx.utils.exceptions.DecodeException; import jadx.utils.exceptions.DecodeException;
import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set; import java.util.Set;
import org.slf4j.Logger; import org.slf4j.Logger;
...@@ -37,12 +34,12 @@ public class MethodGen { ...@@ -37,12 +34,12 @@ public class MethodGen {
private static final Logger LOG = LoggerFactory.getLogger(MethodGen.class); private static final Logger LOG = LoggerFactory.getLogger(MethodGen.class);
private final MethodNode mth; private final MethodNode mth;
private final Set<String> mthArgsDecls;
private final Map<String, ArgType> varDecls = new HashMap<String, ArgType>();
private final ClassGen classGen; private final ClassGen classGen;
private final boolean fallback; private final boolean fallback;
private final AnnotationGen annotationGen; private final AnnotationGen annotationGen;
private final Set<String> varNames = new HashSet<String>();
public MethodGen(ClassGen classGen, MethodNode mth) { public MethodGen(ClassGen classGen, MethodNode mth) {
this.mth = mth; this.mth = mth;
this.classGen = classGen; this.classGen = classGen;
...@@ -50,9 +47,8 @@ public class MethodGen { ...@@ -50,9 +47,8 @@ public class MethodGen {
this.annotationGen = classGen.getAnnotationGen(); this.annotationGen = classGen.getAnnotationGen();
List<RegisterArg> args = mth.getArguments(true); List<RegisterArg> args = mth.getArguments(true);
mthArgsDecls = new HashSet<String>(args.size());
for (RegisterArg arg : args) { for (RegisterArg arg : args) {
mthArgsDecls.add(makeArgName(arg)); varNames.add(makeArgName(arg));
} }
} }
...@@ -183,11 +179,28 @@ public class MethodGen { ...@@ -183,11 +179,28 @@ public class MethodGen {
*/ */
public String assignArg(RegisterArg arg) { public String assignArg(RegisterArg arg) {
String name = makeArgName(arg); String name = makeArgName(arg);
if (!mthArgsDecls.contains(name)) if (varNames.add(name))
varDecls.put(name, arg.getType()); return name;
if (fallback)
return name;
name = getUniqVarName(name);
arg.getTypedVar().setName(name);
return name; return name;
} }
private String getUniqVarName(String name) {
String r;
int i = 2;
do {
r = name + i;
i++;
} while (varNames.contains(r));
varNames.add(r);
return r;
}
private void makeInitCode(CodeWriter code) throws CodegenException { private void makeInitCode(CodeWriter code) throws CodegenException {
InsnGen igen = new InsnGen(this, mth, fallback); InsnGen igen = new InsnGen(this, mth, fallback);
// generate super call // generate super call
...@@ -195,17 +208,6 @@ public class MethodGen { ...@@ -195,17 +208,6 @@ public class MethodGen {
igen.makeInsn(mth.getSuperCall(), code); igen.makeInsn(mth.getSuperCall(), code);
} }
public void makeVariablesDeclaration(CodeWriter code) {
for (Entry<String, ArgType> var : varDecls.entrySet()) {
code.startLine(TypeGen.translate(classGen, var.getValue()));
code.add(" ");
code.add(var.getKey());
code.add(";");
}
if (!varDecls.isEmpty())
code.endl();
}
public CodeWriter makeInstructions(int mthIndent) throws CodegenException { public CodeWriter makeInstructions(int mthIndent) throws CodegenException {
CodeWriter code = new CodeWriter(mthIndent + 1); CodeWriter code = new CodeWriter(mthIndent + 1);
...@@ -242,7 +244,6 @@ public class MethodGen { ...@@ -242,7 +244,6 @@ public class MethodGen {
(new RegionGen(this, mth)).makeRegion(insns, mth.getRegion()); (new RegionGen(this, mth)).makeRegion(insns, mth.getRegion());
makeInitCode(code); makeInitCode(code);
makeVariablesDeclaration(code);
code.add(insns); code.add(insns);
} else { } else {
makeFallbackMethod(code, mth); makeFallbackMethod(code, mth);
......
...@@ -107,13 +107,16 @@ public class ProcessVariables extends AbstractVisitor { ...@@ -107,13 +107,16 @@ public class ProcessVariables extends AbstractVisitor {
// reduce assigns map // reduce assigns map
List<RegisterArg> mthArgs = mth.getArguments(true); List<RegisterArg> mthArgs = mth.getArguments(true);
for (RegisterArg arg : mthArgs) {
usageMap.remove(arg);
}
for (Iterator<Entry<RegisterArg, Usage>> it = usageMap.entrySet().iterator(); it.hasNext();) { for (Iterator<Entry<RegisterArg, Usage>> it = usageMap.entrySet().iterator(); it.hasNext();) {
Entry<RegisterArg, Usage> entry = it.next(); Entry<RegisterArg, Usage> entry = it.next();
Usage u = entry.getValue(); Usage u = entry.getValue();
RegisterArg r = u.getArg();
// if no assigns or method argument => remove // if no assigns => remove
if (u.getAssigns().isEmpty() || mthArgs.indexOf(r) != -1) { if (u.getAssigns().isEmpty()) {
it.remove(); it.remove();
continue; continue;
} }
...@@ -122,7 +125,7 @@ public class ProcessVariables extends AbstractVisitor { ...@@ -122,7 +125,7 @@ public class ProcessVariables extends AbstractVisitor {
for (IRegion assignRegion : u.getAssigns()) { for (IRegion assignRegion : u.getAssigns()) {
if (u.getArgRegion() == assignRegion if (u.getArgRegion() == assignRegion
&& canDeclareInRegion(u, assignRegion)) { && canDeclareInRegion(u, assignRegion)) {
r.getParentInsn().getAttributes().add(new DeclareVariableAttr()); u.getArg().getParentInsn().getAttributes().add(new DeclareVariableAttr());
it.remove(); it.remove();
break; break;
} }
...@@ -132,8 +135,8 @@ public class ProcessVariables extends AbstractVisitor { ...@@ -132,8 +135,8 @@ public class ProcessVariables extends AbstractVisitor {
// apply // apply
for (Entry<RegisterArg, Usage> entry : usageMap.entrySet()) { for (Entry<RegisterArg, Usage> entry : usageMap.entrySet()) {
Usage u = entry.getValue(); Usage u = entry.getValue();
// find common region which contains all usage regions
// find region which contain all usage regions
Set<IRegion> set = u.getUseRegions(); Set<IRegion> set = u.getUseRegions();
for (Iterator<IRegion> it = set.iterator(); it.hasNext();) { for (Iterator<IRegion> it = set.iterator(); it.hasNext();) {
IRegion r = it.next(); IRegion r = it.next();
......
...@@ -4,6 +4,18 @@ public class TestCF2 extends AbstractTest { ...@@ -4,6 +4,18 @@ public class TestCF2 extends AbstractTest {
private final Object ready_mutex = new Object(); private final Object ready_mutex = new Object();
private boolean ready = false; private boolean ready = false;
public int simple_loops() throws InterruptedException {
int[] a = new int[] { 1, 2, 4, 6, 8 };
int b = 0;
for (int i = 0; i < a.length; i++) {
b += a[i];
}
for (long i = b; i > 0; i--) {
b += i;
}
return b;
}
/** /**
* Test infinite loop * Test infinite loop
*/ */
...@@ -92,6 +104,7 @@ public class TestCF2 extends AbstractTest { ...@@ -92,6 +104,7 @@ public class TestCF2 extends AbstractTest {
@Override @Override
public boolean testRun() throws Exception { public boolean testRun() throws Exception {
assertEquals(simple_loops(), 252);
// TODO add checks // TODO add checks
return true; return true;
} }
......
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