1 /*
   2  * Copyright (c) 1998, 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "code/codeBlob.hpp"
  27 #include "code/codeCache.hpp"
  28 #include "code/nmethod.hpp"
  29 #include "code/scopeDesc.hpp"
  30 #include "compiler/oopMap.hpp"
  31 #include "gc/shared/collectedHeap.hpp"
  32 #include "memory/allocation.inline.hpp"
  33 #include "memory/resourceArea.hpp"
  34 #include "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 void OopMapStream::find_next() {
  62   while(_position++ < _size) {
  63     _omv.read_from(_stream);
  64     if(((int)_omv.type() & _mask) > 0) {
  65       _valid_omv = true;
  66       return;
  67     }
  68   }
  69   _valid_omv = false;
  70 }
  71 
  72 
  73 // OopMap
  74 
  75 // frame_size units are stack-slots (4 bytes) NOT intptr_t; we can name odd
  76 // slots to hold 4-byte values like ints and floats in the LP64 build.
  77 OopMap::OopMap(int frame_size, int arg_count) {
  78   // OopMaps are usually quite so small, so pick a small initial size
  79   set_write_stream(new CompressedWriteStream(32));
  80   set_omv_count(0);
  81 
  82 #ifdef ASSERT
  83   _locs_length = VMRegImpl::stack2reg(0)->value() + frame_size + arg_count;
  84   _locs_used   = NEW_RESOURCE_ARRAY(OopMapValue::oop_types, _locs_length);
  85   for(int i = 0; i < _locs_length; i++) _locs_used[i] = OopMapValue::unused_value;
  86 #endif
  87 }
  88 
  89 
  90 OopMap::OopMap(OopMap::DeepCopyToken, OopMap* source) {
  91   // This constructor does a deep copy
  92   // of the source OopMap.
  93   set_write_stream(new CompressedWriteStream(source->omv_count() * 2));
  94   set_omv_count(0);
  95   set_offset(source->offset());
  96 
  97 #ifdef ASSERT
  98   _locs_length = source->_locs_length;
  99   _locs_used = NEW_RESOURCE_ARRAY(OopMapValue::oop_types, _locs_length);
 100   for(int i = 0; i < _locs_length; i++) _locs_used[i] = OopMapValue::unused_value;
 101 #endif
 102 
 103   // We need to copy the entries too.
 104   for (OopMapStream oms(source); !oms.is_done(); oms.next()) {
 105     OopMapValue omv = oms.current();
 106     omv.write_on(write_stream());
 107     increment_count();
 108   }
 109 }
 110 
 111 
 112 OopMap* OopMap::deep_copy() {
 113   return new OopMap(_deep_copy_token, this);
 114 }
 115 
 116 void OopMap::copy_data_to(address addr) const {
 117   memcpy(addr, write_stream()->buffer(), write_stream()->position());
 118 }
 119 
 120 int OopMap::heap_size() const {
 121   int size = sizeof(OopMap);
 122   int align = sizeof(void *) - 1;
 123   size += write_stream()->position();
 124   // Align to a reasonable ending point
 125   size = ((size+align) & ~align);
 126   return size;
 127 }
 128 
 129 // frame_size units are stack-slots (4 bytes) NOT intptr_t; we can name odd
 130 // slots to hold 4-byte values like ints and floats in the LP64 build.
 131 void OopMap::set_xxx(VMReg reg, OopMapValue::oop_types x, VMReg optional) {
 132 
 133   assert(reg->value() < _locs_length, "too big reg value for stack size");
 134   assert( _locs_used[reg->value()] == OopMapValue::unused_value, "cannot insert twice" );
 135   debug_only( _locs_used[reg->value()] = x; )
 136 
 137   OopMapValue o(reg, x);
 138 
 139   if(x == OopMapValue::callee_saved_value) {
 140     // This can never be a stack location, so we don't need to transform it.
 141     assert(optional->is_reg(), "Trying to callee save a stack location");
 142     o.set_content_reg(optional);
 143   } else if(x == OopMapValue::derived_oop_value) {
 144     o.set_content_reg(optional);
 145   }
 146 
 147   o.write_on(write_stream());
 148   increment_count();
 149 }
 150 
 151 
 152 void OopMap::set_oop(VMReg reg) {
 153   set_xxx(reg, OopMapValue::oop_value, VMRegImpl::Bad());
 154 }
 155 
 156 
 157 void OopMap::set_value(VMReg reg) {
 158   // At this time, we don't need value entries in our OopMap.
 159 }
 160 
 161 
 162 void OopMap::set_narrowoop(VMReg reg) {
 163   set_xxx(reg, OopMapValue::narrowoop_value, VMRegImpl::Bad());
 164 }
 165 
 166 
 167 void OopMap::set_callee_saved(VMReg reg, VMReg caller_machine_register ) {
 168   set_xxx(reg, OopMapValue::callee_saved_value, caller_machine_register);
 169 }
 170 
 171 
 172 void OopMap::set_derived_oop(VMReg reg, VMReg derived_from_local_register ) {
 173   if( reg == derived_from_local_register ) {
 174     // Actually an oop, derived shares storage with base,
 175     set_oop(reg);
 176   } else {
 177     set_xxx(reg, OopMapValue::derived_oop_value, derived_from_local_register);
 178   }
 179 }
 180 
 181 // OopMapSet
 182 
 183 OopMapSet::OopMapSet() {
 184   set_om_size(MinOopMapAllocation);
 185   set_om_count(0);
 186   OopMap** temp = NEW_RESOURCE_ARRAY(OopMap*, om_size());
 187   set_om_data(temp);
 188 }
 189 
 190 
 191 void OopMapSet::grow_om_data() {
 192   int new_size = om_size() * 2;
 193   OopMap** new_data = NEW_RESOURCE_ARRAY(OopMap*, new_size);
 194   memcpy(new_data,om_data(),om_size() * sizeof(OopMap*));
 195   set_om_size(new_size);
 196   set_om_data(new_data);
 197 }
 198 
 199 void OopMapSet::add_gc_map(int pc_offset, OopMap *map ) {
 200   assert(om_size() != -1,"Cannot grow a fixed OopMapSet");
 201 
 202   if(om_count() >= om_size()) {
 203     grow_om_data();
 204   }
 205   map->set_offset(pc_offset);
 206 
 207 #ifdef ASSERT
 208   if(om_count() > 0) {
 209     OopMap* last = at(om_count()-1);
 210     if (last->offset() == map->offset() ) {
 211       fatal("OopMap inserted twice");
 212     }
 213     if(last->offset() > map->offset()) {
 214       tty->print_cr( "WARNING, maps not sorted: pc[%d]=%d, pc[%d]=%d",
 215                       om_count(),last->offset(),om_count()+1,map->offset());
 216     }
 217   }
 218 #endif // ASSERT
 219 
 220   set(om_count(),map);
 221   increment_count();
 222 }
 223 
 224 
 225 int OopMapSet::heap_size() const {
 226   // The space we use
 227   int size = sizeof(OopMap);
 228   int align = sizeof(void *) - 1;
 229   size = ((size+align) & ~align);
 230   size += om_count() * sizeof(OopMap*);
 231 
 232   // Now add in the space needed for the indivdiual OopMaps
 233   for(int i=0; i < om_count(); i++) {
 234     size += at(i)->heap_size();
 235   }
 236   // We don't need to align this, it will be naturally pointer aligned
 237   return size;
 238 }
 239 
 240 
 241 OopMap* OopMapSet::singular_oop_map() {
 242   guarantee(om_count() == 1, "Make sure we only have a single gc point");
 243   return at(0);
 244 }
 245 
 246 
 247 OopMap* OopMapSet::find_map_at_offset(int pc_offset) const {
 248   int i, len = om_count();
 249   assert( len > 0, "must have pointer maps" );
 250 
 251   // Scan through oopmaps. Stop when current offset is either equal or greater
 252   // than the one we are looking for.
 253   for( i = 0; i < len; i++) {
 254     if( at(i)->offset() >= pc_offset )
 255       break;
 256   }
 257 
 258   assert( i < len, "oopmap not found" );
 259 
 260   OopMap* m = at(i);
 261   assert( m->offset() == pc_offset, "oopmap not found" );
 262   return m;
 263 }
 264 
 265 class DoNothingClosure: public OopClosure {
 266  public:
 267   void do_oop(oop* p)       {}
 268   void do_oop(narrowOop* p) {}
 269 };
 270 static DoNothingClosure do_nothing;
 271 
 272 static void add_derived_oop(oop* base, oop* derived) {
 273 #ifndef TIERED
 274   COMPILER1_PRESENT(ShouldNotReachHere();)





 275 #endif // TIERED
 276 #ifdef COMPILER2
 277   DerivedPointerTable::add(derived, base);
 278 #endif // COMPILER2
 279 }
 280 
 281 
 282 #ifndef PRODUCT
 283 static void trace_codeblob_maps(const frame *fr, const RegisterMap *reg_map) {
 284   // Print oopmap and regmap
 285   tty->print_cr("------ ");
 286   CodeBlob* cb = fr->cb();
 287   ImmutableOopMapSet* maps = cb->oop_maps();
 288   const ImmutableOopMap* map = cb->oop_map_for_return_address(fr->pc());
 289   map->print();
 290   if( cb->is_nmethod() ) {
 291     nmethod* nm = (nmethod*)cb;
 292     // native wrappers have no scope data, it is implied
 293     if (nm->is_native_method()) {
 294       tty->print("bci: 0 (native)");
 295     } else {
 296       ScopeDesc* scope  = nm->scope_desc_at(fr->pc());
 297       tty->print("bci: %d ",scope->bci());
 298     }
 299   }
 300   tty->cr();
 301   fr->print_on(tty);
 302   tty->print("     ");
 303   cb->print_value_on(tty);  tty->cr();
 304   reg_map->print();
 305   tty->print_cr("------ ");
 306 
 307 }
 308 #endif // PRODUCT
 309 
 310 void OopMapSet::oops_do(const frame *fr, const RegisterMap* reg_map, OopClosure* f) {
 311   // add derived oops to a table
 312   all_do(fr, reg_map, f, add_derived_oop, &do_nothing);
 313 }
 314 
 315 
 316 void OopMapSet::all_do(const frame *fr, const RegisterMap *reg_map,
 317                        OopClosure* oop_fn, void derived_oop_fn(oop*, oop*),
 318                        OopClosure* value_fn) {
 319   CodeBlob* cb = fr->cb();
 320   assert(cb != NULL, "no codeblob");
 321 
 322   NOT_PRODUCT(if (TraceCodeBlobStacks) trace_codeblob_maps(fr, reg_map);)
 323 
 324   ImmutableOopMapSet* maps = cb->oop_maps();
 325   const ImmutableOopMap* map = cb->oop_map_for_return_address(fr->pc());
 326   assert(map != NULL, "no ptr map found");
 327 
 328   // handle derived pointers first (otherwise base pointer may be
 329   // changed before derived pointer offset has been collected)
 330   OopMapValue omv;
 331   {
 332     OopMapStream oms(map,OopMapValue::derived_oop_value);
 333     if (!oms.is_done()) {
 334 #ifndef TIERED
 335       COMPILER1_PRESENT(ShouldNotReachHere();)





 336 #endif // !TIERED
 337       // Protect the operation on the derived pointers.  This
 338       // protects the addition of derived pointers to the shared
 339       // derived pointer table in DerivedPointerTable::add().
 340       MutexLockerEx x(DerivedPointerTableGC_lock, Mutex::_no_safepoint_check_flag);
 341       do {
 342         omv = oms.current();
 343         oop* loc = fr->oopmapreg_to_location(omv.reg(),reg_map);
 344         guarantee(loc != NULL, "missing saved register");
 345         oop *base_loc    = fr->oopmapreg_to_location(omv.content_reg(), reg_map);
 346         oop *derived_loc = loc;
 347         oop val = *base_loc;
 348         if (val == (oop)NULL || Universe::is_narrow_oop_base(val)) {
 349           // Ignore NULL oops and decoded NULL narrow oops which
 350           // equal to Universe::narrow_oop_base when a narrow oop
 351           // implicit null check is used in compiled code.
 352           // The narrow_oop_base could be NULL or be the address
 353           // of the page below heap depending on compressed oops mode.
 354         } else {
 355           derived_oop_fn(base_loc, derived_loc);
 356         }
 357         oms.next();
 358       }  while (!oms.is_done());
 359     }
 360   }
 361 
 362   // We want coop and oop oop_types
 363   int mask = OopMapValue::oop_value | OopMapValue::narrowoop_value;
 364   {
 365     for (OopMapStream oms(map,mask); !oms.is_done(); oms.next()) {
 366       omv = oms.current();
 367       oop* loc = fr->oopmapreg_to_location(omv.reg(),reg_map);
 368       // It should be an error if no location can be found for a
 369       // register mentioned as contained an oop of some kind.  Maybe
 370       // this was allowed previously because value_value items might
 371       // be missing?
 372       guarantee(loc != NULL, "missing saved register");
 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::narrowoop_value ) {
 399         narrowOop *nl = (narrowOop*)loc;
 400 #ifndef VM_LITTLE_ENDIAN
 401         VMReg vmReg = omv.reg();
 402         // Don't do this on SPARC float registers as they can be individually addressed
 403         if (!vmReg->is_stack() SPARC_ONLY(&& !vmReg->is_FloatRegister())) {
 404           // compressed oops in registers only take up 4 bytes of an
 405           // 8 byte register but they are in the wrong part of the
 406           // word so adjust loc to point at the right place.
 407           nl = (narrowOop*)((address)nl + 4);
 408         }
 409 #endif
 410         oop_fn->do_oop(nl);
 411       }
 412     }
 413   }
 414 }
 415 
 416 
 417 // Update callee-saved register info for the following frame
 418 void OopMapSet::update_register_map(const frame *fr, RegisterMap *reg_map) {
 419   ResourceMark rm;
 420   CodeBlob* cb = fr->cb();
 421   assert(cb != NULL, "no codeblob");
 422 
 423   // Any reg might be saved by a safepoint handler (see generate_handler_blob).
 424   assert( reg_map->_update_for_id == NULL || fr->is_older(reg_map->_update_for_id),
 425          "already updated this map; do not 'update' it twice!" );
 426   debug_only(reg_map->_update_for_id = fr->id());
 427 
 428   // Check if caller must update oop argument
 429   assert((reg_map->include_argument_oops() ||
 430           !cb->caller_must_gc_arguments(reg_map->thread())),
 431          "include_argument_oops should already be set");
 432 
 433   // Scan through oopmap and find location of all callee-saved registers
 434   // (we do not do update in place, since info could be overwritten)
 435 
 436   address pc = fr->pc();
 437   const ImmutableOopMap* map  = cb->oop_map_for_return_address(pc);
 438   assert(map != NULL, "no ptr map found");
 439   DEBUG_ONLY(int nof_callee = 0;)
 440 
 441   for (OopMapStream oms(map, OopMapValue::callee_saved_value); !oms.is_done(); oms.next()) {
 442     OopMapValue omv = oms.current();
 443     VMReg reg = omv.content_reg();
 444     oop* loc = fr->oopmapreg_to_location(omv.reg(), reg_map);
 445     reg_map->set_location(reg, (address) loc);
 446     DEBUG_ONLY(nof_callee++;)
 447   }
 448 
 449   // Check that runtime stubs save all callee-saved registers
 450 #ifdef COMPILER2
 451   assert(cb->is_compiled_by_c1() || !cb->is_runtime_stub() ||
 452          (nof_callee >= SAVED_ON_ENTRY_REG_COUNT || nof_callee >= C_SAVED_ON_ENTRY_REG_COUNT),
 453          "must save all");
 454 #endif // COMPILER2
 455 }
 456 
 457 //=============================================================================
 458 // Non-Product code
 459 
 460 #ifndef PRODUCT
 461 
 462 bool ImmutableOopMap::has_derived_pointer() const {
 463 #ifndef TIERED
 464   COMPILER1_PRESENT(return false);





 465 #endif // !TIERED
 466 #ifdef COMPILER2
 467   OopMapStream oms((OopMap*)this,OopMapValue::derived_oop_value);
 468   return oms.is_done();
 469 #else
 470   return false;
 471 #endif // COMPILER2
 472 }
 473 
 474 #endif //PRODUCT
 475 
 476 // Printing code is present in product build for -XX:+PrintAssembly.
 477 
 478 static
 479 void print_register_type(OopMapValue::oop_types x, VMReg optional,
 480                          outputStream* st) {
 481   switch( x ) {
 482   case OopMapValue::oop_value:
 483     st->print("Oop");
 484     break;
 485   case OopMapValue::narrowoop_value:
 486     st->print("NarrowOop");
 487     break;
 488   case OopMapValue::callee_saved_value:
 489     st->print("Callers_");
 490     optional->print_on(st);
 491     break;
 492   case OopMapValue::derived_oop_value:
 493     st->print("Derived_oop_");
 494     optional->print_on(st);
 495     break;
 496   default:
 497     ShouldNotReachHere();
 498   }
 499 }
 500 
 501 void OopMapValue::print_on(outputStream* st) const {
 502   reg()->print_on(st);
 503   st->print("=");
 504   print_register_type(type(),content_reg(),st);
 505   st->print(" ");
 506 }
 507 
 508 void ImmutableOopMap::print_on(outputStream* st) const {
 509   OopMapValue omv;
 510   st->print("ImmutableOopMap{");
 511   for(OopMapStream oms(this); !oms.is_done(); oms.next()) {
 512     omv = oms.current();
 513     omv.print_on(st);
 514   }
 515   st->print("}");
 516 }
 517 
 518 void OopMap::print_on(outputStream* st) const {
 519   OopMapValue omv;
 520   st->print("OopMap{");
 521   for(OopMapStream oms((OopMap*)this); !oms.is_done(); oms.next()) {
 522     omv = oms.current();
 523     omv.print_on(st);
 524   }
 525   st->print("off=%d}", (int) offset());
 526 }
 527 
 528 void ImmutableOopMapSet::print_on(outputStream* st) const {
 529   const ImmutableOopMap* last = NULL;
 530   for (int i = 0; i < _count; ++i) {
 531     const ImmutableOopMapPair* pair = pair_at(i);
 532     const ImmutableOopMap* map = pair->get_from(this);
 533     if (map != last) {
 534       st->cr();
 535       map->print_on(st);
 536       st->print("pc offsets: ");
 537     }
 538     last = map;
 539     st->print("%d ", pair->pc_offset());
 540   }
 541 }
 542 
 543 void OopMapSet::print_on(outputStream* st) const {
 544   int i, len = om_count();
 545 
 546   st->print_cr("OopMapSet contains %d OopMaps\n",len);
 547 
 548   for( i = 0; i < len; i++) {
 549     OopMap* m = at(i);
 550     st->print_cr("#%d ",i);
 551     m->print_on(st);
 552     st->cr();
 553   }
 554 }
 555 
 556 bool OopMap::equals(const OopMap* other) const {
 557   if (other->_omv_count != _omv_count) {
 558     return false;
 559   }
 560   if (other->write_stream()->position() != write_stream()->position()) {
 561     return false;
 562   }
 563   if (memcmp(other->write_stream()->buffer(), write_stream()->buffer(), write_stream()->position()) != 0) {
 564     return false;
 565   }
 566   return true;
 567 }
 568 
 569 const ImmutableOopMap* ImmutableOopMapSet::find_map_at_offset(int pc_offset) const {
 570   ImmutableOopMapPair* pairs = get_pairs();
 571   ImmutableOopMapPair* last = NULL;
 572 
 573   for (int i = 0; i < _count; ++i) {
 574     if (pairs[i].pc_offset() >= pc_offset) {
 575       last = &pairs[i];
 576       break;
 577     }
 578   }
 579 
 580   assert(last->pc_offset() == pc_offset, "oopmap not found");
 581   return last->get_from(this);
 582 }
 583 
 584 const ImmutableOopMap* ImmutableOopMapPair::get_from(const ImmutableOopMapSet* set) const {
 585   return set->oopmap_at_offset(_oopmap_offset);
 586 }
 587 
 588 ImmutableOopMap::ImmutableOopMap(const OopMap* oopmap) : _count(oopmap->count()) {
 589   address addr = data_addr();
 590   oopmap->copy_data_to(addr);
 591 }
 592 
 593 #ifdef ASSERT
 594 int ImmutableOopMap::nr_of_bytes() const {
 595   OopMapStream oms(this);
 596 
 597   while (!oms.is_done()) {
 598     oms.next();
 599   }
 600   return sizeof(ImmutableOopMap) + oms.stream_position();
 601 }
 602 #endif
 603 
 604 class ImmutableOopMapBuilder {
 605 private:
 606   class Mapping;
 607 
 608 private:
 609   const OopMapSet* _set;
 610   const OopMap* _empty;
 611   const OopMap* _last;
 612   int _empty_offset;
 613   int _last_offset;
 614   int _offset;
 615   Mapping* _mapping;
 616   ImmutableOopMapSet* _new_set;
 617 
 618   /* Used for bookkeeping when building ImmutableOopMaps */
 619   class Mapping : public ResourceObj {
 620   public:
 621     enum kind_t { OOPMAP_UNKNOWN = 0, OOPMAP_NEW = 1, OOPMAP_EMPTY = 2, OOPMAP_DUPLICATE = 3 };
 622 
 623     kind_t _kind;
 624     int _offset;
 625     int _size;
 626     const OopMap* _map;
 627     const OopMap* _other;
 628 
 629     Mapping() : _kind(OOPMAP_UNKNOWN), _offset(-1), _size(-1), _map(NULL) {}
 630 
 631     void set(kind_t kind, int offset, int size, const OopMap* map = 0, const OopMap* other = 0) {
 632       _kind = kind;
 633       _offset = offset;
 634       _size = size;
 635       _map = map;
 636       _other = other;
 637     }
 638   };
 639 
 640 public:
 641   ImmutableOopMapBuilder(const OopMapSet* set) : _set(set), _new_set(NULL), _empty(NULL), _last(NULL), _empty_offset(-1), _last_offset(-1), _offset(0) {
 642     _mapping = NEW_RESOURCE_ARRAY(Mapping, _set->size());
 643   }
 644 
 645   int heap_size();
 646   ImmutableOopMapSet* build();
 647 private:
 648   bool is_empty(const OopMap* map) const {
 649     return map->count() == 0;
 650   }
 651 
 652   bool is_last_duplicate(const OopMap* map) {
 653     if (_last != NULL && _last->count() > 0 && _last->equals(map)) {
 654       return true;
 655     }
 656     return false;
 657   }
 658 
 659 #ifdef ASSERT
 660   void verify(address buffer, int size, const ImmutableOopMapSet* set);
 661 #endif
 662 
 663   bool has_empty() const {
 664     return _empty_offset != -1;
 665   }
 666 
 667   int size_for(const OopMap* map) const;
 668   void fill_pair(ImmutableOopMapPair* pair, const OopMap* map, int offset, const ImmutableOopMapSet* set);
 669   int fill_map(ImmutableOopMapPair* pair, const OopMap* map, int offset, const ImmutableOopMapSet* set);
 670   void fill(ImmutableOopMapSet* set, int size);
 671 };
 672 
 673 int ImmutableOopMapBuilder::size_for(const OopMap* map) const {
 674   return align_size_up(sizeof(ImmutableOopMap) + map->data_size(), 8);
 675 }
 676 
 677 int ImmutableOopMapBuilder::heap_size() {
 678   int base = sizeof(ImmutableOopMapSet);
 679   base = align_size_up(base, 8);
 680 
 681   // all of ours pc / offset pairs
 682   int pairs = _set->size() * sizeof(ImmutableOopMapPair);
 683   pairs = align_size_up(pairs, 8);
 684 
 685   for (int i = 0; i < _set->size(); ++i) {
 686     int size = 0;
 687     OopMap* map = _set->at(i);
 688 
 689     if (is_empty(map)) {
 690       /* only keep a single empty map in the set */
 691       if (has_empty()) {
 692         _mapping[i].set(Mapping::OOPMAP_EMPTY, _empty_offset, 0, map, _empty);
 693       } else {
 694         _empty_offset = _offset;
 695         _empty = map;
 696         size = size_for(map);
 697         _mapping[i].set(Mapping::OOPMAP_NEW, _offset, size, map);
 698       }
 699     } else if (is_last_duplicate(map)) {
 700       /* if this entry is identical to the previous one, just point it there */
 701       _mapping[i].set(Mapping::OOPMAP_DUPLICATE, _last_offset, 0, map, _last);
 702     } else {
 703       /* not empty, not an identical copy of the previous entry */
 704       size = size_for(map);
 705       _mapping[i].set(Mapping::OOPMAP_NEW, _offset, size, map);
 706       _last_offset = _offset;
 707       _last = map;
 708     }
 709 
 710     assert(_mapping[i]._map == map, "check");
 711     _offset += size;
 712   }
 713 
 714   int total = base + pairs + _offset;
 715   DEBUG_ONLY(total += 8);

 716   return total;
 717 }
 718 
 719 void ImmutableOopMapBuilder::fill_pair(ImmutableOopMapPair* pair, const OopMap* map, int offset, const ImmutableOopMapSet* set) {
 720   assert(offset < set->nr_of_bytes(), "check");
 721   new ((address) pair) ImmutableOopMapPair(map->offset(), offset);
 722 }
 723 
 724 int ImmutableOopMapBuilder::fill_map(ImmutableOopMapPair* pair, const OopMap* map, int offset, const ImmutableOopMapSet* set) {
 725   fill_pair(pair, map, offset, set);
 726   address addr = (address) pair->get_from(_new_set); // location of the ImmutableOopMap
 727 
 728   new (addr) ImmutableOopMap(map);
 729   return align_size_up(sizeof(ImmutableOopMap) + map->data_size(), 8);
 730 }
 731 
 732 void ImmutableOopMapBuilder::fill(ImmutableOopMapSet* set, int sz) {
 733   ImmutableOopMapPair* pairs = set->get_pairs();
 734 
 735   for (int i = 0; i < set->count(); ++i) {
 736     const OopMap* map = _mapping[i]._map;
 737     ImmutableOopMapPair* pair = NULL;
 738     int size = 0;
 739 
 740     if (_mapping[i]._kind == Mapping::OOPMAP_NEW) {
 741       size = fill_map(&pairs[i], map, _mapping[i]._offset, set);
 742     } else if (_mapping[i]._kind == Mapping::OOPMAP_DUPLICATE || _mapping[i]._kind == Mapping::OOPMAP_EMPTY) {
 743       fill_pair(&pairs[i], map, _mapping[i]._offset, set);
 744     }
 745 
 746     const ImmutableOopMap* nv = set->find_map_at_offset(map->offset());
 747     assert(memcmp(map->data(), nv->data_addr(), map->data_size()) == 0, "check identity");
 748   }
 749 }
 750 
 751 #ifdef ASSERT
 752 void ImmutableOopMapBuilder::verify(address buffer, int size, const ImmutableOopMapSet* set) {
 753   for (int i = 0; i < 8; ++i) {
 754     assert(buffer[size - 8 + i] == (unsigned char) 0xff, "overwritten memory check");
 755   }
 756 
 757   for (int i = 0; i < set->count(); ++i) {
 758     const ImmutableOopMapPair* pair = set->pair_at(i);
 759     assert(pair->oopmap_offset() < set->nr_of_bytes(), "check size");
 760     const ImmutableOopMap* map = pair->get_from(set);
 761     int nr_of_bytes = map->nr_of_bytes();
 762     assert(pair->oopmap_offset() + nr_of_bytes <= set->nr_of_bytes(), "check size + size");
 763   }
 764 }
 765 #endif
 766 
 767 ImmutableOopMapSet* ImmutableOopMapBuilder::build() {
 768   int required = heap_size();
 769 
 770   // We need to allocate a chunk big enough to hold the ImmutableOopMapSet and all of its ImmutableOopMaps
 771   address buffer = (address) NEW_C_HEAP_ARRAY(unsigned char, required, mtCode);
 772   DEBUG_ONLY(memset(&buffer[required-8], 0xff, 8));
 773 
 774   _new_set = new (buffer) ImmutableOopMapSet(_set, required);
 775   fill(_new_set, required);
 776 
 777   DEBUG_ONLY(verify(buffer, required, _new_set));
 778 
 779   return _new_set;
 780 }
 781 








 782 ImmutableOopMapSet* ImmutableOopMapSet::build_from(const OopMapSet* oopmap_set) {
 783   ResourceMark mark;
 784   ImmutableOopMapBuilder builder(oopmap_set);
 785   return builder.build();
 786 }
 787 
 788 
 789 //------------------------------DerivedPointerTable---------------------------
 790 
 791 #ifdef COMPILER2
 792 
 793 class DerivedPointerEntry : public CHeapObj<mtCompiler> {
 794  private:
 795   oop*     _location; // Location of derived pointer (also pointing to the base)
 796   intptr_t _offset;   // Offset from base pointer
 797  public:
 798   DerivedPointerEntry(oop* location, intptr_t offset) { _location = location; _offset = offset; }
 799   oop* location()    { return _location; }
 800   intptr_t  offset() { return _offset; }
 801 };
 802 
 803 
 804 GrowableArray<DerivedPointerEntry*>* DerivedPointerTable::_list = NULL;
 805 bool DerivedPointerTable::_active = false;
 806 
 807 
 808 void DerivedPointerTable::clear() {
 809   // The first time, we create the list.  Otherwise it should be
 810   // empty.  If not, then we have probably forgotton to call
 811   // update_pointers after last GC/Scavenge.
 812   assert (!_active, "should not be active");
 813   assert(_list == NULL || _list->length() == 0, "table not empty");
 814   if (_list == NULL) {
 815     _list = new (ResourceObj::C_HEAP, mtCompiler) GrowableArray<DerivedPointerEntry*>(10, true); // Allocated on C heap
 816   }
 817   _active = true;
 818 }
 819 
 820 
 821 // Returns value of location as an int
 822 intptr_t value_of_loc(oop *pointer) { return cast_from_oop<intptr_t>((*pointer)); }
 823 
 824 
 825 void DerivedPointerTable::add(oop *derived_loc, oop *base_loc) {
 826   assert(Universe::heap()->is_in_or_null(*base_loc), "not an oop");
 827   assert(derived_loc != base_loc, "Base and derived in same location");
 828   if (_active) {
 829     assert(*derived_loc != (oop)base_loc, "location already added");
 830     assert(_list != NULL, "list must exist");
 831     intptr_t offset = value_of_loc(derived_loc) - value_of_loc(base_loc);
 832     // This assert is invalid because derived pointers can be
 833     // arbitrarily far away from their base.
 834     // assert(offset >= -1000000, "wrong derived pointer info");
 835 
 836     if (TraceDerivedPointers) {
 837       tty->print_cr(
 838         "Add derived pointer@" INTPTR_FORMAT
 839         " - Derived: " INTPTR_FORMAT
 840         " Base: " INTPTR_FORMAT " (@" INTPTR_FORMAT ") (Offset: " INTX_FORMAT ")",
 841         p2i(derived_loc), p2i((address)*derived_loc), p2i((address)*base_loc), p2i(base_loc), offset
 842       );
 843     }
 844     // Set derived oop location to point to base.
 845     *derived_loc = (oop)base_loc;
 846     assert_lock_strong(DerivedPointerTableGC_lock);
 847     DerivedPointerEntry *entry = new DerivedPointerEntry(derived_loc, offset);
 848     _list->append(entry);
 849   }
 850 }
 851 
 852 
 853 void DerivedPointerTable::update_pointers() {
 854   assert(_list != NULL, "list must exist");
 855   for(int i = 0; i < _list->length(); i++) {
 856     DerivedPointerEntry* entry = _list->at(i);
 857     oop* derived_loc = entry->location();
 858     intptr_t offset  = entry->offset();
 859     // The derived oop was setup to point to location of base
 860     oop  base        = **(oop**)derived_loc;
 861     assert(Universe::heap()->is_in_or_null(base), "must be an oop");
 862 
 863     *derived_loc = (oop)(((address)base) + offset);
 864     assert(value_of_loc(derived_loc) - value_of_loc(&base) == offset, "sanity check");
 865 
 866     if (TraceDerivedPointers) {
 867       tty->print_cr("Updating derived pointer@" INTPTR_FORMAT
 868                     " - Derived: " INTPTR_FORMAT "  Base: " INTPTR_FORMAT " (Offset: " INTX_FORMAT ")",
 869           p2i(derived_loc), p2i((address)*derived_loc), p2i((address)base), offset);
 870     }
 871 
 872     // Delete entry
 873     delete entry;
 874     _list->at_put(i, NULL);
 875   }
 876   // Clear list, so it is ready for next traversal (this is an invariant)
 877   if (TraceDerivedPointers && !_list->is_empty()) {
 878     tty->print_cr("--------------------------");
 879   }
 880   _list->clear();
 881   _active = false;
 882 }
 883 
 884 #endif // COMPILER2
--- EOF ---