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