Wrote callgraph.
authorbdemsky <bdemsky>
Fri, 19 May 2006 21:34:46 +0000 (21:34 +0000)
committerbdemsky <bdemsky>
Fri, 19 May 2006 21:34:46 +0000 (21:34 +0000)
Working on flag analysis.

Robust/src/Analysis/CallGraph/CallGraph.java [new file with mode: 0644]
Robust/src/Analysis/Flag/FlagAnalysis.java [new file with mode: 0644]
Robust/src/Analysis/Flag/FlagState.java [new file with mode: 0644]
Robust/src/IR/Tree/SemanticCheck.java
Robust/src/Makefile

diff --git a/Robust/src/Analysis/CallGraph/CallGraph.java b/Robust/src/Analysis/CallGraph/CallGraph.java
new file mode 100644 (file)
index 0000000..2b08617
--- /dev/null
@@ -0,0 +1,105 @@
+package Analysis.CallGraph;
+import IR.State;
+import IR.Flat.FlatMethod;
+import IR.Flat.FlatNode;
+import IR.Flat.FlatCall;
+import IR.Flat.FKind;
+import java.util.*;
+import IR.ClassDescriptor;
+import IR.MethodDescriptor;
+
+public class CallGraph {
+    State state;
+    Hashtable methods;
+    Hashtable methodmap;
+
+    public CallGraph(State state) {
+       this.state=state;
+       methods=new Hashtable();
+       methodmap=new Hashtable();
+       buildMethodTable();
+       buildGraph();
+    }
+    
+    private void buildMethodTable() {
+       //Iterator through classes
+       Iterator it=state.getClassSymbolTable().getDescriptorsIterator();
+       while(it.hasNext()) {
+           ClassDescriptor cn=(ClassDescriptor)it.next();
+           Iterator methodit=cn.getMethods();
+           //Iterator through methods
+           while(methodit.hasNext()) {
+               MethodDescriptor md=(MethodDescriptor)methodit.next();
+               if (md.isStatic()||md.getReturnType()==null)
+                   continue;
+               ClassDescriptor superdesc=cn.getSuperDesc();
+               if (superdesc!=null) {
+                   Set possiblematches=superdesc.getMethodTable().getSet(md.getSymbol());
+                   boolean foundmatch=false;
+                   for(Iterator matchit=possiblematches.iterator();matchit.hasNext();) {
+                       MethodDescriptor matchmd=(MethodDescriptor)matchit.next();
+                       if (md.matches(matchmd)) {
+                           if (!methods.containsKey(matchmd))
+                               methods.put(matchmd,new HashSet());
+                           ((HashSet)methods.get(matchmd)).add(md);
+                           break;
+                       }
+                   }
+               }
+           }
+       }
+    }
+
+    /** Given a call to MethodDescriptor, lists the methods which
+        could actually be called due to virtual dispatch. */
+    public Set getMethods(MethodDescriptor md) {
+       HashSet ns=new HashSet();
+       ns.add(md);
+       Set s=(Set)methods.get(md);
+       if (s!=null)
+           for(Iterator it=s.iterator();it.hasNext();) {
+               MethodDescriptor md2=(MethodDescriptor)it.next();
+               ns.addAll(getMethods(md2));
+           }
+       return ns;
+    }
+
+    private void buildGraph() { 
+       Iterator it=state.getClassSymbolTable().getDescriptorsIterator();
+       while(it.hasNext()) {
+           ClassDescriptor cn=(ClassDescriptor)it.next();
+           Iterator methodit=cn.getMethods();
+           //Iterator through methods
+           while(methodit.hasNext()) {
+               MethodDescriptor md=(MethodDescriptor)methodit.next();
+               analyzeMethod(md);
+           }
+       }
+    }
+
+    private void analyzeMethod(MethodDescriptor md) {
+       FlatMethod fm=state.getMethodFlat(md);
+       HashSet toexplore=new HashSet();
+       toexplore.add(fm);
+       HashSet explored=new HashSet();
+       //look at all the nodes in the flat representation
+       while(!toexplore.isEmpty()) {
+           FlatNode fn=(FlatNode)(toexplore.iterator()).next();
+           toexplore.remove(fn);
+           explored.add(fn);
+           for(int i=0;i<fn.numNext();i++) {
+               FlatNode fnnext=fn.getNext(i);
+               if (!explored.contains(fnnext))
+                   toexplore.add(fnnext);
+           }
+           if (fn.kind()==FKind.FlatCall) {
+               FlatCall fc=(FlatCall)fn;
+               MethodDescriptor calledmethod=fc.getMethod();
+               Set methodsthatcouldbecalled=getMethods(calledmethod);
+               if (!methodmap.containsKey(md))
+                   methodmap.put(md,new HashSet());
+               ((HashSet)methodmap.get(md)).addAll(methodsthatcouldbecalled);
+           }
+       }
+    }
+}
diff --git a/Robust/src/Analysis/Flag/FlagAnalysis.java b/Robust/src/Analysis/Flag/FlagAnalysis.java
new file mode 100644 (file)
index 0000000..ca2a1f8
--- /dev/null
@@ -0,0 +1,16 @@
+package Analysis.Flag;
+import IR.State;
+
+public class FlagAnalysis {
+    State state;
+    
+
+    public FlagAnalysis(State state) {
+       this.state=state;
+    }
+
+    public void doAnalysis() {
+       
+    }
+    
+}
diff --git a/Robust/src/Analysis/Flag/FlagState.java b/Robust/src/Analysis/Flag/FlagState.java
new file mode 100644 (file)
index 0000000..17f9f36
--- /dev/null
@@ -0,0 +1,37 @@
+package Analysis.Flag;
+import java.util.*;
+import IR.FlagDescriptor;
+
+public class FlagState {
+    HashSet flags;
+
+    public FlagState() {
+       flags=new HashSet();
+    }
+
+    public void setFlag(FlagDescriptor fd, boolean status) {
+       if (status)
+           flags.add(fd);
+       else 
+           flags.remove(fd);
+    }
+
+    public boolean getFlagState(FlagDescriptor fd) {
+       return flags.contains(fd);
+    }
+    
+    public Set getFlags() {
+       return flags;
+    }
+
+    public boolean equals(Object o) {
+       if (!(o instanceof FlagState))
+           return false;
+       FlagState fs=(FlagState)o;
+       return fs.flags.equals(flags);
+    }
+
+    public int hashCode() {
+       return flags.hashCode();
+    }
+}
index 5782b95f1bb8ea555815c55f2e3489ac12020858..a6e07f5a074a69d1b42bee742f69425ec28e7384 100644 (file)
@@ -421,6 +421,18 @@ public class SemanticCheck {
              (an.getDest() instanceof NameNode)))
            throw new Error("Bad lside in "+an.printNode(0));
        checkExpressionNode(md, nametable, an.getDest(), null);
