View Javadoc

1   package joeq.Compiler.Analysis.IPSSA;
2   
3   import java.util.HashMap;
4   import java.util.LinkedHashSet;
5   import joeq.Class.jq_Method;
6   import joeq.Compiler.Analysis.IPSSA.SSALocation.LocalLocation;
7   import joeq.Compiler.Analysis.IPSSA.Utils.DefinitionSet;
8   import joeq.Compiler.Quad.Quad;
9   
10  /***
11   * This is a definition in the SSA sense, meaning it's unique. The meaning of the 
12   * definition is affected by the underlying location.
13   * 
14   * @see joeq.Compiler.Analysis.IPSSA.SSALocation
15   * @version $Id: SSADefinition.java 1931 2004-09-22 22:17:47Z joewhaley $  
16   * */
17  public class SSADefinition {
18      protected SSALocation    _location;
19      int                     _version;
20      LinkedHashSet            _uses;
21      long                     _id;        // this is an absolutely unique definition ID
22      jq_Method               _method;
23      Quad                    _quad;
24      
25      public static class Helper {
26          protected static HashMap/*<SSALocation, Integer>*/             _versionMap = new HashMap();
27          protected static long                                         _globalID;
28          /*** The set of all definitions in the program, can be quite huge */
29          protected static DefinitionSet                                 _definitionCache = new DefinitionSet(); 
30          
31          static SSADefinition create_ssa_definition(SSALocation location, Quad quad, jq_Method method){
32              int version = 0;
33              if(_versionMap.containsKey(location)){
34                  Integer i = (Integer)_versionMap.get(location);
35                  version = i.intValue();
36                  _versionMap.put(location, new Integer(version+1));
37              }else{
38                  _versionMap.put(location, new Integer(1));
39              }
40              
41              SSADefinition def = new SSADefinition(location, version, quad, method, _globalID++);
42              _definitionCache.add(def);
43              //System.err.println(_definitionCache.size() + " definitions now");
44              return def;
45          }
46          
47          public static SSAIterator.DefinitionIterator getAllDefinitionIterator(){
48              return _definitionCache.getDefinitionIterator();
49          }
50  
51          /***
52           *  This is slow reverse lookup. Don't use this very often.
53           * */
54          public static SSADefinition lookupDefinition(String name) {
55              //System.err.println("Searching through " + _definitionCache.size() + " definitions");
56              for(SSAIterator.DefinitionIterator iter = getAllDefinitionIterator(); iter.hasNext(); ) {
57                  SSADefinition def = iter.nextDefinition();
58                  
59                  if(def.toString().equals(name)) {
60                      // don't even check for duplicates                   
61                      return def;
62                  }else {
63                      //System.err.println("Skipping " + def);
64                  }
65              }
66              return null;
67          }
68      };
69      
70      private SSADefinition(SSALocation location, int version, Quad quad, jq_Method method, long id){
71          this._location    = location;
72          this._version     = version;    
73          this._quad        = quad;
74          this._method      = method;
75          this._id          = id;        
76          
77          _uses = new LinkedHashSet();
78      }
79      
80      public SSALocation getLocation() {return _location;}
81      public int getVersion(){return _version;}
82      public jq_Method getMethod() {return _method;}
83      public Quad getQuad() { return _quad;}
84      
85      public String toString(){
86          String result = _location.toString() + "_" + _version; 
87          if(! (_location instanceof SSALocation.LocalLocation)) {
88             return result;
89          }
90          SSALocation.LocalLocation loc = (LocalLocation)_location;
91          String name = loc.getName(_method, _quad);
92          return (name == null) ? result : result + "(" + name + ")";
93      }
94  
95      public long getID() {
96          return _id;
97      }
98  
99      public SSAIterator.ValueIterator getUseIterator() {
100         return new SSAIterator.ValueIterator(_uses.iterator());
101     }
102 
103     public void appendUse(SSAValue value) {
104         _uses.add(value);        
105     }
106 }
107