6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25 #include "precompiled.hpp" 26 #include "interpreter/interpreter.hpp" 27 #include "oops/oop.inline.hpp" 28 #include "oops/fieldStreams.hpp" 29 #include "oops/method.hpp" 30 #include "oops/objArrayKlass.hpp" 31 #include "oops/valueKlass.hpp" 32 #include "oops/valueArrayKlass.hpp" 33 #include "utilities/copy.hpp" 34 35 int ValueKlass::first_field_offset() const { 36 #ifdef ASSERT 37 int first_offset = INT_MAX; 38 for (JavaFieldStream fs(this); !fs.done(); fs.next()) { 39 if (fs.offset() < first_offset) first_offset= fs.offset(); 40 } 41 #endif 42 int base_offset = instanceOopDesc::base_offset_in_bytes(); 43 // The first field of value types is aligned on a long boundary 44 base_offset = align_size_up(base_offset, BytesPerLong); 45 assert(base_offset = first_offset, "inconsistent offsets"); 46 return base_offset; 47 } 48 49 int ValueKlass::raw_value_byte_size() const { 50 int heapOopAlignedSize = nonstatic_field_size() << LogBytesPerHeapOop; 51 // If bigger than 64 bits or needs oop alignment, then use jlong aligned 52 // which for values should be jlong aligned, asserts in raw_field_copy otherwise 220 // Post-barriers... 221 map = start_of_nonstatic_oop_maps(); 222 while (map != end) { 223 address doop_address = dst_oop_addr + map->offset(); 224 oopDesc::bs()->write_ref_array((HeapWord*) doop_address, map->count()); 225 map++; 226 } 227 } else { // Primitive-only case... 228 raw_field_copy(src, dst, raw_byte_size); 229 } 230 } 231 232 oop ValueKlass::derive_value_type_copy(Handle src, InstanceKlass* target_klass, TRAPS) { 233 assert(InstanceKlass::cast(src->klass())->derive_value_type_klass() == target_klass, "Not this DVT"); 234 235 // Allocate new for safety, simply reinstalling the klass pointer is a little too risky 236 target_klass->initialize(CHECK_0); 237 instanceOop value = target_klass->allocate_instance(CHECK_0); 238 value_store(data_for_oop(src()), data_for_oop(value), true, true); 239 return value; 240 } | 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25 #include "precompiled.hpp" 26 #include "gc/shared/gcLocker.inline.hpp" 27 #include "interpreter/interpreter.hpp" 28 #include "oops/oop.inline.hpp" 29 #include "oops/fieldStreams.hpp" 30 #include "oops/method.hpp" 31 #include "oops/objArrayKlass.hpp" 32 #include "oops/valueKlass.hpp" 33 #include "oops/valueArrayKlass.hpp" 34 #include "runtime/signature.hpp" 35 #include "utilities/copy.hpp" 36 37 int ValueKlass::first_field_offset() const { 38 #ifdef ASSERT 39 int first_offset = INT_MAX; 40 for (JavaFieldStream fs(this); !fs.done(); fs.next()) { 41 if (fs.offset() < first_offset) first_offset= fs.offset(); 42 } 43 #endif 44 int base_offset = instanceOopDesc::base_offset_in_bytes(); 45 // The first field of value types is aligned on a long boundary 46 base_offset = align_size_up(base_offset, BytesPerLong); 47 assert(base_offset = first_offset, "inconsistent offsets"); 48 return base_offset; 49 } 50 51 int ValueKlass::raw_value_byte_size() const { 52 int heapOopAlignedSize = nonstatic_field_size() << LogBytesPerHeapOop; 53 // If bigger than 64 bits or needs oop alignment, then use jlong aligned 54 // which for values should be jlong aligned, asserts in raw_field_copy otherwise 222 // Post-barriers... 223 map = start_of_nonstatic_oop_maps(); 224 while (map != end) { 225 address doop_address = dst_oop_addr + map->offset(); 226 oopDesc::bs()->write_ref_array((HeapWord*) doop_address, map->count()); 227 map++; 228 } 229 } else { // Primitive-only case... 230 raw_field_copy(src, dst, raw_byte_size); 231 } 232 } 233 234 oop ValueKlass::derive_value_type_copy(Handle src, InstanceKlass* target_klass, TRAPS) { 235 assert(InstanceKlass::cast(src->klass())->derive_value_type_klass() == target_klass, "Not this DVT"); 236 237 // Allocate new for safety, simply reinstalling the klass pointer is a little too risky 238 target_klass->initialize(CHECK_0); 239 instanceOop value = target_klass->allocate_instance(CHECK_0); 240 value_store(data_for_oop(src()), data_for_oop(value), true, true); 241 return value; 242 } 243 244 // Value type arguments are not passed by reference, instead each 245 // field of the value type is passed as an argument. This helper 246 // function collects the fields of the value types (including embedded 247 // value type's fields) in a list. Included with the field's type is 248 // the offset of each field in the value type: i2c and c2i adapters 249 // need that to load or store fields. Finally, the list of fields is 250 // sorted in order of increasing offsets: the adapters and the 251 // compiled code need and agreed upon order of fields. 252 // 253 // The list of basic types that is returned starts with a T_VALUETYPE 254 // and ends with an extra T_VOID. T_VALUETYPE/T_VOID are used as 255 // delimiters. Every entry between the two is a field of the value 256 // type. If there's an embedded value type in the list, it also starts 257 // with a T_VALUETYPE and ends with a T_VOID. This is so we can 258 // generate a unique fingerprint for the method's adapters and we can 259 // generate the list of basic types from the interpreter point of view 260 // (value types passed as reference: iterate on the list until a 261 // T_VALUETYPE, drop everything until and including the closing 262 // T_VOID) or the compiler point of view (each field of the value 263 // types is an argument: drop all T_VALUETYPE/T_VOID from the list). 264 GrowableArray<SigEntry> ValueKlass::collect_fields(int base_off) const { 265 GrowableArray<SigEntry> sig_extended; 266 sig_extended.push(SigEntry(T_VALUETYPE, base_off)); 267 for (JavaFieldStream fs(this); !fs.done(); fs.next()) { 268 if (fs.access_flags().is_static()) continue; 269 fieldDescriptor& fd = fs.field_descriptor(); 270 BasicType bt = fd.field_type(); 271 int offset = base_off + fd.offset() - (base_off > 0 ? first_field_offset() : 0); 272 if (bt == T_VALUETYPE) { 273 Symbol* signature = fd.signature(); 274 JavaThread* THREAD = JavaThread::current(); 275 oop loader = class_loader(); 276 oop domain = protection_domain(); 277 ResetNoHandleMark rnhm; 278 HandleMark hm; 279 NoSafepointVerifier nsv; 280 Klass* klass = SystemDictionary::resolve_or_null(signature, 281 Handle(THREAD, loader), Handle(THREAD, domain), 282 THREAD); 283 assert(klass != NULL && !HAS_PENDING_EXCEPTION, "lookup shouldn't fail"); 284 const GrowableArray<SigEntry>& embedded = ValueKlass::cast(klass)->collect_fields(offset); 285 sig_extended.appendAll(&embedded); 286 } else { 287 sig_extended.push(SigEntry(bt, offset)); 288 if (bt == T_LONG || bt == T_DOUBLE) { 289 sig_extended.push(SigEntry(T_VOID, offset)); 290 } 291 } 292 } 293 int offset = base_off + size_helper()*HeapWordSize - (base_off > 0 ? first_field_offset() : 0); 294 sig_extended.push(SigEntry(T_VOID, offset)); // hack: use T_VOID to mark end of value type fields 295 if (base_off == 0) { 296 sig_extended.sort(SigEntry::compare); 297 } 298 assert(sig_extended.at(0)._bt == T_VALUETYPE && sig_extended.at(sig_extended.length()-1)._bt == T_VOID, "broken structure"); 299 return sig_extended; 300 } 301 302 // Returns the basic types and registers for fields to return an 303 // instance of this value type in registers if possible. 304 GrowableArray<SigEntry> ValueKlass::return_convention(VMRegPair*& regs, int& nb_fields) const { 305 assert(ValueTypeReturnedAsFields, "inconsistent"); 306 const GrowableArray<SigEntry>& sig_vk = collect_fields(); 307 nb_fields = SigEntry::count_fields(sig_vk)+1; 308 BasicType* sig_bt = NEW_RESOURCE_ARRAY(BasicType, nb_fields); 309 sig_bt[0] = T_METADATA; 310 SigEntry::fill_sig_bt(sig_vk, sig_bt+1, nb_fields-1, true); 311 regs = NEW_RESOURCE_ARRAY(VMRegPair, nb_fields); 312 int total = SharedRuntime::java_return_convention(sig_bt, regs, nb_fields); 313 314 if (total <= 0) { 315 regs = NULL; 316 } 317 318 return sig_vk; 319 } 320 321 // Create handles for all oop fields returned in registers that are 322 // going to be live across a safepoint. 323 bool ValueKlass::save_oop_results(RegisterMap& reg_map, GrowableArray<Handle>& handles) const { 324 if (ValueTypeReturnedAsFields) { 325 int nb_fields; 326 VMRegPair* regs; 327 const GrowableArray<SigEntry>& sig_vk = return_convention(regs, nb_fields); 328 329 if (regs != NULL) { 330 regs++; 331 nb_fields--; 332 save_oop_fields(sig_vk, reg_map, regs, handles, nb_fields); 333 return true; 334 } 335 } 336 return false; 337 } 338 339 // Same as above but with pre-computed return convention 340 void ValueKlass::save_oop_fields(const GrowableArray<SigEntry>& sig_vk, RegisterMap& reg_map, const VMRegPair* regs, GrowableArray<Handle>& handles, int nb_fields) const { 341 int j = 0; 342 Thread* thread = Thread::current(); 343 for (int i = 0; i < sig_vk.length(); i++) { 344 BasicType bt = sig_vk.at(i)._bt; 345 if (bt == T_OBJECT || bt == T_ARRAY) { 346 int off = sig_vk.at(i)._offset; 347 VMRegPair pair = regs[j]; 348 address loc = reg_map.location(pair.first()); 349 oop v = *(oop*)loc; 350 assert(v == NULL || v->is_oop(), "not an oop?"); 351 assert(Universe::heap()->is_in_or_null(v), "must be heap pointer"); 352 handles.push(Handle(thread, v)); 353 } 354 if (bt == T_VALUETYPE) { 355 continue; 356 } 357 if (bt == T_VOID && 358 sig_vk.at(i-1)._bt != T_LONG && 359 sig_vk.at(i-1)._bt != T_DOUBLE) { 360 continue; 361 } 362 j++; 363 } 364 assert(j == nb_fields, "missed a field?"); 365 } 366 367 // Update oop fields in registers from handles after a safepoint 368 void ValueKlass::restore_oop_results(RegisterMap& reg_map, GrowableArray<Handle>& handles) const { 369 assert(ValueTypeReturnedAsFields, "inconsistent"); 370 int nb_fields; 371 VMRegPair* regs; 372 const GrowableArray<SigEntry>& sig_vk = return_convention(regs, nb_fields); 373 assert(regs != NULL, "inconsistent"); 374 375 regs++; 376 nb_fields--; 377 378 int j = 0; 379 for (int i = 0, k = 0; i < sig_vk.length(); i++) { 380 BasicType bt = sig_vk.at(i)._bt; 381 if (bt == T_OBJECT || bt == T_ARRAY) { 382 int off = sig_vk.at(i)._offset; 383 VMRegPair pair = regs[j]; 384 address loc = reg_map.location(pair.first()); 385 *(oop*)loc = handles.at(k++)(); 386 } 387 if (bt == T_VALUETYPE) { 388 continue; 389 } 390 if (bt == T_VOID && 391 sig_vk.at(i-1)._bt != T_LONG && 392 sig_vk.at(i-1)._bt != T_DOUBLE) { 393 continue; 394 } 395 j++; 396 } 397 assert(j == nb_fields, "missed a field?"); 398 } 399 400 // Fields are in registers. Create an instance of the value type and 401 // initialize it with the values of the fields. 402 oop ValueKlass::realloc_result(const GrowableArray<SigEntry>& sig_vk, const RegisterMap& reg_map, const VMRegPair* regs, 403 const GrowableArray<Handle>& handles, int nb_fields, TRAPS) { 404 oop new_vt = allocate_instance(CHECK_NULL); 405 406 int j = 0; 407 int k = 0; 408 for (int i = 0; i < sig_vk.length(); i++) { 409 BasicType bt = sig_vk.at(i)._bt; 410 if (bt == T_VALUETYPE) { 411 continue; 412 } 413 if (bt == T_VOID) { 414 if (sig_vk.at(i-1)._bt == T_LONG || 415 sig_vk.at(i-1)._bt == T_DOUBLE) { 416 j++; 417 } 418 continue; 419 } 420 int off = sig_vk.at(i)._offset; 421 VMRegPair pair = regs[j]; 422 address loc = reg_map.location(pair.first()); 423 switch(bt) { 424 case T_BOOLEAN: { 425 jboolean v = *(intptr_t*)loc; 426 *(jboolean*)((address)new_vt + off) = v; 427 break; 428 } 429 case T_CHAR: { 430 jchar v = *(intptr_t*)loc; 431 *(jchar*)((address)new_vt + off) = v; 432 break; 433 } 434 case T_BYTE: { 435 jbyte v = *(intptr_t*)loc; 436 *(jbyte*)((address)new_vt + off) = v; 437 break; 438 } 439 case T_SHORT: { 440 jshort v = *(intptr_t*)loc; 441 *(jshort*)((address)new_vt + off) = v; 442 break; 443 } 444 case T_INT: { 445 jint v = *(intptr_t*)loc; 446 *(jint*)((address)new_vt + off) = v; 447 break; 448 } 449 case T_LONG: { 450 #ifdef _LP64 451 jlong v = *(intptr_t*)loc; 452 *(jlong*)((address)new_vt + off) = v; 453 #else 454 Unimplemented(); 455 #endif 456 break; 457 } 458 case T_OBJECT: 459 case T_ARRAY: { 460 Handle handle = handles.at(k++); 461 oop v = handle(); 462 if (!UseCompressedOops) { 463 oop* p = (oop*)((address)new_vt + off); 464 oopDesc::store_heap_oop(p, v); 465 } else { 466 narrowOop* p = (narrowOop*)((address)new_vt + off); 467 oopDesc::encode_store_heap_oop(p, v); 468 } 469 break; 470 } 471 case T_FLOAT: { 472 jfloat v = *(jfloat*)loc; 473 *(jfloat*)((address)new_vt + off) = v; 474 break; 475 } 476 case T_DOUBLE: { 477 jdouble v = *(jdouble*)loc; 478 *(jdouble*)((address)new_vt + off) = v; 479 break; 480 } 481 default: 482 ShouldNotReachHere(); 483 } 484 j++; 485 } 486 assert(j == nb_fields, "missed a field?"); 487 assert(k == handles.length(), "missed an oop?"); 488 return new_vt; 489 } 490 491 ValueKlass* ValueKlass::returned_value_type(const RegisterMap& map) { 492 BasicType bt = T_METADATA; 493 VMRegPair pair; 494 int nb = SharedRuntime::java_return_convention(&bt, &pair, 1); 495 assert(nb == 1, "broken"); 496 497 address loc = map.location(pair.first()); 498 intptr_t ptr = *(intptr_t*)loc; 499 if (Universe::heap()->is_in_reserved((void*)ptr)) { 500 return NULL; 501 } 502 return (ValueKlass*)ptr; 503 } |