KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > soot > tagkit > CodeAttribute


1 /* Soot - a J*va Optimization Framework
2  * Copyright (C) 2000 Patrice Pominville and Feng Qian
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 /*
21  * Modified by the Sable Research Group and others 1997-1999.
22  * See the 'credits' file distributed with Soot for the complete list of
23  * contributors. (Soot is distributed at http://www.sable.mcgill.ca/soot)
24  */

25
26
27 package soot.tagkit;
28 import soot.options.*;
29
30 import java.util.*;
31 import soot.baf.*;
32 import soot.*;
33
34
35 /** A CodeAttribute object holds PC -> Tag pairs.
36  * It represents abstracted attributes of Code_attribute
37  * such as LineNumberTable, ArrayBoundsCheck.
38  */

39 public class CodeAttribute extends JasminAttribute
40 {
41     protected List mUnits;
42     protected List mTags;
43
44     private byte[] value;
45     
46     private String JavaDoc name = "CodeAtribute";
47
48     public CodeAttribute(){}
49     
50   /** Creates an attribute object with the given name. */
51     public CodeAttribute(String JavaDoc name)
52     {
53         this.name = name;
54     }
55
56   /** Create an attribute object with the name and lists of unit-tag pairs. */
57     public CodeAttribute(String JavaDoc name, List units, List tags)
58     {
59         this.name = name;
60     this.mUnits = units;
61     this.mTags = tags;
62     }
63
64   /** Returns the name.*/
65     public String JavaDoc toString()
66     {
67     return name;
68     }
69
70   /** Returns the attribute name. */
71     public String JavaDoc getName()
72     {
73     return name;
74     }
75
76   /** Only used by SOOT to read in an existing attribute without interpret it.*/
77     public void setValue(byte[] v)
78     {
79         this.value = v;
80     }
81     
82   /** Also only used as setValue(). */
83     public byte[] getValue() throws AttributeValueException
84     {
85     if (value == null)
86         throw new AttributeValueException();
87     else
88         return value;
89     }
90
91   /** Generates Jasmin Value String */
92     public String JavaDoc getJasminValue(Map instToLabel)
93     {
94         // some benchmarks fail because of the returned string larger than
95
// the possible buffer size.
96
StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
97     
98     if (mTags.size() != mUnits.size())
99         throw new RuntimeException JavaDoc("Sizes must match!");
100     
101     Iterator tagIt = mTags.iterator();
102     Iterator unitIt = mUnits.iterator();
103
104     while (tagIt.hasNext())
105     {
106         Object JavaDoc unit = unitIt.next();
107         Object JavaDoc tag = tagIt.next();
108
109         buf.append("%"+instToLabel.get(unit) + "%"+
110                new String JavaDoc(Base64.encode(((Tag)tag).getValue())));
111     }
112     
113     return buf.toString();
114     }
115
116   /** Returns a list of unit boxes that have tags attached. */
117     public List getUnitBoxes()
118     {
119     List unitBoxes = new ArrayList(mUnits.size());
120     
121     Iterator it = mUnits.iterator();
122     
123     while(it.hasNext()) {
124         unitBoxes.add(Baf.v().newInstBox((Unit)it.next()));
125     }
126
127     return unitBoxes;
128     }
129
130     public byte[] decode(String JavaDoc attr, Hashtable labelToPc)
131     {
132     if (Options.v().verbose())
133         G.v().out.println("[] JasminAttribute decode...");
134
135     List attributeHunks = new LinkedList();
136     int attributeSize = 0;
137
138     StringTokenizer st = new StringTokenizer(attr, "%");
139     boolean isLabel = false;
140     if(attr.startsWith("%"))
141         isLabel = true;
142
143     int tablesize = 0;
144
145     byte[] pcArray;
146     while(st.hasMoreTokens()) {
147         String JavaDoc token = st.nextToken();
148         if(isLabel) {
149         Integer JavaDoc pc = (Integer JavaDoc) labelToPc.get(token);
150
151         if(pc == null)
152             throw new RuntimeException JavaDoc("PC is null, the token is "+token);
153
154         int pcvalue = pc.intValue();
155         if(pcvalue > 65535)
156             throw new RuntimeException JavaDoc("PC great than 65535, the token is "+token+" : " +pcvalue);
157
158         pcArray = new byte[2];
159
160         pcArray[1] = (byte)(pcvalue&0x0FF);
161                 
162         pcArray[0] = (byte)((pcvalue>>8)&0x0FF);
163
164         attributeHunks.add(pcArray);
165         attributeSize += 2;
166         tablesize++;
167         } else {
168
169         byte[] hunk = Base64.decode(token.toCharArray());
170         attributeSize += hunk.length;
171
172         attributeHunks.add(hunk);
173         }
174         isLabel = !isLabel;
175     }
176     
177     /* first two bytes indicate the length of attribute table. */
178     attributeSize += 2;
179     byte[] attributeValue = new byte[attributeSize];
180     {
181         attributeValue[0] = (byte)((tablesize>>8)&0x0FF);
182         attributeValue[1] = (byte)(tablesize&0x0FF);
183     }
184     int index=2;
185     Iterator it = attributeHunks.iterator();
186     while(it.hasNext()) {
187         byte[] hunk = (byte[]) it.next();
188         for(int i = 0; i < hunk.length; i++) {
189         attributeValue[index++] = hunk[i];
190         }
191     }
192
193     if(index != (attributeSize))
194         throw new RuntimeException JavaDoc("Index does not euqal to attrubute size :"+index+" -- "+attributeSize);
195
196     if (Options.v().verbose())
197         G.v().out.println("[] Jasmin.decode finished...");
198
199     return attributeValue;
200     }
201 }
202       
203
Popular Tags