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