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