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 #ifndef SHARE_COMPILER_OOPMAP_HPP
 26 #define SHARE_COMPILER_OOPMAP_HPP
 27 
 28 #include "code/compressedStream.hpp"
 29 #include "code/vmreg.hpp"
 30 #include "memory/allocation.hpp"
 31 #include "oops/oopsHierarchy.hpp"
 32 #include "utilities/growableArray.hpp"
 33 
 34 // Interface for generating the frame map for compiled code.  A frame map
 35 // describes for a specific pc whether each register and frame stack slot is:
 36 //   Oop         - A GC root for current frame
 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 OopClosure;
 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                = 2,
 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 {
 69          oop_value,
 70          narrowoop_value,
 71          callee_saved_value,
 72          derived_oop_value,
 73          unused_value = -1          // Only used as a sentinel value
 74   };
 75 
 76   // Constructors
 77   OopMapValue () { set_value(0); set_content_reg(VMRegImpl::Bad()); }
 78   OopMapValue (VMReg reg, oop_types t, VMReg reg2) {
 79     set_reg_type(reg, t);
 80     set_content_reg(reg2);
 81   }
 82 
 83  private:
 84     void set_reg_type(VMReg p, oop_types t) {
 85     set_value((p->value() << register_shift) | t);
 86     assert(reg() == p, "sanity check" );
 87     assert(type() == t, "sanity check" );
 88   }
 89 
 90   void set_content_reg(VMReg r) {
 91     if (is_callee_saved()) {
 92       // This can never be a stack location, so we don't need to transform it.
 93       assert(r->is_reg(), "Trying to callee save a stack location");
 94     } else if (is_derived_oop()) {
 95       assert (r->is_valid(), "must have a valid VMReg");
 96     } else {
 97       assert (!r->is_valid(), "valid VMReg not allowed");
 98     }
 99     _content_reg = r->value();
100   }
101 
102  public:
103   // Archiving
104   void write_on(CompressedWriteStream* stream) {
105     stream->write_int(value());
106     if(is_callee_saved() || is_derived_oop()) {
107       stream->write_int(content_reg()->value());
108     }
109   }
110 
111   void read_from(CompressedReadStream* stream) {
112     set_value(stream->read_int());
113     if (is_callee_saved() || is_derived_oop()) {
114       set_content_reg(VMRegImpl::as_VMReg(stream->read_int(), true));
115     }
116   }
117 
118   // Querying
119   bool is_oop()               { return mask_bits(value(), type_mask_in_place) == oop_value; }
120   bool is_narrowoop()         { return mask_bits(value(), type_mask_in_place) == narrowoop_value; }
121   bool is_callee_saved()      { return mask_bits(value(), type_mask_in_place) == callee_saved_value; }
122   bool is_derived_oop()       { return mask_bits(value(), type_mask_in_place) == derived_oop_value; }
123 
124   VMReg reg() const { return VMRegImpl::as_VMReg(mask_bits(value(), register_mask_in_place) >> register_shift); }
125   oop_types type() const      { return (oop_types)mask_bits(value(), type_mask_in_place); }
126 
127   static bool legal_vm_reg_name(VMReg p) {
128     return (p->value()  == (p->value() & register_mask));
129   }
130 
131   VMReg content_reg() const       { return VMRegImpl::as_VMReg(_content_reg, true); }
132 
133   // Returns offset from sp.
134   int stack_offset() {
135     assert(reg()->is_stack(), "must be stack location");
136     return reg()->reg2stack();
137   }
138 
139   void print_on(outputStream* st) const;
140   void print() const;
141 };
142 
143 
144 class OopMap: public ResourceObj {
145   friend class OopMapStream;
146   friend class VMStructs;
147  private:
148   int  _pc_offset; // offset in the code that this OopMap corresponds to
149   int  _omv_count; // number of OopMapValues in the stream
150   CompressedWriteStream* _write_stream;
151 
152   debug_only( OopMapValue::oop_types* _locs_used; int _locs_length;)
153 
154   // Accessors
155   int omv_count() const                       { return _omv_count; }
156   void set_omv_count(int value)               { _omv_count = value; }
157   void increment_count()                      { _omv_count++; }
158   CompressedWriteStream* write_stream() const { return _write_stream; }
159   void set_write_stream(CompressedWriteStream* value) { _write_stream = value; }
160 
161  private:
162   enum DeepCopyToken { _deep_copy_token };
163   OopMap(DeepCopyToken, OopMap* source);  // used only by deep_copy
164 
165   void set_xxx(VMReg reg, OopMapValue::oop_types x, VMReg optional);
166 
167  public:
168   OopMap(int frame_size, int arg_count);
169 
170   // pc-offset handling
171   int offset() const     { return _pc_offset; }
172   void set_offset(int o) { _pc_offset = o; }
173   int count() const { return _omv_count; }
174   int data_size() const  { return write_stream()->position(); }
175   address data() const { return write_stream()->buffer(); }
176 
177   // Construction
178   // frame_size units are stack-slots (4 bytes) NOT intptr_t; we can name odd
179   // slots to hold 4-byte values like ints and floats in the LP64 build.
180   void set_oop  ( VMReg local);
181   void set_narrowoop(VMReg local);
182   void set_callee_saved( VMReg local, VMReg caller_machine_register );
183   void set_derived_oop ( VMReg local, VMReg derived_from_local_register );
184 
185   int heap_size() const;
186   void copy_data_to(address addr) const;
187   OopMap* deep_copy();
188 
189   bool legal_vm_reg_name(VMReg local) {
190      return OopMapValue::legal_vm_reg_name(local);
191   }
192 
193   // Printing
194   void print_on(outputStream* st) const;
195   void print() const;
196   bool equals(const OopMap* other) const;
197 };
198 
199 
200 class OopMapSet : public ResourceObj {
201   friend class VMStructs;
202   friend class OopClosureWalker;
203  private:
204   GrowableArray<OopMap*> _list;
205 
206   void add(OopMap* value) { _list.append(value); }
207 
208   static void stack_oop_do(oop *p, OopClosure* oop_fn,
209                       GrowableArray<oop> *stack_oops,
210                       intptr_t *stack_base, intptr_t *stack_top);
211 
212  public:
213   OopMapSet();
214 
215   // returns the number of OopMaps in this OopMapSet
216   int size() const            { return _list.length(); }
217   // returns the OopMap at a given index
218   OopMap* at(int index) const { return _list.at(index); }
219 
220   // Collect OopMaps.
221   void add_gc_map(int pc, OopMap* map);
222 
223   // Methods oops_do() and all_do() filter out NULL oops and
224   // oop == CompressedOops::base() before passing oops
225   // to closures.
226 
227   // Iterates through frame for a compiled method
228   static void oops_do            (const frame* fr,
229                                   const RegisterMap* reg_map, OopClosure* f);
230   static void update_register_map(const frame* fr, RegisterMap *reg_map);
231 
232   // Iterates through frame for a compiled method for dead ones and values, too
233   static void all_do(const frame* fr, const RegisterMap* reg_map,
234                      OopClosure* oop_fn,
235                      void derived_oop_fn(oop* base, oop* derived),
236                      OopClosure* value_fn);
237 
238   // Printing
239   void print_on(outputStream* st) const;
240   void print() const;
241 };
242 
243 class ImmutableOopMapBuilder;
244 
245 class ImmutableOopMap {
246   friend class OopMapStream;
247   friend class VMStructs;
248 #ifdef ASSERT
249   friend class ImmutableOopMapBuilder;
250 #endif
251 private:
252   int _count; // contains the number of entries in this OopMap
253 
254   address data_addr() const { return (address) this + sizeof(ImmutableOopMap); }
255 public:
256   ImmutableOopMap(const OopMap* oopmap);
257 
258   int count() const { return _count; }
259 #ifdef ASSERT
260   int nr_of_bytes() const; // this is an expensive operation, only used in debug builds
261 #endif
262 
263   // Printing
264   void print_on(outputStream* st) const;
265   void print() const;
266 };
267 
268 class ImmutableOopMapSet;
269 class ImmutableOopMap;
270 class OopMapSet;
271 
272 class ImmutableOopMapPair {
273   friend class VMStructs;
274 private:
275   int _pc_offset; // program counter offset from the beginning of the method
276   int _oopmap_offset; // offset in the data in the ImmutableOopMapSet where the ImmutableOopMap is located
277 public:
278   ImmutableOopMapPair(int pc_offset, int oopmap_offset) : _pc_offset(pc_offset), _oopmap_offset(oopmap_offset) {
279     assert(pc_offset >= 0 && oopmap_offset >= 0, "check");
280   }
281   const ImmutableOopMap* get_from(const ImmutableOopMapSet* set) const;
282 
283   int pc_offset() const { return _pc_offset; }
284   int oopmap_offset() const { return _oopmap_offset; }
285 };
286 
287 class ImmutableOopMapSet {
288   friend class VMStructs;
289 private:
290   int _count; // nr of ImmutableOopMapPairs in the Set
291   int _size; // nr of bytes including ImmutableOopMapSet itself
292 
293   address data() const { return (address) this + sizeof(*this) + sizeof(ImmutableOopMapPair) * _count; }
294 
295 public:
296   ImmutableOopMapSet(const OopMapSet* oopmap_set, int size) : _count(oopmap_set->size()), _size(size) {}
297 
298   ImmutableOopMap* oopmap_at_offset(int offset) const {
299     assert(offset >= 0 && offset < _size, "must be within boundaries");
300     address addr = data() + offset;
301     return (ImmutableOopMap*) addr;
302   }
303 
304   ImmutableOopMapPair* get_pairs() const { return (ImmutableOopMapPair*) ((address) this + sizeof(*this)); }
305 
306   static ImmutableOopMapSet* build_from(const OopMapSet* oopmap_set);
307 
308   const ImmutableOopMap* find_map_at_offset(int pc_offset) const;
309 
310   const ImmutableOopMapPair* pair_at(int index) const { assert(index >= 0 && index < _count, "check"); return &get_pairs()[index]; }
311 
312   int count() const { return _count; }
313   int nr_of_bytes() const { return _size; }
314 
315   void print_on(outputStream* st) const;
316   void print() const;
317 };
318 
319 class OopMapStream : public StackObj {
320  private:
321   CompressedReadStream* _stream;
322   int _size;
323   int _position;
324   bool _valid_omv;
325   OopMapValue _omv;
326   void find_next();
327 
328  public:
329   OopMapStream(OopMap* oop_map);
330   OopMapStream(const ImmutableOopMap* oop_map);
331   bool is_done()                        { if(!_valid_omv) { find_next(); } return !_valid_omv; }
332   void next()                           { find_next(); }
333   OopMapValue current()                 { return _omv; }
334 #ifdef ASSERT
335   int stream_position() const           { return _stream->position(); }
336 #endif
337 };
338 
339 class ImmutableOopMapBuilder {
340 private:
341   class Mapping;
342 
343 private:
344   const OopMapSet* _set;
345   const OopMap* _empty;
346   const OopMap* _last;
347   int _empty_offset;
348   int _last_offset;
349   int _offset;
350   int _required;
351   Mapping* _mapping;
352   ImmutableOopMapSet* _new_set;
353 
354   /* Used for bookkeeping when building ImmutableOopMaps */
355   class Mapping : public ResourceObj {
356   public:
357     enum kind_t { OOPMAP_UNKNOWN = 0, OOPMAP_NEW = 1, OOPMAP_EMPTY = 2, OOPMAP_DUPLICATE = 3 };
358 
359     kind_t _kind;
360     int _offset;
361     int _size;
362     const OopMap* _map;
363     const OopMap* _other;
364 
365     Mapping() : _kind(OOPMAP_UNKNOWN), _offset(-1), _size(-1), _map(NULL) {}
366 
367     void set(kind_t kind, int offset, int size, const OopMap* map = 0, const OopMap* other = 0) {
368       _kind = kind;
369       _offset = offset;
370       _size = size;
371       _map = map;
372       _other = other;
373     }
374   };
375 
376 public:
377   ImmutableOopMapBuilder(const OopMapSet* set);
378 
379   int heap_size();
380   ImmutableOopMapSet* build();
381   ImmutableOopMapSet* generate_into(address buffer);
382 private:
383   bool is_empty(const OopMap* map) const {
384     return map->count() == 0;
385   }
386 
387   bool is_last_duplicate(const OopMap* map) {
388     if (_last != NULL && _last->count() > 0 && _last->equals(map)) {
389       return true;
390     }
391     return false;
392   }
393 
394 #ifdef ASSERT
395   void verify(address buffer, int size, const ImmutableOopMapSet* set);
396 #endif
397 
398   bool has_empty() const {
399     return _empty_offset != -1;
400   }
401 
402   int size_for(const OopMap* map) const;
403   void fill_pair(ImmutableOopMapPair* pair, const OopMap* map, int offset, const ImmutableOopMapSet* set);
404   int fill_map(ImmutableOopMapPair* pair, const OopMap* map, int offset, const ImmutableOopMapSet* set);
405   void fill(ImmutableOopMapSet* set, int size);
406 };
407 
408 
409 // Derived pointer support. This table keeps track of all derived points on a
410 // stack.  It is cleared before each scavenge/GC.  During the traversal of all
411 // oops, it is filled in with references to all locations that contains a
412 // derived oop (assumed to be very few).  When the GC is complete, the derived
413 // pointers are updated based on their base pointers new value and an offset.
414 #if COMPILER2_OR_JVMCI
415 class DerivedPointerTable : public AllStatic {
416   friend class VMStructs;
417  private:
418   class Entry;
419   static bool _active;                      // do not record pointers for verify pass etc.
420 
421  public:
422   static void clear();                       // Called before scavenge/GC
423   static void add(oop *derived, oop *base);  // Called during scavenge/GC
424   static void update_pointers();             // Called after  scavenge/GC
425   static bool is_empty();
426   static bool is_active()                    { return _active; }
427   static void set_active(bool value)         { _active = value; }
428 };
429 
430 // A utility class to temporarily "deactivate" the DerivedPointerTable.
431 // (Note: clients are responsible for any MT-safety issues)
432 class DerivedPointerTableDeactivate: public StackObj {
433  private:
434   bool _active;
435  public:
436   DerivedPointerTableDeactivate() {
437     _active = DerivedPointerTable::is_active();
438     if (_active) {
439       DerivedPointerTable::set_active(false);
440     }
441   }
442 
443   ~DerivedPointerTableDeactivate() {
444     assert(!DerivedPointerTable::is_active(),
445            "Inconsistency: not MT-safe");
446     if (_active) {
447       DerivedPointerTable::set_active(true);
448     }
449   }
450 };
451 #endif // COMPILER2_OR_JVMCI
452 
453 #endif // SHARE_COMPILER_OOPMAP_HPP