1 /* 2 * Copyright (c) 1998, 2010, 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 #ifndef SHARE_VM_COMPILER_OOPMAP_HPP 26 #define SHARE_VM_COMPILER_OOPMAP_HPP 27 28 #include "code/compressedStream.hpp" 29 #include "code/vmreg.hpp" 30 #include "memory/allocation.hpp" 31 #include "utilities/growableArray.hpp" 32 33 // Interface for generating the frame map for compiled code. A frame map 34 // describes for a specific pc whether each register and frame stack slot is: 35 // Oop - A GC root for current frame 36 // Value - Live non-oop, non-float value: int, either half of double 37 // Dead - Dead; can be Zapped for debugging 38 // CalleeXX - Callee saved; also describes which caller register is saved 39 // DerivedXX - A derived oop; original oop is described. 40 // 41 // OopMapValue describes a single OopMap entry 42 43 class frame; 44 class RegisterMap; 45 class DerivedPointerEntry; 46 47 class OopMapValue: public StackObj { 48 friend class VMStructs; 49 private: 50 short _value; 51 int value() const { return _value; } 52 void set_value(int value) { _value = value; } 53 short _content_reg; 54 55 public: 56 // Constants 57 enum { type_bits = 5, 58 register_bits = BitsPerShort - type_bits }; 59 60 enum { type_shift = 0, 61 register_shift = type_bits }; 62 63 enum { type_mask = right_n_bits(type_bits), 64 type_mask_in_place = type_mask << type_shift, 65 register_mask = right_n_bits(register_bits), 66 register_mask_in_place = register_mask << register_shift }; 67 68 enum oop_types { // must fit in type_bits 69 unused_value =0, // powers of 2, for masking OopMapStream 70 oop_value = 1, 71 value_value = 2, 72 narrowoop_value = 4, 73 callee_saved_value = 8, 74 derived_oop_value= 16 }; 75 76 // Constructors 77 OopMapValue () { set_value(0); set_content_reg(VMRegImpl::Bad()); } 78 OopMapValue (VMReg reg, oop_types t) { set_reg_type(reg,t); } 79 OopMapValue (VMReg reg, oop_types t, VMReg reg2) { set_reg_type(reg,t); set_content_reg(reg2); } 80 OopMapValue (CompressedReadStream* stream) { read_from(stream); } 81 82 // Archiving 83 void write_on(CompressedWriteStream* stream) { 84 stream->write_int(value()); 85 if(is_callee_saved() || is_derived_oop()) { 86 stream->write_int(content_reg()->value()); 87 } 88 } 89 90 void read_from(CompressedReadStream* stream) { 91 set_value(stream->read_int()); 92 if(is_callee_saved() || is_derived_oop()) { 93 set_content_reg(VMRegImpl::as_VMReg(stream->read_int(), true)); 94 } 95 } 96 97 // Querying 98 bool is_oop() { return mask_bits(value(), type_mask_in_place) == oop_value; } 99 bool is_value() { return mask_bits(value(), type_mask_in_place) == value_value; } 100 bool is_narrowoop() { return mask_bits(value(), type_mask_in_place) == narrowoop_value; } 101 bool is_callee_saved() { return mask_bits(value(), type_mask_in_place) == callee_saved_value; } 102 bool is_derived_oop() { return mask_bits(value(), type_mask_in_place) == derived_oop_value; } 103 104 void set_oop() { set_value((value() & register_mask_in_place) | oop_value); } 105 void set_value() { set_value((value() & register_mask_in_place) | value_value); } 106 void set_narrowoop() { set_value((value() & register_mask_in_place) | narrowoop_value); } 107 void set_callee_saved() { set_value((value() & register_mask_in_place) | callee_saved_value); } 108 void set_derived_oop() { set_value((value() & register_mask_in_place) | derived_oop_value); } 109 110 VMReg reg() const { return VMRegImpl::as_VMReg(mask_bits(value(), register_mask_in_place) >> register_shift); } 111 oop_types type() const { return (oop_types)mask_bits(value(), type_mask_in_place); } 112 113 static bool legal_vm_reg_name(VMReg p) { 114 return (p->value() == (p->value() & register_mask)); 115 } 116 117 void set_reg_type(VMReg p, oop_types t) { 118 set_value((p->value() << register_shift) | t); 119 assert(reg() == p, "sanity check" ); 120 assert(type() == t, "sanity check" ); 121 } 122 123 124 VMReg content_reg() const { return VMRegImpl::as_VMReg(_content_reg, true); } 125 void set_content_reg(VMReg r) { _content_reg = r->value(); } 126 127 // Physical location queries 128 bool is_register_loc() { return reg()->is_reg(); } 129 bool is_stack_loc() { return reg()->is_stack(); } 130 131 // Returns offset from sp. 132 int stack_offset() { 133 assert(is_stack_loc(), "must be stack location"); 134 return reg()->reg2stack(); 135 } 136 137 void print_on(outputStream* st) const; 138 void print() const { print_on(tty); } 139 }; 140 141 142 class OopMap: public ResourceObj { 143 friend class OopMapStream; 144 friend class VMStructs; 145 private: 146 int _pc_offset; // offset in the code that this OopMap corresponds to 147 int _omv_count; // number of OopMapValues in the stream 148 CompressedWriteStream* _write_stream; 149 150 debug_only( OopMapValue::oop_types* _locs_used; int _locs_length;) 151 152 // Accessors 153 int omv_count() const { return _omv_count; } 154 void set_omv_count(int value) { _omv_count = value; } 155 void increment_count() { _omv_count++; } 156 CompressedWriteStream* write_stream() const { return _write_stream; } 157 void set_write_stream(CompressedWriteStream* value) { _write_stream = value; } 158 159 private: 160 enum DeepCopyToken { _deep_copy_token }; 161 OopMap(DeepCopyToken, OopMap* source); // used only by deep_copy 162 163 public: 164 OopMap(int frame_size, int arg_count); 165 166 // pc-offset handling 167 int offset() const { return _pc_offset; } 168 void set_offset(int o) { _pc_offset = o; } 169 int count() const { return _omv_count; } 170 int data_size() const { return write_stream()->position(); } 171 address data() const { return write_stream()->buffer(); } 172 173 // Check to avoid double insertion 174 debug_only(OopMapValue::oop_types locs_used( int indx ) { return _locs_used[indx]; }) 175 176 // Construction 177 // frame_size units are stack-slots (4 bytes) NOT intptr_t; we can name odd 178 // slots to hold 4-byte values like ints and floats in the LP64 build. 179 void set_oop ( VMReg local); 180 void set_value( VMReg local); 181 void set_narrowoop(VMReg local); 182 void set_dead ( VMReg local); 183 void set_callee_saved( VMReg local, VMReg caller_machine_register ); 184 void set_derived_oop ( VMReg local, VMReg derived_from_local_register ); 185 void set_xxx(VMReg reg, OopMapValue::oop_types x, VMReg optional); 186 187 int heap_size() const; 188 void copy_data_to(address addr) const; 189 OopMap* deep_copy(); 190 191 bool legal_vm_reg_name(VMReg local) { 192 return OopMapValue::legal_vm_reg_name(local); 193 } 194 195 // Printing 196 void print_on(outputStream* st) const; 197 void print() const { print_on(tty); } 198 bool equals(const OopMap* other) const; 199 }; 200 201 202 class OopMapSet : public ResourceObj { 203 friend class VMStructs; 204 private: 205 int _om_count; 206 int _om_size; 207 OopMap** _om_data; 208 209 int om_count() const { return _om_count; } 210 void set_om_count(int value) { _om_count = value; } 211 void increment_count() { _om_count++; } 212 int om_size() const { return _om_size; } 213 void set_om_size(int value) { _om_size = value; } 214 OopMap** om_data() const { return _om_data; } 215 void set_om_data(OopMap** value) { _om_data = value; } 216 void grow_om_data(); 217 void set(int index,OopMap* value) { assert((index == 0) || ((index > 0) && (index < om_size())),"bad index"); _om_data[index] = value; } 218 219 public: 220 OopMapSet(); 221 222 // returns the number of OopMaps in this OopMapSet 223 int size() const { return _om_count; } 224 // returns the OopMap at a given index 225 OopMap* at(int index) const { assert((index >= 0) && (index <= om_count()),"bad index"); return _om_data[index]; } 226 227 // Collect OopMaps. 228 void add_gc_map(int pc, OopMap* map); 229 230 // Returns the only oop map. Used for reconstructing 231 // Adapter frames during deoptimization 232 OopMap* singular_oop_map(); 233 234 // returns OopMap in that is anchored to the pc 235 OopMap* find_map_at_offset(int pc_offset) const; 236 237 int heap_size() const; 238 239 // Methods oops_do() and all_do() filter out NULL oops and 240 // oop == Universe::narrow_oop_base() before passing oops 241 // to closures. 242 243 // Iterates through frame for a compiled method 244 static void oops_do (const frame* fr, 245 const RegisterMap* reg_map, OopClosure* f); 246 static void update_register_map(const frame* fr, RegisterMap *reg_map); 247 248 // Iterates through frame for a compiled method for dead ones and values, too 249 static void all_do(const frame* fr, const RegisterMap* reg_map, 250 OopClosure* oop_fn, 251 void derived_oop_fn(oop* base, oop* derived), 252 OopClosure* value_fn); 253 254 // Printing 255 void print_on(outputStream* st) const; 256 void print() const { print_on(tty); } 257 }; 258 259 class ImmutableOopMapBuilder; 260 261 class ImmutableOopMap { 262 friend class OopMapStream; 263 friend class VMStructs; 264 #ifdef ASSERT 265 friend class ImmutableOopMapBuilder; 266 #endif 267 private: 268 int _count; // contains the number of entries in this OopMap 269 270 address data_addr() const { return (address) this + sizeof(ImmutableOopMap); } 271 public: 272 ImmutableOopMap(const OopMap* oopmap); 273 274 bool has_derived_pointer() const PRODUCT_RETURN0; 275 int count() const { return _count; } 276 277 // Printing 278 void print_on(outputStream* st) const; 279 void print() const { print_on(tty); } 280 }; 281 282 class ImmutableOopMapSet; 283 class ImmutableOopMap; 284 class OopMapSet; 285 286 class ImmutableOopMapPair { 287 friend class VMStructs; 288 private: 289 int _pc_offset; // program counter offset from the beginning of the method 290 int _oopmap_offset; // offset in the data in the ImmutableOopMapSet where the ImmutableOopMap is located 291 public: 292 ImmutableOopMapPair(int pc_offset, int oopmap_offset) : _pc_offset(pc_offset), _oopmap_offset(oopmap_offset) { 293 assert(pc_offset >= 0 && oopmap_offset >= 0, "check"); 294 } 295 const ImmutableOopMap* get_from(const ImmutableOopMapSet* set) const; 296 297 int pc_offset() const { return _pc_offset; } 298 int oopmap_offset() const { return _oopmap_offset; } 299 }; 300 301 class ImmutableOopMapSet { 302 friend class VMStructs; 303 private: 304 int _count; // nr of ImmutableOopMapPairs in the Set 305 int _size; // nr of bytes including ImmutableOopMapSet itself 306 307 address data() const { return (address) this + sizeof(*this) + sizeof(ImmutableOopMapPair) * _count; } 308 309 public: 310 ImmutableOopMapSet(const OopMapSet* oopmap_set, int size) : _count(oopmap_set->size()), _size(size) {} 311 312 ImmutableOopMap* oopmap_at_offset(int offset) const { 313 assert(offset >= 0 && offset < _size, "must be within boundaries"); 314 address addr = data() + offset; 315 return (ImmutableOopMap*) addr; 316 } 317 318 ImmutableOopMapPair* get_pairs() const { return (ImmutableOopMapPair*) ((address) this + sizeof(*this)); } 319 320 static ImmutableOopMapSet* build_from(const OopMapSet* oopmap_set); 321 322 const ImmutableOopMap* find_map_at_offset(int pc_offset) const; 323 324 const ImmutableOopMapPair* pair_at(int index) const { assert(index >= 0 && index < _count, "check"); return &get_pairs()[index]; } 325 326 int count() const { return _count; } 327 int size() const { return _size; } 328 329 void print_on(outputStream* st) const; 330 void print() const { print_on(tty); } 331 }; 332 333 class OopMapStream : public StackObj { 334 private: 335 CompressedReadStream* _stream; 336 int _mask; 337 int _size; 338 int _position; 339 bool _valid_omv; 340 OopMapValue _omv; 341 void find_next(); 342 343 public: 344 OopMapStream(OopMap* oop_map, int oop_types_mask = OopMapValue::type_mask_in_place); 345 OopMapStream(const ImmutableOopMap* oop_map, int oop_types_mask = OopMapValue::type_mask_in_place); 346 bool is_done() { if(!_valid_omv) { find_next(); } return !_valid_omv; } 347 void next() { find_next(); } 348 OopMapValue current() { return _omv; } 349 }; 350 351 352 // Derived pointer support. This table keeps track of all derived points on a 353 // stack. It is cleared before each scavenge/GC. During the traversal of all 354 // oops, it is filled in with references to all locations that contains a 355 // derived oop (assumed to be very few). When the GC is complete, the derived 356 // pointers are updated based on their base pointers new value and an offset. 357 #ifdef COMPILER2 358 class DerivedPointerTable : public AllStatic { 359 friend class VMStructs; 360 private: 361 static GrowableArray<DerivedPointerEntry*>* _list; 362 static bool _active; // do not record pointers for verify pass etc. 363 public: 364 static void clear(); // Called before scavenge/GC 365 static void add(oop *derived, oop *base); // Called during scavenge/GC 366 static void update_pointers(); // Called after scavenge/GC 367 static bool is_empty() { return _list == NULL || _list->is_empty(); } 368 static bool is_active() { return _active; } 369 static void set_active(bool value) { _active = value; } 370 }; 371 372 // A utility class to temporarily "deactivate" the DerivedPointerTable. 373 // (Note: clients are responsible for any MT-safety issues) 374 class DerivedPointerTableDeactivate: public StackObj { 375 private: 376 bool _active; 377 public: 378 DerivedPointerTableDeactivate() { 379 _active = DerivedPointerTable::is_active(); 380 if (_active) { 381 DerivedPointerTable::set_active(false); 382 } 383 } 384 385 ~DerivedPointerTableDeactivate() { 386 assert(!DerivedPointerTable::is_active(), 387 "Inconsistency: not MT-safe"); 388 if (_active) { 389 DerivedPointerTable::set_active(true); 390 } 391 } 392 }; 393 #endif // COMPILER2 394 395 #endif // SHARE_VM_COMPILER_OOPMAP_HPP