001: //
002: // Copyright (C) 2005 United States Government as represented by the
003: // Administrator of the National Aeronautics and Space Administration
004: // (NASA). All Rights Reserved.
005: //
006: // This software is distributed under the NASA Open Source Agreement
007: // (NOSA), version 1.3. The NOSA has been approved by the Open Source
008: // Initiative. See the file NOSA-1.3-JPF at the top of the distribution
009: // directory tree for the complete NOSA document.
010: //
011: // THE SUBJECT SOFTWARE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY OF ANY
012: // KIND, EITHER EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT
013: // LIMITED TO, ANY WARRANTY THAT THE SUBJECT SOFTWARE WILL CONFORM TO
014: // SPECIFICATIONS, ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR
015: // A PARTICULAR PURPOSE, OR FREEDOM FROM INFRINGEMENT, ANY WARRANTY THAT
016: // THE SUBJECT SOFTWARE WILL BE ERROR FREE, OR ANY WARRANTY THAT
017: // DOCUMENTATION, IF PROVIDED, WILL CONFORM TO THE SUBJECT SOFTWARE.
018: //
019: package gov.nasa.jpf.jvm;
020:
021: /**
022: *a Field (data value) store for array objects
023: */
024: public class ArrayFields extends Fields {
025:
026: private int elementStorageSize;
027: private int length;
028: private String elementType;
029: private boolean isReference;
030:
031: public ArrayFields(String type, ClassInfo ci, int storageSize,
032: int length, boolean isReference) {
033: super (type, ci, storageSize);
034: this .length = length;
035: this .isReference = isReference;
036: elementType = type.substring(1);
037: elementStorageSize = Types.getTypeSize(type);
038:
039: // Ok, this seems to be a design anomaly, but array elements are not fields,
040: // hence the only non-0 element initialization is for reference arrays, and
041: // only with const, default 'null' values. All other inits (even the
042: // static init "a = {...}:") is compiled into explicit NEWARRAY, ASTORE..
043: // sequences, and does not need any special, automatic init code
044: if (isReference) {
045: for (int i = 0; i < length; i++) {
046: values[i] = -1;
047: }
048: }
049: }
050:
051: public int arrayLength() {
052: return length;
053: }
054:
055: public int getHeapSize() {
056: return Types.getTypeSizeInBytes(elementType) * length;
057: }
058:
059: public FieldInfo getFieldInfo(String clsBase, String fname) {
060: // TODO
061: return null;
062: }
063:
064: public int getNumberOfFields() {
065: // TODO
066: return 0;
067: }
068:
069: public FieldInfo getFieldInfo(int fieldIndex) {
070: // TODO
071: return null;
072: }
073:
074: /**
075: * @see gov.nasa.jpf.jvm.iFields#getFieldIndex(String, String)
076: */
077: public int getFieldIndex(String name, String referenceType) {
078: // will this ever happen?
079: throw new NoSuchFieldError("array does not have any fields!"
080: + getClassInfo().getName() + "." + name);
081: }
082:
083: public String getFieldName(int index) {
084: return Integer.toString(index / elementStorageSize);
085: }
086:
087: public String getFieldType(String name, String referenceType) {
088: // will this ever happen?
089: throw new NoSuchFieldError("array does not have any fields!"
090: + getClassInfo().getName() + "." + name);
091: }
092:
093: public String getFieldType(int findex) {
094: if (elementType == null) {
095: elementType = getType().substring(1);
096: }
097:
098: return elementType;
099: }
100:
101: public String getLogChar() {
102: return "*";
103: }
104:
105: public void setLongField(String name, String referenceType,
106: long value) {
107: throw new NoSuchFieldError("array does not have any fields!"
108: + getClassInfo().getName() + "." + name);
109: }
110:
111: public long getLongField(String name, String referenceType) {
112: throw new NoSuchFieldError("array does not have any fields!"
113: + getClassInfo().getName() + "." + name);
114: }
115:
116: public boolean isReferenceArray() {
117: return isReference;
118: }
119:
120: public boolean isRef(String name, String referenceType) {
121: throw new NoSuchFieldError("array does not have any fields!"
122: + getClassInfo().getName() + "." + name);
123: }
124:
125: public boolean[] asBooleanArray() {
126: // <2do> we probably should check the type first
127: int length = values.length;
128: boolean[] result = new boolean[length];
129:
130: for (int i = 0; i < length; i++) {
131: result[i] = Types.intToBoolean(values[i]);
132: }
133:
134: return result;
135: }
136:
137: public byte[] asByteArray() {
138: // <2do> we probably should check the type first
139: int length = values.length;
140: byte[] result = new byte[length];
141:
142: for (int i = 0; i < length; i++) {
143: result[i] = (byte) values[i];
144: }
145:
146: return result;
147: }
148:
149: public char[] asCharArray() {
150: // <2do> we probably should check the type first
151: int length = values.length;
152: char[] result = new char[length];
153:
154: for (int i = 0; i < length; i++) {
155: result[i] = (char) values[i];
156: }
157:
158: return result;
159: }
160:
161: public short[] asShortArray() {
162: // <2do> we probably should check the type first
163: int length = values.length;
164: short[] result = new short[length];
165:
166: for (int i = 0; i < length; i++) {
167: result[i] = (short) values[i];
168: }
169:
170: return result;
171: }
172:
173: public int[] asIntArray() {
174: // <2do> we probably should check the type first
175: int length = values.length;
176: int[] result = new int[length];
177:
178: for (int i = 0; i < length; i++) {
179: result[i] = values[i];
180: }
181:
182: return result;
183: }
184:
185: public long[] asLongArray() {
186: // <2do> we probably should check the type first
187: int length = values.length;
188: long[] result = new long[length];
189:
190: length--;
191: for (int i = 0, j = 0; i < length; i++, j += 2) {
192: result[i] = Types.intsToLong(values[j + 1], values[j]);
193: }
194:
195: return result;
196: }
197:
198: public float[] asFloatArray() {
199: // <2do> we probably should check the type first
200: int length = values.length;
201: float[] result = new float[length];
202:
203: for (int i = 0; i < length; i++) {
204: result[i] = Types.intToFloat(values[i]);
205: }
206:
207: return result;
208: }
209:
210: public double[] asDoubleArray() {
211: // <2do> we probably should check the type first
212: int length = values.length;
213: double[] result = new double[length];
214:
215: length--;
216: for (int i = 0, j = 0; i < length; i++, j += 2) {
217: result[i] = Types.intsToDouble(values[j + 1], values[j]);
218: }
219:
220: return result;
221: }
222:
223: }
|