codeBlob.cpp revision 1564:2a47bd84841f
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# include "incls/_precompiled.incl"
26# include "incls/_codeBlob.cpp.incl"
27
28unsigned int align_code_offset(int offset) {
29  // align the size to CodeEntryAlignment
30  return
31    ((offset + (int)CodeHeap::header_size() + (CodeEntryAlignment-1)) & ~(CodeEntryAlignment-1))
32    - (int)CodeHeap::header_size();
33}
34
35
36// This must be consistent with the CodeBlob constructor's layout actions.
37unsigned int CodeBlob::allocation_size(CodeBuffer* cb, int header_size) {
38  unsigned int size = header_size;
39  size += round_to(cb->total_relocation_size(), oopSize);
40  // align the size to CodeEntryAlignment
41  size = align_code_offset(size);
42  size += round_to(cb->total_code_size(), oopSize);
43  size += round_to(cb->total_oop_size(), oopSize);
44  return size;
45}
46
47
48// Creates a simple CodeBlob. Sets up the size of the different regions.
49CodeBlob::CodeBlob(const char* name, int header_size, int size, int frame_complete, int locs_size) {
50  assert(size == round_to(size, oopSize), "unaligned size");
51  assert(locs_size == round_to(locs_size, oopSize), "unaligned size");
52  assert(header_size == round_to(header_size, oopSize), "unaligned size");
53  assert(!UseRelocIndex, "no space allocated for reloc index yet");
54
55  // Note: If UseRelocIndex is enabled, there needs to be (at least) one
56  //       extra word for the relocation information, containing the reloc
57  //       index table length. Unfortunately, the reloc index table imple-
58  //       mentation is not easily understandable and thus it is not clear
59  //       what exactly the format is supposed to be. For now, we just turn
60  //       off the use of this table (gri 7/6/2000).
61
62  _name                  = name;
63  _size                  = size;
64  _frame_complete_offset = frame_complete;
65  _header_size           = header_size;
66  _relocation_size       = locs_size;
67  _instructions_offset   = align_code_offset(header_size + locs_size);
68  _data_offset           = size;
69  _frame_size            =  0;
70  set_oop_maps(NULL);
71}
72
73
74// Creates a CodeBlob from a CodeBuffer. Sets up the size of the different regions,
75// and copy code and relocation info.
76CodeBlob::CodeBlob(
77  const char* name,
78  CodeBuffer* cb,
79  int         header_size,
80  int         size,
81  int         frame_complete,
82  int         frame_size,
83  OopMapSet*  oop_maps
84) {
85  assert(size == round_to(size, oopSize), "unaligned size");
86  assert(header_size == round_to(header_size, oopSize), "unaligned size");
87
88  _name                  = name;
89  _size                  = size;
90  _frame_complete_offset = frame_complete;
91  _header_size           = header_size;
92  _relocation_size       = round_to(cb->total_relocation_size(), oopSize);
93  _instructions_offset   = align_code_offset(header_size + _relocation_size);
94  _data_offset           = _instructions_offset + round_to(cb->total_code_size(), oopSize);
95  assert(_data_offset <= size, "codeBlob is too small");
96
97  cb->copy_code_and_locs_to(this);
98  set_oop_maps(oop_maps);
99  _frame_size = frame_size;
100#ifdef COMPILER1
101  // probably wrong for tiered
102  assert(_frame_size >= -1, "must use frame size or -1 for runtime stubs");
103#endif // COMPILER1
104}
105
106
107void CodeBlob::set_oop_maps(OopMapSet* p) {
108  // Danger Will Robinson! This method allocates a big
109  // chunk of memory, its your job to free it.
110  if (p != NULL) {
111    // We need to allocate a chunk big enough to hold the OopMapSet and all of its OopMaps
112    _oop_maps = (OopMapSet* )NEW_C_HEAP_ARRAY(unsigned char, p->heap_size());
113    p->copy_to((address)_oop_maps);
114  } else {
115    _oop_maps = NULL;
116  }
117}
118
119
120void CodeBlob::flush() {
121  if (_oop_maps) {
122    FREE_C_HEAP_ARRAY(unsigned char, _oop_maps);
123    _oop_maps = NULL;
124  }
125  _comments.free();
126}
127
128
129OopMap* CodeBlob::oop_map_for_return_address(address return_address) {
130  address pc = return_address ;
131  assert (oop_maps() != NULL, "nope");
132  return oop_maps()->find_map_at_offset ((intptr_t) pc - (intptr_t) instructions_begin());
133}
134
135
136//----------------------------------------------------------------------------------------------------
137// Implementation of BufferBlob
138
139
140BufferBlob::BufferBlob(const char* name, int size)
141: CodeBlob(name, sizeof(BufferBlob), size, CodeOffsets::frame_never_safe, /*locs_size:*/ 0)
142{}
143
144BufferBlob* BufferBlob::create(const char* name, int buffer_size) {
145  ThreadInVMfromUnknown __tiv;  // get to VM state in case we block on CodeCache_lock
146
147  BufferBlob* blob = NULL;
148  unsigned int size = sizeof(BufferBlob);
149  // align the size to CodeEntryAlignment
150  size = align_code_offset(size);
151  size += round_to(buffer_size, oopSize);
152  assert(name != NULL, "must provide a name");
153  {
154    MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
155    blob = new (size) BufferBlob(name, size);
156  }
157  // Track memory usage statistic after releasing CodeCache_lock
158  MemoryService::track_code_cache_memory_usage();
159
160  return blob;
161}
162
163
164BufferBlob::BufferBlob(const char* name, int size, CodeBuffer* cb)
165  : CodeBlob(name, cb, sizeof(BufferBlob), size, CodeOffsets::frame_never_safe, 0, NULL)
166{}
167
168BufferBlob* BufferBlob::create(const char* name, CodeBuffer* cb) {
169  ThreadInVMfromUnknown __tiv;  // get to VM state in case we block on CodeCache_lock
170
171  BufferBlob* blob = NULL;
172  unsigned int size = allocation_size(cb, sizeof(BufferBlob));
173  assert(name != NULL, "must provide a name");
174  {
175    MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
176    blob = new (size) BufferBlob(name, size, cb);
177  }
178  // Track memory usage statistic after releasing CodeCache_lock
179  MemoryService::track_code_cache_memory_usage();
180
181  return blob;
182}
183
184
185void* BufferBlob::operator new(size_t s, unsigned size) {
186  void* p = CodeCache::allocate(size);
187  return p;
188}
189
190
191void BufferBlob::free( BufferBlob *blob ) {
192  ThreadInVMfromUnknown __tiv;  // get to VM state in case we block on CodeCache_lock
193  {
194    MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
195    CodeCache::free((CodeBlob*)blob);
196  }
197  // Track memory usage statistic after releasing CodeCache_lock
198  MemoryService::track_code_cache_memory_usage();
199}
200
201
202//----------------------------------------------------------------------------------------------------
203// Implementation of AdapterBlob
204
205AdapterBlob* AdapterBlob::create(CodeBuffer* cb) {
206  ThreadInVMfromUnknown __tiv;  // get to VM state in case we block on CodeCache_lock
207
208  AdapterBlob* blob = NULL;
209  unsigned int size = allocation_size(cb, sizeof(AdapterBlob));
210  {
211    MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
212    blob = new (size) AdapterBlob(size, cb);
213    CodeCache::commit(blob);
214  }
215  // Track memory usage statistic after releasing CodeCache_lock
216  MemoryService::track_code_cache_memory_usage();
217
218  return blob;
219}
220
221
222//----------------------------------------------------------------------------------------------------
223// Implementation of MethodHandlesAdapterBlob
224
225MethodHandlesAdapterBlob* MethodHandlesAdapterBlob::create(int buffer_size) {
226  ThreadInVMfromUnknown __tiv;  // get to VM state in case we block on CodeCache_lock
227
228  MethodHandlesAdapterBlob* blob = NULL;
229  unsigned int size = sizeof(MethodHandlesAdapterBlob);
230  // align the size to CodeEntryAlignment
231  size = align_code_offset(size);
232  size += round_to(buffer_size, oopSize);
233  {
234    MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
235    blob = new (size) MethodHandlesAdapterBlob(size);
236  }
237  // Track memory usage statistic after releasing CodeCache_lock
238  MemoryService::track_code_cache_memory_usage();
239
240  return blob;
241}
242
243
244//----------------------------------------------------------------------------------------------------
245// Implementation of RuntimeStub
246
247RuntimeStub::RuntimeStub(
248  const char* name,
249  CodeBuffer* cb,
250  int         size,
251  int         frame_complete,
252  int         frame_size,
253  OopMapSet*  oop_maps,
254  bool        caller_must_gc_arguments
255)
256: CodeBlob(name, cb, sizeof(RuntimeStub), size, frame_complete, frame_size, oop_maps)
257{
258  _caller_must_gc_arguments = caller_must_gc_arguments;
259}
260
261
262RuntimeStub* RuntimeStub::new_runtime_stub(const char* stub_name,
263                                           CodeBuffer* cb,
264                                           int frame_complete,
265                                           int frame_size,
266                                           OopMapSet* oop_maps,
267                                           bool caller_must_gc_arguments)
268{
269  RuntimeStub* stub = NULL;
270  ThreadInVMfromUnknown __tiv;  // get to VM state in case we block on CodeCache_lock
271  {
272    MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
273    unsigned int size = allocation_size(cb, sizeof(RuntimeStub));
274    stub = new (size) RuntimeStub(stub_name, cb, size, frame_complete, frame_size, oop_maps, caller_must_gc_arguments);
275  }
276
277  // Do not hold the CodeCache lock during name formatting.
278  if (stub != NULL) {
279    char stub_id[256];
280    jio_snprintf(stub_id, sizeof(stub_id), "RuntimeStub - %s", stub_name);
281    if (PrintStubCode) {
282      tty->print_cr("Decoding %s " INTPTR_FORMAT, stub_id, stub);
283      Disassembler::decode(stub->instructions_begin(), stub->instructions_end());
284    }
285    Forte::register_stub(stub_id, stub->instructions_begin(), stub->instructions_end());
286
287    if (JvmtiExport::should_post_dynamic_code_generated()) {
288      JvmtiExport::post_dynamic_code_generated(stub_name, stub->instructions_begin(), stub->instructions_end());
289    }
290  }
291
292  // Track memory usage statistic after releasing CodeCache_lock
293  MemoryService::track_code_cache_memory_usage();
294
295  return stub;
296}
297
298
299void* RuntimeStub::operator new(size_t s, unsigned size) {
300  void* p = CodeCache::allocate(size);
301  if (!p) fatal("Initial size of CodeCache is too small");
302  return p;
303}
304
305
306//----------------------------------------------------------------------------------------------------
307// Implementation of DeoptimizationBlob
308
309DeoptimizationBlob::DeoptimizationBlob(
310  CodeBuffer* cb,
311  int         size,
312  OopMapSet*  oop_maps,
313  int         unpack_offset,
314  int         unpack_with_exception_offset,
315  int         unpack_with_reexecution_offset,
316  int         frame_size
317)
318: SingletonBlob("DeoptimizationBlob", cb, sizeof(DeoptimizationBlob), size, frame_size, oop_maps)
319{
320  _unpack_offset           = unpack_offset;
321  _unpack_with_exception   = unpack_with_exception_offset;
322  _unpack_with_reexecution = unpack_with_reexecution_offset;
323#ifdef COMPILER1
324  _unpack_with_exception_in_tls   = -1;
325#endif
326}
327
328
329DeoptimizationBlob* DeoptimizationBlob::create(
330  CodeBuffer* cb,
331  OopMapSet*  oop_maps,
332  int        unpack_offset,
333  int        unpack_with_exception_offset,
334  int        unpack_with_reexecution_offset,
335  int        frame_size)
336{
337  DeoptimizationBlob* blob = NULL;
338  ThreadInVMfromUnknown __tiv;  // get to VM state in case we block on CodeCache_lock
339  {
340    MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
341    unsigned int size = allocation_size(cb, sizeof(DeoptimizationBlob));
342    blob = new (size) DeoptimizationBlob(cb,
343                                         size,
344                                         oop_maps,
345                                         unpack_offset,
346                                         unpack_with_exception_offset,
347                                         unpack_with_reexecution_offset,
348                                         frame_size);
349  }
350
351  // Do not hold the CodeCache lock during name formatting.
352  if (blob != NULL) {
353    char blob_id[256];
354    jio_snprintf(blob_id, sizeof(blob_id), "DeoptimizationBlob@" PTR_FORMAT, blob->instructions_begin());
355    if (PrintStubCode) {
356      tty->print_cr("Decoding %s " INTPTR_FORMAT, blob_id, blob);
357      Disassembler::decode(blob->instructions_begin(), blob->instructions_end());
358    }
359    Forte::register_stub(blob_id, blob->instructions_begin(), blob->instructions_end());
360
361    if (JvmtiExport::should_post_dynamic_code_generated()) {
362      JvmtiExport::post_dynamic_code_generated("DeoptimizationBlob",
363                                               blob->instructions_begin(),
364                                               blob->instructions_end());
365    }
366  }
367
368  // Track memory usage statistic after releasing CodeCache_lock
369  MemoryService::track_code_cache_memory_usage();
370
371  return blob;
372}
373
374
375void* DeoptimizationBlob::operator new(size_t s, unsigned size) {
376  void* p = CodeCache::allocate(size);
377  if (!p) fatal("Initial size of CodeCache is too small");
378  return p;
379}
380
381//----------------------------------------------------------------------------------------------------
382// Implementation of UncommonTrapBlob
383
384#ifdef COMPILER2
385UncommonTrapBlob::UncommonTrapBlob(
386  CodeBuffer* cb,
387  int         size,
388  OopMapSet*  oop_maps,
389  int         frame_size
390)
391: SingletonBlob("UncommonTrapBlob", cb, sizeof(UncommonTrapBlob), size, frame_size, oop_maps)
392{}
393
394
395UncommonTrapBlob* UncommonTrapBlob::create(
396  CodeBuffer* cb,
397  OopMapSet*  oop_maps,
398  int        frame_size)
399{
400  UncommonTrapBlob* blob = NULL;
401  ThreadInVMfromUnknown __tiv;  // get to VM state in case we block on CodeCache_lock
402  {
403    MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
404    unsigned int size = allocation_size(cb, sizeof(UncommonTrapBlob));
405    blob = new (size) UncommonTrapBlob(cb, size, oop_maps, frame_size);
406  }
407
408  // Do not hold the CodeCache lock during name formatting.
409  if (blob != NULL) {
410    char blob_id[256];
411    jio_snprintf(blob_id, sizeof(blob_id), "UncommonTrapBlob@" PTR_FORMAT, blob->instructions_begin());
412    if (PrintStubCode) {
413      tty->print_cr("Decoding %s " INTPTR_FORMAT, blob_id, blob);
414      Disassembler::decode(blob->instructions_begin(), blob->instructions_end());
415    }
416    Forte::register_stub(blob_id, blob->instructions_begin(), blob->instructions_end());
417
418    if (JvmtiExport::should_post_dynamic_code_generated()) {
419      JvmtiExport::post_dynamic_code_generated("UncommonTrapBlob",
420                                               blob->instructions_begin(),
421                                               blob->instructions_end());
422    }
423  }
424
425  // Track memory usage statistic after releasing CodeCache_lock
426  MemoryService::track_code_cache_memory_usage();
427
428  return blob;
429}
430
431
432void* UncommonTrapBlob::operator new(size_t s, unsigned size) {
433  void* p = CodeCache::allocate(size);
434  if (!p) fatal("Initial size of CodeCache is too small");
435  return p;
436}
437#endif // COMPILER2
438
439
440//----------------------------------------------------------------------------------------------------
441// Implementation of ExceptionBlob
442
443#ifdef COMPILER2
444ExceptionBlob::ExceptionBlob(
445  CodeBuffer* cb,
446  int         size,
447  OopMapSet*  oop_maps,
448  int         frame_size
449)
450: SingletonBlob("ExceptionBlob", cb, sizeof(ExceptionBlob), size, frame_size, oop_maps)
451{}
452
453
454ExceptionBlob* ExceptionBlob::create(
455  CodeBuffer* cb,
456  OopMapSet*  oop_maps,
457  int         frame_size)
458{
459  ExceptionBlob* blob = NULL;
460  ThreadInVMfromUnknown __tiv;  // get to VM state in case we block on CodeCache_lock
461  {
462    MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
463    unsigned int size = allocation_size(cb, sizeof(ExceptionBlob));
464    blob = new (size) ExceptionBlob(cb, size, oop_maps, frame_size);
465  }
466
467  // We do not need to hold the CodeCache lock during name formatting
468  if (blob != NULL) {
469    char blob_id[256];
470    jio_snprintf(blob_id, sizeof(blob_id), "ExceptionBlob@" PTR_FORMAT, blob->instructions_begin());
471    if (PrintStubCode) {
472      tty->print_cr("Decoding %s " INTPTR_FORMAT, blob_id, blob);
473      Disassembler::decode(blob->instructions_begin(), blob->instructions_end());
474    }
475    Forte::register_stub(blob_id, blob->instructions_begin(), blob->instructions_end());
476
477    if (JvmtiExport::should_post_dynamic_code_generated()) {
478      JvmtiExport::post_dynamic_code_generated("ExceptionBlob",
479                                               blob->instructions_begin(),
480                                               blob->instructions_end());
481    }
482  }
483
484  // Track memory usage statistic after releasing CodeCache_lock
485  MemoryService::track_code_cache_memory_usage();
486
487  return blob;
488}
489
490
491void* ExceptionBlob::operator new(size_t s, unsigned size) {
492  void* p = CodeCache::allocate(size);
493  if (!p) fatal("Initial size of CodeCache is too small");
494  return p;
495}
496#endif // COMPILER2
497
498
499//----------------------------------------------------------------------------------------------------
500// Implementation of SafepointBlob
501
502SafepointBlob::SafepointBlob(
503  CodeBuffer* cb,
504  int         size,
505  OopMapSet*  oop_maps,
506  int         frame_size
507)
508: SingletonBlob("SafepointBlob", cb, sizeof(SafepointBlob), size, frame_size, oop_maps)
509{}
510
511
512SafepointBlob* SafepointBlob::create(
513  CodeBuffer* cb,
514  OopMapSet*  oop_maps,
515  int         frame_size)
516{
517  SafepointBlob* blob = NULL;
518  ThreadInVMfromUnknown __tiv;  // get to VM state in case we block on CodeCache_lock
519  {
520    MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
521    unsigned int size = allocation_size(cb, sizeof(SafepointBlob));
522    blob = new (size) SafepointBlob(cb, size, oop_maps, frame_size);
523  }
524
525  // We do not need to hold the CodeCache lock during name formatting.
526  if (blob != NULL) {
527    char blob_id[256];
528    jio_snprintf(blob_id, sizeof(blob_id), "SafepointBlob@" PTR_FORMAT, blob->instructions_begin());
529    if (PrintStubCode) {
530      tty->print_cr("Decoding %s " INTPTR_FORMAT, blob_id, blob);
531      Disassembler::decode(blob->instructions_begin(), blob->instructions_end());
532    }
533    Forte::register_stub(blob_id, blob->instructions_begin(), blob->instructions_end());
534
535    if (JvmtiExport::should_post_dynamic_code_generated()) {
536      JvmtiExport::post_dynamic_code_generated("SafepointBlob",
537                                               blob->instructions_begin(),
538                                               blob->instructions_end());
539    }
540  }
541
542  // Track memory usage statistic after releasing CodeCache_lock
543  MemoryService::track_code_cache_memory_usage();
544
545  return blob;
546}
547
548
549void* SafepointBlob::operator new(size_t s, unsigned size) {
550  void* p = CodeCache::allocate(size);
551  if (!p) fatal("Initial size of CodeCache is too small");
552  return p;
553}
554
555
556//----------------------------------------------------------------------------------------------------
557// Verification and printing
558
559void CodeBlob::verify() {
560  ShouldNotReachHere();
561}
562
563#ifndef PRODUCT
564
565void CodeBlob::print() const {
566  tty->print_cr("[CodeBlob (" INTPTR_FORMAT ")]", this);
567  tty->print_cr("Framesize: %d", _frame_size);
568}
569
570
571void CodeBlob::print_value_on(outputStream* st) const {
572  st->print_cr("[CodeBlob]");
573}
574
575#endif
576
577void BufferBlob::verify() {
578  // unimplemented
579}
580
581#ifndef PRODUCT
582
583void BufferBlob::print() const {
584  CodeBlob::print();
585  print_value_on(tty);
586}
587
588
589void BufferBlob::print_value_on(outputStream* st) const {
590  st->print_cr("BufferBlob (" INTPTR_FORMAT  ") used for %s", this, name());
591}
592
593
594#endif
595
596void RuntimeStub::verify() {
597  // unimplemented
598}
599
600#ifndef PRODUCT
601
602void RuntimeStub::print() const {
603  CodeBlob::print();
604  tty->print("Runtime Stub (" INTPTR_FORMAT "): ", this);
605  tty->print_cr(name());
606  Disassembler::decode((CodeBlob*)this);
607}
608
609
610void RuntimeStub::print_value_on(outputStream* st) const {
611  st->print("RuntimeStub (" INTPTR_FORMAT "): ", this); st->print(name());
612}
613
614#endif
615
616void SingletonBlob::verify() {
617  // unimplemented
618}
619
620#ifndef PRODUCT
621
622void SingletonBlob::print() const {
623  CodeBlob::print();
624  tty->print_cr(name());
625  Disassembler::decode((CodeBlob*)this);
626}
627
628
629void SingletonBlob::print_value_on(outputStream* st) const {
630  st->print_cr(name());
631}
632
633void DeoptimizationBlob::print_value_on(outputStream* st) const {
634  st->print_cr("Deoptimization (frame not available)");
635}
636
637#endif // PRODUCT
638