KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > soot > LabeledUnitPrinter


1 /* Soot - a J*va Optimization Framework
2  * Copyright (C) 2003, 2004 Ondrej 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;
21 import soot.jimple.*;
22 import soot.util.*;
23 import java.util.*;
24
25 /**
26 * UnitPrinter implementation for representations that have labelled stmts,
27 * such as Jimple, Grimp, and Baf
28 */

29 public abstract class LabeledUnitPrinter extends AbstractUnitPrinter {
30     /** branch targets **/
31     protected Map labels;
32     /** for unit references in Phi nodes **/
33     protected Map references;
34
35     public LabeledUnitPrinter( Body b ) {
36         createLabelMaps(b);
37     }
38
39     public Map labels() { return labels; }
40     public Map references() { return references; }
41
42     public abstract void literal( String JavaDoc s );
43     public abstract void methodRef( SootMethodRef m );
44     public abstract void fieldRef( SootFieldRef f );
45     public abstract void identityRef( IdentityRef r );
46     public abstract void type( Type t );
47
48     public void unitRef( Unit u, boolean branchTarget ) {
49         String JavaDoc oldIndent = getIndent();
50         
51         // normal case, ie labels
52
if(branchTarget){
53             setIndent(labelIndent);
54             handleIndent();
55             setIndent(oldIndent);
56             String JavaDoc label = (String JavaDoc) labels.get( u );
57             if( label == null || label.equals( "<unnamed>" ) )
58                 label = "[?= "+u+"]";
59             output.append(label);
60         }
61         // refs to control flow predecessors (for Shimple)
62
else{
63             String JavaDoc ref = (String JavaDoc) references.get( u );
64
65             if(startOfLine){
66                 String JavaDoc newIndent = "(" + ref + ")" +
67                     indent.substring(ref.length() + 2);
68                 setIndent(newIndent);
69                 handleIndent();
70                 setIndent(oldIndent);
71             }
72             else
73                 output.append(ref);
74         }
75     }
76     
77     private void createLabelMaps(Body body) {
78         Chain units = body.getUnits();
79
80         labels = new HashMap(units.size() * 2 + 1, 0.7f);
81         references = new HashMap(units.size() * 2 + 1, 0.7f);
82         
83         // Create statement name table
84
{
85             Iterator boxIt = body.getAllUnitBoxes().iterator();
86
87             Set labelStmts = new HashSet();
88             Set refStmts = new HashSet();
89             
90             // Build labelStmts and refStmts
91
{
92                 while (boxIt.hasNext()) {
93                     UnitBox box = (UnitBox) boxIt.next();
94                     Unit stmt = (Unit) box.getUnit();
95
96                     if(box.isBranchTarget())
97                         labelStmts.add(stmt);
98                     else
99                         refStmts.add(stmt);
100                 }
101
102             }
103
104             // Traverse the stmts and assign a label if necessary
105
{
106                 int labelCount = 0;
107                 int refCount = 0;
108                 
109                 Iterator stmtIt = units.iterator();
110
111                 while (stmtIt.hasNext()) {
112                     Unit s = (Unit) stmtIt.next();
113
114                     if (labelStmts.contains(s))
115                         labels.put(s, "label" + (labelCount++));
116
117                     if (refStmts.contains(s))
118                         references.put(s, Integer.toString(refCount++));
119                 }
120             }
121         }
122     }
123
124     protected String JavaDoc labelIndent = " ";
125 }
126
127
Popular Tags