codeBlob.hpp revision 1472:c18cbe5936b8
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// CodeBlob - superclass for all entries in the CodeCache.
26//
27// Suptypes are:
28//   nmethod            : Compiled Java methods (include method that calls to native code)
29//   RuntimeStub        : Call to VM runtime methods
30//   DeoptimizationBlob : Used for deoptimizatation
31//   ExceptionBlob      : Used for stack unrolling
32//   SafepointBlob      : Used to handle illegal instruction exceptions
33//
34//
35// Layout:
36//   - header
37//   - relocation
38//   - instruction space
39//   - data space
40class DeoptimizationBlob;
41
42class CodeBlob VALUE_OBJ_CLASS_SPEC {
43
44  friend class VMStructs;
45
46 private:
47  const char* _name;
48  int        _size;                              // total size of CodeBlob in bytes
49  int        _header_size;                       // size of header (depends on subclass)
50  int        _relocation_size;                   // size of relocation
51  int        _instructions_offset;               // offset to where instructions region begins
52  int        _frame_complete_offset;             // instruction offsets in [0.._frame_complete_offset) have
53                                                 // not finished setting up their frame. Beware of pc's in
54                                                 // that range. There is a similar range(s) on returns
55                                                 // which we don't detect.
56  int        _data_offset;                       // offset to where data region begins
57  int        _oops_offset;                       // offset to where embedded oop table begins (inside data)
58  int        _oops_length;                       // number of embedded oops
59  int        _frame_size;                        // size of stack frame
60  OopMapSet* _oop_maps;                          // OopMap for this CodeBlob
61  CodeComments _comments;
62
63  friend class OopRecorder;
64
65  void fix_oop_relocations(address begin, address end, bool initialize_immediates);
66  inline void initialize_immediate_oop(oop* dest, jobject handle);
67
68 public:
69  // Returns the space needed for CodeBlob
70  static unsigned int allocation_size(CodeBuffer* cb, int header_size);
71
72  // Creation
73  // a) simple CodeBlob
74  // frame_complete is the offset from the beginning of the instructions
75  // to where the frame setup (from stackwalk viewpoint) is complete.
76  CodeBlob(const char* name, int header_size, int size, int frame_complete, int locs_size);
77
78  // b) full CodeBlob
79  CodeBlob(
80    const char* name,
81    CodeBuffer* cb,
82    int         header_size,
83    int         size,
84    int         frame_complete,
85    int         frame_size,
86    OopMapSet*  oop_maps
87  );
88
89  // Deletion
90  void flush();
91
92  // Typing
93  virtual bool is_buffer_blob() const                 { return false; }
94  virtual bool is_nmethod() const                     { return false; }
95  virtual bool is_runtime_stub() const                { return false; }
96  virtual bool is_deoptimization_stub() const         { return false; }
97  virtual bool is_uncommon_trap_stub() const          { return false; }
98  virtual bool is_exception_stub() const              { return false; }
99  virtual bool is_safepoint_stub() const              { return false; }
100  virtual bool is_adapter_blob() const                { return false; }
101  virtual bool is_method_handles_adapter_blob() const { return false; }
102
103  virtual bool is_compiled_by_c2() const         { return false; }
104  virtual bool is_compiled_by_c1() const         { return false; }
105
106  // Casting
107  nmethod* as_nmethod_or_null()                  { return is_nmethod() ? (nmethod*) this : NULL; }
108
109  // Boundaries
110  address    header_begin() const                { return (address)    this; }
111  address    header_end() const                  { return ((address)   this) + _header_size; };
112  relocInfo* relocation_begin() const            { return (relocInfo*) header_end(); };
113  relocInfo* relocation_end() const              { return (relocInfo*)(header_end()   + _relocation_size); }
114  address    instructions_begin() const          { return (address)    header_begin() + _instructions_offset;  }
115  address    instructions_end() const            { return (address)    header_begin() + _data_offset; }
116  address    data_begin() const                  { return (address)    header_begin() + _data_offset; }
117  address    data_end() const                    { return (address)    header_begin() + _size; }
118  oop*       oops_begin() const                  { return (oop*)      (header_begin() + _oops_offset); }
119  oop*       oops_end() const                    { return                oops_begin() + _oops_length; }
120
121  // Offsets
122  int relocation_offset() const                  { return _header_size; }
123  int instructions_offset() const                { return _instructions_offset; }
124  int data_offset() const                        { return _data_offset; }
125  int oops_offset() const                        { return _oops_offset; }
126
127  // Sizes
128  int size() const                               { return _size; }
129  int header_size() const                        { return _header_size; }
130  int relocation_size() const                    { return (address) relocation_end() - (address) relocation_begin(); }
131  int instructions_size() const                  { return instructions_end() - instructions_begin();  }
132  int data_size() const                          { return data_end() - data_begin(); }
133  int oops_size() const                          { return (address) oops_end() - (address) oops_begin(); }
134
135  // Containment
136  bool blob_contains(address addr) const         { return header_begin()       <= addr && addr < data_end(); }
137  bool relocation_contains(relocInfo* addr) const{ return relocation_begin()   <= addr && addr < relocation_end(); }
138  bool instructions_contains(address addr) const { return instructions_begin() <= addr && addr < instructions_end(); }
139  bool data_contains(address addr) const         { return data_begin()         <= addr && addr < data_end(); }
140  bool oops_contains(oop* addr) const            { return oops_begin()         <= addr && addr < oops_end(); }
141  bool contains(address addr) const              { return instructions_contains(addr); }
142  bool is_frame_complete_at(address addr) const  { return instructions_contains(addr) &&
143                                                          addr >= instructions_begin() + _frame_complete_offset; }
144
145  // Relocation support
146  void fix_oop_relocations(address begin, address end) {
147    fix_oop_relocations(begin, end, false);
148  }
149  void fix_oop_relocations() {
150    fix_oop_relocations(NULL, NULL, false);
151  }
152  relocInfo::relocType reloc_type_for_address(address pc);
153  bool is_at_poll_return(address pc);
154  bool is_at_poll_or_poll_return(address pc);
155
156  // Support for oops in scopes and relocs:
157  // Note: index 0 is reserved for null.
158  oop  oop_at(int index) const                   { return index == 0? (oop)NULL: *oop_addr_at(index); }
159  oop* oop_addr_at(int index) const{             // for GC
160    // relocation indexes are biased by 1 (because 0 is reserved)
161    assert(index > 0 && index <= _oops_length, "must be a valid non-zero index");
162    return &oops_begin()[index-1];
163  }
164
165  void copy_oops(GrowableArray<jobject>* oops);
166
167  // CodeCache support: really only used by the nmethods, but in order to get
168  // asserts and certain bookkeeping to work in the CodeCache they are defined
169  // virtual here.
170  virtual bool is_zombie() const                 { return false; }
171  virtual bool is_locked_by_vm() const           { return false; }
172
173  virtual bool is_unloaded() const               { return false; }
174  virtual bool is_not_entrant() const            { return false; }
175
176  // GC support
177  virtual bool is_alive() const                  = 0;
178  virtual void do_unloading(BoolObjectClosure* is_alive,
179                            OopClosure* keep_alive,
180                            bool unloading_occurred);
181  virtual void oops_do(OopClosure* f) = 0;
182  // (All CodeBlob subtypes other than NMethod currently have
183  // an empty oops_do() method.
184
185  // OopMap for frame
186  OopMapSet* oop_maps() const                    { return _oop_maps; }
187  void set_oop_maps(OopMapSet* p);
188  OopMap* oop_map_for_return_address(address return_address);
189  virtual void preserve_callee_argument_oops(frame fr, const RegisterMap* reg_map, OopClosure* f)  { ShouldNotReachHere(); }
190
191  // Frame support
192  int  frame_size() const                        { return _frame_size; }
193  void set_frame_size(int size)                  { _frame_size = size; }
194
195  // Returns true, if the next frame is responsible for GC'ing oops passed as arguments
196  virtual bool caller_must_gc_arguments(JavaThread* thread) const { return false; }
197
198  // Naming
199  const char* name() const                       { return _name; }
200  void set_name(const char* name)                { _name = name; }
201
202  // Debugging
203  virtual void verify();
204  virtual void print() const                     PRODUCT_RETURN;
205  virtual void print_value_on(outputStream* st) const PRODUCT_RETURN;
206
207  // Print the comment associated with offset on stream, if there is one
208  virtual void print_block_comment(outputStream* stream, address block_begin) {
209    intptr_t offset = (intptr_t)(block_begin - instructions_begin());
210    _comments.print_block_comment(stream, offset);
211  }
212
213  // Transfer ownership of comments to this CodeBlob
214  void set_comments(CodeComments& comments) {
215    _comments.assign(comments);
216  }
217};
218
219
220//----------------------------------------------------------------------------------------------------
221// BufferBlob: used to hold non-relocatable machine code such as the interpreter, stubroutines, etc.
222
223class BufferBlob: public CodeBlob {
224  friend class VMStructs;
225  friend class AdapterBlob;
226  friend class MethodHandlesAdapterBlob;
227
228 private:
229  // Creation support
230  BufferBlob(const char* name, int size);
231  BufferBlob(const char* name, int size, CodeBuffer* cb);
232
233  void* operator new(size_t s, unsigned size);
234
235 public:
236  // Creation
237  static BufferBlob* create(const char* name, int buffer_size);
238  static BufferBlob* create(const char* name, CodeBuffer* cb);
239
240  static void free(BufferBlob* buf);
241
242  // Typing
243  virtual bool is_buffer_blob() const            { return true; }
244
245  // GC/Verification support
246  void preserve_callee_argument_oops(frame fr, const RegisterMap* reg_map, OopClosure* f)  { /* nothing to do */ }
247  bool is_alive() const                          { return true; }
248  void do_unloading(BoolObjectClosure* is_alive,
249                    OopClosure* keep_alive,
250                    bool unloading_occurred)     { /* do nothing */ }
251
252  void oops_do(OopClosure* f)                    { /* do nothing*/ }
253
254  void verify();
255  void print() const                             PRODUCT_RETURN;
256  void print_value_on(outputStream* st) const    PRODUCT_RETURN;
257};
258
259
260//----------------------------------------------------------------------------------------------------
261// AdapterBlob: used to hold C2I/I2C adapters
262
263class AdapterBlob: public BufferBlob {
264private:
265  AdapterBlob(int size)                 : BufferBlob("I2C/C2I adapters", size) {}
266  AdapterBlob(int size, CodeBuffer* cb) : BufferBlob("I2C/C2I adapters", size, cb) {}
267
268public:
269  // Creation
270  static AdapterBlob* create(CodeBuffer* cb);
271
272  // Typing
273  virtual bool is_adapter_blob() const { return true; }
274};
275
276
277//----------------------------------------------------------------------------------------------------
278// MethodHandlesAdapterBlob: used to hold MethodHandles adapters
279
280class MethodHandlesAdapterBlob: public BufferBlob {
281private:
282  MethodHandlesAdapterBlob(int size)                 : BufferBlob("MethodHandles adapters", size) {}
283  MethodHandlesAdapterBlob(int size, CodeBuffer* cb) : BufferBlob("MethodHandles adapters", size, cb) {}
284
285public:
286  // Creation
287  static MethodHandlesAdapterBlob* create(int buffer_size);
288
289  // Typing
290  virtual bool is_method_handles_adapter_blob() const { return true; }
291};
292
293
294//----------------------------------------------------------------------------------------------------
295// RuntimeStub: describes stubs used by compiled code to call a (static) C++ runtime routine
296
297class RuntimeStub: public CodeBlob {
298  friend class VMStructs;
299 private:
300  bool        _caller_must_gc_arguments;
301
302  // Creation support
303  RuntimeStub(
304    const char* name,
305    CodeBuffer* cb,
306    int         size,
307    int         frame_complete,
308    int         frame_size,
309    OopMapSet*  oop_maps,
310    bool        caller_must_gc_arguments
311  );
312
313  void* operator new(size_t s, unsigned size);
314
315 public:
316  // Creation
317  static RuntimeStub* new_runtime_stub(
318    const char* stub_name,
319    CodeBuffer* cb,
320    int         frame_complete,
321    int         frame_size,
322    OopMapSet*  oop_maps,
323    bool        caller_must_gc_arguments
324  );
325
326  // Typing
327  bool is_runtime_stub() const                   { return true; }
328
329  // GC support
330  bool caller_must_gc_arguments(JavaThread* thread) const { return _caller_must_gc_arguments; }
331
332  address entry_point()                          { return instructions_begin(); }
333
334  // GC/Verification support
335  void preserve_callee_argument_oops(frame fr, const RegisterMap *reg_map, OopClosure* f)  { /* nothing to do */ }
336  bool is_alive() const                          { return true; }
337  void do_unloading(BoolObjectClosure* is_alive,
338                    OopClosure* keep_alive,
339                    bool unloading_occurred)     { /* do nothing */ }
340  void oops_do(OopClosure* f) { /* do-nothing*/ }
341
342  void verify();
343  void print() const                             PRODUCT_RETURN;
344  void print_value_on(outputStream* st) const    PRODUCT_RETURN;
345};
346
347
348//----------------------------------------------------------------------------------------------------
349// Super-class for all blobs that exist in only one instance. Implements default behaviour.
350
351class SingletonBlob: public CodeBlob {
352  friend class VMStructs;
353  public:
354   SingletonBlob(
355     const char* name,
356     CodeBuffer* cb,
357     int         header_size,
358     int         size,
359     int         frame_size,
360     OopMapSet*  oop_maps
361   )
362   : CodeBlob(name, cb, header_size, size, CodeOffsets::frame_never_safe, frame_size, oop_maps)
363   {};
364
365   bool is_alive() const                         { return true; }
366   void do_unloading(BoolObjectClosure* is_alive,
367                     OopClosure* keep_alive,
368                     bool unloading_occurred)    { /* do-nothing*/ }
369
370   void verify(); // does nothing
371   void print() const                            PRODUCT_RETURN;
372   void print_value_on(outputStream* st) const   PRODUCT_RETURN;
373};
374
375
376//----------------------------------------------------------------------------------------------------
377// DeoptimizationBlob
378
379class DeoptimizationBlob: public SingletonBlob {
380  friend class VMStructs;
381 private:
382  int _unpack_offset;
383  int _unpack_with_exception;
384  int _unpack_with_reexecution;
385
386  int _unpack_with_exception_in_tls;
387
388  // Creation support
389  DeoptimizationBlob(
390    CodeBuffer* cb,
391    int         size,
392    OopMapSet*  oop_maps,
393    int         unpack_offset,
394    int         unpack_with_exception_offset,
395    int         unpack_with_reexecution_offset,
396    int         frame_size
397  );
398
399  void* operator new(size_t s, unsigned size);
400
401 public:
402  // Creation
403  static DeoptimizationBlob* create(
404    CodeBuffer* cb,
405    OopMapSet*  oop_maps,
406    int         unpack_offset,
407    int         unpack_with_exception_offset,
408    int         unpack_with_reexecution_offset,
409    int         frame_size
410  );
411
412  // Typing
413  bool is_deoptimization_stub() const { return true; }
414  const DeoptimizationBlob *as_deoptimization_stub() const { return this; }
415  bool exception_address_is_unpack_entry(address pc) const {
416    address unpack_pc = unpack();
417    return (pc == unpack_pc || (pc + frame::pc_return_offset) == unpack_pc);
418  }
419
420
421
422
423  // GC for args
424  void preserve_callee_argument_oops(frame fr, const RegisterMap *reg_map, OopClosure* f) { /* Nothing to do */ }
425
426  // Iteration
427  void oops_do(OopClosure* f) {}
428
429  // Printing
430  void print_value_on(outputStream* st) const PRODUCT_RETURN;
431
432  address unpack() const                         { return instructions_begin() + _unpack_offset;           }
433  address unpack_with_exception() const          { return instructions_begin() + _unpack_with_exception;   }
434  address unpack_with_reexecution() const        { return instructions_begin() + _unpack_with_reexecution; }
435
436  // Alternate entry point for C1 where the exception and issuing pc
437  // are in JavaThread::_exception_oop and JavaThread::_exception_pc
438  // instead of being in registers.  This is needed because C1 doesn't
439  // model exception paths in a way that keeps these registers free so
440  // there may be live values in those registers during deopt.
441  void set_unpack_with_exception_in_tls_offset(int offset) {
442    _unpack_with_exception_in_tls = offset;
443    assert(contains(instructions_begin() + _unpack_with_exception_in_tls), "must be PC inside codeblob");
444  }
445  address unpack_with_exception_in_tls() const   { return instructions_begin() + _unpack_with_exception_in_tls;   }
446};
447
448
449//----------------------------------------------------------------------------------------------------
450// UncommonTrapBlob (currently only used by Compiler 2)
451
452#ifdef COMPILER2
453
454class UncommonTrapBlob: public SingletonBlob {
455  friend class VMStructs;
456 private:
457  // Creation support
458  UncommonTrapBlob(
459    CodeBuffer* cb,
460    int         size,
461    OopMapSet*  oop_maps,
462    int         frame_size
463  );
464
465  void* operator new(size_t s, unsigned size);
466
467 public:
468  // Creation
469  static UncommonTrapBlob* create(
470    CodeBuffer* cb,
471    OopMapSet*  oop_maps,
472    int         frame_size
473  );
474
475  // GC for args
476  void preserve_callee_argument_oops(frame fr, const RegisterMap *reg_map, OopClosure* f)  { /* nothing to do */ }
477
478  // Typing
479  bool is_uncommon_trap_stub() const             { return true; }
480
481  // Iteration
482  void oops_do(OopClosure* f) {}
483};
484
485
486//----------------------------------------------------------------------------------------------------
487// ExceptionBlob: used for exception unwinding in compiled code (currently only used by Compiler 2)
488
489class ExceptionBlob: public SingletonBlob {
490  friend class VMStructs;
491 private:
492  // Creation support
493  ExceptionBlob(
494    CodeBuffer* cb,
495    int         size,
496    OopMapSet*  oop_maps,
497    int         frame_size
498  );
499
500  void* operator new(size_t s, unsigned size);
501
502 public:
503  // Creation
504  static ExceptionBlob* create(
505    CodeBuffer* cb,
506    OopMapSet*  oop_maps,
507    int         frame_size
508  );
509
510  // GC for args
511  void preserve_callee_argument_oops(frame fr, const RegisterMap* reg_map, OopClosure* f)  { /* nothing to do */ }
512
513  // Typing
514  bool is_exception_stub() const                 { return true; }
515
516  // Iteration
517  void oops_do(OopClosure* f) {}
518};
519#endif // COMPILER2
520
521
522//----------------------------------------------------------------------------------------------------
523// SafepointBlob: handles illegal_instruction exceptions during a safepoint
524
525class SafepointBlob: public SingletonBlob {
526  friend class VMStructs;
527 private:
528  // Creation support
529  SafepointBlob(
530    CodeBuffer* cb,
531    int         size,
532    OopMapSet*  oop_maps,
533    int         frame_size
534  );
535
536  void* operator new(size_t s, unsigned size);
537
538 public:
539  // Creation
540  static SafepointBlob* create(
541    CodeBuffer* cb,
542    OopMapSet*  oop_maps,
543    int         frame_size
544  );
545
546  // GC for args
547  void preserve_callee_argument_oops(frame fr, const RegisterMap* reg_map, OopClosure* f)  { /* nothing to do */ }
548
549  // Typing
550  bool is_safepoint_stub() const                 { return true; }
551
552  // Iteration
553  void oops_do(OopClosure* f) {}
554};
555