1 /*
   2  * Copyright (c) 1998, 2019, 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 "memory/universe.hpp"
  36 #include "oops/compressedOops.hpp"
  37 #include "runtime/frame.inline.hpp"
  38 #include "runtime/handles.inline.hpp"
  39 #include "runtime/signature.hpp"
  40 #include "utilities/align.hpp"
  41 #ifdef COMPILER1
  42 #include "c1/c1_Defs.hpp"
  43 #endif
  44 #ifdef COMPILER2
  45 #include "opto/optoreg.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 static void add_derived_oop(oop* base, oop* derived) {
 271 #if !defined(TIERED) && !INCLUDE_JVMCI
 272   COMPILER1_PRESENT(ShouldNotReachHere();)
 273 #endif // !defined(TIERED) && !INCLUDE_JVMCI
 274 #if COMPILER2_OR_JVMCI
 275   DerivedPointerTable::add(derived, base);
 276 #endif // COMPILER2_OR_JVMCI
 277 }
 278 
 279 
 280 #ifndef PRODUCT
 281 static void trace_codeblob_maps(const frame *fr, const RegisterMap *reg_map) {
 282   // Print oopmap and regmap
 283   tty->print_cr("------ ");
 284   CodeBlob* cb = fr->cb();
 285   const ImmutableOopMapSet* maps = cb->oop_maps();
 286   const ImmutableOopMap* map = cb->oop_map_for_return_address(fr->pc());
 287   map->print();
 288   if( cb->is_nmethod() ) {
 289     nmethod* nm = (nmethod*)cb;
 290     // native wrappers have no scope data, it is implied
 291     if (nm->is_native_method()) {
 292       tty->print("bci: 0 (native)");
 293     } else {
 294       ScopeDesc* scope  = nm->scope_desc_at(fr->pc());
 295       tty->print("bci: %d ",scope->bci());
 296     }
 297   }
 298   tty->cr();
 299   fr->print_on(tty);
 300   tty->print("     ");
 301   cb->print_value_on(tty);  tty->cr();
 302   reg_map->print();
 303   tty->print_cr("------ ");
 304 
 305 }
 306 #endif // PRODUCT
 307 
 308 void OopMapSet::oops_do(const frame *fr, const RegisterMap* reg_map, OopClosure* f) {
 309   // add derived oops to a table
 310   all_do(fr, reg_map, f, add_derived_oop, &do_nothing_cl);
 311 }
 312 
 313 
 314 void OopMapSet::all_do(const frame *fr, const RegisterMap *reg_map,
 315                        OopClosure* oop_fn, void derived_oop_fn(oop*, oop*),
 316                        OopClosure* value_fn) {
 317   CodeBlob* cb = fr->cb();
 318   assert(cb != NULL, "no codeblob");
 319 
 320   NOT_PRODUCT(if (TraceCodeBlobStacks) trace_codeblob_maps(fr, reg_map);)
 321 
 322   const ImmutableOopMapSet* maps = cb->oop_maps();
 323   const ImmutableOopMap* map = cb->oop_map_for_return_address(fr->pc());
 324   assert(map != NULL, "no ptr map found");
 325 
 326   // handle derived pointers first (otherwise base pointer may be
 327   // changed before derived pointer offset has been collected)
 328   OopMapValue omv;
 329   {
 330     OopMapStream oms(map,OopMapValue::derived_oop_value);
 331     if (!oms.is_done()) {
 332 #ifndef TIERED
 333       COMPILER1_PRESENT(ShouldNotReachHere();)
 334 #if INCLUDE_JVMCI
 335       if (UseJVMCICompiler) {
 336         ShouldNotReachHere();
 337       }
 338 #endif
 339 #endif // !TIERED
 340       // Protect the operation on the derived pointers.  This
 341       // protects the addition of derived pointers to the shared
 342       // derived pointer table in DerivedPointerTable::add().
 343       MutexLocker x(DerivedPointerTableGC_lock, Mutex::_no_safepoint_check_flag);
 344       do {
 345         omv = oms.current();
 346         oop* loc = fr->oopmapreg_to_location(omv.reg(),reg_map);
 347         guarantee(loc != NULL, "missing saved register");
 348         oop *derived_loc = loc;
 349         oop *base_loc    = fr->oopmapreg_to_location(omv.content_reg(), reg_map);
 350         // Ignore NULL oops and decoded NULL narrow oops which
 351         // equal to CompressedOops::base() when a narrow oop
 352         // implicit null check is used in compiled code.
 353         // The narrow_oop_base could be NULL or be the address
 354         // of the page below heap depending on compressed oops mode.
 355         if (base_loc != NULL && *base_loc != NULL && !CompressedOops::is_base(*base_loc)) {
 356           derived_oop_fn(base_loc, derived_loc);
 357         }
 358         oms.next();
 359       }  while (!oms.is_done());
 360     }
 361   }
 362 
 363   // We want coop and oop oop_types
 364   int mask = OopMapValue::oop_value | OopMapValue::narrowoop_value;
 365   {
 366     for (OopMapStream oms(map,mask); !oms.is_done(); oms.next()) {
 367       omv = oms.current();
 368       oop* loc = fr->oopmapreg_to_location(omv.reg(),reg_map);
 369       // It should be an error if no location can be found for a
 370       // register mentioned as contained an oop of some kind.  Maybe
 371       // this was allowed previously because value_value items might
 372       // be missing?
 373       guarantee(loc != NULL, "missing saved register");
 374       if ( omv.type() == OopMapValue::oop_value ) {
 375         oop val = *loc;
 376         if (val == NULL || CompressedOops::is_base(val)) {
 377           // Ignore NULL oops and decoded NULL narrow oops which
 378           // equal to CompressedOops::base() when a narrow oop
 379           // implicit null check is used in compiled code.
 380           // The narrow_oop_base could be NULL or be the address
 381           // of the page below heap depending on compressed oops mode.
 382           continue;
 383         }
 384 #ifdef ASSERT
 385         // We can not verify the oop here if we are using ZGC, the oop
 386         // will be bad in case we had a safepoint between a load and a
 387         // load barrier.
 388         if (!UseZGC &&
 389             ((((uintptr_t)loc & (sizeof(*loc)-1)) != 0) ||
 390              !Universe::heap()->is_in_or_null(*loc))) {
 391           tty->print_cr("# Found non oop pointer.  Dumping state at failure");
 392           // try to dump out some helpful debugging information
 393           trace_codeblob_maps(fr, reg_map);
 394           omv.print();
 395           tty->print_cr("register r");
 396           omv.reg()->print();
 397           tty->print_cr("loc = %p *loc = %p\n", loc, (address)*loc);
 398           // do the real assert.
 399           assert(Universe::heap()->is_in_or_null(*loc), "found non oop pointer");
 400         }
 401 #endif // ASSERT
 402         oop_fn->do_oop(loc);
 403       } else if ( omv.type() == OopMapValue::narrowoop_value ) {
 404         narrowOop *nl = (narrowOop*)loc;
 405 #ifndef VM_LITTLE_ENDIAN
 406         VMReg vmReg = omv.reg();
 407         // Don't do this on SPARC float registers as they can be individually addressed
 408         if (!vmReg->is_stack() SPARC_ONLY(&& !vmReg->is_FloatRegister())) {
 409           // compressed oops in registers only take up 4 bytes of an
 410           // 8 byte register but they are in the wrong part of the
 411           // word so adjust loc to point at the right place.
 412           nl = (narrowOop*)((address)nl + 4);
 413         }
 414 #endif
 415         oop_fn->do_oop(nl);
 416       }
 417     }
 418   }
 419 }
 420 
 421 
 422 // Update callee-saved register info for the following frame
 423 void OopMapSet::update_register_map(const frame *fr, RegisterMap *reg_map) {
 424   ResourceMark rm;
 425   CodeBlob* cb = fr->cb();
 426   assert(cb != NULL, "no codeblob");
 427 
 428   // Any reg might be saved by a safepoint handler (see generate_handler_blob).
 429   assert( reg_map->_update_for_id == NULL || fr->is_older(reg_map->_update_for_id),
 430          "already updated this map; do not 'update' it twice!" );
 431   debug_only(reg_map->_update_for_id = fr->id());
 432 
 433   // Check if caller must update oop argument
 434   assert((reg_map->include_argument_oops() ||
 435           !cb->caller_must_gc_arguments(reg_map->thread())),
 436          "include_argument_oops should already be set");
 437 
 438   // Scan through oopmap and find location of all callee-saved registers
 439   // (we do not do update in place, since info could be overwritten)
 440 
 441   address pc = fr->pc();
 442   const ImmutableOopMap* map  = cb->oop_map_for_return_address(pc);
 443   assert(map != NULL, "no ptr map found");
 444   DEBUG_ONLY(int nof_callee = 0;)
 445 
 446   for (OopMapStream oms(map, OopMapValue::callee_saved_value); !oms.is_done(); oms.next()) {
 447     OopMapValue omv = oms.current();
 448     VMReg reg = omv.content_reg();
 449     oop* loc = fr->oopmapreg_to_location(omv.reg(), reg_map);
 450     reg_map->set_location(reg, (address) loc);
 451     DEBUG_ONLY(nof_callee++;)
 452   }
 453 
 454   // Check that runtime stubs save all callee-saved registers
 455 #ifdef COMPILER2
 456   assert(cb->is_compiled_by_c1() || cb->is_compiled_by_jvmci() || !cb->is_runtime_stub() ||
 457          (nof_callee >= SAVED_ON_ENTRY_REG_COUNT || nof_callee >= C_SAVED_ON_ENTRY_REG_COUNT),
 458          "must save all");
 459 #endif // COMPILER2
 460 }
 461 
 462 //=============================================================================
 463 // Non-Product code
 464 
 465 #ifndef PRODUCT
 466 
 467 bool ImmutableOopMap::has_derived_pointer() const {
 468 #if !defined(TIERED) && !INCLUDE_JVMCI
 469   COMPILER1_PRESENT(return false);
 470 #endif // !TIERED
 471 #if COMPILER2_OR_JVMCI
 472   OopMapStream oms(this,OopMapValue::derived_oop_value);
 473   return oms.is_done();
 474 #else
 475   return false;
 476 #endif // COMPILER2_OR_JVMCI
 477 }
 478 
 479 #endif //PRODUCT
 480 
 481 // Printing code is present in product build for -XX:+PrintAssembly.
 482 
 483 static
 484 void print_register_type(OopMapValue::oop_types x, VMReg optional,
 485                          outputStream* st) {
 486   switch( x ) {
 487   case OopMapValue::oop_value:
 488     st->print("Oop");
 489     break;
 490   case OopMapValue::narrowoop_value:
 491     st->print("NarrowOop");
 492     break;
 493   case OopMapValue::callee_saved_value:
 494     st->print("Callers_");
 495     optional->print_on(st);
 496     break;
 497   case OopMapValue::derived_oop_value:
 498     st->print("Derived_oop_");
 499     optional->print_on(st);
 500     break;
 501   default:
 502     ShouldNotReachHere();
 503   }
 504 }
 505 
 506 void OopMapValue::print_on(outputStream* st) const {
 507   reg()->print_on(st);
 508   st->print("=");
 509   print_register_type(type(),content_reg(),st);
 510   st->print(" ");
 511 }
 512 
 513 void OopMapValue::print() const { print_on(tty); }
 514 
 515 void ImmutableOopMap::print_on(outputStream* st) const {
 516   OopMapValue omv;
 517   st->print("ImmutableOopMap{");
 518   for(OopMapStream oms(this); !oms.is_done(); oms.next()) {
 519     omv = oms.current();
 520     omv.print_on(st);
 521   }
 522   st->print("}");
 523 }
 524 
 525 void ImmutableOopMap::print() const { print_on(tty); }
 526 
 527 void OopMap::print_on(outputStream* st) const {
 528   OopMapValue omv;
 529   st->print("OopMap{");
 530   for(OopMapStream oms((OopMap*)this); !oms.is_done(); oms.next()) {
 531     omv = oms.current();
 532     omv.print_on(st);
 533   }
 534   st->print("off=%d}", (int) offset());
 535 }
 536 
 537 void OopMap::print() const { print_on(tty); }
 538 
 539 void ImmutableOopMapSet::print_on(outputStream* st) const {
 540   const ImmutableOopMap* last = NULL;
 541   for (int i = 0; i < _count; ++i) {
 542     const ImmutableOopMapPair* pair = pair_at(i);
 543     const ImmutableOopMap* map = pair->get_from(this);
 544     if (map != last) {
 545       st->cr();
 546       map->print_on(st);
 547       st->print("pc offsets: ");
 548     }
 549     last = map;
 550     st->print("%d ", pair->pc_offset());
 551   }
 552 }
 553 
 554 void ImmutableOopMapSet::print() const { print_on(tty); }
 555 
 556 void OopMapSet::print_on(outputStream* st) const {
 557   int i, len = om_count();
 558 
 559   st->print_cr("OopMapSet contains %d OopMaps\n",len);
 560 
 561   for( i = 0; i < len; i++) {
 562     OopMap* m = at(i);
 563     st->print_cr("#%d ",i);
 564     m->print_on(st);
 565     st->cr();
 566   }
 567 }
 568 
 569 void OopMapSet::print() const { print_on(tty); }
 570 
 571 bool OopMap::equals(const OopMap* other) const {
 572   if (other->_omv_count != _omv_count) {
 573     return false;
 574   }
 575   if (other->write_stream()->position() != write_stream()->position()) {
 576     return false;
 577   }
 578   if (memcmp(other->write_stream()->buffer(), write_stream()->buffer(), write_stream()->position()) != 0) {
 579     return false;
 580   }
 581   return true;
 582 }
 583 
 584 const ImmutableOopMap* ImmutableOopMapSet::find_map_at_offset(int pc_offset) const {
 585   ImmutableOopMapPair* pairs = get_pairs();
 586 
 587   int i;
 588   for (i = 0; i < _count; ++i) {
 589     if (pairs[i].pc_offset() >= pc_offset) {
 590       break;
 591     }
 592   }
 593   ImmutableOopMapPair* last = &pairs[i];
 594 
 595   assert(last->pc_offset() == pc_offset, "oopmap not found");
 596   return last->get_from(this);
 597 }
 598 
 599 const ImmutableOopMap* ImmutableOopMapPair::get_from(const ImmutableOopMapSet* set) const {
 600   return set->oopmap_at_offset(_oopmap_offset);
 601 }
 602 
 603 ImmutableOopMap::ImmutableOopMap(const OopMap* oopmap) : _count(oopmap->count()) {
 604   address addr = data_addr();
 605   oopmap->copy_data_to(addr);
 606 }
 607 
 608 #ifdef ASSERT
 609 int ImmutableOopMap::nr_of_bytes() const {
 610   OopMapStream oms(this);
 611 
 612   while (!oms.is_done()) {
 613     oms.next();
 614   }
 615   return sizeof(ImmutableOopMap) + oms.stream_position();
 616 }
 617 #endif
 618 
 619 ImmutableOopMapBuilder::ImmutableOopMapBuilder(const OopMapSet* set) : _set(set), _empty(NULL), _last(NULL), _empty_offset(-1), _last_offset(-1), _offset(0), _required(-1), _new_set(NULL) {
 620   _mapping = NEW_RESOURCE_ARRAY(Mapping, _set->size());
 621 }
 622 
 623 int ImmutableOopMapBuilder::size_for(const OopMap* map) const {
 624   return align_up((int)sizeof(ImmutableOopMap) + map->data_size(), 8);
 625 }
 626 
 627 int ImmutableOopMapBuilder::heap_size() {
 628   int base = sizeof(ImmutableOopMapSet);
 629   base = align_up(base, 8);
 630 
 631   // all of ours pc / offset pairs
 632   int pairs = _set->size() * sizeof(ImmutableOopMapPair);
 633   pairs = align_up(pairs, 8);
 634 
 635   for (int i = 0; i < _set->size(); ++i) {
 636     int size = 0;
 637     OopMap* map = _set->at(i);
 638 
 639     if (is_empty(map)) {
 640       /* only keep a single empty map in the set */
 641       if (has_empty()) {
 642         _mapping[i].set(Mapping::OOPMAP_EMPTY, _empty_offset, 0, map, _empty);
 643       } else {
 644         _empty_offset = _offset;
 645         _empty = map;
 646         size = size_for(map);
 647         _mapping[i].set(Mapping::OOPMAP_NEW, _offset, size, map);
 648       }
 649     } else if (is_last_duplicate(map)) {
 650       /* if this entry is identical to the previous one, just point it there */
 651       _mapping[i].set(Mapping::OOPMAP_DUPLICATE, _last_offset, 0, map, _last);
 652     } else {
 653       /* not empty, not an identical copy of the previous entry */
 654       size = size_for(map);
 655       _mapping[i].set(Mapping::OOPMAP_NEW, _offset, size, map);
 656       _last_offset = _offset;
 657       _last = map;
 658     }
 659 
 660     assert(_mapping[i]._map == map, "check");
 661     _offset += size;
 662   }
 663 
 664   int total = base + pairs + _offset;
 665   DEBUG_ONLY(total += 8);
 666   _required = total;
 667   return total;
 668 }
 669 
 670 void ImmutableOopMapBuilder::fill_pair(ImmutableOopMapPair* pair, const OopMap* map, int offset, const ImmutableOopMapSet* set) {
 671   assert(offset < set->nr_of_bytes(), "check");
 672   new ((address) pair) ImmutableOopMapPair(map->offset(), offset);
 673 }
 674 
 675 int ImmutableOopMapBuilder::fill_map(ImmutableOopMapPair* pair, const OopMap* map, int offset, const ImmutableOopMapSet* set) {
 676   fill_pair(pair, map, offset, set);
 677   address addr = (address) pair->get_from(_new_set); // location of the ImmutableOopMap
 678 
 679   new (addr) ImmutableOopMap(map);
 680   return size_for(map);
 681 }
 682 
 683 void ImmutableOopMapBuilder::fill(ImmutableOopMapSet* set, int sz) {
 684   ImmutableOopMapPair* pairs = set->get_pairs();
 685 
 686   for (int i = 0; i < set->count(); ++i) {
 687     const OopMap* map = _mapping[i]._map;
 688     ImmutableOopMapPair* pair = NULL;
 689     int size = 0;
 690 
 691     if (_mapping[i]._kind == Mapping::OOPMAP_NEW) {
 692       size = fill_map(&pairs[i], map, _mapping[i]._offset, set);
 693     } else if (_mapping[i]._kind == Mapping::OOPMAP_DUPLICATE || _mapping[i]._kind == Mapping::OOPMAP_EMPTY) {
 694       fill_pair(&pairs[i], map, _mapping[i]._offset, set);
 695     }
 696 
 697     const ImmutableOopMap* nv = set->find_map_at_offset(map->offset());
 698     assert(memcmp(map->data(), nv->data_addr(), map->data_size()) == 0, "check identity");
 699   }
 700 }
 701 
 702 #ifdef ASSERT
 703 void ImmutableOopMapBuilder::verify(address buffer, int size, const ImmutableOopMapSet* set) {
 704   for (int i = 0; i < 8; ++i) {
 705     assert(buffer[size - 8 + i] == (unsigned char) 0xff, "overwritten memory check");
 706   }
 707 
 708   for (int i = 0; i < set->count(); ++i) {
 709     const ImmutableOopMapPair* pair = set->pair_at(i);
 710     assert(pair->oopmap_offset() < set->nr_of_bytes(), "check size");
 711     const ImmutableOopMap* map = pair->get_from(set);
 712     int nr_of_bytes = map->nr_of_bytes();
 713     assert(pair->oopmap_offset() + nr_of_bytes <= set->nr_of_bytes(), "check size + size");
 714   }
 715 }
 716 #endif
 717 
 718 ImmutableOopMapSet* ImmutableOopMapBuilder::generate_into(address buffer) {
 719   DEBUG_ONLY(memset(&buffer[_required-8], 0xff, 8));
 720 
 721   _new_set = new (buffer) ImmutableOopMapSet(_set, _required);
 722   fill(_new_set, _required);
 723 
 724   DEBUG_ONLY(verify(buffer, _required, _new_set));
 725 
 726   return _new_set;
 727 }
 728 
 729 ImmutableOopMapSet* ImmutableOopMapBuilder::build() {
 730   _required = heap_size();
 731 
 732   // We need to allocate a chunk big enough to hold the ImmutableOopMapSet and all of its ImmutableOopMaps
 733   address buffer = (address) NEW_C_HEAP_ARRAY(unsigned char, _required, mtCode);
 734   return generate_into(buffer);
 735 }
 736 
 737 ImmutableOopMapSet* ImmutableOopMapSet::build_from(const OopMapSet* oopmap_set) {
 738   ResourceMark mark;
 739   ImmutableOopMapBuilder builder(oopmap_set);
 740   return builder.build();
 741 }
 742 
 743 
 744 //------------------------------DerivedPointerTable---------------------------
 745 
 746 #if COMPILER2_OR_JVMCI
 747 
 748 class DerivedPointerEntry : public CHeapObj<mtCompiler> {
 749  private:
 750   oop*     _location; // Location of derived pointer (also pointing to the base)
 751   intptr_t _offset;   // Offset from base pointer
 752  public:
 753   DerivedPointerEntry(oop* location, intptr_t offset) { _location = location; _offset = offset; }
 754   oop* location()    { return _location; }
 755   intptr_t  offset() { return _offset; }
 756 };
 757 
 758 
 759 GrowableArray<DerivedPointerEntry*>* DerivedPointerTable::_list = NULL;
 760 bool DerivedPointerTable::_active = false;
 761 
 762 
 763 void DerivedPointerTable::clear() {
 764   // The first time, we create the list.  Otherwise it should be
 765   // empty.  If not, then we have probably forgotton to call
 766   // update_pointers after last GC/Scavenge.
 767   assert (!_active, "should not be active");
 768   assert(_list == NULL || _list->length() == 0, "table not empty");
 769   if (_list == NULL) {
 770     _list = new (ResourceObj::C_HEAP, mtCompiler) GrowableArray<DerivedPointerEntry*>(10, true); // Allocated on C heap
 771   }
 772   _active = true;
 773 }
 774 
 775 
 776 // Returns value of location as an int
 777 intptr_t value_of_loc(oop *pointer) { return cast_from_oop<intptr_t>((*pointer)); }
 778 
 779 
 780 void DerivedPointerTable::add(oop *derived_loc, oop *base_loc) {
 781   assert(Universe::heap()->is_in_or_null(*base_loc), "not an oop");
 782   assert(derived_loc != base_loc, "Base and derived in same location");
 783   if (_active) {
 784     assert(*derived_loc != (void*)base_loc, "location already added");
 785     assert(_list != NULL, "list must exist");
 786     intptr_t offset = value_of_loc(derived_loc) - value_of_loc(base_loc);
 787     // This assert is invalid because derived pointers can be
 788     // arbitrarily far away from their base.
 789     // assert(offset >= -1000000, "wrong derived pointer info");
 790 
 791     if (TraceDerivedPointers) {
 792       tty->print_cr(
 793         "Add derived pointer@" INTPTR_FORMAT
 794         " - Derived: " INTPTR_FORMAT
 795         " Base: " INTPTR_FORMAT " (@" INTPTR_FORMAT ") (Offset: " INTX_FORMAT ")",
 796         p2i(derived_loc), p2i((address)*derived_loc), p2i((address)*base_loc), p2i(base_loc), offset
 797       );
 798     }
 799     // Set derived oop location to point to base.
 800     *derived_loc = (oop)base_loc;
 801     assert_lock_strong(DerivedPointerTableGC_lock);
 802     DerivedPointerEntry *entry = new DerivedPointerEntry(derived_loc, offset);
 803     _list->append(entry);
 804   }
 805 }
 806 
 807 
 808 void DerivedPointerTable::update_pointers() {
 809   assert(_list != NULL, "list must exist");
 810   for(int i = 0; i < _list->length(); i++) {
 811     DerivedPointerEntry* entry = _list->at(i);
 812     oop* derived_loc = entry->location();
 813     intptr_t offset  = entry->offset();
 814     // The derived oop was setup to point to location of base
 815     oop  base        = **(oop**)derived_loc;
 816     assert(Universe::heap()->is_in_or_null(base), "must be an oop");
 817 
 818     *derived_loc = (oop)(((address)base) + offset);
 819     assert(value_of_loc(derived_loc) - value_of_loc(&base) == offset, "sanity check");
 820 
 821     if (TraceDerivedPointers) {
 822       tty->print_cr("Updating derived pointer@" INTPTR_FORMAT
 823                     " - Derived: " INTPTR_FORMAT "  Base: " INTPTR_FORMAT " (Offset: " INTX_FORMAT ")",
 824           p2i(derived_loc), p2i((address)*derived_loc), p2i((address)base), offset);
 825     }
 826 
 827     // Delete entry
 828     delete entry;
 829     _list->at_put(i, NULL);
 830   }
 831   // Clear list, so it is ready for next traversal (this is an invariant)
 832   if (TraceDerivedPointers && !_list->is_empty()) {
 833     tty->print_cr("--------------------------");
 834   }
 835   _list->clear();
 836   _active = false;
 837 }
 838 
 839 #endif // COMPILER2_OR_JVMCI