001: /* Soot - a J*va Optimization Framework
002: * Copyright (C) 2003 Jennifer Lhotak
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2.1 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the
016: * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
017: * Boston, MA 02111-1307, USA.
018: */
019:
020: package soot.jimple.toolkits.annotation.parity;
021:
022: import soot.*;
023: import java.util.*;
024: import soot.toolkits.graph.*;
025: import soot.toolkits.scalar.*;
026: import soot.tagkit.*;
027: import soot.jimple.*;
028: import soot.options.*;
029:
030: /** A body transformer that records parity analysis
031: * information in tags. */
032: public class ParityTagger extends BodyTransformer {
033: public ParityTagger(Singletons.Global g) {
034: }
035:
036: public static ParityTagger v() {
037: return G.v()
038: .soot_jimple_toolkits_annotation_parity_ParityTagger();
039: }
040:
041: protected void internalTransform(Body b, String phaseName,
042: Map options) {
043:
044: //System.out.println("parity tagger for method: "+b.getMethod().getName());
045: boolean isInteractive = Options.v().interactive_mode();
046: Options.v().set_interactive_mode(false);
047: ParityAnalysis a;
048:
049: if (isInteractive) {
050: LiveLocals sll = new SimpleLiveLocals(new BriefUnitGraph(b));
051: Options.v().set_interactive_mode(isInteractive);
052:
053: a = new ParityAnalysis(new BriefUnitGraph(b), sll);
054: } else {
055: a = new ParityAnalysis(new BriefUnitGraph(b));
056: }
057:
058: Iterator sIt = b.getUnits().iterator();
059: while (sIt.hasNext()) {
060:
061: Stmt s = (Stmt) sIt.next();
062:
063: HashMap parityVars = (HashMap) a.getFlowAfter(s);
064:
065: Iterator it = parityVars.keySet().iterator();
066: while (it.hasNext()) {
067:
068: final Value variable = (Value) it.next();
069: if ((variable instanceof IntConstant)
070: || (variable instanceof LongConstant)) {
071: // don't add string tags (just color tags)
072: } else {
073: StringTag t = new StringTag(
074: "Parity variable: " + variable + " "
075: + parityVars.get(variable),
076: "Parity Analysis");
077: s.addTag(t);
078: }
079: }
080:
081: HashMap parityVarsUses = (HashMap) a.getFlowBefore(s);
082: HashMap parityVarsDefs = (HashMap) a.getFlowAfter(s);
083:
084: //uses
085:
086: Iterator valBoxIt = s.getUseBoxes().iterator();
087:
088: while (valBoxIt.hasNext()) {
089: ValueBox vb = (ValueBox) valBoxIt.next();
090: if (parityVarsUses.containsKey(vb.getValue())) {
091: //G.v().out.println("Parity variable for: "+vb.getValue());
092: String type = (String) parityVarsUses.get(vb
093: .getValue());
094: addColorTag(vb, type);
095: }
096: }
097:
098: // defs
099:
100: valBoxIt = s.getDefBoxes().iterator();
101:
102: while (valBoxIt.hasNext()) {
103: ValueBox vb = (ValueBox) valBoxIt.next();
104: if (parityVarsDefs.containsKey(vb.getValue())) {
105: //G.v().out.println("Parity variable for: "+vb.getValue());
106: String type = (String) parityVarsDefs.get(vb
107: .getValue());
108: addColorTag(vb, type);
109: }
110: }
111: }
112:
113: // add key to class
114: Iterator keyIt = b.getMethod().getDeclaringClass().getTags()
115: .iterator();
116: boolean keysAdded = false;
117: while (keyIt.hasNext()) {
118: Object next = keyIt.next();
119: if (next instanceof KeyTag) {
120: if (((KeyTag) next).analysisType().equals(
121: "Parity Analysis")) {
122: keysAdded = true;
123: }
124: }
125: }
126: if (!keysAdded) {
127: b.getMethod().getDeclaringClass().addTag(
128: new KeyTag(255, 0, 0, "Parity: Top",
129: "Parity Analysis"));
130: b.getMethod().getDeclaringClass().addTag(
131: new KeyTag(45, 255, 84, "Parity: Bottom",
132: "Parity Analysis"));
133: b.getMethod().getDeclaringClass().addTag(
134: new KeyTag(255, 248, 35, "Parity: Even",
135: "Parity Analysis"));
136: b.getMethod().getDeclaringClass().addTag(
137: new KeyTag(174, 210, 255, "Parity: Odd",
138: "Parity Analysis"));
139: }
140: }
141:
142: private void addColorTag(ValueBox vb, String type) {
143: if (type.equals("bottom")) {
144: //green
145: vb.addTag(new ColorTag(ColorTag.GREEN, "Parity Analysis"));
146: } else if (type.equals("top")) {
147: //red
148: vb.addTag(new ColorTag(ColorTag.RED, "Parity Analysis"));
149: } else if (type.equals("even")) {
150: //yellow
151: vb.addTag(new ColorTag(ColorTag.YELLOW, "Parity Analysis"));
152: } else if (type.equals("odd")) {
153: //blue
154: vb.addTag(new ColorTag(ColorTag.BLUE, "Parity Analysis"));
155: }
156: }
157: }
|