KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > soot > jimple > toolkits > annotation > parity > ParityTagger


1 /* Soot - a J*va Optimization Framework
2  * Copyright (C) 2003 Jennifer Lhotak
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */

19
20 package soot.jimple.toolkits.annotation.parity;
21 import soot.*;
22 import java.util.*;
23 import soot.toolkits.graph.*;
24 import soot.toolkits.scalar.*;
25 import soot.tagkit.*;
26 import soot.jimple.*;
27 import soot.options.*;
28
29 /** A body transformer that records parity analysis
30  * information in tags. */

31 public class ParityTagger extends BodyTransformer
32 {
33     public ParityTagger( Singletons.Global g ) {}
34     public static ParityTagger v() { return G.v().soot_jimple_toolkits_annotation_parity_ParityTagger(); }
35
36     protected void internalTransform(
37             Body b, String JavaDoc phaseName, Map options)
38     {
39
40         //System.out.println("parity tagger for method: "+b.getMethod().getName());
41
boolean isInteractive = Options.v().interactive_mode();
42         Options.v().set_interactive_mode(false);
43         ParityAnalysis a;
44         
45         if (isInteractive){
46             LiveLocals sll = new SimpleLiveLocals(new BriefUnitGraph(b));
47             Options.v().set_interactive_mode(isInteractive);
48         
49             a = new ParityAnalysis(
50                 new BriefUnitGraph( b ) , sll);
51         }
52         else {
53             a = new ParityAnalysis(new BriefUnitGraph(b));
54         }
55         
56         Iterator sIt = b.getUnits().iterator();
57         while( sIt.hasNext() ) {
58
59             Stmt s = (Stmt) sIt.next();
60
61             HashMap parityVars = (HashMap) a.getFlowAfter( s );
62
63             Iterator it = parityVars.keySet().iterator();
64             while( it.hasNext() ) {
65             
66                 final Value variable = (Value) it.next();
67                 if ((variable instanceof IntConstant) || (variable instanceof LongConstant)){
68                     // don't add string tags (just color tags)
69
}
70                 else{
71                     StringTag t = new StringTag(
72                         "Parity variable: "+variable+" "+
73                         parityVars.get(variable) , "Parity Analysis");
74                     s.addTag( t );
75                 }
76             }
77
78             HashMap parityVarsUses = (HashMap) a.getFlowBefore( s );
79             HashMap parityVarsDefs = (HashMap) a.getFlowAfter( s );
80
81             
82             //uses
83

84             Iterator valBoxIt = s.getUseBoxes().iterator();
85             
86             while (valBoxIt.hasNext()){
87                 ValueBox vb = (ValueBox)valBoxIt.next();
88                 if (parityVarsUses.containsKey(vb.getValue())){
89                     //G.v().out.println("Parity variable for: "+vb.getValue());
90
String JavaDoc type = (String JavaDoc)parityVarsUses.get(vb.getValue());
91                     addColorTag(vb, type);
92                 }
93             }
94
95             // defs
96

97             valBoxIt = s.getDefBoxes().iterator();
98             
99             while (valBoxIt.hasNext()){
100                 ValueBox vb = (ValueBox)valBoxIt.next();
101                 if (parityVarsDefs.containsKey(vb.getValue())){
102                     //G.v().out.println("Parity variable for: "+vb.getValue());
103
String JavaDoc type = (String JavaDoc)parityVarsDefs.get(vb.getValue());
104                     addColorTag(vb, type);
105                 }
106             }
107         }
108
109         // add key to class
110
Iterator keyIt = b.getMethod().getDeclaringClass().getTags().iterator();
111         boolean keysAdded = false;
112         while (keyIt.hasNext()){
113             Object JavaDoc next = keyIt.next();
114             if (next instanceof KeyTag){
115                 if (((KeyTag)next).analysisType().equals("Parity Analysis")){
116                     keysAdded = true;
117                 }
118             }
119         }
120         if (!keysAdded){
121             b.getMethod().getDeclaringClass().addTag(new KeyTag(255,0,0, "Parity: Top", "Parity Analysis"));
122             b.getMethod().getDeclaringClass().addTag(new KeyTag(45,255,84, "Parity: Bottom", "Parity Analysis"));
123             b.getMethod().getDeclaringClass().addTag(new KeyTag(255,248,35, "Parity: Even", "Parity Analysis"));
124             b.getMethod().getDeclaringClass().addTag(new KeyTag(174,210,255, "Parity: Odd", "Parity Analysis"));
125         }
126     }
127
128     private void addColorTag(ValueBox vb, String JavaDoc type) {
129         if (type.equals("bottom")){
130             //green
131
vb.addTag(new ColorTag(ColorTag.GREEN, "Parity Analysis"));
132         }
133         else if (type.equals("top")){
134             //red
135
vb.addTag(new ColorTag(ColorTag.RED, "Parity Analysis"));
136         }
137         else if (type.equals("even")){
138             //yellow
139
vb.addTag(new ColorTag(ColorTag.YELLOW, "Parity Analysis"));
140         }
141         else if (type.equals("odd")){
142             //blue
143
vb.addTag(new ColorTag(ColorTag.BLUE, "Parity Analysis"));
144         }
145     }
146 }
147
148
149
Popular Tags