comparison agent/src/share/classes/sun/jvm/hotspot/oops/ConstantPool.java @ 1602:136b78722a08

6939203: JSR 292 needs method handle constants Summary: Add new CP types CONSTANT_MethodHandle, CONSTANT_MethodType; extend 'ldc' bytecode. Reviewed-by: twisti, never
author jrose
date Wed, 09 Jun 2010 18:50:45 -0700
parents c18cbe5936b8
children 083fde3b838e
comparison
equal deleted inserted replaced
1585:49fac4acd688 1602:136b78722a08
150 System.err.println("ConstantPool.getFieldOrMethodAt(" + i + "): result = " + res); 150 System.err.println("ConstantPool.getFieldOrMethodAt(" + i + "): result = " + res);
151 } 151 }
152 return res; 152 return res;
153 } 153 }
154 154
155 public int getNameAndTypeAt(int which) { 155 public int[] getNameAndTypeAt(int which) {
156 if (Assert.ASSERTS_ENABLED) { 156 if (Assert.ASSERTS_ENABLED) {
157 Assert.that(getTagAt(which).isNameAndType(), "Corrupted constant pool"); 157 Assert.that(getTagAt(which).isNameAndType(), "Corrupted constant pool");
158 } 158 }
159 int i = getIntAt(which); 159 int i = getIntAt(which);
160 if (DEBUG) { 160 if (DEBUG) {
161 System.err.println("ConstantPool.getNameAndTypeAt(" + which + "): result = " + i); 161 System.err.println("ConstantPool.getNameAndTypeAt(" + which + "): result = " + i);
162 } 162 }
163 return i; 163 return new int[] { extractLowShortFromInt(i), extractHighShortFromInt(i) };
164 } 164 }
165 165
166 public Symbol getNameRefAt(int which) { 166 public Symbol getNameRefAt(int which) {
167 int refIndex = getNameAndTypeAt(getNameAndTypeRefIndexAt(which)); 167 int nameIndex = getNameAndTypeAt(getNameAndTypeRefIndexAt(which))[0];
168 int nameIndex = extractLowShortFromInt(refIndex);
169 return getSymbolAt(nameIndex); 168 return getSymbolAt(nameIndex);
170 } 169 }
171 170
172 public Symbol getSignatureRefAt(int which) { 171 public Symbol getSignatureRefAt(int which) {
173 int refIndex = getNameAndTypeAt(getNameAndTypeRefIndexAt(which)); 172 int sigIndex = getNameAndTypeAt(getNameAndTypeRefIndexAt(which))[1];
174 int sigIndex = extractHighShortFromInt(refIndex);
175 return getSymbolAt(sigIndex); 173 return getSymbolAt(sigIndex);
176 } 174 }
177 175
178 // returns null, if not resolved. 176 // returns null, if not resolved.
179 public Klass getKlassRefAt(int which) { 177 public Klass getKlassRefAt(int which) {
218 return i; 216 return i;
219 } 217 }
220 218
221 /** Lookup for entries consisting of (name_index, signature_index) */ 219 /** Lookup for entries consisting of (name_index, signature_index) */
222 public int getNameRefIndexAt(int index) { 220 public int getNameRefIndexAt(int index) {
223 int refIndex = getNameAndTypeAt(index); 221 int[] refIndex = getNameAndTypeAt(index);
224 if (DEBUG) { 222 if (DEBUG) {
225 System.err.println("ConstantPool.getNameRefIndexAt(" + index + "): refIndex = " + refIndex); 223 System.err.println("ConstantPool.getNameRefIndexAt(" + index + "): refIndex = " + refIndex[0]+"/"+refIndex[1]);
226 } 224 }
227 int i = extractLowShortFromInt(refIndex); 225 int i = refIndex[0];
228 if (DEBUG) { 226 if (DEBUG) {
229 System.err.println("ConstantPool.getNameRefIndexAt(" + index + "): result = " + i); 227 System.err.println("ConstantPool.getNameRefIndexAt(" + index + "): result = " + i);
230 } 228 }
231 return i; 229 return i;
232 } 230 }
233 231
234 /** Lookup for entries consisting of (name_index, signature_index) */ 232 /** Lookup for entries consisting of (name_index, signature_index) */
235 public int getSignatureRefIndexAt(int index) { 233 public int getSignatureRefIndexAt(int index) {
236 int refIndex = getNameAndTypeAt(index); 234 int[] refIndex = getNameAndTypeAt(index);
237 if (DEBUG) { 235 if (DEBUG) {
238 System.err.println("ConstantPool.getSignatureRefIndexAt(" + index + "): refIndex = " + refIndex); 236 System.err.println("ConstantPool.getSignatureRefIndexAt(" + index + "): refIndex = " + refIndex[0]+"/"+refIndex[1]);
239 } 237 }
240 int i = extractHighShortFromInt(refIndex); 238 int i = refIndex[1];
241 if (DEBUG) { 239 if (DEBUG) {
242 System.err.println("ConstantPool.getSignatureRefIndexAt(" + index + "): result = " + i); 240 System.err.println("ConstantPool.getSignatureRefIndexAt(" + index + "): result = " + i);
243 } 241 }
244 return i; 242 return i;
243 }
244
245 /** Lookup for MethodHandle entries. */
246 public int getMethodHandleIndexAt(int i) {
247 if (Assert.ASSERTS_ENABLED) {
248 Assert.that(getTagAt(i).isMethodHandle(), "Corrupted constant pool");
249 }
250 int res = extractHighShortFromInt(getIntAt(i));
251 if (DEBUG) {
252 System.err.println("ConstantPool.getMethodHandleIndexAt(" + i + "): result = " + res);
253 }
254 return res;
255 }
256
257 /** Lookup for MethodHandle entries. */
258 public int getMethodHandleRefKindAt(int i) {
259 if (Assert.ASSERTS_ENABLED) {
260 Assert.that(getTagAt(i).isMethodHandle(), "Corrupted constant pool");
261 }
262 int res = extractLowShortFromInt(getIntAt(i));
263 if (DEBUG) {
264 System.err.println("ConstantPool.getMethodHandleRefKindAt(" + i + "): result = " + res);
265 }
266 return res;
267 }
268
269 /** Lookup for MethodType entries. */
270 public int getMethodTypeIndexAt(int i) {
271 if (Assert.ASSERTS_ENABLED) {
272 Assert.that(getTagAt(i).isMethodType(), "Corrupted constant pool");
273 }
274 int res = getIntAt(i);
275 if (DEBUG) {
276 System.err.println("ConstantPool.getMethodHandleTypeAt(" + i + "): result = " + res);
277 }
278 return res;
245 } 279 }
246 280
247 final private static String[] nameForTag = new String[] { 281 final private static String[] nameForTag = new String[] {
248 }; 282 };
249 283
259 case JVM_CONSTANT_String: return "JVM_CONSTANT_String"; 293 case JVM_CONSTANT_String: return "JVM_CONSTANT_String";
260 case JVM_CONSTANT_Fieldref: return "JVM_CONSTANT_Fieldref"; 294 case JVM_CONSTANT_Fieldref: return "JVM_CONSTANT_Fieldref";
261 case JVM_CONSTANT_Methodref: return "JVM_CONSTANT_Methodref"; 295 case JVM_CONSTANT_Methodref: return "JVM_CONSTANT_Methodref";
262 case JVM_CONSTANT_InterfaceMethodref: return "JVM_CONSTANT_InterfaceMethodref"; 296 case JVM_CONSTANT_InterfaceMethodref: return "JVM_CONSTANT_InterfaceMethodref";
263 case JVM_CONSTANT_NameAndType: return "JVM_CONSTANT_NameAndType"; 297 case JVM_CONSTANT_NameAndType: return "JVM_CONSTANT_NameAndType";
298 case JVM_CONSTANT_MethodHandle: return "JVM_CONSTANT_MethodHandle";
299 case JVM_CONSTANT_MethodType: return "JVM_CONSTANT_MethodType";
264 case JVM_CONSTANT_Invalid: return "JVM_CONSTANT_Invalid"; 300 case JVM_CONSTANT_Invalid: return "JVM_CONSTANT_Invalid";
265 case JVM_CONSTANT_UnresolvedClass: return "JVM_CONSTANT_UnresolvedClass"; 301 case JVM_CONSTANT_UnresolvedClass: return "JVM_CONSTANT_UnresolvedClass";
266 case JVM_CONSTANT_UnresolvedClassInError: return "JVM_CONSTANT_UnresolvedClassInError"; 302 case JVM_CONSTANT_UnresolvedClassInError: return "JVM_CONSTANT_UnresolvedClassInError";
267 case JVM_CONSTANT_ClassIndex: return "JVM_CONSTANT_ClassIndex"; 303 case JVM_CONSTANT_ClassIndex: return "JVM_CONSTANT_ClassIndex";
268 case JVM_CONSTANT_UnresolvedString: return "JVM_CONSTANT_UnresolvedString"; 304 case JVM_CONSTANT_UnresolvedString: return "JVM_CONSTANT_UnresolvedString";
315 351
316 case JVM_CONSTANT_Fieldref: 352 case JVM_CONSTANT_Fieldref:
317 case JVM_CONSTANT_Methodref: 353 case JVM_CONSTANT_Methodref:
318 case JVM_CONSTANT_InterfaceMethodref: 354 case JVM_CONSTANT_InterfaceMethodref:
319 case JVM_CONSTANT_NameAndType: 355 case JVM_CONSTANT_NameAndType:
356 case JVM_CONSTANT_MethodHandle:
357 case JVM_CONSTANT_MethodType:
320 visitor.doInt(new IntField(new NamedFieldIdentifier(nameForTag(ctag)), indexOffset(index), true), true); 358 visitor.doInt(new IntField(new NamedFieldIdentifier(nameForTag(ctag)), indexOffset(index), true), true);
321 break; 359 break;
322 } 360 }
323 } 361 }
324 } 362 }
465 dos.writeShort(signatureIndex); 503 dos.writeShort(signatureIndex);
466 if (DEBUG) debugMessage("CP[" + ci + "] = N&T name = " + nameIndex 504 if (DEBUG) debugMessage("CP[" + ci + "] = N&T name = " + nameIndex
467 + ", type = " + signatureIndex); 505 + ", type = " + signatureIndex);
468 break; 506 break;
469 } 507 }
508
509 case JVM_CONSTANT_MethodHandle: {
510 dos.writeByte(cpConstType);
511 int value = getIntAt(ci);
512 short nameIndex = (short) extractLowShortFromInt(value);
513 short signatureIndex = (short) extractHighShortFromInt(value);
514 dos.writeShort(nameIndex);
515 dos.writeShort(signatureIndex);
516 if (DEBUG) debugMessage("CP[" + ci + "] = N&T name = " + nameIndex
517 + ", type = " + signatureIndex);
518 break;
519 }
470 default: 520 default:
471 throw new InternalError("unknown tag: " + cpConstType); 521 throw new InternalError("unknown tag: " + cpConstType);
472 } // switch 522 } // switch
473 } 523 }
474 dos.flush(); 524 dos.flush();
486 //---------------------------------------------------------------------- 536 //----------------------------------------------------------------------
487 // Internals only below this point 537 // Internals only below this point
488 // 538 //
489 539
490 private static int extractHighShortFromInt(int val) { 540 private static int extractHighShortFromInt(int val) {
541 // must stay in sync with constantPoolOopDesc::name_and_type_at_put, method_at_put, etc.
491 return (val >> 16) & 0xFFFF; 542 return (val >> 16) & 0xFFFF;
492 } 543 }
493 544
494 private static int extractLowShortFromInt(int val) { 545 private static int extractLowShortFromInt(int val) {
546 // must stay in sync with constantPoolOopDesc::name_and_type_at_put, method_at_put, etc.
495 return val & 0xFFFF; 547 return val & 0xFFFF;
496 } 548 }
497 } 549 }