01: //
02: // Copyright (C) 2005 United States Government as represented by the
03: // Administrator of the National Aeronautics and Space Administration
04: // (NASA). All Rights Reserved.
05: //
06: // This software is distributed under the NASA Open Source Agreement
07: // (NOSA), version 1.3. The NOSA has been approved by the Open Source
08: // Initiative. See the file NOSA-1.3-JPF at the top of the distribution
09: // directory tree for the complete NOSA document.
10: //
11: // THE SUBJECT SOFTWARE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY OF ANY
12: // KIND, EITHER EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT
13: // LIMITED TO, ANY WARRANTY THAT THE SUBJECT SOFTWARE WILL CONFORM TO
14: // SPECIFICATIONS, ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR
15: // A PARTICULAR PURPOSE, OR FREEDOM FROM INFRINGEMENT, ANY WARRANTY THAT
16: // THE SUBJECT SOFTWARE WILL BE ERROR FREE, OR ANY WARRANTY THAT
17: // DOCUMENTATION, IF PROVIDED, WILL CONFORM TO THE SUBJECT SOFTWARE.
18: //
19: package gov.nasa.jpf.jvm;
20:
21: import org.apache.bcel.classfile.*;
22: import gov.nasa.jpf.JPFException;
23:
24: /**
25: * field info for object fields
26: */
27: public class ReferenceFieldInfo extends FieldInfo {
28: int init = -1;
29: String sInit; // <2do> pcm - just a temporary quirk to init from string literals
30:
31: // check if there are other non-object reference inits
32:
33: public ReferenceFieldInfo(String name, String type,
34: boolean isStatic, ConstantValue cv, ClassInfo ci, int idx,
35: int off) {
36: super (name, type, isStatic, cv, ci, idx, off);
37: init = computeInitValue(cv);
38: }
39:
40: public String valueToString(Fields f) {
41: int i = f.getIntValue(storageOffset);
42: if (i == -1) {
43: return "null";
44: } else {
45: return DynamicArea.getHeap().get(i).toString();
46: }
47: }
48:
49: public boolean isReference() {
50: return true;
51: }
52:
53: public boolean isArrayField() {
54: return ci.isArray;
55: }
56:
57: int computeInitValue(ConstantValue cv) {
58: // <2do> pcm - check what other constants we might encounter, this is most
59: // probably not just used for Strings.
60: // Besides the type issue, there is an even bigger problem with identities.
61: // For instance, all String refs initialized via the same string literal
62: // inside a single classfile are in fact refering to the same object. This
63: // means we have to keep a registry (hashtab) with string-literal created
64: // String objects per ClassInfo, and use this when we assign or init
65: // String references.
66: // For the sake of progress, we ignore this for now, but have to come back
67: // to it because it violates the VM spec
68:
69: if (cv == null)
70: return -1;
71:
72: // here the mess starts
73: //DynamicArea heap = DynamicArea.getHeap();
74: String s = cv.toString();
75:
76: if (s.charAt(0) == '"') {
77: s = s.substring(1, s.length() - 1); // chop off the double quotes
78: sInit = s;
79:
80: //init = heap.newString(s, null); // turn literal into a string object
81: // but how do we pin it down?
82: } else {
83: throw new JPFException(
84: "unsupported reference initialization: " + s);
85: }
86:
87: return -1;
88: }
89:
90: public void initialize(Fields f) {
91: int ref = init;
92: if (sInit != null) {
93: ref = DynamicArea.getHeap().newString(sInit, null);
94: }
95: f.setReferenceValue(storageOffset, ref);
96: }
97: }
|