+
+       /* We want parameter variables to tasks to be immutable */
+       if (md instanceof TaskDescriptor) {
+           if (an.getDest() instanceof NameNode) {
+               NameNode nn=(NameNode)an.getDest();
+               if (nn.getVar()!=null) {
+                   if (((TaskDescriptor)md).getParameterTable().contains(nn.getVar().getSymbol()))
+                       throw new Error("Can't modify parameter "+nn.getVar()+ " to task "+td.getSymbol());
+               }
+           }
+       }
+       
        if (!typeutil.isSuperorType(an.getDest().getType(),an.getSrc().getType())) {
            throw new Error("Type of rside ("+an.getSrc().getType()+") not compatible with type of lside ("+an.getDest().getType()+")"+an.printNode(0));
        }
index 9e7dafd86ccba139f29ea0c51b2ecfcdf9fabd33..fc0e93c75338faa6148135896ad5fa81d5f8fc8c 100644 (file)
@@ -38,7 +38,8 @@ Lex/Identifier.class Lex/InputElement.class Lex/IntegerLiteral.class  \
 Lex/Keyword.class Lex/Lexer.class Lex/Literal.class                    \
 Lex/LongLiteral.class Lex/NullLiteral.class Lex/NumericLiteral.class   \
 Lex/Operator.class Lex/Separator.class Lex/StringLiteral.class         \
-Lex/Token.class Lex/TraditionalComment.class Lex/WhiteSpace.class
+Lex/Token.class Lex/TraditionalComment.class Lex/WhiteSpace.class      \
+Analysis/Flag/FlagState.class Analysis/CallGraph/CallGraph.class
 
 all: Parse/Sym.class Parse/Parser.class $(CLASSFILES)
 
@@ -50,4 +51,4 @@ Parse/Parser.java Parse/Sym.java: Parse/java14.cup
        javac -cp ../cup:.:$(CLASSPATH) $<
 
 clean:
-       rm IR/*.class IR/Tree/*.class Main/*.class Lex/*.class Parse/*.class Parse/Sym.java Parse/Parser.java IR/Flat/*.class classdefs.h methodheaders.h methods.c structdefs.h virtualtable.h
+       rm IR/*.class IR/Tree/*.class Main/*.class Lex/*.class Parse/*.class Parse/Sym.java Parse/Parser.java IR/Flat/*.class classdefs.h methodheaders.h methods.c structdefs.h virtualtable.h Analysis/*.class Analysis/Flag/*.class