1 /* 2 * Copyright (c) 1998, 2015, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 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 "code/codeBlob.hpp" 27 #include "code/codeCache.hpp" 28 #include "code/nmethod.hpp" 29 #include "code/scopeDesc.hpp" 30 #include "compiler/oopMap.hpp" 31 #include "gc/shared/collectedHeap.hpp" 32 #include "memory/allocation.inline.hpp" 33 #include "memory/resourceArea.hpp" 34 #include "runtime/frame.inline.hpp" 35 #include "runtime/signature.hpp" 36 #ifdef COMPILER1 37 #include "c1/c1_Defs.hpp" 38 #endif 39 #ifdef COMPILER2 40 #include "opto/optoreg.hpp" 41 #endif 42 43 // OopMapStream 44 45 OopMapStream::OopMapStream(OopMap* oop_map, int oop_types_mask) { 46 _stream = new CompressedReadStream(oop_map->write_stream()->buffer()); 47 _mask = oop_types_mask; 48 _size = oop_map->omv_count(); 49 _position = 0; 50 _valid_omv = false; 51 } 52 53 OopMapStream::OopMapStream(const ImmutableOopMap* oop_map, int oop_types_mask) { 54 _stream = new CompressedReadStream(oop_map->data_addr()); 55 _mask = oop_types_mask; 56 _size = oop_map->count(); 57 _position = 0; 58 _valid_omv = false; 59 } 60 61 62 void OopMapStream::find_next() { 63 while(_position++ < _size) { 64 _omv.read_from(_stream); 65 if(((int)_omv.type() & _mask) > 0) { 66 _valid_omv = true; 67 return; 68 } 69 } 70 _valid_omv = false; 71 } 72 73 74 // OopMap 75 76 // frame_size units are stack-slots (4 bytes) NOT intptr_t; we can name odd 77 // slots to hold 4-byte values like ints and floats in the LP64 build. 78 OopMap::OopMap(int frame_size, int arg_count) { 79 // OopMaps are usually quite so small, so pick a small initial size 80 set_write_stream(new CompressedWriteStream(32)); 81 set_omv_count(0); 82 83 #ifdef ASSERT 84 _locs_length = VMRegImpl::stack2reg(0)->value() + frame_size + arg_count; 85 _locs_used = NEW_RESOURCE_ARRAY(OopMapValue::oop_types, _locs_length); 86 for(int i = 0; i < _locs_length; i++) _locs_used[i] = OopMapValue::unused_value; 87 #endif 88 } 89 90 91 OopMap::OopMap(OopMap::DeepCopyToken, OopMap* source) { 92 // This constructor does a deep copy 93 // of the source OopMap. 94 set_write_stream(new CompressedWriteStream(source->omv_count() * 2)); 95 set_omv_count(0); 96 set_offset(source->offset()); 97 98 #ifdef ASSERT 99 _locs_length = source->_locs_length; 100 _locs_used = NEW_RESOURCE_ARRAY(OopMapValue::oop_types, _locs_length); 101 for(int i = 0; i < _locs_length; i++) _locs_used[i] = OopMapValue::unused_value; 102 #endif 103 104 // We need to copy the entries too. 105 for (OopMapStream oms(source); !oms.is_done(); oms.next()) { 106 OopMapValue omv = oms.current(); 107 omv.write_on(write_stream()); 108 increment_count(); 109 } 110 } 111 112 113 OopMap* OopMap::deep_copy() { 114 return new OopMap(_deep_copy_token, this); 115 } 116 117 void OopMap::copy_data_to(address addr) const { 118 memcpy(addr, write_stream()->buffer(), write_stream()->position()); 119 } 120 121 int OopMap::heap_size() const { 122 int size = sizeof(OopMap); 123 int align = sizeof(void *) - 1; 124 size += write_stream()->position(); 125 // Align to a reasonable ending point 126 size = ((size+align) & ~align); 127 return size; 128 } 129 130 // frame_size units are stack-slots (4 bytes) NOT intptr_t; we can name odd 131 // slots to hold 4-byte values like ints and floats in the LP64 build. 132 void OopMap::set_xxx(VMReg reg, OopMapValue::oop_types x, VMReg optional) { 133 134 assert(reg->value() < _locs_length, "too big reg value for stack size"); 135 assert( _locs_used[reg->value()] == OopMapValue::unused_value, "cannot insert twice" ); 136 debug_only( _locs_used[reg->value()] = x; ) 137 138 OopMapValue o(reg, x); 139 140 if(x == OopMapValue::callee_saved_value) { 141 // This can never be a stack location, so we don't need to transform it. 142 assert(optional->is_reg(), "Trying to callee save a stack location"); 143 o.set_content_reg(optional); 144 } else if(x == OopMapValue::derived_oop_value) { 145 o.set_content_reg(optional); 146 } 147 148 o.write_on(write_stream()); 149 increment_count(); 150 } 151 152 153 void OopMap::set_oop(VMReg reg) { 154 set_xxx(reg, OopMapValue::oop_value, VMRegImpl::Bad()); 155 } 156 157 158 void OopMap::set_value(VMReg reg) { 159 // At this time, we only need value entries in our OopMap when ZapDeadCompiledLocals is active. 160 if (ZapDeadCompiledLocals) 161 set_xxx(reg, OopMapValue::value_value, VMRegImpl::Bad()); 162 } 163 164 165 void OopMap::set_narrowoop(VMReg reg) { 166 set_xxx(reg, OopMapValue::narrowoop_value, VMRegImpl::Bad()); 167 } 168 169 170 void OopMap::set_callee_saved(VMReg reg, VMReg caller_machine_register ) { 171 set_xxx(reg, OopMapValue::callee_saved_value, caller_machine_register); 172 } 173 174 175 void OopMap::set_derived_oop(VMReg reg, VMReg derived_from_local_register ) { 176 if( reg == derived_from_local_register ) { 177 // Actually an oop, derived shares storage with base, 178 set_oop(reg); 179 } else { 180 set_xxx(reg, OopMapValue::derived_oop_value, derived_from_local_register); 181 } 182 } 183 184 // OopMapSet 185 186 OopMapSet::OopMapSet() { 187 set_om_size(MinOopMapAllocation); 188 set_om_count(0); 189 OopMap** temp = NEW_RESOURCE_ARRAY(OopMap*, om_size()); 190 set_om_data(temp); 191 } 192 193 194 void OopMapSet::grow_om_data() { 195 int new_size = om_size() * 2; 196 OopMap** new_data = NEW_RESOURCE_ARRAY(OopMap*, new_size); 197 memcpy(new_data,om_data(),om_size() * sizeof(OopMap*)); 198 set_om_size(new_size); 199 set_om_data(new_data); 200 } 201 202 203 void OopMapSet::add_gc_map(int pc_offset, OopMap *map ) { 204 assert(om_size() != -1,"Cannot grow a fixed OopMapSet"); 205 206 if(om_count() >= om_size()) { 207 grow_om_data(); 208 } 209 map->set_offset(pc_offset); 210 211 #ifdef ASSERT 212 if(om_count() > 0) { 213 OopMap* last = at(om_count()-1); 214 if (last->offset() == map->offset() ) { 215 fatal("OopMap inserted twice"); 216 } 217 if(last->offset() > map->offset()) { 218 tty->print_cr( "WARNING, maps not sorted: pc[%d]=%d, pc[%d]=%d", 219 om_count(),last->offset(),om_count()+1,map->offset()); 220 } 221 } 222 #endif // ASSERT 223 224 set(om_count(),map); 225 increment_count(); 226 } 227 228 229 int OopMapSet::heap_size() const { 230 // The space we use 231 int size = sizeof(OopMap); 232 int align = sizeof(void *) - 1; 233 size = ((size+align) & ~align); 234 size += om_count() * sizeof(OopMap*); 235 236 // Now add in the space needed for the indivdiual OopMaps 237 for(int i=0; i < om_count(); i++) { 238 size += at(i)->heap_size(); 239 } 240 // We don't need to align this, it will be naturally pointer aligned 241 return size; 242 } 243 244 245 OopMap* OopMapSet::singular_oop_map() { 246 guarantee(om_count() == 1, "Make sure we only have a single gc point"); 247 return at(0); 248 } 249 250 251 OopMap* OopMapSet::find_map_at_offset(int pc_offset) const { 252 int i, len = om_count(); 253 assert( len > 0, "must have pointer maps" ); 254 255 // Scan through oopmaps. Stop when current offset is either equal or greater 256 // than the one we are looking for. 257 for( i = 0; i < len; i++) { 258 if( at(i)->offset() >= pc_offset ) 259 break; 260 } 261 262 assert( i < len, "oopmap not found" ); 263 264 OopMap* m = at(i); 265 assert( m->offset() == pc_offset, "oopmap not found" ); 266 return m; 267 } 268 269 class DoNothingClosure: public OopClosure { 270 public: 271 void do_oop(oop* p) {} 272 void do_oop(narrowOop* p) {} 273 }; 274 static DoNothingClosure do_nothing; 275 276 static void add_derived_oop(oop* base, oop* derived) { 277 #ifndef TIERED 278 COMPILER1_PRESENT(ShouldNotReachHere();) 279 #endif // TIERED 280 #ifdef COMPILER2 281 DerivedPointerTable::add(derived, base); 282 #endif // COMPILER2 283 } 284 285 286 #ifndef PRODUCT 287 static void trace_codeblob_maps(const frame *fr, const RegisterMap *reg_map) { 288 // Print oopmap and regmap 289 tty->print_cr("------ "); 290 CodeBlob* cb = fr->cb(); 291 ImmutableOopMapSet* maps = cb->oop_maps(); 292 const ImmutableOopMap* map = cb->oop_map_for_return_address(fr->pc()); 293 map->print(); 294 if( cb->is_nmethod() ) { 295 nmethod* nm = (nmethod*)cb; 296 // native wrappers have no scope data, it is implied 297 if (nm->is_native_method()) { 298 tty->print("bci: 0 (native)"); 299 } else { 300 ScopeDesc* scope = nm->scope_desc_at(fr->pc()); 301 tty->print("bci: %d ",scope->bci()); 302 } 303 } 304 tty->cr(); 305 fr->print_on(tty); 306 tty->print(" "); 307 cb->print_value_on(tty); tty->cr(); 308 reg_map->print(); 309 tty->print_cr("------ "); 310 311 } 312 #endif // PRODUCT 313 314 void OopMapSet::oops_do(const frame *fr, const RegisterMap* reg_map, OopClosure* f) { 315 // add derived oops to a table 316 all_do(fr, reg_map, f, add_derived_oop, &do_nothing); 317 } 318 319 320 void OopMapSet::all_do(const frame *fr, const RegisterMap *reg_map, 321 OopClosure* oop_fn, void derived_oop_fn(oop*, oop*), 322 OopClosure* value_fn) { 323 CodeBlob* cb = fr->cb(); 324 assert(cb != NULL, "no codeblob"); 325 326 NOT_PRODUCT(if (TraceCodeBlobStacks) trace_codeblob_maps(fr, reg_map);) 327 328 ImmutableOopMapSet* maps = cb->oop_maps(); 329 const ImmutableOopMap* map = cb->oop_map_for_return_address(fr->pc()); 330 assert(map != NULL, "no ptr map found"); 331 332 // handle derived pointers first (otherwise base pointer may be 333 // changed before derived pointer offset has been collected) 334 OopMapValue omv; 335 { 336 OopMapStream oms(map,OopMapValue::derived_oop_value); 337 if (!oms.is_done()) { 338 #ifndef TIERED 339 COMPILER1_PRESENT(ShouldNotReachHere();) 340 #endif // !TIERED 341 // Protect the operation on the derived pointers. This 342 // protects the addition of derived pointers to the shared 343 // derived pointer table in DerivedPointerTable::add(). 344 MutexLockerEx x(DerivedPointerTableGC_lock, Mutex::_no_safepoint_check_flag); 345 do { 346 omv = oms.current(); 347 oop* loc = fr->oopmapreg_to_location(omv.reg(),reg_map); 348 if ( loc != NULL ) { 349 oop *base_loc = fr->oopmapreg_to_location(omv.content_reg(), reg_map); 350 oop *derived_loc = loc; 351 oop val = *base_loc; 352 if (val == (oop)NULL || Universe::is_narrow_oop_base(val)) { 353 // Ignore NULL oops and decoded NULL narrow oops which 354 // equal to Universe::narrow_oop_base when a narrow oop 355 // implicit null check is used in compiled code. 356 // The narrow_oop_base could be NULL or be the address 357 // of the page below heap depending on compressed oops mode. 358 } else 359 derived_oop_fn(base_loc, derived_loc); 360 } 361 oms.next(); 362 } while (!oms.is_done()); 363 } 364 } 365 366 // We want coop, value and oop oop_types 367 int mask = OopMapValue::oop_value | OopMapValue::value_value | OopMapValue::narrowoop_value; 368 { 369 for (OopMapStream oms(map,mask); !oms.is_done(); oms.next()) { 370 omv = oms.current(); 371 oop* loc = fr->oopmapreg_to_location(omv.reg(),reg_map); 372 if ( loc != NULL ) { 373 if ( omv.type() == OopMapValue::oop_value ) { 374 oop val = *loc; 375 if (val == (oop)NULL || Universe::is_narrow_oop_base(val)) { 376 // Ignore NULL oops and decoded NULL narrow oops which 377 // equal to Universe::narrow_oop_base when a narrow oop 378 // implicit null check is used in compiled code. 379 // The narrow_oop_base could be NULL or be the address 380 // of the page below heap depending on compressed oops mode. 381 continue; 382 } 383 #ifdef ASSERT 384 if ((((uintptr_t)loc & (sizeof(*loc)-1)) != 0) || 385 !Universe::heap()->is_in_or_null(*loc)) { 386 tty->print_cr("# Found non oop pointer. Dumping state at failure"); 387 // try to dump out some helpful debugging information 388 trace_codeblob_maps(fr, reg_map); 389 omv.print(); 390 tty->print_cr("register r"); 391 omv.reg()->print(); 392 tty->print_cr("loc = %p *loc = %p\n", loc, (address)*loc); 393 // do the real assert. 394 assert(Universe::heap()->is_in_or_null(*loc), "found non oop pointer"); 395 } 396 #endif // ASSERT 397 oop_fn->do_oop(loc); 398 } else if ( omv.type() == OopMapValue::value_value ) { 399 assert((*loc) == (oop)NULL || !Universe::is_narrow_oop_base(*loc), 400 "found invalid value pointer"); 401 value_fn->do_oop(loc); 402 } else if ( omv.type() == OopMapValue::narrowoop_value ) { 403 narrowOop *nl = (narrowOop*)loc; 404 #ifndef VM_LITTLE_ENDIAN 405 if (!omv.reg()->is_stack()) { 406 // compressed oops in registers only take up 4 bytes of an 407 // 8 byte register but they are in the wrong part of the 408 // word so adjust loc to point at the right place. 409 nl = (narrowOop*)((address)nl + 4); 410 } 411 #endif 412 oop_fn->do_oop(nl); 413 } 414 } 415 } 416 } 417 } 418 419 420 // Update callee-saved register info for the following frame 421 void OopMapSet::update_register_map(const frame *fr, RegisterMap *reg_map) { 422 ResourceMark rm; 423 CodeBlob* cb = fr->cb(); 424 assert(cb != NULL, "no codeblob"); 425 426 // Any reg might be saved by a safepoint handler (see generate_handler_blob). 427 assert( reg_map->_update_for_id == NULL || fr->is_older(reg_map->_update_for_id), 428 "already updated this map; do not 'update' it twice!" ); 429 debug_only(reg_map->_update_for_id = fr->id()); 430 431 // Check if caller must update oop argument 432 assert((reg_map->include_argument_oops() || 433 !cb->caller_must_gc_arguments(reg_map->thread())), 434 "include_argument_oops should already be set"); 435 436 // Scan through oopmap and find location of all callee-saved registers 437 // (we do not do update in place, since info could be overwritten) 438 439 address pc = fr->pc(); 440 const ImmutableOopMap* map = cb->oop_map_for_return_address(pc); 441 assert(map != NULL, "no ptr map found"); 442 DEBUG_ONLY(int nof_callee = 0;) 443 444 for (OopMapStream oms(map, OopMapValue::callee_saved_value); !oms.is_done(); oms.next()) { 445 OopMapValue omv = oms.current(); 446 VMReg reg = omv.content_reg(); 447 oop* loc = fr->oopmapreg_to_location(omv.reg(), reg_map); 448 reg_map->set_location(reg, (address) loc); 449 DEBUG_ONLY(nof_callee++;) 450 } 451 452 // Check that runtime stubs save all callee-saved registers 453 #ifdef COMPILER2 454 assert(cb->is_compiled_by_c1() || !cb->is_runtime_stub() || 455 (nof_callee >= SAVED_ON_ENTRY_REG_COUNT || nof_callee >= C_SAVED_ON_ENTRY_REG_COUNT), 456 "must save all"); 457 #endif // COMPILER2 458 } 459 460 //============================================================================= 461 // Non-Product code 462 463 #ifndef PRODUCT 464 465 bool ImmutableOopMap::has_derived_pointer() const { 466 #ifndef TIERED 467 COMPILER1_PRESENT(return false); 468 #endif // !TIERED 469 #ifdef COMPILER2 470 OopMapStream oms((OopMap*)this,OopMapValue::derived_oop_value); 471 return oms.is_done(); 472 #else 473 return false; 474 #endif // COMPILER2 475 } 476 477 #endif //PRODUCT 478 479 // Printing code is present in product build for -XX:+PrintAssembly. 480 481 static 482 void print_register_type(OopMapValue::oop_types x, VMReg optional, 483 outputStream* st) { 484 switch( x ) { 485 case OopMapValue::oop_value: 486 st->print("Oop"); 487 break; 488 case OopMapValue::value_value: 489 st->print("Value"); 490 break; 491 case OopMapValue::narrowoop_value: 492 st->print("NarrowOop"); 493 break; 494 case OopMapValue::callee_saved_value: 495 st->print("Callers_"); 496 optional->print_on(st); 497 break; 498 case OopMapValue::derived_oop_value: 499 st->print("Derived_oop_"); 500 optional->print_on(st); 501 break; 502 default: 503 ShouldNotReachHere(); 504 } 505 } 506 507 void OopMapValue::print_on(outputStream* st) const { 508 reg()->print_on(st); 509 st->print("="); 510 print_register_type(type(),content_reg(),st); 511 st->print(" "); 512 } 513 514 void ImmutableOopMap::print_on(outputStream* st) const { 515 OopMapValue omv; 516 st->print("ImmutableOopMap{"); 517 for(OopMapStream oms(this); !oms.is_done(); oms.next()) { 518 omv = oms.current(); 519 omv.print_on(st); 520 } 521 st->print("}"); 522 } 523 524 void OopMap::print_on(outputStream* st) const { 525 OopMapValue omv; 526 st->print("OopMap{"); 527 for(OopMapStream oms((OopMap*)this); !oms.is_done(); oms.next()) { 528 omv = oms.current(); 529 omv.print_on(st); 530 } 531 st->print("off=%d}", (int) offset()); 532 } 533 534 void ImmutableOopMapSet::print_on(outputStream* st) const { 535 const ImmutableOopMap* last = NULL; 536 for (int i = 0; i < _count; ++i) { 537 const ImmutableOopMapPair* pair = pair_at(i); 538 const ImmutableOopMap* map = pair->get_from(this); 539 if (map != last) { 540 st->cr(); 541 map->print_on(st); 542 st->print("pc offsets: "); 543 } 544 last = map; 545 st->print("%d ", pair->pc_offset()); 546 } 547 } 548 549 void OopMapSet::print_on(outputStream* st) const { 550 int i, len = om_count(); 551 552 st->print_cr("OopMapSet contains %d OopMaps\n",len); 553 554 for( i = 0; i < len; i++) { 555 OopMap* m = at(i); 556 st->print_cr("#%d ",i); 557 m->print_on(st); 558 st->cr(); 559 } 560 } 561 562 bool OopMap::equals(const OopMap* other) const { 563 if (other->_omv_count != _omv_count) { 564 return false; 565 } 566 if (other->write_stream()->position() != write_stream()->position()) { 567 return false; 568 } 569 if (memcmp(other->write_stream()->buffer(), write_stream()->buffer(), write_stream()->position()) != 0) { 570 return false; 571 } 572 return true; 573 } 574 575 const ImmutableOopMap* ImmutableOopMapSet::find_map_at_offset(int pc_offset) const { 576 ImmutableOopMapPair* pairs = get_pairs(); 577 ImmutableOopMapPair* last = NULL; 578 579 for (int i = 0; i < _count; ++i) { 580 if (pairs[i].pc_offset() >= pc_offset) { 581 last = &pairs[i]; 582 break; 583 } 584 } 585 586 assert(last->pc_offset() == pc_offset, "oopmap not found"); 587 return last->get_from(this); 588 } 589 590 const ImmutableOopMap* ImmutableOopMapPair::get_from(const ImmutableOopMapSet* set) const { 591 return set->oopmap_at_offset(_oopmap_offset); 592 } 593 594 ImmutableOopMap::ImmutableOopMap(const OopMap* oopmap) : _count(oopmap->count()) { 595 address addr = data_addr(); 596 oopmap->copy_data_to(addr); 597 } 598 599 #ifdef ASSERT 600 int ImmutableOopMap::nr_of_bytes() const { 601 OopMapStream oms(this); 602 603 while (!oms.is_done()) { 604 oms.next(); 605 } 606 return sizeof(ImmutableOopMap) + oms.stream_position(); 607 } 608 #endif 609 610 class ImmutableOopMapBuilder { 611 private: 612 class Mapping; 613 614 private: 615 const OopMapSet* _set; 616 const OopMap* _empty; 617 const OopMap* _last; 618 int _empty_offset; 619 int _last_offset; 620 int _offset; 621 Mapping* _mapping; 622 ImmutableOopMapSet* _new_set; 623 624 /* Used for bookkeeping when building ImmutableOopMaps */ 625 class Mapping : public ResourceObj { 626 public: 627 enum kind_t { OOPMAP_UNKNOWN = 0, OOPMAP_NEW = 1, OOPMAP_EMPTY = 2, OOPMAP_DUPLICATE = 3 }; 628 629 kind_t _kind; 630 int _offset; 631 int _size; 632 const OopMap* _map; 633 const OopMap* _other; 634 635 Mapping() : _kind(OOPMAP_UNKNOWN), _offset(-1), _size(-1), _map(NULL) {} 636 637 void set(kind_t kind, int offset, int size, const OopMap* map = 0, const OopMap* other = 0) { 638 _kind = kind; 639 _offset = offset; 640 _size = size; 641 _map = map; 642 _other = other; 643 } 644 }; 645 646 public: 647 ImmutableOopMapBuilder(const OopMapSet* set) : _set(set), _new_set(NULL), _empty(NULL), _last(NULL), _empty_offset(-1), _last_offset(-1), _offset(0) { 648 _mapping = NEW_RESOURCE_ARRAY(Mapping, _set->size()); 649 } 650 651 int heap_size(); 652 ImmutableOopMapSet* build(); 653 private: 654 bool is_empty(const OopMap* map) const { 655 return map->count() == 0; 656 } 657 658 bool is_last_duplicate(const OopMap* map) { 659 if (_last != NULL && _last->count() > 0 && _last->equals(map)) { 660 return true; 661 } 662 return false; 663 } 664 665 #ifdef ASSERT 666 void verify(address buffer, int size, const ImmutableOopMapSet* set); 667 #endif 668 669 bool has_empty() const { 670 return _empty_offset != -1; 671 } 672 673 int size_for(const OopMap* map) const; 674 void fill_pair(ImmutableOopMapPair* pair, const OopMap* map, int offset, const ImmutableOopMapSet* set); 675 int fill_map(ImmutableOopMapPair* pair, const OopMap* map, int offset, const ImmutableOopMapSet* set); 676 void fill(ImmutableOopMapSet* set, int size); 677 }; 678 679 int ImmutableOopMapBuilder::size_for(const OopMap* map) const { 680 return align_size_up(sizeof(ImmutableOopMap) + map->data_size(), 8); 681 } 682 683 int ImmutableOopMapBuilder::heap_size() { 684 int base = sizeof(ImmutableOopMapSet); 685 base = align_size_up(base, 8); 686 687 // all of ours pc / offset pairs 688 int pairs = _set->size() * sizeof(ImmutableOopMapPair); 689 pairs = align_size_up(pairs, 8); 690 691 for (int i = 0; i < _set->size(); ++i) { 692 int size = 0; 693 OopMap* map = _set->at(i); 694 695 if (is_empty(map)) { 696 /* only keep a single empty map in the set */ 697 if (has_empty()) { 698 _mapping[i].set(Mapping::OOPMAP_EMPTY, _empty_offset, 0, map, _empty); 699 } else { 700 _empty_offset = _offset; 701 _empty = map; 702 size = size_for(map); 703 _mapping[i].set(Mapping::OOPMAP_NEW, _offset, size, map); 704 } 705 } else if (is_last_duplicate(map)) { 706 /* if this entry is identical to the previous one, just point it there */ 707 _mapping[i].set(Mapping::OOPMAP_DUPLICATE, _last_offset, 0, map, _last); 708 } else { 709 /* not empty, not an identical copy of the previous entry */ 710 size = size_for(map); 711 _mapping[i].set(Mapping::OOPMAP_NEW, _offset, size, map); 712 _last_offset = _offset; 713 _last = map; 714 } 715 716 assert(_mapping[i]._map == map, "check"); 717 _offset += size; 718 } 719 720 int total = base + pairs + _offset; 721 DEBUG_ONLY(total += 8); 722 return total; 723 } 724 725 void ImmutableOopMapBuilder::fill_pair(ImmutableOopMapPair* pair, const OopMap* map, int offset, const ImmutableOopMapSet* set) { 726 assert(offset < set->nr_of_bytes(), "check"); 727 new ((address) pair) ImmutableOopMapPair(map->offset(), offset); 728 } 729 730 int ImmutableOopMapBuilder::fill_map(ImmutableOopMapPair* pair, const OopMap* map, int offset, const ImmutableOopMapSet* set) { 731 fill_pair(pair, map, offset, set); 732 address addr = (address) pair->get_from(_new_set); // location of the ImmutableOopMap 733 734 new (addr) ImmutableOopMap(map); 735 return align_size_up(sizeof(ImmutableOopMap) + map->data_size(), 8); 736 } 737 738 void ImmutableOopMapBuilder::fill(ImmutableOopMapSet* set, int sz) { 739 ImmutableOopMapPair* pairs = set->get_pairs(); 740 741 for (int i = 0; i < set->count(); ++i) { 742 const OopMap* map = _mapping[i]._map; 743 ImmutableOopMapPair* pair = NULL; 744 int size = 0; 745 746 if (_mapping[i]._kind == Mapping::OOPMAP_NEW) { 747 size = fill_map(&pairs[i], map, _mapping[i]._offset, set); 748 } else if (_mapping[i]._kind == Mapping::OOPMAP_DUPLICATE || _mapping[i]._kind == Mapping::OOPMAP_EMPTY) { 749 fill_pair(&pairs[i], map, _mapping[i]._offset, set); 750 } 751 752 const ImmutableOopMap* nv = set->find_map_at_offset(map->offset()); 753 assert(memcmp(map->data(), nv->data_addr(), map->data_size()) == 0, "check identity"); 754 } 755 } 756 757 #ifdef ASSERT 758 void ImmutableOopMapBuilder::verify(address buffer, int size, const ImmutableOopMapSet* set) { 759 for (int i = 0; i < 8; ++i) { 760 assert(buffer[size - 8 + i] == (unsigned char) 0xff, "overwritten memory check"); 761 } 762 763 for (int i = 0; i < set->count(); ++i) { 764 const ImmutableOopMapPair* pair = set->pair_at(i); 765 assert(pair->oopmap_offset() < set->nr_of_bytes(), "check size"); 766 const ImmutableOopMap* map = pair->get_from(set); 767 int nr_of_bytes = map->nr_of_bytes(); 768 assert(pair->oopmap_offset() + nr_of_bytes <= set->nr_of_bytes(), "check size + size"); 769 } 770 } 771 #endif 772 773 ImmutableOopMapSet* ImmutableOopMapBuilder::build() { 774 int required = heap_size(); 775 776 // We need to allocate a chunk big enough to hold the ImmutableOopMapSet and all of its ImmutableOopMaps 777 address buffer = (address) NEW_C_HEAP_ARRAY(unsigned char, required, mtCode); 778 DEBUG_ONLY(memset(&buffer[required-8], 0xff, 8)); 779 780 _new_set = new (buffer) ImmutableOopMapSet(_set, required); 781 fill(_new_set, required); 782 783 DEBUG_ONLY(verify(buffer, required, _new_set)); 784 785 return _new_set; 786 } 787 788 ImmutableOopMapSet* ImmutableOopMapSet::build_from(const OopMapSet* oopmap_set) { 789 ResourceMark mark; 790 ImmutableOopMapBuilder builder(oopmap_set); 791 return builder.build(); 792 } 793 794 795 //------------------------------DerivedPointerTable--------------------------- 796 797 #ifdef COMPILER2 798 799 class DerivedPointerEntry : public CHeapObj<mtCompiler> { 800 private: 801 oop* _location; // Location of derived pointer (also pointing to the base) 802 intptr_t _offset; // Offset from base pointer 803 public: 804 DerivedPointerEntry(oop* location, intptr_t offset) { _location = location; _offset = offset; } 805 oop* location() { return _location; } 806 intptr_t offset() { return _offset; } 807 }; 808 809 810 GrowableArray<DerivedPointerEntry*>* DerivedPointerTable::_list = NULL; 811 bool DerivedPointerTable::_active = false; 812 813 814 void DerivedPointerTable::clear() { 815 // The first time, we create the list. Otherwise it should be 816 // empty. If not, then we have probably forgotton to call 817 // update_pointers after last GC/Scavenge. 818 assert (!_active, "should not be active"); 819 assert(_list == NULL || _list->length() == 0, "table not empty"); 820 if (_list == NULL) { 821 _list = new (ResourceObj::C_HEAP, mtCompiler) GrowableArray<DerivedPointerEntry*>(10, true); // Allocated on C heap 822 } 823 _active = true; 824 } 825 826 827 // Returns value of location as an int 828 intptr_t value_of_loc(oop *pointer) { return cast_from_oop<intptr_t>((*pointer)); } 829 830 831 void DerivedPointerTable::add(oop *derived_loc, oop *base_loc) { 832 assert(Universe::heap()->is_in_or_null(*base_loc), "not an oop"); 833 assert(derived_loc != base_loc, "Base and derived in same location"); 834 if (_active) { 835 assert(*derived_loc != (oop)base_loc, "location already added"); 836 assert(_list != NULL, "list must exist"); 837 intptr_t offset = value_of_loc(derived_loc) - value_of_loc(base_loc); 838 // This assert is invalid because derived pointers can be 839 // arbitrarily far away from their base. 840 // assert(offset >= -1000000, "wrong derived pointer info"); 841 842 if (TraceDerivedPointers) { 843 tty->print_cr( 844 "Add derived pointer@" INTPTR_FORMAT 845 " - Derived: " INTPTR_FORMAT 846 " Base: " INTPTR_FORMAT " (@" INTPTR_FORMAT ") (Offset: " INTX_FORMAT ")", 847 p2i(derived_loc), p2i((address)*derived_loc), p2i((address)*base_loc), p2i(base_loc), offset 848 ); 849 } 850 // Set derived oop location to point to base. 851 *derived_loc = (oop)base_loc; 852 assert_lock_strong(DerivedPointerTableGC_lock); 853 DerivedPointerEntry *entry = new DerivedPointerEntry(derived_loc, offset); 854 _list->append(entry); 855 } 856 } 857 858 859 void DerivedPointerTable::update_pointers() { 860 assert(_list != NULL, "list must exist"); 861 for(int i = 0; i < _list->length(); i++) { 862 DerivedPointerEntry* entry = _list->at(i); 863 oop* derived_loc = entry->location(); 864 intptr_t offset = entry->offset(); 865 // The derived oop was setup to point to location of base 866 oop base = **(oop**)derived_loc; 867 assert(Universe::heap()->is_in_or_null(base), "must be an oop"); 868 869 *derived_loc = (oop)(((address)base) + offset); 870 assert(value_of_loc(derived_loc) - value_of_loc(&base) == offset, "sanity check"); 871 872 if (TraceDerivedPointers) { 873 tty->print_cr("Updating derived pointer@" INTPTR_FORMAT 874 " - Derived: " INTPTR_FORMAT " Base: " INTPTR_FORMAT " (Offset: " INTX_FORMAT ")", 875 p2i(derived_loc), p2i((address)*derived_loc), p2i((address)base), offset); 876 } 877 878 // Delete entry 879 delete entry; 880 _list->at_put(i, NULL); 881 } 882 // Clear list, so it is ready for next traversal (this is an invariant) 883 if (TraceDerivedPointers && !_list->is_empty()) { 884 tty->print_cr("--------------------------"); 885 } 886 _list->clear(); 887 _active = false; 888 } 889 890 #endif // COMPILER2