1 /*
  2  * Copyright (c) 1997, 2024, 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_CODE_DEBUGINFO_HPP
 26 #define SHARE_CODE_DEBUGINFO_HPP
 27 
 28 #include "code/compressedStream.hpp"
 29 #include "code/location.hpp"
 30 #include "code/nmethod.hpp"
 31 #include "code/oopRecorder.hpp"
 32 #include "runtime/javaThread.hpp"
 33 #include "utilities/growableArray.hpp"
 34 
 35 // Classes used for serializing debugging information.
 36 // These abstractions are introducted to provide symmetric
 37 // read and write operations.
 38 
 39 // ScopeValue        describes the value of a variable/expression in a scope
 40 // - LocationValue   describes a value in a given location (in frame or register)
 41 // - ConstantValue   describes a constant
 42 
 43 class ConstantOopReadValue;
 44 class ConstantOopWriteValue;
 45 class LocationValue;
 46 class ObjectValue;
 47 class ObjectMergeValue;
 48 
 49 class ScopeValue: public AnyObj {
 50  public:
 51   // Testers
 52   virtual bool is_location() const { return false; }
 53   virtual bool is_object() const { return false; }
 54   virtual bool is_object_merge() const { return false; }
 55   virtual bool is_auto_box() const { return false; }
 56   virtual bool is_marker() const { return false; }
 57   virtual bool is_constant_int() const { return false; }
 58   virtual bool is_constant_double() const { return false; }
 59   virtual bool is_constant_long() const { return false; }
 60   virtual bool is_constant_oop() const { return false; }
 61   virtual bool equals(ScopeValue* other) const { return false; }
 62 
 63   ConstantOopReadValue* as_ConstantOopReadValue() {
 64     assert(is_constant_oop(), "must be");
 65     return (ConstantOopReadValue*) this;
 66   }
 67 
 68   ConstantOopWriteValue* as_ConstantOopWriteValue() {
 69     assert(is_constant_oop(), "must be");
 70     return (ConstantOopWriteValue*) this;
 71   }
 72 
 73   ObjectValue* as_ObjectValue() {
 74     assert(is_object(), "must be");
 75     return (ObjectValue*)this;
 76   }
 77 
 78   ObjectMergeValue* as_ObjectMergeValue() {
 79     assert(is_object_merge(), "must be");
 80     return (ObjectMergeValue*)this;
 81   }
 82 
 83   LocationValue* as_LocationValue() {
 84     assert(is_location(), "must be");
 85     return (LocationValue*)this;
 86   }
 87 
 88   // Serialization of debugging information
 89   virtual void write_on(DebugInfoWriteStream* stream) = 0;
 90   static ScopeValue* read_from(DebugInfoReadStream* stream);
 91 };
 92 
 93 
 94 // A Location value describes a value in a given location; i.e. the corresponding
 95 // logical entity (e.g., a method temporary) lives in this location.
 96 
 97 class LocationValue: public ScopeValue {
 98  private:
 99   Location  _location;
100  public:
101   LocationValue(Location location)           { _location = location; }
102   bool      is_location() const              { return true; }
103   Location  location() const                 { return _location; }
104 
105   // Serialization of debugging information
106   LocationValue(DebugInfoReadStream* stream);
107   void write_on(DebugInfoWriteStream* stream);
108 
109   // Printing
110   void print_on(outputStream* st) const;
111 };
112 
113 // A placeholder value that has no concrete meaning other than helping constructing
114 // other values.
115 
116 class MarkerValue: public ScopeValue {
117 public:
118   bool      is_marker() const                { return true; }
119 
120   // Serialization of debugging information
121   void write_on(DebugInfoWriteStream* stream);
122 
123   // Printing
124   void print_on(outputStream* st) const;
125 };
126 
127 // An ObjectValue describes an object eliminated by escape analysis.
128 
129 class ObjectValue: public ScopeValue {
130  protected:
131   int                        _id;
132   ScopeValue*                _klass;

133   GrowableArray<ScopeValue*> _field_values;
134   Handle                     _value;
135   bool                       _visited;
136   bool                       _is_scalar_replaced;      // Whether this ObjectValue describes an object scalar replaced or just
137                                                        // an object (possibly null) participating in an allocation merge.
138   bool                       _is_root;   // Will be true if this object is referred to
139                                          // as a local/expression/monitor in the JVMs.
140                                          // Otherwise false, meaning it's just a candidate
141                                          // in an object allocation merge.
142  public:
143   ObjectValue(int id, ScopeValue* klass = nullptr, bool is_scalar_replaced = true)
144      : _id(id)
145      , _klass(klass)

146      , _field_values()
147      , _value()
148      , _visited(false)
149      , _is_scalar_replaced(is_scalar_replaced)
150      , _is_root(true) {
151     assert(klass == nullptr || klass->is_constant_oop(), "should be constant java mirror oop");
152   }
153 
154   // Accessors
155   bool                        is_object() const           { return true; }
156   int                         id() const                  { return _id; }
157   virtual ScopeValue*         klass() const               { return _klass; }

158   virtual GrowableArray<ScopeValue*>* field_values()      { return &_field_values; }
159   virtual ScopeValue*         field_at(int i) const       { return _field_values.at(i); }
160   virtual int                 field_size()                { return _field_values.length(); }
161   virtual Handle              value() const               { return _value; }
162   bool                        is_visited() const          { return _visited; }
163   bool                        is_scalar_replaced() const  { return _is_scalar_replaced; }
164   bool                        is_root() const             { return _is_root; }

165 
166   void                        set_id(int id)                   { _id = id; }
167   virtual void                set_value(oop value);
168   void                        set_visited(bool visited)        { _visited = visited; }
169   void                        set_is_scalar_replaced(bool scd) { _is_scalar_replaced = scd; }
170   void                        set_root(bool root)              { _is_root = root; }
171 
172   // Serialization of debugging information
173   void read_object(DebugInfoReadStream* stream);
174   void write_on(DebugInfoWriteStream* stream);
175 
176   // Printing
177   void print_on(outputStream* st) const;
178   void print_fields_on(outputStream* st) const;
179 };
180 
181 // An ObjectMergeValue describes objects that were inputs to a Phi in C2 and at
182 // least one of them was scalar replaced.
183 // '_selector' is an integer value that will be '-1' if during the execution of
184 // the C2 compiled code the path taken was that of the Phi input that was NOT
185 // scalar replaced. In that case '_merge_pointer' is a pointer to an already
186 // allocated object. If '_selector' is not '-1' then it should be the index of
187 // an object in '_possible_objects'. That object is an ObjectValue describing an
188 // object that was scalar replaced.
189 
190 class ObjectMergeValue: public ObjectValue {
191 protected:
192   ScopeValue*                _selector;
193   ScopeValue*                _merge_pointer;
194   GrowableArray<ScopeValue*> _possible_objects;
195 
196   // This holds the ObjectValue that should be used in place of this
197   // ObjectMergeValue. I.e., it's the ScopeValue from _possible_objects that was
198   // selected by 'select()' or is a on-the-fly created ScopeValue representing
199   // the _merge_pointer if _selector is -1.
200   //
201   // We need to keep this reference around because there will be entries in
202   // ScopeDesc that reference this ObjectMergeValue directly. After
203   // rematerialization ObjectMergeValue will be just a wrapper for the
204   // Objectvalue pointed by _selected.
205   ObjectValue*               _selected;
206 public:
207   ObjectMergeValue(int id, ScopeValue* merge_pointer, ScopeValue* selector)
208      : ObjectValue(id, nullptr, false)
209      , _selector(selector)
210      , _merge_pointer(merge_pointer)
211      , _possible_objects()
212      , _selected(nullptr) {}
213 
214   ObjectMergeValue(int id)
215      : ObjectValue(id, nullptr, false)
216      , _selector(nullptr)
217      , _merge_pointer(nullptr)
218      , _possible_objects()
219      , _selected(nullptr) {}
220 
221   bool                        is_object_merge() const         { return true; }
222   ScopeValue*                 selector() const                { return _selector; }
223   ScopeValue*                 merge_pointer() const           { return _merge_pointer; }
224   GrowableArray<ScopeValue*>* possible_objects()              { return &_possible_objects; }
225   ObjectValue*                select(frame& fr, RegisterMap& reg_map) ;
226 
227   ScopeValue*                 klass() const                   { ShouldNotReachHere(); return nullptr; }
228   GrowableArray<ScopeValue*>* field_values()                  { ShouldNotReachHere(); return nullptr; }
229   ScopeValue*                 field_at(int i) const           { ShouldNotReachHere(); return nullptr; }
230   int                         field_size()                    { ShouldNotReachHere(); return -1; }
231 
232   Handle                      value() const;
233   void                        set_value(oop value)            { assert(_selected != nullptr, "Should call select() first."); _selected->set_value(value); }
234 
235   // Serialization of debugging information
236   void read_object(DebugInfoReadStream* stream);
237   void write_on(DebugInfoWriteStream* stream);
238 };
239 
240 class AutoBoxObjectValue : public ObjectValue {
241   bool                       _cached;
242 public:
243   bool                       is_auto_box() const        { return true; }
244   bool                       is_cached() const          { return _cached; }
245   void                       set_cached(bool cached)    { _cached = cached; }
246   AutoBoxObjectValue(int id, ScopeValue* klass) : ObjectValue(id, klass), _cached(false) { }
247   AutoBoxObjectValue(int id) : ObjectValue(id), _cached(false) { }
248 };
249 
250 
251 // A ConstantIntValue describes a constant int; i.e., the corresponding logical entity
252 // is either a source constant or its computation has been constant-folded.
253 
254 class ConstantIntValue: public ScopeValue {
255  private:
256   jint _value;
257  public:
258   ConstantIntValue(jint value)         { _value = value; }
259   jint value() const                   { return _value;  }
260   bool is_constant_int() const         { return true;    }
261   bool equals(ScopeValue* other) const { return false;   }
262 
263   // Serialization of debugging information
264   ConstantIntValue(DebugInfoReadStream* stream);
265   void write_on(DebugInfoWriteStream* stream);
266 
267   // Printing
268   void print_on(outputStream* st) const;
269 };
270 
271 class ConstantLongValue: public ScopeValue {
272  private:
273   jlong _value;
274  public:
275   ConstantLongValue(jlong value)       { _value = value; }
276   jlong value() const                  { return _value;  }
277   bool is_constant_long() const        { return true;    }
278   bool equals(ScopeValue* other) const { return false;   }
279 
280   // Serialization of debugging information
281   ConstantLongValue(DebugInfoReadStream* stream);
282   void write_on(DebugInfoWriteStream* stream);
283 
284   // Printing
285   void print_on(outputStream* st) const;
286 };
287 
288 class ConstantDoubleValue: public ScopeValue {
289  private:
290   jdouble _value;
291  public:
292   ConstantDoubleValue(jdouble value)   { _value = value; }
293   jdouble value() const                { return _value;  }
294   bool is_constant_double() const      { return true;    }
295   bool equals(ScopeValue* other) const { return false;   }
296 
297   // Serialization of debugging information
298   ConstantDoubleValue(DebugInfoReadStream* stream);
299   void write_on(DebugInfoWriteStream* stream);
300 
301   // Printing
302   void print_on(outputStream* st) const;
303 };
304 
305 // A ConstantOopWriteValue is created by the compiler to
306 // be written as debugging information.
307 
308 class ConstantOopWriteValue: public ScopeValue {
309  private:
310   jobject _value;
311  public:
312   ConstantOopWriteValue(jobject value) { _value = value; }
313   jobject value() const                { return _value;  }
314   bool is_constant_oop() const         { return true;    }
315   bool equals(ScopeValue* other) const { return false;   }
316 
317   // Serialization of debugging information
318   void write_on(DebugInfoWriteStream* stream);
319 
320   // Printing
321   void print_on(outputStream* st) const;
322 };
323 
324 // A ConstantOopReadValue is created by the VM when reading
325 // debug information
326 
327 class ConstantOopReadValue: public ScopeValue {
328  private:
329   Handle _value;
330  public:
331   Handle value() const                 { return _value;  }
332   bool is_constant_oop() const         { return true;    }
333   bool equals(ScopeValue* other) const { return false;   }
334 
335   // Serialization of debugging information
336   ConstantOopReadValue(DebugInfoReadStream* stream);
337   void write_on(DebugInfoWriteStream* stream);
338 
339   // Printing
340   void print_on(outputStream* st) const;
341 };
342 
343 // MonitorValue describes the pair used for monitor_enter and monitor_exit.
344 
345 class MonitorValue: public ResourceObj {
346  private:
347   ScopeValue* _owner;
348   Location    _basic_lock;
349   bool        _eliminated;
350  public:
351   // Constructor
352   MonitorValue(ScopeValue* owner, Location basic_lock, bool eliminated = false);
353 
354   // Accessors
355   ScopeValue*  owner()      const { return _owner; }
356   Location     basic_lock() const { return _basic_lock;  }
357   bool         eliminated() const { return _eliminated; }
358 
359   // Serialization of debugging information
360   MonitorValue(DebugInfoReadStream* stream);
361   void write_on(DebugInfoWriteStream* stream);
362 
363   // Printing
364   void print_on(outputStream* st) const;
365 };
366 
367 // DebugInfoReadStream specializes CompressedReadStream for reading
368 // debugging information. Used by ScopeDesc.
369 
370 class DebugInfoReadStream : public CompressedReadStream {
371  private:
372   const nmethod* _code;
373   const nmethod* code() const { return _code; }
374   GrowableArray<ScopeValue*>* _obj_pool;
375  public:
376   DebugInfoReadStream(const nmethod* code, int offset, GrowableArray<ScopeValue*>* obj_pool = nullptr) :
377     CompressedReadStream(code->scopes_data_begin(), offset) {
378     _code = code;
379     _obj_pool = obj_pool;
380 
381   } ;
382 
383   oop read_oop();
384   Method* read_method() {
385     Method* o = (Method*)(code()->metadata_at(read_int()));
386     // is_metadata() is a faster check than is_metaspace_object()
387     assert(o == nullptr || o->is_metadata(), "meta data only");
388     return o;
389   }
390   ScopeValue* read_object_value(bool is_auto_box);
391   ScopeValue* read_object_merge_value();
392   ScopeValue* get_cached_object();
393   // BCI encoding is mostly unsigned, but -1 is a distinguished value
394   int read_bci() { return read_int() + InvocationEntryBci; }
395 };
396 
397 // DebugInfoWriteStream specializes CompressedWriteStream for
398 // writing debugging information. Used by ScopeDescRecorder.
399 
400 class DebugInfoWriteStream : public CompressedWriteStream {
401  private:
402   DebugInformationRecorder* _recorder;
403   DebugInformationRecorder* recorder() const { return _recorder; }
404  public:
405   DebugInfoWriteStream(DebugInformationRecorder* recorder, int initial_size);
406   void write_handle(jobject h);
407   void write_bci(int bci) { write_int(bci - InvocationEntryBci); }
408 
409   void write_metadata(Metadata* m);
410 };
411 
412 #endif // SHARE_CODE_DEBUGINFO_HPP
--- EOF ---