Created
October 10, 2015 03:27
-
-
Save gnuvince/5a1b319f9334ad22c8e2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Set; | |
import mclint.util.Parsing; | |
import ast.*; | |
import natlab.toolkits.analysis.core.ReachingDefs; | |
import natlab.toolkits.analysis.core.UseDefDefUseChain; | |
import natlab.toolkits.filehandling.GenericFile; | |
import natlab.toolkits.path.FileEnvironment; | |
import natlab.tame.BasicTamerTool; | |
import natlab.tame.tir.*; | |
import natlab.tame.tir.analysis.TIRAbstractNodeCaseHandler; | |
import natlab.tame.valueanalysis.IntraproceduralValueAnalysis; | |
import natlab.tame.valueanalysis.ValueAnalysis; | |
import natlab.tame.valueanalysis.aggrvalue.AggrValue; | |
import natlab.tame.valueanalysis.basicmatrix.BasicMatrixValue; | |
public class Test { | |
private static <T> T getOnlyElement(Set<T> S) { | |
if (S.size() != 1) { | |
throw new RuntimeException(); | |
} | |
return S.iterator().next(); | |
} | |
public static void main(String[] args) { | |
// We *really* ought to make it simpler to get a Tamer tree... | |
GenericFile gfile = GenericFile.create(args[0]); | |
FileEnvironment fenv = new FileEnvironment(gfile); | |
String[] shapeDesc = { "DOUBLE&10*10&REAL" }; | |
ValueAnalysis<AggrValue<BasicMatrixValue>> analysis = BasicTamerTool.analyze(shapeDesc, fenv); | |
IntraproceduralValueAnalysis<AggrValue<BasicMatrixValue>> funcAnalysis = analysis.getNodeList().get(0).getAnalysis(); | |
TIRFunction tamerFunction = funcAnalysis.getTree(); | |
System.out.println("===== END OF TAMER JUNK ====="); | |
ReachingDefs rdefs = new ReachingDefs(tamerFunction); | |
rdefs.analyze(); | |
UseDefDefUseChain uddu = UseDefDefUseChain.fromReachingDefs(rdefs); | |
for (Name param : tamerFunction.getInputParams()) { | |
Walker w = new Walker(param, uddu); | |
tamerFunction.analyze(w); | |
} | |
/* | |
Program ast = Parsing.file(args[0]); | |
ReachingDefs rd = new ReachingDefs(ast); | |
rd.analyze(); | |
UseDefDefUseChain uddu = UseDefDefUseChain.fromReachingDefs(rd); | |
Function f = ((FunctionList) ast).getFunction(0); | |
Stmt S1 = f.getStmt(0); | |
AssignStmt S2 = (AssignStmt) f.getStmt(1); | |
Name xAtS2 = ((NameExpr)((ParameterizedExpr) S2.getLHS()).getTarget()).getName(); | |
Stmt def = (Stmt) getOnlyElement(uddu.getDefs(xAtS2)); | |
System.out.println("defs of x at S2:"); | |
System.out.println(def.getPrettyPrinted()); | |
System.out.println("names defined by S1:"); | |
Name name = getOnlyElement(uddu.getDefinedNames(S1)); | |
System.out.println(name.getPrettyPrinted()); | |
*/ | |
} | |
private static class Walker extends TIRAbstractNodeCaseHandler { | |
private Name param; | |
private UseDefDefUseChain uddu; | |
public Walker(Name param, UseDefDefUseChain uddu) { | |
this.param = param; | |
this.uddu = uddu; | |
} | |
@Override | |
public void caseASTNode(ASTNode astNode) { | |
for (int i = 0; i < astNode.getNumChild(); i++) { | |
ASTNode child = astNode.getChild(i); | |
if (child instanceof TIRNode) { | |
((TIRNode) child).tirAnalyze(this); | |
} | |
else { | |
child.analyze(this); | |
} | |
} | |
} | |
@Override | |
public void caseTIRArraySetStmt(TIRArraySetStmt stmt) { | |
Name arrayName = stmt.getArrayName(); | |
if (uddu.getDefs(arrayName).contains(param)) { | |
System.out.printf("The statement %s modifies the input parameter %s\n", | |
stmt.getPrettyPrinted(), param.getID()); | |
} | |
else { | |
System.out.printf("The statement %s does not modify the input parameter %s\n", | |
stmt.getPrettyPrinted(), param.getID()); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment