From be51da047ec7d43446649ad7726211baa0e6640c Mon Sep 17 00:00:00 2001 From: bdemsky Date: Fri, 18 Sep 2009 04:32:27 +0000 Subject: [PATCH] add SESETree --- Robust/src/Analysis/MLP/SESETree.java | 154 ++++++++++++++++++++++++++ 1 file changed, 154 insertions(+) create mode 100644 Robust/src/Analysis/MLP/SESETree.java diff --git a/Robust/src/Analysis/MLP/SESETree.java b/Robust/src/Analysis/MLP/SESETree.java new file mode 100644 index 00000000..62e91c10 --- /dev/null +++ b/Robust/src/Analysis/MLP/SESETree.java @@ -0,0 +1,154 @@ +package Analysis.MLP; +import IR.*; +import IR.Flat.*; +import java.util.*; +import Analysis.CallGraph.*; + +public class SESETree { + State state; + TypeUtil typeutil; + CallGraph callgraph; + SESENode root; + Hashtable> toanalyze=new Hashtable>(); + Hashtable> discovered=new Hashtable>(); + Hashtable sesemap=new Hashtable(); + + public SESETree(State state, TypeUtil typeutil, CallGraph callgraph) { + this.state=state; + this.typeutil=typeutil; + this.callgraph=callgraph; + root=new SESENode(null, true); + doAnalysis(); + } + + public SESENode getRoot() { + return root; + } + + public void doAnalysis() { + MethodDescriptor main=typeutil.getMain(); + add(toanalyze, main, root); + add(discovered, main, root); + + while(!toanalyze.isEmpty()) { + MethodDescriptor md=toanalyze.keySet().iterator().next(); + Set context=toanalyze.get(md); + toanalyze.remove(md); + FlatMethod fm=state.getMethodFlat(md); + analyzeMethod(fm, context); + } + } + + public SESENode getSESE(FlatSESEEnterNode enter) { + if (!sesemap.containsKey(enter)) { + sesemap.put(enter, new SESENode(enter, false)); + } + return sesemap.get(enter); + } + + private void analyzeMethod(FlatMethod fm, Set context) { + Hashtable> stacks=new Hashtable> (); + stacks.put(fm, new Stack()); + HashSet tovisit=new HashSet(); + HashSet fndiscovered=new HashSet(); + tovisit.add(fm); + fndiscovered.add(fm); + while(!tovisit.isEmpty()) { + FlatNode fn=tovisit.iterator().next(); + tovisit.remove(fn); + Stack instack=stacks.get(fm); + switch(fn.kind()) { + case FKind.FlatCall: { + FlatCall fc=(FlatCall)fn; + //handle method call + Set parents; + if (instack.isEmpty()) { + parents=context; + } else { + parents=new HashSet(); + parents.add(instack.peek()); + } + for(Iterator parentit=parents.iterator();parentit.hasNext();) { + SESENode parentsese=parentit.next(); + for(Iterator calleeit=(fc.getThis()==null?callgraph.getMethods(fc.getMethod()):callgraph.getMethods(fc.getMethod(), fc.getThis().getType())).iterator(); calleeit.hasNext();) { + MethodDescriptor md=calleeit.next(); + if (add(discovered,md, parentsese)) { + add(toanalyze, md, parentsese); + } + } + } + break; + } + case FKind.FlatSESEEnterNode: { + FlatSESEEnterNode enter=(FlatSESEEnterNode)fn; + Set parents; + if (instack.isEmpty()) { + parents=context; + } else { + parents=new HashSet(); + parents.add(instack.peek()); + } + SESENode sese=getSESE(enter); + for(Iterator parentit=parents.iterator();parentit.hasNext();) { + SESENode parentsese=parentit.next(); + parentsese.addChild(sese); + } + Stack copy=(Stack)instack.clone(); + copy.push(sese); + instack=copy; + break; + } + case FKind.FlatSESEExitNode: { + FlatSESEExitNode exit=(FlatSESEExitNode)fn; + Stack copy=(Stack)instack.clone(); + copy.pop(); + instack=copy; + break; + } + } + for(int i=0;i> discovered, MethodDescriptor md, SESENode sese) { + if (!discovered.containsKey(md)) + discovered.put(md, new HashSet()); + if (discovered.get(md).contains(sese)) + return false; + discovered.get(md).add(sese); + return true; + } + + + class SESENode { + boolean isRoot; + HashSet children; + FlatSESEEnterNode node; + SESENode(FlatSESEEnterNode node, boolean isRoot) { + children=new HashSet(); + this.isRoot=isRoot; + this.node=node; + } + + public boolean isLeaf() { + return children.isEmpty(); + } + + public void addChild(SESENode child) { + children.add(child); + } + + public Set getChildren() { + return children; + } + } +} \ No newline at end of file -- 2.34.1