001: /*
002: * Bytecode Analysis Framework
003: * Copyright (C) 2003,2004 University of Maryland
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: */
019:
020: package edu.umd.cs.findbugs.ba.npe;
021:
022: import java.util.Collection;
023: import java.util.HashMap;
024: import java.util.Iterator;
025: import java.util.Map;
026:
027: import edu.umd.cs.findbugs.TigerSubstitutes;
028: import edu.umd.cs.findbugs.annotations.CheckForNull;
029: import edu.umd.cs.findbugs.annotations.NonNull;
030: import edu.umd.cs.findbugs.ba.Frame;
031: import edu.umd.cs.findbugs.ba.vna.ValueNumber;
032: import edu.umd.cs.findbugs.ba.vna.ValueNumberAnalysisFeatures;
033: import edu.umd.cs.findbugs.ba.vna.ValueNumberFrame;
034: import edu.umd.cs.findbugs.util.Strings;
035: import edu.umd.cs.findbugs.util.Util;
036:
037: public class IsNullValueFrame extends Frame<IsNullValue> {
038: private IsNullConditionDecision decision;
039: private boolean trackValueNumbers;
040: private Map<ValueNumber, IsNullValue> knownValueMap;
041:
042: public IsNullValueFrame(int numLocals, boolean trackValueNumbers) {
043: super (numLocals);
044: this .trackValueNumbers = trackValueNumbers;
045: if (trackValueNumbers) {
046: this .knownValueMap = new HashMap<ValueNumber, IsNullValue>();
047: }
048: }
049:
050: public void cleanStaleKnowledge(ValueNumberFrame vnaFrameAfter) {
051: if (vnaFrameAfter.isTop() && !isTop())
052: throw new IllegalArgumentException("VNA frame is top");
053: if (!trackValueNumbers)
054: return;
055: if (!ValueNumberAnalysisFeatures.REDUNDANT_LOAD_ELIMINATION)
056: return;
057: for (Iterator<ValueNumber> i = knownValueMap.keySet()
058: .iterator(); i.hasNext();) {
059: ValueNumber v = i.next();
060: if (vnaFrameAfter.getLoad(v) == null) {
061: if (IsNullValueAnalysis.DEBUG)
062: System.out.println("PURGING " + v);
063: i.remove();
064: }
065: }
066:
067: }
068:
069: @Override
070: public void setTop() {
071: super .setTop();
072: if (trackValueNumbers) {
073: knownValueMap.clear();
074: }
075: decision = null;
076: }
077:
078: public void toExceptionValues() {
079: for (int i = 0; i < getNumSlots(); ++i)
080: setValue(i, getValue(i).toExceptionValue());
081:
082: if (trackValueNumbers) {
083: Map<ValueNumber, IsNullValue> replaceMap = new HashMap<ValueNumber, IsNullValue>();
084: for (Map.Entry<ValueNumber, IsNullValue> entry : knownValueMap
085: .entrySet()) {
086: replaceMap.put(entry.getKey(), entry.getValue()
087: .toExceptionValue());
088: }
089: this .knownValueMap = replaceMap;
090: }
091: }
092:
093: public void setDecision(@CheckForNull
094: IsNullConditionDecision decision) {
095: this .decision = decision;
096: }
097:
098: public @CheckForNull
099: IsNullConditionDecision getDecision() {
100: return decision;
101: }
102:
103: public void setKnownValue(@NonNull
104: ValueNumber valueNumber, @NonNull
105: IsNullValue knownValue) {
106: assert trackValueNumbers;
107: if (valueNumber == null || knownValue == null)
108: throw new NullPointerException();
109: knownValueMap.put(valueNumber, knownValue);
110: if (IsNullValueAnalysis.DEBUG) {
111: System.out
112: .println("Updated information for " + valueNumber);
113: System.out.println(" now " + this );
114: }
115: }
116:
117: public void useNewValueNumberForLoad(ValueNumber oldValueNumber,
118: ValueNumber newValueNumber) {
119: if (oldValueNumber == null || newValueNumber == null)
120: throw new NullPointerException();
121: if (newValueNumber.equals(oldValueNumber) || !trackValueNumbers)
122: return;
123: IsNullValue isNullValue = knownValueMap.get(oldValueNumber);
124: if (isNullValue != null) {
125: knownValueMap.put(newValueNumber, isNullValue);
126: knownValueMap.remove(oldValueNumber);
127: }
128: }
129:
130: public IsNullValue getKnownValue(ValueNumber valueNumber) {
131: assert trackValueNumbers;
132: return knownValueMap.get(valueNumber);
133: }
134:
135: public Collection<ValueNumber> getKnownValues() {
136: if (trackValueNumbers) {
137: return knownValueMap.keySet();
138: } else {
139: return TigerSubstitutes.emptySet();
140: }
141: }
142:
143: public Collection<Map.Entry<ValueNumber, IsNullValue>> getKnownValueMapEntrySet() {
144: if (trackValueNumbers) {
145: return knownValueMap.entrySet();
146: } else {
147: return TigerSubstitutes.emptySet();
148: }
149: }
150:
151: public void mergeKnownValuesWith(IsNullValueFrame otherFrame) {
152: assert trackValueNumbers;
153: if (IsNullValueAnalysis.DEBUG) {
154: System.out.println("merge");
155: System.out.println(" " + this );
156: System.out.println(" with" + otherFrame);
157: }
158: Map<ValueNumber, IsNullValue> replaceMap = new HashMap<ValueNumber, IsNullValue>();
159: for (Map.Entry<ValueNumber, IsNullValue> entry : knownValueMap
160: .entrySet()) {
161: IsNullValue otherKnownValue = otherFrame.knownValueMap
162: .get(entry.getKey());
163: if (otherKnownValue == null) {
164: if (IsNullValueAnalysis.DEBUG) {
165: System.out
166: .println("No match for " + entry.getKey());
167:
168: }
169: continue;
170: }
171: IsNullValue mergedValue = IsNullValue.merge(entry
172: .getValue(), otherKnownValue);
173: replaceMap.put(entry.getKey(), mergedValue);
174: if (IsNullValueAnalysis.DEBUG
175: && !mergedValue.equals(entry.getValue())) {
176:
177: System.out.println("Updated information for "
178: + entry.getKey());
179: System.out.println(" was "
180: + entry.getValue());
181: System.out.println(" merged value "
182: + mergedValue);
183:
184: }
185: }
186: knownValueMap.clear();
187: knownValueMap.putAll(replaceMap);
188: if (IsNullValueAnalysis.DEBUG) {
189: System.out.println("resulting in " + this );
190:
191: }
192: }
193:
194: /* (non-Javadoc)
195: * @see edu.umd.cs.findbugs.ba.Frame#copyFrom(edu.umd.cs.findbugs.ba.Frame)
196: */
197: @Override
198: public void copyFrom(Frame<IsNullValue> other) {
199: super .copyFrom(other);
200: decision = ((IsNullValueFrame) other).decision;
201: if (trackValueNumbers) {
202: knownValueMap = new HashMap<ValueNumber, IsNullValue>(
203: ((IsNullValueFrame) other).knownValueMap);
204: }
205: }
206:
207: @Override
208: public boolean sameAs(Frame<IsNullValue> other) {
209: if (!(other instanceof IsNullValueFrame))
210: return false;
211: if (!super .sameAs(other))
212: return false;
213: IsNullValueFrame o2 = (IsNullValueFrame) other;
214: if (!Util.nullSafeEquals(decision, o2.decision))
215: return false;
216: if (trackValueNumbers
217: && !Util
218: .nullSafeEquals(knownValueMap, o2.knownValueMap))
219: return false;
220:
221: return true;
222: }
223:
224: @Override
225: public String toString() {
226: String result = super .toString();
227: if (decision != null) {
228: result = result + ", [decision=" + decision.toString()
229: + "]";
230: }
231: if (knownValueMap != null) {
232: // result = result + ", [known=" + knownValueMap.toString() + "]";
233: StringBuffer buf = new StringBuffer();
234: buf.append("{");
235: boolean first = true;
236: for (Map.Entry<ValueNumber, IsNullValue> entry : knownValueMap
237: .entrySet()) {
238: if (!first) {
239: buf.append(", ");
240: } else {
241: first = false;
242: }
243: buf
244: .append(Strings.trimComma(entry.getKey()
245: .toString()));
246: buf.append("->");
247: buf.append(Strings.trimComma(entry.getValue()
248: .toString()));
249: }
250: buf.append("}");
251: result += ", [known=" + buf.toString() + "]";
252: }
253: return result;
254: }
255:
256: /**
257: * Downgrade all NSP values in frame.
258: * Should be called when a non-exception control split occurs.
259: */
260: public void downgradeOnControlSplit() {
261: final int numSlots = getNumSlots();
262: for (int i = 0; i < numSlots; ++i) {
263: IsNullValue value = getValue(i);
264: value = value.downgradeOnControlSplit();
265: setValue(i, value);
266: }
267:
268: if (knownValueMap != null) {
269: for (Map.Entry<ValueNumber, IsNullValue> entry : knownValueMap
270: .entrySet()) {
271: entry.setValue(entry.getValue()
272: .downgradeOnControlSplit());
273: }
274: }
275: }
276: }
277:
278: // vim:ts=4
|