codeBlob.cpp revision 1564:2a47bd84841f
11590Srgrimes/*
21590Srgrimes * Copyright (c) 1998, 2010, Oracle and/or its affiliates. All rights reserved.
31590Srgrimes * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
41590Srgrimes *
51590Srgrimes * This code is free software; you can redistribute it and/or modify it
61590Srgrimes * under the terms of the GNU General Public License version 2 only, as
71590Srgrimes * published by the Free Software Foundation.
81590Srgrimes *
91590Srgrimes * This code is distributed in the hope that it will be useful, but WITHOUT
101590Srgrimes * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
111590Srgrimes * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
121590Srgrimes * version 2 for more details (a copy is included in the LICENSE file that
131590Srgrimes * accompanied this code).
141590Srgrimes *
151590Srgrimes * You should have received a copy of the GNU General Public License version
161590Srgrimes * 2 along with this work; if not, write to the Free Software Foundation,
171590Srgrimes * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
181590Srgrimes *
191590Srgrimes * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
201590Srgrimes * or visit www.oracle.com if you need additional information or have any
211590Srgrimes * questions.
221590Srgrimes *
231590Srgrimes */
241590Srgrimes
251590Srgrimes# include "incls/_precompiled.incl"
261590Srgrimes# include "incls/_codeBlob.cpp.incl"
271590Srgrimes
281590Srgrimesunsigned int align_code_offset(int offset) {
291590Srgrimes  // align the size to CodeEntryAlignment
301590Srgrimes  return
311590Srgrimes    ((offset + (int)CodeHeap::header_size() + (CodeEntryAlignment-1)) & ~(CodeEntryAlignment-1))
321590Srgrimes    - (int)CodeHeap::header_size();
331590Srgrimes}
341590Srgrimes
3528693Scharnier
361590Srgrimes// This must be consistent with the CodeBlob constructor's layout actions.
371590Srgrimesunsigned int CodeBlob::allocation_size(CodeBuffer* cb, int header_size) {
38123441Sbde  unsigned int size = header_size;
391590Srgrimes  size += round_to(cb->total_relocation_size(), oopSize);
40123441Sbde  // align the size to CodeEntryAlignment
411590Srgrimes  size = align_code_offset(size);
42123441Sbde  size += round_to(cb->total_code_size(), oopSize);
43123441Sbde  size += round_to(cb->total_oop_size(), oopSize);
4428693Scharnier  return size;
451590Srgrimes}
46123441Sbde
47123441Sbde
48123441Sbde// Creates a simple CodeBlob. Sets up the size of the different regions.
491590SrgrimesCodeBlob::CodeBlob(const char* name, int header_size, int size, int frame_complete, int locs_size) {
501590Srgrimes  assert(size == round_to(size, oopSize), "unaligned size");
511590Srgrimes  assert(locs_size == round_to(locs_size, oopSize), "unaligned size");
5212811Sbde  assert(header_size == round_to(header_size, oopSize), "unaligned size");
531590Srgrimes  assert(!UseRelocIndex, "no space allocated for reloc index yet");
541590Srgrimes
551590Srgrimes  // Note: If UseRelocIndex is enabled, there needs to be (at least) one
561590Srgrimes  //       extra word for the relocation information, containing the reloc
571590Srgrimes  //       index table length. Unfortunately, the reloc index table imple-
58111008Sphk  //       mentation is not easily understandable and thus it is not clear
591590Srgrimes  //       what exactly the format is supposed to be. For now, we just turn
6012804Speter  //       off the use of this table (gri 7/6/2000).
61198620Sjhb
6212811Sbde  _name                  = name;
6312811Sbde  _size                  = size;
6412811Sbde  _frame_complete_offset = frame_complete;
6528693Scharnier  _header_size           = header_size;
6687690Smarkm  _relocation_size       = locs_size;
6728693Scharnier  _instructions_offset   = align_code_offset(header_size + locs_size);
6828693Scharnier  _data_offset           = size;
6928693Scharnier  _frame_size            =  0;
7028693Scharnier  set_oop_maps(NULL);
71148413Srwatson}
721590Srgrimes
7328693Scharnier
741590Srgrimes// Creates a CodeBlob from a CodeBuffer. Sets up the size of the different regions,
751590Srgrimes// and copy code and relocation info.
761590SrgrimesCodeBlob::CodeBlob(
7730180Sdima  const char* name,
7828693Scharnier  CodeBuffer* cb,
7928693Scharnier  int         header_size,
80174573Speter  int         size,
811590Srgrimes  int         frame_complete,
8287690Smarkm  int         frame_size,
8387690Smarkm  OopMapSet*  oop_maps
8487690Smarkm) {
85181881Sjhb  assert(size == round_to(size, oopSize), "unaligned size");
861590Srgrimes  assert(header_size == round_to(header_size, oopSize), "unaligned size");
87181881Sjhb
881590Srgrimes  _name                  = name;
89181881Sjhb  _size                  = size;
901590Srgrimes  _frame_complete_offset = frame_complete;
91181881Sjhb  _header_size           = header_size;
921590Srgrimes  _relocation_size       = round_to(cb->total_relocation_size(), oopSize);
93181881Sjhb  _instructions_offset   = align_code_offset(header_size + _relocation_size);
941590Srgrimes  _data_offset           = _instructions_offset + round_to(cb->total_code_size(), oopSize);
95181881Sjhb  assert(_data_offset <= size, "codeBlob is too small");
961590Srgrimes
97181881Sjhb  cb->copy_code_and_locs_to(this);
981590Srgrimes  set_oop_maps(oop_maps);
99181881Sjhb  _frame_size = frame_size;
1001590Srgrimes#ifdef COMPILER1
101181881Sjhb  // probably wrong for tiered
1021590Srgrimes  assert(_frame_size >= -1, "must use frame size or -1 for runtime stubs");
103181881Sjhb#endif // COMPILER1
104131300Sgreen}
105181881Sjhb
106131300Sgreen
10730180Sdimavoid CodeBlob::set_oop_maps(OopMapSet* p) {
108131300Sgreen  // Danger Will Robinson! This method allocates a big
1091590Srgrimes  // chunk of memory, its your job to free it.
110131300Sgreen  if (p != NULL) {
1111590Srgrimes    // We need to allocate a chunk big enough to hold the OopMapSet and all of its OopMaps
112131300Sgreen    _oop_maps = (OopMapSet* )NEW_C_HEAP_ARRAY(unsigned char, p->heap_size());
1131590Srgrimes    p->copy_to((address)_oop_maps);
114131300Sgreen  } else {
1151590Srgrimes    _oop_maps = NULL;
116131300Sgreen  }
11792653Sjeff}
118181881Sjhb
1191590Srgrimes
1201590Srgrimesvoid CodeBlob::flush() {
1211590Srgrimes  if (_oop_maps) {
1221590Srgrimes    FREE_C_HEAP_ARRAY(unsigned char, _oop_maps);
123123250Sdes    _oop_maps = NULL;
124123250Sdes  }
125123250Sdes  _comments.free();
126123250Sdes}
127123250Sdes
128123250Sdes
129123250SdesOopMap* CodeBlob::oop_map_for_return_address(address return_address) {
130123250Sdes  address pc = return_address ;
131123250Sdes  assert (oop_maps() != NULL, "nope");
132123250Sdes  return oop_maps()->find_map_at_offset ((intptr_t) pc - (intptr_t) instructions_begin());
133123250Sdes}
1341590Srgrimes
135123250Sdes
1361590Srgrimes//----------------------------------------------------------------------------------------------------
137184645Skeramida// Implementation of BufferBlob
138184645Skeramida
139184645Skeramida
140184645SkeramidaBufferBlob::BufferBlob(const char* name, int size)
141123250Sdes: CodeBlob(name, sizeof(BufferBlob), size, CodeOffsets::frame_never_safe, /*locs_size:*/ 0)
142123250Sdes{}
143174573Speter
144174573SpeterBufferBlob* BufferBlob::create(const char* name, int buffer_size) {
1451590Srgrimes  ThreadInVMfromUnknown __tiv;  // get to VM state in case we block on CodeCache_lock
146123250Sdes
1471590Srgrimes  BufferBlob* blob = NULL;
1481590Srgrimes  unsigned int size = sizeof(BufferBlob);
1491590Srgrimes  // align the size to CodeEntryAlignment
1501590Srgrimes  size = align_code_offset(size);
1511590Srgrimes  size += round_to(buffer_size, oopSize);
1521590Srgrimes  assert(name != NULL, "must provide a name");
1531590Srgrimes  {
15443962Sdillon    MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
1551590Srgrimes    blob = new (size) BufferBlob(name, size);
15692922Simp  }
157174573Speter  // Track memory usage statistic after releasing CodeCache_lock
15892922Simp  MemoryService::track_code_cache_memory_usage();
159122300Sjmg
16092922Simp  return blob;
16192922Simp}
162123407Sdes
163148790Srwatson
164148630SrwatsonBufferBlob::BufferBlob(const char* name, int size, CodeBuffer* cb)
16592922Simp  : CodeBlob(name, cb, sizeof(BufferBlob), size, CodeOffsets::frame_never_safe, 0, NULL)
166131300Sgreen{}
167131300Sgreen
16892922SimpBufferBlob* BufferBlob::create(const char* name, CodeBuffer* cb) {
169184645Skeramida  ThreadInVMfromUnknown __tiv;  // get to VM state in case we block on CodeCache_lock
170184645Skeramida
171174573Speter  BufferBlob* blob = NULL;
17292922Simp  unsigned int size = allocation_size(cb, sizeof(BufferBlob));
1731590Srgrimes  assert(name != NULL, "must provide a name");
17492922Simp  {
17592922Simp    MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
17687690Smarkm    blob = new (size) BufferBlob(name, size, cb);
177123250Sdes  }
17887690Smarkm  // Track memory usage statistic after releasing CodeCache_lock
17928693Scharnier  MemoryService::track_code_cache_memory_usage();
180123250Sdes
1811590Srgrimes  return blob;
18287690Smarkm}
183123407Sdes
1841590Srgrimes
1851590Srgrimesvoid* BufferBlob::operator new(size_t s, unsigned size) {
18678474Sschweikh  void* p = CodeCache::allocate(size);
1871590Srgrimes  return p;
1881590Srgrimes}
1891590Srgrimes
19043822Sken
191174573Spetervoid BufferBlob::free( BufferBlob *blob ) {
192174573Speter  ThreadInVMfromUnknown __tiv;  // get to VM state in case we block on CodeCache_lock
1931590Srgrimes  {
194123250Sdes    MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
195123250Sdes    CodeCache::free((CodeBlob*)blob);
196123250Sdes  }
1971590Srgrimes  // Track memory usage statistic after releasing CodeCache_lock
1981590Srgrimes  MemoryService::track_code_cache_memory_usage();
1991590Srgrimes}
200174573Speter
201174573Speter
202174573Speter//----------------------------------------------------------------------------------------------------
2031590Srgrimes// Implementation of AdapterBlob
204113460Stjr
2051590SrgrimesAdapterBlob* AdapterBlob::create(CodeBuffer* cb) {
206174573Speter  ThreadInVMfromUnknown __tiv;  // get to VM state in case we block on CodeCache_lock
207174573Speter
208174573Speter  AdapterBlob* blob = NULL;
209174573Speter  unsigned int size = allocation_size(cb, sizeof(AdapterBlob));
210174573Speter  {
211174573Speter    MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
2121590Srgrimes    blob = new (size) AdapterBlob(size, cb);
2131590Srgrimes    CodeCache::commit(blob);
2141590Srgrimes  }
2151590Srgrimes  // Track memory usage statistic after releasing CodeCache_lock
2161590Srgrimes  MemoryService::track_code_cache_memory_usage();
2171590Srgrimes
2181590Srgrimes  return blob;
2191590Srgrimes}
2201590Srgrimes
2211590Srgrimes
2221590Srgrimes//----------------------------------------------------------------------------------------------------
2231590Srgrimes// Implementation of MethodHandlesAdapterBlob
22439230Sgibbs
22539372SdillonMethodHandlesAdapterBlob* MethodHandlesAdapterBlob::create(int buffer_size) {
22639230Sgibbs  ThreadInVMfromUnknown __tiv;  // get to VM state in case we block on CodeCache_lock
22739230Sgibbs
22839230Sgibbs  MethodHandlesAdapterBlob* blob = NULL;
22939230Sgibbs  unsigned int size = sizeof(MethodHandlesAdapterBlob);
23039230Sgibbs  // align the size to CodeEntryAlignment
23139230Sgibbs  size = align_code_offset(size);
232112284Sphk  size += round_to(buffer_size, oopSize);
23339230Sgibbs  {
23439230Sgibbs    MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
2351590Srgrimes    blob = new (size) MethodHandlesAdapterBlob(size);
2361590Srgrimes  }
2371590Srgrimes  // Track memory usage statistic after releasing CodeCache_lock
2381590Srgrimes  MemoryService::track_code_cache_memory_usage();
23930180Sdima
2401590Srgrimes  return blob;
24130180Sdima}
24230180Sdima
24330180Sdima
2441590Srgrimes//----------------------------------------------------------------------------------------------------
2451590Srgrimes// Implementation of RuntimeStub
2461590Srgrimes
2471590SrgrimesRuntimeStub::RuntimeStub(
24844067Sbde  const char* name,
24944067Sbde  CodeBuffer* cb,
25044067Sbde  int         size,
2511590Srgrimes  int         frame_complete,
2521590Srgrimes  int         frame_size,
2531590Srgrimes  OopMapSet*  oop_maps,
2541590Srgrimes  bool        caller_must_gc_arguments
2551590Srgrimes)
2561590Srgrimes: CodeBlob(name, cb, sizeof(RuntimeStub), size, frame_complete, frame_size, oop_maps)
2571590Srgrimes{
2581590Srgrimes  _caller_must_gc_arguments = caller_must_gc_arguments;
2591590Srgrimes}
2601590Srgrimes
2611590Srgrimes
262123250SdesRuntimeStub* RuntimeStub::new_runtime_stub(const char* stub_name,
263123250Sdes                                           CodeBuffer* cb,
264123250Sdes                                           int frame_complete,
265123250Sdes                                           int frame_size,
266123250Sdes                                           OopMapSet* oop_maps,
2671590Srgrimes                                           bool caller_must_gc_arguments)
268123250Sdes{
2691590Srgrimes  RuntimeStub* stub = NULL;
27028693Scharnier  ThreadInVMfromUnknown __tiv;  // get to VM state in case we block on CodeCache_lock
2711590Srgrimes  {
272123250Sdes    MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
273123250Sdes    unsigned int size = allocation_size(cb, sizeof(RuntimeStub));
2741590Srgrimes    stub = new (size) RuntimeStub(stub_name, cb, size, frame_complete, frame_size, oop_maps, caller_must_gc_arguments);
27581537Sken  }
2761590Srgrimes
2771590Srgrimes  // Do not hold the CodeCache lock during name formatting.
2781590Srgrimes  if (stub != NULL) {
27928693Scharnier    char stub_id[256];
2801590Srgrimes    jio_snprintf(stub_id, sizeof(stub_id), "RuntimeStub - %s", stub_name);
2811590Srgrimes    if (PrintStubCode) {
282174573Speter      tty->print_cr("Decoding %s " INTPTR_FORMAT, stub_id, stub);
283174573Speter      Disassembler::decode(stub->instructions_begin(), stub->instructions_end());
2841590Srgrimes    }
2851590Srgrimes    Forte::register_stub(stub_id, stub->instructions_begin(), stub->instructions_end());
28643819Sken
28743819Sken    if (JvmtiExport::should_post_dynamic_code_generated()) {
28843819Sken      JvmtiExport::post_dynamic_code_generated(stub_name, stub->instructions_begin(), stub->instructions_end());
28943819Sken    }
29043819Sken  }
291112284Sphk
29243819Sken  // Track memory usage statistic after releasing CodeCache_lock
29343819Sken  MemoryService::track_code_cache_memory_usage();
29443819Sken
2951590Srgrimes  return stub;
2961590Srgrimes}
2971590Srgrimes
2981590Srgrimes
2991590Srgrimesvoid* RuntimeStub::operator new(size_t s, unsigned size) {
3001590Srgrimes  void* p = CodeCache::allocate(size);
3011590Srgrimes  if (!p) fatal("Initial size of CodeCache is too small");
3021590Srgrimes  return p;
3031590Srgrimes}
3041590Srgrimes
3051590Srgrimes
3061590Srgrimes//----------------------------------------------------------------------------------------------------
3071590Srgrimes// Implementation of DeoptimizationBlob
3081590Srgrimes
3091590SrgrimesDeoptimizationBlob::DeoptimizationBlob(
3101590Srgrimes  CodeBuffer* cb,
3111590Srgrimes  int         size,
3121590Srgrimes  OopMapSet*  oop_maps,
3131590Srgrimes  int         unpack_offset,
3141590Srgrimes  int         unpack_with_exception_offset,
3151590Srgrimes  int         unpack_with_reexecution_offset,
316148790Srwatson  int         frame_size
31743962Sdillon)
318148630Srwatson: SingletonBlob("DeoptimizationBlob", cb, sizeof(DeoptimizationBlob), size, frame_size, oop_maps)
3191590Srgrimes{
3201590Srgrimes  _unpack_offset           = unpack_offset;
32130180Sdima  _unpack_with_exception   = unpack_with_exception_offset;
3221590Srgrimes  _unpack_with_reexecution = unpack_with_reexecution_offset;
3231590Srgrimes#ifdef COMPILER1
3241590Srgrimes  _unpack_with_exception_in_tls   = -1;
3251590Srgrimes#endif
3261590Srgrimes}
3271590Srgrimes
3281590Srgrimes
3291590SrgrimesDeoptimizationBlob* DeoptimizationBlob::create(
3301590Srgrimes  CodeBuffer* cb,
3311590Srgrimes  OopMapSet*  oop_maps,
332123250Sdes  int        unpack_offset,
333123250Sdes  int        unpack_with_exception_offset,
334123250Sdes  int        unpack_with_reexecution_offset,
3351590Srgrimes  int        frame_size)
336123250Sdes{
337123250Sdes  DeoptimizationBlob* blob = NULL;
338123250Sdes  ThreadInVMfromUnknown __tiv;  // get to VM state in case we block on CodeCache_lock
339123250Sdes  {
340123250Sdes    MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
341123250Sdes    unsigned int size = allocation_size(cb, sizeof(DeoptimizationBlob));
342123250Sdes    blob = new (size) DeoptimizationBlob(cb,
343123250Sdes                                         size,
344123250Sdes                                         oop_maps,
345123250Sdes                                         unpack_offset,
346123250Sdes                                         unpack_with_exception_offset,
347112284Sphk                                         unpack_with_reexecution_offset,
34839230Sgibbs                                         frame_size);
3491590Srgrimes  }
350188888Sdelphij
351188888Sdelphij  // Do not hold the CodeCache lock during name formatting.
35239230Sgibbs  if (blob != NULL) {
353112284Sphk    char blob_id[256];
35439230Sgibbs    jio_snprintf(blob_id, sizeof(blob_id), "DeoptimizationBlob@" PTR_FORMAT, blob->instructions_begin());
35539230Sgibbs    if (PrintStubCode) {
35639230Sgibbs      tty->print_cr("Decoding %s " INTPTR_FORMAT, blob_id, blob);
35739230Sgibbs      Disassembler::decode(blob->instructions_begin(), blob->instructions_end());
35839230Sgibbs    }
35939230Sgibbs    Forte::register_stub(blob_id, blob->instructions_begin(), blob->instructions_end());
36039230Sgibbs
3611590Srgrimes    if (JvmtiExport::should_post_dynamic_code_generated()) {
3621590Srgrimes      JvmtiExport::post_dynamic_code_generated("DeoptimizationBlob",
36339230Sgibbs                                               blob->instructions_begin(),
36439230Sgibbs                                               blob->instructions_end());
36539230Sgibbs    }
36639230Sgibbs  }
36739230Sgibbs
3681590Srgrimes  // Track memory usage statistic after releasing CodeCache_lock
36939230Sgibbs  MemoryService::track_code_cache_memory_usage();
37039230Sgibbs
37139372Sdillon  return blob;
37239372Sdillon}
37339372Sdillon
37439230Sgibbs
37539230Sgibbsvoid* DeoptimizationBlob::operator new(size_t s, unsigned size) {
37639230Sgibbs  void* p = CodeCache::allocate(size);
37739230Sgibbs  if (!p) fatal("Initial size of CodeCache is too small");
37839230Sgibbs  return p;
37939230Sgibbs}
38039230Sgibbs
38139230Sgibbs//----------------------------------------------------------------------------------------------------
38239230Sgibbs// Implementation of UncommonTrapBlob
38339230Sgibbs
38439230Sgibbs#ifdef COMPILER2
385112284SphkUncommonTrapBlob::UncommonTrapBlob(
38639230Sgibbs  CodeBuffer* cb,
38739230Sgibbs  int         size,
38839230Sgibbs  OopMapSet*  oop_maps,
38939230Sgibbs  int         frame_size
39039230Sgibbs)
39139230Sgibbs: SingletonBlob("UncommonTrapBlob", cb, sizeof(UncommonTrapBlob), size, frame_size, oop_maps)
39239230Sgibbs{}
39339230Sgibbs
39439230Sgibbs
39539230SgibbsUncommonTrapBlob* UncommonTrapBlob::create(
39639230Sgibbs  CodeBuffer* cb,
397112284Sphk  OopMapSet*  oop_maps,
39839230Sgibbs  int        frame_size)
39939230Sgibbs{
40039230Sgibbs  UncommonTrapBlob* blob = NULL;
40139230Sgibbs  ThreadInVMfromUnknown __tiv;  // get to VM state in case we block on CodeCache_lock
40239230Sgibbs  {
40339230Sgibbs    MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
4041590Srgrimes    unsigned int size = allocation_size(cb, sizeof(UncommonTrapBlob));
4051590Srgrimes    blob = new (size) UncommonTrapBlob(cb, size, oop_maps, frame_size);
4061590Srgrimes  }
407123250Sdes
408123250Sdes  // Do not hold the CodeCache lock during name formatting.
4091590Srgrimes  if (blob != NULL) {
410151417Sandre    char blob_id[256];
4111590Srgrimes    jio_snprintf(blob_id, sizeof(blob_id), "UncommonTrapBlob@" PTR_FORMAT, blob->instructions_begin());
4121590Srgrimes    if (PrintStubCode) {
413151417Sandre      tty->print_cr("Decoding %s " INTPTR_FORMAT, blob_id, blob);
414151417Sandre      Disassembler::decode(blob->instructions_begin(), blob->instructions_end());
41528693Scharnier    }
41628693Scharnier    Forte::register_stub(blob_id, blob->instructions_begin(), blob->instructions_end());
417151417Sandre
4181590Srgrimes    if (JvmtiExport::should_post_dynamic_code_generated()) {
4191590Srgrimes      JvmtiExport::post_dynamic_code_generated("UncommonTrapBlob",
4201590Srgrimes                                               blob->instructions_begin(),
421123250Sdes                                               blob->instructions_end());
422198620Sjhb    }
423198620Sjhb  }
424198620Sjhb
425198620Sjhb  // Track memory usage statistic after releasing CodeCache_lock
426198620Sjhb  MemoryService::track_code_cache_memory_usage();
427198620Sjhb
428198620Sjhb  return blob;
429198620Sjhb}
430198620Sjhb
431198620Sjhb
432198620Sjhbvoid* UncommonTrapBlob::operator new(size_t s, unsigned size) {
433198620Sjhb  void* p = CodeCache::allocate(size);
434198620Sjhb  if (!p) fatal("Initial size of CodeCache is too small");
435198620Sjhb  return p;
436198620Sjhb}
437198620Sjhb#endif // COMPILER2
438198620Sjhb
439198620Sjhb
440198620Sjhb//----------------------------------------------------------------------------------------------------
441198620Sjhb// Implementation of ExceptionBlob
442198620Sjhb
443198620Sjhb#ifdef COMPILER2
444198620SjhbExceptionBlob::ExceptionBlob(
445198620Sjhb  CodeBuffer* cb,
446198620Sjhb  int         size,
447198620Sjhb  OopMapSet*  oop_maps,
448198620Sjhb  int         frame_size
449198620Sjhb)
450198620Sjhb: SingletonBlob("ExceptionBlob", cb, sizeof(ExceptionBlob), size, frame_size, oop_maps)
451198620Sjhb{}
452198620Sjhb
453198620Sjhb
454198620SjhbExceptionBlob* ExceptionBlob::create(
455198620Sjhb  CodeBuffer* cb,
456198620Sjhb  OopMapSet*  oop_maps,
457198620Sjhb  int         frame_size)
458198620Sjhb{
459198620Sjhb  ExceptionBlob* blob = NULL;
460198620Sjhb  ThreadInVMfromUnknown __tiv;  // get to VM state in case we block on CodeCache_lock
461198620Sjhb  {
462123250Sdes    MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
463123250Sdes    unsigned int size = allocation_size(cb, sizeof(ExceptionBlob));
464198620Sjhb    blob = new (size) ExceptionBlob(cb, size, oop_maps, frame_size);
465198620Sjhb  }
466198620Sjhb
467123250Sdes  // We do not need to hold the CodeCache lock during name formatting
468123250Sdes  if (blob != NULL) {
469198620Sjhb    char blob_id[256];
470198620Sjhb    jio_snprintf(blob_id, sizeof(blob_id), "ExceptionBlob@" PTR_FORMAT, blob->instructions_begin());
471198620Sjhb    if (PrintStubCode) {
472198620Sjhb      tty->print_cr("Decoding %s " INTPTR_FORMAT, blob_id, blob);
473198620Sjhb      Disassembler::decode(blob->instructions_begin(), blob->instructions_end());
474198620Sjhb    }
475198620Sjhb    Forte::register_stub(blob_id, blob->instructions_begin(), blob->instructions_end());
476198620Sjhb
477198620Sjhb    if (JvmtiExport::should_post_dynamic_code_generated()) {
478198620Sjhb      JvmtiExport::post_dynamic_code_generated("ExceptionBlob",
479198620Sjhb                                               blob->instructions_begin(),
480198620Sjhb                                               blob->instructions_end());
481198620Sjhb    }
482198620Sjhb  }
483198620Sjhb
484198620Sjhb  // Track memory usage statistic after releasing CodeCache_lock
485198620Sjhb  MemoryService::track_code_cache_memory_usage();
486198620Sjhb
487198620Sjhb  return blob;
488198620Sjhb}
489198620Sjhb
490198620Sjhb
491198620Sjhbvoid* ExceptionBlob::operator new(size_t s, unsigned size) {
492198620Sjhb  void* p = CodeCache::allocate(size);
493198620Sjhb  if (!p) fatal("Initial size of CodeCache is too small");
494198620Sjhb  return p;
495198620Sjhb}
496198620Sjhb#endif // COMPILER2
497198620Sjhb
498198620Sjhb
499198620Sjhb//----------------------------------------------------------------------------------------------------
500198620Sjhb// Implementation of SafepointBlob
501198620Sjhb
502198620SjhbSafepointBlob::SafepointBlob(
503198620Sjhb  CodeBuffer* cb,
504198620Sjhb  int         size,
505198620Sjhb  OopMapSet*  oop_maps,
506123250Sdes  int         frame_size
507123250Sdes)
508123250Sdes: SingletonBlob("SafepointBlob", cb, sizeof(SafepointBlob), size, frame_size, oop_maps)
509123250Sdes{}
510123250Sdes
511123250Sdes
512123250SdesSafepointBlob* SafepointBlob::create(
513123250Sdes  CodeBuffer* cb,
514123250Sdes  OopMapSet*  oop_maps,
515123250Sdes  int         frame_size)
5161590Srgrimes{
517123250Sdes  SafepointBlob* blob = NULL;
518123414Sdes  ThreadInVMfromUnknown __tiv;  // get to VM state in case we block on CodeCache_lock
519123414Sdes  {
520123414Sdes    MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
521123414Sdes    unsigned int size = allocation_size(cb, sizeof(SafepointBlob));
522123414Sdes    blob = new (size) SafepointBlob(cb, size, oop_maps, frame_size);
523123414Sdes  }
524123414Sdes
525123414Sdes  // We do not need to hold the CodeCache lock during name formatting.
526123414Sdes  if (blob != NULL) {
527123414Sdes    char blob_id[256];
528123414Sdes    jio_snprintf(blob_id, sizeof(blob_id), "SafepointBlob@" PTR_FORMAT, blob->instructions_begin());
529123414Sdes    if (PrintStubCode) {
530123414Sdes      tty->print_cr("Decoding %s " INTPTR_FORMAT, blob_id, blob);
531123414Sdes      Disassembler::decode(blob->instructions_begin(), blob->instructions_end());
532123414Sdes    }
533123414Sdes    Forte::register_stub(blob_id, blob->instructions_begin(), blob->instructions_end());
534123414Sdes
535171633Salc    if (JvmtiExport::should_post_dynamic_code_generated()) {
536123414Sdes      JvmtiExport::post_dynamic_code_generated("SafepointBlob",
537123414Sdes                                               blob->instructions_begin(),
538123414Sdes                                               blob->instructions_end());
539123414Sdes    }
540123414Sdes  }
541123414Sdes
542123414Sdes  // Track memory usage statistic after releasing CodeCache_lock
543123414Sdes  MemoryService::track_code_cache_memory_usage();
544123414Sdes
545123414Sdes  return blob;
546123414Sdes}
547123414Sdes
548123414Sdes
549123414Sdesvoid* SafepointBlob::operator new(size_t s, unsigned size) {
550123414Sdes  void* p = CodeCache::allocate(size);
551123414Sdes  if (!p) fatal("Initial size of CodeCache is too small");
552123414Sdes  return p;
553123414Sdes}
554123414Sdes
555123250Sdes
556123250Sdes//----------------------------------------------------------------------------------------------------
557123250Sdes// Verification and printing
558123250Sdes
559123250Sdesvoid CodeBlob::verify() {
560123250Sdes  ShouldNotReachHere();
561123250Sdes}
562123250Sdes
563123250Sdes#ifndef PRODUCT
564123250Sdes
565123250Sdesvoid CodeBlob::print() const {
566123250Sdes  tty->print_cr("[CodeBlob (" INTPTR_FORMAT ")]", this);
567123250Sdes  tty->print_cr("Framesize: %d", _frame_size);
568123250Sdes}
5691590Srgrimes
570123250Sdes
571123250Sdesvoid CodeBlob::print_value_on(outputStream* st) const {
572123250Sdes  st->print_cr("[CodeBlob]");
573123250Sdes}
574123250Sdes
575123250Sdes#endif
576123250Sdes
577123250Sdesvoid BufferBlob::verify() {
578123250Sdes  // unimplemented
579123250Sdes}
580123250Sdes
581174573Speter#ifndef PRODUCT
582174573Speter
583174573Spetervoid BufferBlob::print() const {
584174573Speter  CodeBlob::print();
585174573Speter  print_value_on(tty);
586174573Speter}
587174573Speter
588174573Speter
589174573Spetervoid BufferBlob::print_value_on(outputStream* st) const {
590174573Speter  st->print_cr("BufferBlob (" INTPTR_FORMAT  ") used for %s", this, name());
591174573Speter}
592174573Speter
593174573Speter
594174573Speter#endif
595174573Speter
596174573Spetervoid RuntimeStub::verify() {
597174573Speter  // unimplemented
598174573Speter}
599174573Speter
600174573Speter#ifndef PRODUCT
601174573Speter
602174573Spetervoid RuntimeStub::print() const {
603174573Speter  CodeBlob::print();
604174573Speter  tty->print("Runtime Stub (" INTPTR_FORMAT "): ", this);
605174573Speter  tty->print_cr(name());
606174573Speter  Disassembler::decode((CodeBlob*)this);
607174573Speter}
608174573Speter
609174573Speter
610174573Spetervoid RuntimeStub::print_value_on(outputStream* st) const {
611174573Speter  st->print("RuntimeStub (" INTPTR_FORMAT "): ", this); st->print(name());
612174573Speter}
613174573Speter
614174573Speter#endif
615174573Speter
616174573Spetervoid SingletonBlob::verify() {
617174573Speter  // unimplemented
618174573Speter}
619174573Speter
620174573Speter#ifndef PRODUCT
621174573Speter
622174573Spetervoid SingletonBlob::print() const {
623174573Speter  CodeBlob::print();
624174573Speter  tty->print_cr(name());
625174573Speter  Disassembler::decode((CodeBlob*)this);
626174573Speter}
627174573Speter
628174573Speter
629174573Spetervoid SingletonBlob::print_value_on(outputStream* st) const {
630174573Speter  st->print_cr(name());
631174573Speter}
632174573Speter
633174573Spetervoid DeoptimizationBlob::print_value_on(outputStream* st) const {
634174573Speter  st->print_cr("Deoptimization (frame not available)");
635174573Speter}
636174573Speter
637174573Speter#endif // PRODUCT
638174573Speter