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