hotspot/src/share/vm/asm/codeBuffer.cpp
author kvn
Tue, 03 Aug 2010 15:55:03 -0700
changeset 6180 53c1bf468c81
parent 5883 8dc4bdc132d5
child 6418 6671edbd230e
permissions -rw-r--r--
6973963: SEGV in ciBlock::start_bci() with EA Summary: Added more checks into ResourceObj and growableArray to verify correctness of allocation type. Reviewed-by: never, coleenp, dholmes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     1
/*
5547
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 2105
diff changeset
     2
 * Copyright (c) 1997, 2009, Oracle and/or its affiliates. All rights reserved.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     4
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
489c9b5090e2 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
489c9b5090e2 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     8
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
489c9b5090e2 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
489c9b5090e2 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
489c9b5090e2 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
489c9b5090e2 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
489c9b5090e2 Initial load
duke
parents:
diff changeset
    14
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
489c9b5090e2 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    18
 *
5547
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 2105
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 2105
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 2105
diff changeset
    21
 * questions.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    22
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    23
 */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    24
489c9b5090e2 Initial load
duke
parents:
diff changeset
    25
# include "incls/_precompiled.incl"
489c9b5090e2 Initial load
duke
parents:
diff changeset
    26
# include "incls/_codeBuffer.cpp.incl"
489c9b5090e2 Initial load
duke
parents:
diff changeset
    27
489c9b5090e2 Initial load
duke
parents:
diff changeset
    28
// The structure of a CodeSection:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    29
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
    30
//    _start ->           +----------------+
489c9b5090e2 Initial load
duke
parents:
diff changeset
    31
//                        | machine code...|
489c9b5090e2 Initial load
duke
parents:
diff changeset
    32
//    _end ->             |----------------|
489c9b5090e2 Initial load
duke
parents:
diff changeset
    33
//                        |                |
489c9b5090e2 Initial load
duke
parents:
diff changeset
    34
//                        |    (empty)     |
489c9b5090e2 Initial load
duke
parents:
diff changeset
    35
//                        |                |
489c9b5090e2 Initial load
duke
parents:
diff changeset
    36
//                        |                |
489c9b5090e2 Initial load
duke
parents:
diff changeset
    37
//                        +----------------+
489c9b5090e2 Initial load
duke
parents:
diff changeset
    38
//    _limit ->           |                |
489c9b5090e2 Initial load
duke
parents:
diff changeset
    39
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
    40
//    _locs_start ->      +----------------+
489c9b5090e2 Initial load
duke
parents:
diff changeset
    41
//                        |reloc records...|
489c9b5090e2 Initial load
duke
parents:
diff changeset
    42
//                        |----------------|
489c9b5090e2 Initial load
duke
parents:
diff changeset
    43
//    _locs_end ->        |                |
489c9b5090e2 Initial load
duke
parents:
diff changeset
    44
//                        |                |
489c9b5090e2 Initial load
duke
parents:
diff changeset
    45
//                        |    (empty)     |
489c9b5090e2 Initial load
duke
parents:
diff changeset
    46
//                        |                |
489c9b5090e2 Initial load
duke
parents:
diff changeset
    47
//                        |                |
489c9b5090e2 Initial load
duke
parents:
diff changeset
    48
//                        +----------------+
489c9b5090e2 Initial load
duke
parents:
diff changeset
    49
//    _locs_limit ->      |                |
489c9b5090e2 Initial load
duke
parents:
diff changeset
    50
// The _end (resp. _limit) pointer refers to the first
489c9b5090e2 Initial load
duke
parents:
diff changeset
    51
// unused (resp. unallocated) byte.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    52
489c9b5090e2 Initial load
duke
parents:
diff changeset
    53
// The structure of the CodeBuffer while code is being accumulated:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    54
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
    55
//    _total_start ->    \
489c9b5090e2 Initial load
duke
parents:
diff changeset
    56
//    _insts._start ->              +----------------+
489c9b5090e2 Initial load
duke
parents:
diff changeset
    57
//                                  |                |
489c9b5090e2 Initial load
duke
parents:
diff changeset
    58
//                                  |     Code       |
489c9b5090e2 Initial load
duke
parents:
diff changeset
    59
//                                  |                |
489c9b5090e2 Initial load
duke
parents:
diff changeset
    60
//    _stubs._start ->              |----------------|
489c9b5090e2 Initial load
duke
parents:
diff changeset
    61
//                                  |                |
489c9b5090e2 Initial load
duke
parents:
diff changeset
    62
//                                  |    Stubs       | (also handlers for deopt/exception)
489c9b5090e2 Initial load
duke
parents:
diff changeset
    63
//                                  |                |
489c9b5090e2 Initial load
duke
parents:
diff changeset
    64
//    _consts._start ->             |----------------|
489c9b5090e2 Initial load
duke
parents:
diff changeset
    65
//                                  |                |
489c9b5090e2 Initial load
duke
parents:
diff changeset
    66
//                                  |   Constants    |
489c9b5090e2 Initial load
duke
parents:
diff changeset
    67
//                                  |                |
489c9b5090e2 Initial load
duke
parents:
diff changeset
    68
//                                  +----------------+
489c9b5090e2 Initial load
duke
parents:
diff changeset
    69
//    + _total_size ->              |                |
489c9b5090e2 Initial load
duke
parents:
diff changeset
    70
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
    71
// When the code and relocations are copied to the code cache,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    72
// the empty parts of each section are removed, and everything
489c9b5090e2 Initial load
duke
parents:
diff changeset
    73
// is copied into contiguous locations.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    74
489c9b5090e2 Initial load
duke
parents:
diff changeset
    75
typedef CodeBuffer::csize_t csize_t;  // file-local definition
489c9b5090e2 Initial load
duke
parents:
diff changeset
    76
489c9b5090e2 Initial load
duke
parents:
diff changeset
    77
// external buffer, in a predefined CodeBlob or other buffer area
489c9b5090e2 Initial load
duke
parents:
diff changeset
    78
// Important: The code_start must be taken exactly, and not realigned.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    79
CodeBuffer::CodeBuffer(address code_start, csize_t code_size) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    80
  assert(code_start != NULL, "sanity");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    81
  initialize_misc("static buffer");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    82
  initialize(code_start, code_size);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    83
  assert(verify_section_allocation(), "initial use of buffer OK");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    84
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    85
489c9b5090e2 Initial load
duke
parents:
diff changeset
    86
void CodeBuffer::initialize(csize_t code_size, csize_t locs_size) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    87
  // Compute maximal alignment.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    88
  int align = _insts.alignment();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    89
  // Always allow for empty slop around each section.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    90
  int slop = (int) CodeSection::end_slop();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    91
489c9b5090e2 Initial load
duke
parents:
diff changeset
    92
  assert(blob() == NULL, "only once");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    93
  set_blob(BufferBlob::create(_name, code_size + (align+slop) * (SECT_LIMIT+1)));
489c9b5090e2 Initial load
duke
parents:
diff changeset
    94
  if (blob() == NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    95
    // The assembler constructor will throw a fatal on an empty CodeBuffer.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    96
    return;  // caller must test this
489c9b5090e2 Initial load
duke
parents:
diff changeset
    97
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    98
489c9b5090e2 Initial load
duke
parents:
diff changeset
    99
  // Set up various pointers into the blob.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   100
  initialize(_total_start, _total_size);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   101
489c9b5090e2 Initial load
duke
parents:
diff changeset
   102
  assert((uintptr_t)code_begin() % CodeEntryAlignment == 0, "instruction start not code entry aligned");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   103
489c9b5090e2 Initial load
duke
parents:
diff changeset
   104
  pd_initialize();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   105
489c9b5090e2 Initial load
duke
parents:
diff changeset
   106
  if (locs_size != 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   107
    _insts.initialize_locs(locs_size / sizeof(relocInfo));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   108
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   109
489c9b5090e2 Initial load
duke
parents:
diff changeset
   110
  assert(verify_section_allocation(), "initial use of blob is OK");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   111
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   112
489c9b5090e2 Initial load
duke
parents:
diff changeset
   113
489c9b5090e2 Initial load
duke
parents:
diff changeset
   114
CodeBuffer::~CodeBuffer() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   115
  // If we allocate our code buffer from the CodeCache
489c9b5090e2 Initial load
duke
parents:
diff changeset
   116
  // via a BufferBlob, and it's not permanent, then
489c9b5090e2 Initial load
duke
parents:
diff changeset
   117
  // free the BufferBlob.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   118
  // The rest of the memory will be freed when the ResourceObj
489c9b5090e2 Initial load
duke
parents:
diff changeset
   119
  // is released.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   120
  assert(verify_section_allocation(), "final storage configuration still OK");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   121
  for (CodeBuffer* cb = this; cb != NULL; cb = cb->before_expand()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   122
    // Previous incarnations of this buffer are held live, so that internal
489c9b5090e2 Initial load
duke
parents:
diff changeset
   123
    // addresses constructed before expansions will not be confused.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   124
    cb->free_blob();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   125
  }
2025
a13c4b3f024e 6782260: Memory leak in CodeBuffer::create_patch_overflow
never
parents: 670
diff changeset
   126
a13c4b3f024e 6782260: Memory leak in CodeBuffer::create_patch_overflow
never
parents: 670
diff changeset
   127
  // free any overflow storage
a13c4b3f024e 6782260: Memory leak in CodeBuffer::create_patch_overflow
never
parents: 670
diff changeset
   128
  delete _overflow_arena;
a13c4b3f024e 6782260: Memory leak in CodeBuffer::create_patch_overflow
never
parents: 670
diff changeset
   129
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   130
#ifdef ASSERT
6180
53c1bf468c81 6973963: SEGV in ciBlock::start_bci() with EA
kvn
parents: 5883
diff changeset
   131
  // Save allocation type to execute assert in ~ResourceObj()
53c1bf468c81 6973963: SEGV in ciBlock::start_bci() with EA
kvn
parents: 5883
diff changeset
   132
  // which is called after this destructor.
53c1bf468c81 6973963: SEGV in ciBlock::start_bci() with EA
kvn
parents: 5883
diff changeset
   133
  ResourceObj::allocation_type at = _default_oop_recorder.get_allocation_type();
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   134
  Copy::fill_to_bytes(this, sizeof(*this), badResourceValue);
6180
53c1bf468c81 6973963: SEGV in ciBlock::start_bci() with EA
kvn
parents: 5883
diff changeset
   135
  ResourceObj::set_allocation_type((address)(&_default_oop_recorder), at);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   136
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
   137
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   138
489c9b5090e2 Initial load
duke
parents:
diff changeset
   139
void CodeBuffer::initialize_oop_recorder(OopRecorder* r) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   140
  assert(_oop_recorder == &_default_oop_recorder && _default_oop_recorder.is_unused(), "do this once");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   141
  DEBUG_ONLY(_default_oop_recorder.oop_size());  // force unused OR to be frozen
489c9b5090e2 Initial load
duke
parents:
diff changeset
   142
  _oop_recorder = r;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   143
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   144
489c9b5090e2 Initial load
duke
parents:
diff changeset
   145
void CodeBuffer::initialize_section_size(CodeSection* cs, csize_t size) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   146
  assert(cs != &_insts, "insts is the memory provider, not the consumer");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   147
#ifdef ASSERT
489c9b5090e2 Initial load
duke
parents:
diff changeset
   148
  for (int n = (int)SECT_INSTS+1; n < (int)SECT_LIMIT; n++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   149
    CodeSection* prevCS = code_section(n);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   150
    if (prevCS == cs)  break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   151
    assert(!prevCS->is_allocated(), "section allocation must be in reverse order");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   152
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   153
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
   154
  csize_t slop = CodeSection::end_slop();  // margin between sections
489c9b5090e2 Initial load
duke
parents:
diff changeset
   155
  int align = cs->alignment();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   156
  assert(is_power_of_2(align), "sanity");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   157
  address start  = _insts._start;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   158
  address limit  = _insts._limit;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   159
  address middle = limit - size;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   160
  middle -= (intptr_t)middle & (align-1);  // align the division point downward
489c9b5090e2 Initial load
duke
parents:
diff changeset
   161
  guarantee(middle - slop > start, "need enough space to divide up");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   162
  _insts._limit = middle - slop;  // subtract desired space, plus slop
489c9b5090e2 Initial load
duke
parents:
diff changeset
   163
  cs->initialize(middle, limit - middle);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   164
  assert(cs->start() == middle, "sanity");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   165
  assert(cs->limit() == limit,  "sanity");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   166
  // give it some relocations to start with, if the main section has them
489c9b5090e2 Initial load
duke
parents:
diff changeset
   167
  if (_insts.has_locs())  cs->initialize_locs(1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   168
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   169
489c9b5090e2 Initial load
duke
parents:
diff changeset
   170
void CodeBuffer::freeze_section(CodeSection* cs) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   171
  CodeSection* next_cs = (cs == consts())? NULL: code_section(cs->index()+1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   172
  csize_t frozen_size = cs->size();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   173
  if (next_cs != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   174
    frozen_size = next_cs->align_at_start(frozen_size);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   175
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   176
  address old_limit = cs->limit();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   177
  address new_limit = cs->start() + frozen_size;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   178
  relocInfo* old_locs_limit = cs->locs_limit();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   179
  relocInfo* new_locs_limit = cs->locs_end();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   180
  // Patch the limits.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   181
  cs->_limit = new_limit;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   182
  cs->_locs_limit = new_locs_limit;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   183
  cs->_frozen = true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   184
  if (!next_cs->is_allocated() && !next_cs->is_frozen()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   185
    // Give remaining buffer space to the following section.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   186
    next_cs->initialize(new_limit, old_limit - new_limit);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   187
    next_cs->initialize_shared_locs(new_locs_limit,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   188
                                    old_locs_limit - new_locs_limit);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   189
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   190
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   191
489c9b5090e2 Initial load
duke
parents:
diff changeset
   192
void CodeBuffer::set_blob(BufferBlob* blob) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   193
  _blob = blob;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   194
  if (blob != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   195
    address start = blob->instructions_begin();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   196
    address end   = blob->instructions_end();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   197
    // Round up the starting address.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   198
    int align = _insts.alignment();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   199
    start += (-(intptr_t)start) & (align-1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   200
    _total_start = start;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   201
    _total_size  = end - start;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   202
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   203
    #ifdef ASSERT
489c9b5090e2 Initial load
duke
parents:
diff changeset
   204
    // Clean out dangling pointers.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   205
    _total_start    = badAddress;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   206
    _insts._start   = _insts._end   = badAddress;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   207
    _stubs._start   = _stubs._end   = badAddress;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   208
    _consts._start  = _consts._end  = badAddress;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   209
    #endif //ASSERT
489c9b5090e2 Initial load
duke
parents:
diff changeset
   210
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   211
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   212
489c9b5090e2 Initial load
duke
parents:
diff changeset
   213
void CodeBuffer::free_blob() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   214
  if (_blob != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   215
    BufferBlob::free(_blob);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   216
    set_blob(NULL);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   217
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   218
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   219
489c9b5090e2 Initial load
duke
parents:
diff changeset
   220
const char* CodeBuffer::code_section_name(int n) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   221
#ifdef PRODUCT
489c9b5090e2 Initial load
duke
parents:
diff changeset
   222
  return NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   223
#else //PRODUCT
489c9b5090e2 Initial load
duke
parents:
diff changeset
   224
  switch (n) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   225
  case SECT_INSTS:             return "insts";
489c9b5090e2 Initial load
duke
parents:
diff changeset
   226
  case SECT_STUBS:             return "stubs";
489c9b5090e2 Initial load
duke
parents:
diff changeset
   227
  case SECT_CONSTS:            return "consts";
489c9b5090e2 Initial load
duke
parents:
diff changeset
   228
  default:                     return NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   229
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   230
#endif //PRODUCT
489c9b5090e2 Initial load
duke
parents:
diff changeset
   231
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   232
489c9b5090e2 Initial load
duke
parents:
diff changeset
   233
int CodeBuffer::section_index_of(address addr) const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   234
  for (int n = 0; n < (int)SECT_LIMIT; n++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   235
    const CodeSection* cs = code_section(n);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   236
    if (cs->allocates(addr))  return n;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   237
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   238
  return SECT_NONE;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   239
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   240
489c9b5090e2 Initial load
duke
parents:
diff changeset
   241
int CodeBuffer::locator(address addr) const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   242
  for (int n = 0; n < (int)SECT_LIMIT; n++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   243
    const CodeSection* cs = code_section(n);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   244
    if (cs->allocates(addr)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   245
      return locator(addr - cs->start(), n);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   246
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   247
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   248
  return -1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   249
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   250
489c9b5090e2 Initial load
duke
parents:
diff changeset
   251
address CodeBuffer::locator_address(int locator) const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   252
  if (locator < 0)  return NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   253
  address start = code_section(locator_sect(locator))->start();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   254
  return start + locator_pos(locator);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   255
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   256
489c9b5090e2 Initial load
duke
parents:
diff changeset
   257
address CodeBuffer::decode_begin() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   258
  address begin = _insts.start();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   259
  if (_decode_begin != NULL && _decode_begin > begin)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   260
    begin = _decode_begin;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   261
  return begin;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   262
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   263
489c9b5090e2 Initial load
duke
parents:
diff changeset
   264
489c9b5090e2 Initial load
duke
parents:
diff changeset
   265
GrowableArray<int>* CodeBuffer::create_patch_overflow() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   266
  if (_overflow_arena == NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   267
    _overflow_arena = new Arena();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   268
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   269
  return new (_overflow_arena) GrowableArray<int>(_overflow_arena, 8, 0, 0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   270
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   271
489c9b5090e2 Initial load
duke
parents:
diff changeset
   272
489c9b5090e2 Initial load
duke
parents:
diff changeset
   273
// Helper function for managing labels and their target addresses.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   274
// Returns a sensible address, and if it is not the label's final
489c9b5090e2 Initial load
duke
parents:
diff changeset
   275
// address, notes the dependency (at 'branch_pc') on the label.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   276
address CodeSection::target(Label& L, address branch_pc) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   277
  if (L.is_bound()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   278
    int loc = L.loc();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   279
    if (index() == CodeBuffer::locator_sect(loc)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   280
      return start() + CodeBuffer::locator_pos(loc);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   281
    } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   282
      return outer()->locator_address(loc);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   283
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   284
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   285
    assert(allocates2(branch_pc), "sanity");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   286
    address base = start();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   287
    int patch_loc = CodeBuffer::locator(branch_pc - base, index());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   288
    L.add_patch_at(outer(), patch_loc);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   289
489c9b5090e2 Initial load
duke
parents:
diff changeset
   290
    // Need to return a pc, doesn't matter what it is since it will be
489c9b5090e2 Initial load
duke
parents:
diff changeset
   291
    // replaced during resolution later.
360
21d113ecbf6a 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 347
diff changeset
   292
    // Don't return NULL or badAddress, since branches shouldn't overflow.
21d113ecbf6a 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 347
diff changeset
   293
    // Don't return base either because that could overflow displacements
21d113ecbf6a 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 347
diff changeset
   294
    // for shorter branches.  It will get checked when bound.
21d113ecbf6a 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 347
diff changeset
   295
    return branch_pc;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   296
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   297
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   298
489c9b5090e2 Initial load
duke
parents:
diff changeset
   299
void CodeSection::relocate(address at, RelocationHolder const& spec, int format) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   300
  Relocation* reloc = spec.reloc();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   301
  relocInfo::relocType rtype = (relocInfo::relocType) reloc->type();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   302
  if (rtype == relocInfo::none)  return;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   303
489c9b5090e2 Initial load
duke
parents:
diff changeset
   304
  // The assertion below has been adjusted, to also work for
489c9b5090e2 Initial load
duke
parents:
diff changeset
   305
  // relocation for fixup.  Sometimes we want to put relocation
489c9b5090e2 Initial load
duke
parents:
diff changeset
   306
  // information for the next instruction, since it will be patched
489c9b5090e2 Initial load
duke
parents:
diff changeset
   307
  // with a call.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   308
  assert(start() <= at && at <= end()+1,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   309
         "cannot relocate data outside code boundaries");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   310
489c9b5090e2 Initial load
duke
parents:
diff changeset
   311
  if (!has_locs()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   312
    // no space for relocation information provided => code cannot be
489c9b5090e2 Initial load
duke
parents:
diff changeset
   313
    // relocated.  Make sure that relocate is only called with rtypes
489c9b5090e2 Initial load
duke
parents:
diff changeset
   314
    // that can be ignored for this kind of code.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   315
    assert(rtype == relocInfo::none              ||
489c9b5090e2 Initial load
duke
parents:
diff changeset
   316
           rtype == relocInfo::runtime_call_type ||
489c9b5090e2 Initial load
duke
parents:
diff changeset
   317
           rtype == relocInfo::internal_word_type||
489c9b5090e2 Initial load
duke
parents:
diff changeset
   318
           rtype == relocInfo::section_word_type ||
489c9b5090e2 Initial load
duke
parents:
diff changeset
   319
           rtype == relocInfo::external_word_type,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   320
           "code needs relocation information");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   321
    // leave behind an indication that we attempted a relocation
489c9b5090e2 Initial load
duke
parents:
diff changeset
   322
    DEBUG_ONLY(_locs_start = _locs_limit = (relocInfo*)badAddress);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   323
    return;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   324
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   325
489c9b5090e2 Initial load
duke
parents:
diff changeset
   326
  // Advance the point, noting the offset we'll have to record.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   327
  csize_t offset = at - locs_point();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   328
  set_locs_point(at);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   329
489c9b5090e2 Initial load
duke
parents:
diff changeset
   330
  // Test for a couple of overflow conditions; maybe expand the buffer.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   331
  relocInfo* end = locs_end();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   332
  relocInfo* req = end + relocInfo::length_limit;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   333
  // Check for (potential) overflow
489c9b5090e2 Initial load
duke
parents:
diff changeset
   334
  if (req >= locs_limit() || offset >= relocInfo::offset_limit()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   335
    req += (uint)offset / (uint)relocInfo::offset_limit();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   336
    if (req >= locs_limit()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   337
      // Allocate or reallocate.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   338
      expand_locs(locs_count() + (req - end));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   339
      // reload pointer
489c9b5090e2 Initial load
duke
parents:
diff changeset
   340
      end = locs_end();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   341
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   342
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   343
489c9b5090e2 Initial load
duke
parents:
diff changeset
   344
  // If the offset is giant, emit filler relocs, of type 'none', but
489c9b5090e2 Initial load
duke
parents:
diff changeset
   345
  // each carrying the largest possible offset, to advance the locs_point.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   346
  while (offset >= relocInfo::offset_limit()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   347
    assert(end < locs_limit(), "adjust previous paragraph of code");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   348
    *end++ = filler_relocInfo();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   349
    offset -= filler_relocInfo().addr_offset();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   350
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   351
489c9b5090e2 Initial load
duke
parents:
diff changeset
   352
  // If it's a simple reloc with no data, we'll just write (rtype | offset).
489c9b5090e2 Initial load
duke
parents:
diff changeset
   353
  (*end) = relocInfo(rtype, offset, format);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   354
489c9b5090e2 Initial load
duke
parents:
diff changeset
   355
  // If it has data, insert the prefix, as (data_prefix_tag | data1), data2.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   356
  end->initialize(this, reloc);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   357
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   358
489c9b5090e2 Initial load
duke
parents:
diff changeset
   359
void CodeSection::initialize_locs(int locs_capacity) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   360
  assert(_locs_start == NULL, "only one locs init step, please");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   361
  // Apply a priori lower limits to relocation size:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   362
  csize_t min_locs = MAX2(size() / 16, (csize_t)4);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   363
  if (locs_capacity < min_locs)  locs_capacity = min_locs;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   364
  relocInfo* locs_start = NEW_RESOURCE_ARRAY(relocInfo, locs_capacity);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   365
  _locs_start    = locs_start;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   366
  _locs_end      = locs_start;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   367
  _locs_limit    = locs_start + locs_capacity;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   368
  _locs_own      = true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   369
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   370
489c9b5090e2 Initial load
duke
parents:
diff changeset
   371
void CodeSection::initialize_shared_locs(relocInfo* buf, int length) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   372
  assert(_locs_start == NULL, "do this before locs are allocated");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   373
  // Internal invariant:  locs buf must be fully aligned.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   374
  // See copy_relocations_to() below.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   375
  while ((uintptr_t)buf % HeapWordSize != 0 && length > 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   376
    ++buf; --length;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   377
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   378
  if (length > 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   379
    _locs_start = buf;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   380
    _locs_end   = buf;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   381
    _locs_limit = buf + length;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   382
    _locs_own   = false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   383
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   384
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   385
489c9b5090e2 Initial load
duke
parents:
diff changeset
   386
void CodeSection::initialize_locs_from(const CodeSection* source_cs) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   387
  int lcount = source_cs->locs_count();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   388
  if (lcount != 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   389
    initialize_shared_locs(source_cs->locs_start(), lcount);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   390
    _locs_end = _locs_limit = _locs_start + lcount;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   391
    assert(is_allocated(), "must have copied code already");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   392
    set_locs_point(start() + source_cs->locs_point_off());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   393
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   394
  assert(this->locs_count() == source_cs->locs_count(), "sanity");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   395
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   396
489c9b5090e2 Initial load
duke
parents:
diff changeset
   397
void CodeSection::expand_locs(int new_capacity) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   398
  if (_locs_start == NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   399
    initialize_locs(new_capacity);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   400
    return;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   401
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   402
    int old_count    = locs_count();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   403
    int old_capacity = locs_capacity();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   404
    if (new_capacity < old_capacity * 2)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   405
      new_capacity = old_capacity * 2;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   406
    relocInfo* locs_start;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   407
    if (_locs_own) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   408
      locs_start = REALLOC_RESOURCE_ARRAY(relocInfo, _locs_start, old_capacity, new_capacity);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   409
    } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   410
      locs_start = NEW_RESOURCE_ARRAY(relocInfo, new_capacity);
5883
8dc4bdc132d5 6730276: JDI_REGRESSION tests fail with "Error: count must be non-zero" error on x86
kvn
parents: 5547
diff changeset
   411
      Copy::conjoint_jbytes(_locs_start, locs_start, old_capacity * sizeof(relocInfo));
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   412
      _locs_own = true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   413
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   414
    _locs_start    = locs_start;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   415
    _locs_end      = locs_start + old_count;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   416
    _locs_limit    = locs_start + new_capacity;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   417
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   418
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   419
489c9b5090e2 Initial load
duke
parents:
diff changeset
   420
489c9b5090e2 Initial load
duke
parents:
diff changeset
   421
/// Support for emitting the code to its final location.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   422
/// The pattern is the same for all functions.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   423
/// We iterate over all the sections, padding each to alignment.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   424
489c9b5090e2 Initial load
duke
parents:
diff changeset
   425
csize_t CodeBuffer::total_code_size() const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   426
  csize_t code_size_so_far = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   427
  for (int n = 0; n < (int)SECT_LIMIT; n++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   428
    const CodeSection* cs = code_section(n);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   429
    if (cs->is_empty())  continue;  // skip trivial section
489c9b5090e2 Initial load
duke
parents:
diff changeset
   430
    code_size_so_far = cs->align_at_start(code_size_so_far);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   431
    code_size_so_far += cs->size();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   432
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   433
  return code_size_so_far;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   434
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   435
489c9b5090e2 Initial load
duke
parents:
diff changeset
   436
void CodeBuffer::compute_final_layout(CodeBuffer* dest) const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   437
  address buf = dest->_total_start;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   438
  csize_t buf_offset = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   439
  assert(dest->_total_size >= total_code_size(), "must be big enough");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   440
489c9b5090e2 Initial load
duke
parents:
diff changeset
   441
  {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   442
    // not sure why this is here, but why not...
489c9b5090e2 Initial load
duke
parents:
diff changeset
   443
    int alignSize = MAX2((intx) sizeof(jdouble), CodeEntryAlignment);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   444
    assert( (dest->_total_start - _insts.start()) % alignSize == 0, "copy must preserve alignment");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   445
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   446
489c9b5090e2 Initial load
duke
parents:
diff changeset
   447
  const CodeSection* prev_cs      = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   448
  CodeSection*       prev_dest_cs = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   449
  for (int n = 0; n < (int)SECT_LIMIT; n++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   450
    // figure compact layout of each section
489c9b5090e2 Initial load
duke
parents:
diff changeset
   451
    const CodeSection* cs = code_section(n);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   452
    address cstart = cs->start();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   453
    address cend   = cs->end();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   454
    csize_t csize  = cend - cstart;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   455
489c9b5090e2 Initial load
duke
parents:
diff changeset
   456
    CodeSection* dest_cs = dest->code_section(n);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   457
    if (!cs->is_empty()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   458
      // Compute initial padding; assign it to the previous non-empty guy.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   459
      // Cf. figure_expanded_capacities.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   460
      csize_t padding = cs->align_at_start(buf_offset) - buf_offset;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   461
      if (padding != 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   462
        buf_offset += padding;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   463
        assert(prev_dest_cs != NULL, "sanity");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   464
        prev_dest_cs->_limit += padding;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   465
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   466
      #ifdef ASSERT
489c9b5090e2 Initial load
duke
parents:
diff changeset
   467
      if (prev_cs != NULL && prev_cs->is_frozen() && n < SECT_CONSTS) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   468
        // Make sure the ends still match up.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   469
        // This is important because a branch in a frozen section
489c9b5090e2 Initial load
duke
parents:
diff changeset
   470
        // might target code in a following section, via a Label,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   471
        // and without a relocation record.  See Label::patch_instructions.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   472
        address dest_start = buf+buf_offset;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   473
        csize_t start2start = cs->start() - prev_cs->start();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   474
        csize_t dest_start2start = dest_start - prev_dest_cs->start();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   475
        assert(start2start == dest_start2start, "cannot stretch frozen sect");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   476
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   477
      #endif //ASSERT
489c9b5090e2 Initial load
duke
parents:
diff changeset
   478
      prev_dest_cs = dest_cs;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   479
      prev_cs      = cs;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   480
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   481
489c9b5090e2 Initial load
duke
parents:
diff changeset
   482
    debug_only(dest_cs->_start = NULL);  // defeat double-initialization assert
489c9b5090e2 Initial load
duke
parents:
diff changeset
   483
    dest_cs->initialize(buf+buf_offset, csize);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   484
    dest_cs->set_end(buf+buf_offset+csize);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   485
    assert(dest_cs->is_allocated(), "must always be allocated");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   486
    assert(cs->is_empty() == dest_cs->is_empty(), "sanity");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   487
489c9b5090e2 Initial load
duke
parents:
diff changeset
   488
    buf_offset += csize;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   489
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   490
489c9b5090e2 Initial load
duke
parents:
diff changeset
   491
  // Done calculating sections; did it come out to the right end?
489c9b5090e2 Initial load
duke
parents:
diff changeset
   492
  assert(buf_offset == total_code_size(), "sanity");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   493
  assert(dest->verify_section_allocation(), "final configuration works");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   494
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   495
489c9b5090e2 Initial load
duke
parents:
diff changeset
   496
csize_t CodeBuffer::total_offset_of(address addr) const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   497
  csize_t code_size_so_far = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   498
  for (int n = 0; n < (int)SECT_LIMIT; n++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   499
    const CodeSection* cs = code_section(n);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   500
    if (!cs->is_empty()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   501
      code_size_so_far = cs->align_at_start(code_size_so_far);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   502
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   503
    if (cs->contains2(addr)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   504
      return code_size_so_far + (addr - cs->start());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   505
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   506
    code_size_so_far += cs->size();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   507
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   508
#ifndef PRODUCT
489c9b5090e2 Initial load
duke
parents:
diff changeset
   509
  tty->print_cr("Dangling address " PTR_FORMAT " in:", addr);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   510
  ((CodeBuffer*)this)->print();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   511
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
   512
  ShouldNotReachHere();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   513
  return -1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   514
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   515
489c9b5090e2 Initial load
duke
parents:
diff changeset
   516
csize_t CodeBuffer::total_relocation_size() const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   517
  csize_t lsize = copy_relocations_to(NULL);  // dry run only
489c9b5090e2 Initial load
duke
parents:
diff changeset
   518
  csize_t csize = total_code_size();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   519
  csize_t total = RelocIterator::locs_and_index_size(csize, lsize);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   520
  return (csize_t) align_size_up(total, HeapWordSize);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   521
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   522
489c9b5090e2 Initial load
duke
parents:
diff changeset
   523
csize_t CodeBuffer::copy_relocations_to(CodeBlob* dest) const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   524
  address buf = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   525
  csize_t buf_offset = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   526
  csize_t buf_limit = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   527
  if (dest != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   528
    buf = (address)dest->relocation_begin();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   529
    buf_limit = (address)dest->relocation_end() - buf;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   530
    assert((uintptr_t)buf % HeapWordSize == 0, "buf must be fully aligned");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   531
    assert(buf_limit % HeapWordSize == 0, "buf must be evenly sized");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   532
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   533
  // if dest == NULL, this is just the sizing pass
489c9b5090e2 Initial load
duke
parents:
diff changeset
   534
489c9b5090e2 Initial load
duke
parents:
diff changeset
   535
  csize_t code_end_so_far = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   536
  csize_t code_point_so_far = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   537
  for (int n = 0; n < (int)SECT_LIMIT; n++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   538
    // pull relocs out of each section
489c9b5090e2 Initial load
duke
parents:
diff changeset
   539
    const CodeSection* cs = code_section(n);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   540
    assert(!(cs->is_empty() && cs->locs_count() > 0), "sanity");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   541
    if (cs->is_empty())  continue;  // skip trivial section
489c9b5090e2 Initial load
duke
parents:
diff changeset
   542
    relocInfo* lstart = cs->locs_start();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   543
    relocInfo* lend   = cs->locs_end();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   544
    csize_t    lsize  = (csize_t)( (address)lend - (address)lstart );
489c9b5090e2 Initial load
duke
parents:
diff changeset
   545
    csize_t    csize  = cs->size();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   546
    code_end_so_far = cs->align_at_start(code_end_so_far);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   547
489c9b5090e2 Initial load
duke
parents:
diff changeset
   548
    if (lsize > 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   549
      // Figure out how to advance the combined relocation point
489c9b5090e2 Initial load
duke
parents:
diff changeset
   550
      // first to the beginning of this section.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   551
      // We'll insert one or more filler relocs to span that gap.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   552
      // (Don't bother to improve this by editing the first reloc's offset.)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   553
      csize_t new_code_point = code_end_so_far;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   554
      for (csize_t jump;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   555
           code_point_so_far < new_code_point;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   556
           code_point_so_far += jump) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   557
        jump = new_code_point - code_point_so_far;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   558
        relocInfo filler = filler_relocInfo();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   559
        if (jump >= filler.addr_offset()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   560
          jump = filler.addr_offset();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   561
        } else {  // else shrink the filler to fit
489c9b5090e2 Initial load
duke
parents:
diff changeset
   562
          filler = relocInfo(relocInfo::none, jump);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   563
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   564
        if (buf != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   565
          assert(buf_offset + (csize_t)sizeof(filler) <= buf_limit, "filler in bounds");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   566
          *(relocInfo*)(buf+buf_offset) = filler;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   567
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   568
        buf_offset += sizeof(filler);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   569
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   570
489c9b5090e2 Initial load
duke
parents:
diff changeset
   571
      // Update code point and end to skip past this section:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   572
      csize_t last_code_point = code_end_so_far + cs->locs_point_off();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   573
      assert(code_point_so_far <= last_code_point, "sanity");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   574
      code_point_so_far = last_code_point; // advance past this guy's relocs
489c9b5090e2 Initial load
duke
parents:
diff changeset
   575
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   576
    code_end_so_far += csize;  // advance past this guy's instructions too
489c9b5090e2 Initial load
duke
parents:
diff changeset
   577
489c9b5090e2 Initial load
duke
parents:
diff changeset
   578
    // Done with filler; emit the real relocations:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   579
    if (buf != NULL && lsize != 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   580
      assert(buf_offset + lsize <= buf_limit, "target in bounds");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   581
      assert((uintptr_t)lstart % HeapWordSize == 0, "sane start");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   582
      if (buf_offset % HeapWordSize == 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   583
        // Use wordwise copies if possible:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   584
        Copy::disjoint_words((HeapWord*)lstart,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   585
                             (HeapWord*)(buf+buf_offset),
489c9b5090e2 Initial load
duke
parents:
diff changeset
   586
                             (lsize + HeapWordSize-1) / HeapWordSize);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   587
      } else {
5883
8dc4bdc132d5 6730276: JDI_REGRESSION tests fail with "Error: count must be non-zero" error on x86
kvn
parents: 5547
diff changeset
   588
        Copy::conjoint_jbytes(lstart, buf+buf_offset, lsize);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   589
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   590
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   591
    buf_offset += lsize;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   592
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   593
489c9b5090e2 Initial load
duke
parents:
diff changeset
   594
  // Align end of relocation info in target.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   595
  while (buf_offset % HeapWordSize != 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   596
    if (buf != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   597
      relocInfo padding = relocInfo(relocInfo::none, 0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   598
      assert(buf_offset + (csize_t)sizeof(padding) <= buf_limit, "padding in bounds");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   599
      *(relocInfo*)(buf+buf_offset) = padding;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   600
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   601
    buf_offset += sizeof(relocInfo);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   602
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   603
489c9b5090e2 Initial load
duke
parents:
diff changeset
   604
  assert(code_end_so_far == total_code_size(), "sanity");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   605
489c9b5090e2 Initial load
duke
parents:
diff changeset
   606
  // Account for index:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   607
  if (buf != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   608
    RelocIterator::create_index(dest->relocation_begin(),
489c9b5090e2 Initial load
duke
parents:
diff changeset
   609
                                buf_offset / sizeof(relocInfo),
489c9b5090e2 Initial load
duke
parents:
diff changeset
   610
                                dest->relocation_end());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   611
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   612
489c9b5090e2 Initial load
duke
parents:
diff changeset
   613
  return buf_offset;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   614
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   615
489c9b5090e2 Initial load
duke
parents:
diff changeset
   616
void CodeBuffer::copy_code_to(CodeBlob* dest_blob) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   617
#ifndef PRODUCT
489c9b5090e2 Initial load
duke
parents:
diff changeset
   618
  if (PrintNMethods && (WizardMode || Verbose)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   619
    tty->print("done with CodeBuffer:");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   620
    ((CodeBuffer*)this)->print();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   621
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   622
#endif //PRODUCT
489c9b5090e2 Initial load
duke
parents:
diff changeset
   623
489c9b5090e2 Initial load
duke
parents:
diff changeset
   624
  CodeBuffer dest(dest_blob->instructions_begin(),
489c9b5090e2 Initial load
duke
parents:
diff changeset
   625
                  dest_blob->instructions_size());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   626
  assert(dest_blob->instructions_size() >= total_code_size(), "good sizing");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   627
  this->compute_final_layout(&dest);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   628
  relocate_code_to(&dest);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   629
489c9b5090e2 Initial load
duke
parents:
diff changeset
   630
  // transfer comments from buffer to blob
489c9b5090e2 Initial load
duke
parents:
diff changeset
   631
  dest_blob->set_comments(_comments);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   632
489c9b5090e2 Initial load
duke
parents:
diff changeset
   633
  // Done moving code bytes; were they the right size?
489c9b5090e2 Initial load
duke
parents:
diff changeset
   634
  assert(round_to(dest.total_code_size(), oopSize) == dest_blob->instructions_size(), "sanity");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   635
489c9b5090e2 Initial load
duke
parents:
diff changeset
   636
  // Flush generated code
489c9b5090e2 Initial load
duke
parents:
diff changeset
   637
  ICache::invalidate_range(dest_blob->instructions_begin(),
489c9b5090e2 Initial load
duke
parents:
diff changeset
   638
                           dest_blob->instructions_size());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   639
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   640
489c9b5090e2 Initial load
duke
parents:
diff changeset
   641
// Move all my code into another code buffer.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   642
// Consult applicable relocs to repair embedded addresses.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   643
void CodeBuffer::relocate_code_to(CodeBuffer* dest) const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   644
  DEBUG_ONLY(address dest_end = dest->_total_start + dest->_total_size);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   645
  for (int n = 0; n < (int)SECT_LIMIT; n++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   646
    // pull code out of each section
489c9b5090e2 Initial load
duke
parents:
diff changeset
   647
    const CodeSection* cs = code_section(n);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   648
    if (cs->is_empty())  continue;  // skip trivial section
489c9b5090e2 Initial load
duke
parents:
diff changeset
   649
    CodeSection* dest_cs = dest->code_section(n);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   650
    assert(cs->size() == dest_cs->size(), "sanity");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   651
    csize_t usize = dest_cs->size();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   652
    csize_t wsize = align_size_up(usize, HeapWordSize);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   653
    assert(dest_cs->start() + wsize <= dest_end, "no overflow");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   654
    // Copy the code as aligned machine words.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   655
    // This may also include an uninitialized partial word at the end.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   656
    Copy::disjoint_words((HeapWord*)cs->start(),
489c9b5090e2 Initial load
duke
parents:
diff changeset
   657
                         (HeapWord*)dest_cs->start(),
489c9b5090e2 Initial load
duke
parents:
diff changeset
   658
                         wsize / HeapWordSize);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   659
489c9b5090e2 Initial load
duke
parents:
diff changeset
   660
    if (dest->blob() == NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   661
      // Destination is a final resting place, not just another buffer.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   662
      // Normalize uninitialized bytes in the final padding.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   663
      Copy::fill_to_bytes(dest_cs->end(), dest_cs->remaining(),
489c9b5090e2 Initial load
duke
parents:
diff changeset
   664
                          Assembler::code_fill_byte());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   665
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   666
489c9b5090e2 Initial load
duke
parents:
diff changeset
   667
    assert(cs->locs_start() != (relocInfo*)badAddress,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   668
           "this section carries no reloc storage, but reloc was attempted");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   669
489c9b5090e2 Initial load
duke
parents:
diff changeset
   670
    // Make the new code copy use the old copy's relocations:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   671
    dest_cs->initialize_locs_from(cs);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   672
489c9b5090e2 Initial load
duke
parents:
diff changeset
   673
    { // Repair the pc relative information in the code after the move
489c9b5090e2 Initial load
duke
parents:
diff changeset
   674
      RelocIterator iter(dest_cs);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   675
      while (iter.next()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   676
        iter.reloc()->fix_relocation_after_move(this, dest);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   677
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   678
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   679
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   680
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   681
489c9b5090e2 Initial load
duke
parents:
diff changeset
   682
csize_t CodeBuffer::figure_expanded_capacities(CodeSection* which_cs,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   683
                                               csize_t amount,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   684
                                               csize_t* new_capacity) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   685
  csize_t new_total_cap = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   686
489c9b5090e2 Initial load
duke
parents:
diff changeset
   687
  int prev_n = -1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   688
  for (int n = 0; n < (int)SECT_LIMIT; n++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   689
    const CodeSection* sect = code_section(n);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   690
489c9b5090e2 Initial load
duke
parents:
diff changeset
   691
    if (!sect->is_empty()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   692
      // Compute initial padding; assign it to the previous non-empty guy.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   693
      // Cf. compute_final_layout.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   694
      csize_t padding = sect->align_at_start(new_total_cap) - new_total_cap;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   695
      if (padding != 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   696
        new_total_cap += padding;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   697
        assert(prev_n >= 0, "sanity");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   698
        new_capacity[prev_n] += padding;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   699
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   700
      prev_n = n;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   701
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   702
489c9b5090e2 Initial load
duke
parents:
diff changeset
   703
    csize_t exp = sect->size();  // 100% increase
489c9b5090e2 Initial load
duke
parents:
diff changeset
   704
    if ((uint)exp < 4*K)  exp = 4*K;       // minimum initial increase
489c9b5090e2 Initial load
duke
parents:
diff changeset
   705
    if (sect == which_cs) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   706
      if (exp < amount)  exp = amount;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   707
      if (StressCodeBuffers)  exp = amount;  // expand only slightly
489c9b5090e2 Initial load
duke
parents:
diff changeset
   708
    } else if (n == SECT_INSTS) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   709
      // scale down inst increases to a more modest 25%
489c9b5090e2 Initial load
duke
parents:
diff changeset
   710
      exp = 4*K + ((exp - 4*K) >> 2);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   711
      if (StressCodeBuffers)  exp = amount / 2;  // expand only slightly
489c9b5090e2 Initial load
duke
parents:
diff changeset
   712
    } else if (sect->is_empty()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   713
      // do not grow an empty secondary section
489c9b5090e2 Initial load
duke
parents:
diff changeset
   714
      exp = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   715
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   716
    // Allow for inter-section slop:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   717
    exp += CodeSection::end_slop();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   718
    csize_t new_cap = sect->size() + exp;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   719
    if (new_cap < sect->capacity()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   720
      // No need to expand after all.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   721
      new_cap = sect->capacity();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   722
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   723
    new_capacity[n] = new_cap;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   724
    new_total_cap += new_cap;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   725
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   726
489c9b5090e2 Initial load
duke
parents:
diff changeset
   727
  return new_total_cap;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   728
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   729
489c9b5090e2 Initial load
duke
parents:
diff changeset
   730
void CodeBuffer::expand(CodeSection* which_cs, csize_t amount) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   731
#ifndef PRODUCT
489c9b5090e2 Initial load
duke
parents:
diff changeset
   732
  if (PrintNMethods && (WizardMode || Verbose)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   733
    tty->print("expanding CodeBuffer:");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   734
    this->print();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   735
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   736
489c9b5090e2 Initial load
duke
parents:
diff changeset
   737
  if (StressCodeBuffers && blob() != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   738
    static int expand_count = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   739
    if (expand_count >= 0)  expand_count += 1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   740
    if (expand_count > 100 && is_power_of_2(expand_count)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   741
      tty->print_cr("StressCodeBuffers: have expanded %d times", expand_count);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   742
      // simulate an occasional allocation failure:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   743
      free_blob();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   744
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   745
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   746
#endif //PRODUCT
489c9b5090e2 Initial load
duke
parents:
diff changeset
   747
489c9b5090e2 Initial load
duke
parents:
diff changeset
   748
  // Resizing must be allowed
489c9b5090e2 Initial load
duke
parents:
diff changeset
   749
  {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   750
    if (blob() == NULL)  return;  // caller must check for blob == NULL
489c9b5090e2 Initial load
duke
parents:
diff changeset
   751
    for (int n = 0; n < (int)SECT_LIMIT; n++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   752
      guarantee(!code_section(n)->is_frozen(), "resizing not allowed when frozen");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   753
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   754
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   755
489c9b5090e2 Initial load
duke
parents:
diff changeset
   756
  // Figure new capacity for each section.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   757
  csize_t new_capacity[SECT_LIMIT];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   758
  csize_t new_total_cap
489c9b5090e2 Initial load
duke
parents:
diff changeset
   759
    = figure_expanded_capacities(which_cs, amount, new_capacity);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   760
489c9b5090e2 Initial load
duke
parents:
diff changeset
   761
  // Create a new (temporary) code buffer to hold all the new data
489c9b5090e2 Initial load
duke
parents:
diff changeset
   762
  CodeBuffer cb(name(), new_total_cap, 0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   763
  if (cb.blob() == NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   764
    // Failed to allocate in code cache.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   765
    free_blob();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   766
    return;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   767
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   768
489c9b5090e2 Initial load
duke
parents:
diff changeset
   769
  // Create an old code buffer to remember which addresses used to go where.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   770
  // This will be useful when we do final assembly into the code cache,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   771
  // because we will need to know how to warp any internal address that
489c9b5090e2 Initial load
duke
parents:
diff changeset
   772
  // has been created at any time in this CodeBuffer's past.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   773
  CodeBuffer* bxp = new CodeBuffer(_total_start, _total_size);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   774
  bxp->take_over_code_from(this);  // remember the old undersized blob
489c9b5090e2 Initial load
duke
parents:
diff changeset
   775
  DEBUG_ONLY(this->_blob = NULL);  // silence a later assert
489c9b5090e2 Initial load
duke
parents:
diff changeset
   776
  bxp->_before_expand = this->_before_expand;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   777
  this->_before_expand = bxp;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   778
489c9b5090e2 Initial load
duke
parents:
diff changeset
   779
  // Give each section its required (expanded) capacity.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   780
  for (int n = (int)SECT_LIMIT-1; n >= SECT_INSTS; n--) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   781
    CodeSection* cb_sect   = cb.code_section(n);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   782
    CodeSection* this_sect = code_section(n);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   783
    if (new_capacity[n] == 0)  continue;  // already nulled out
489c9b5090e2 Initial load
duke
parents:
diff changeset
   784
    if (n > SECT_INSTS) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   785
      cb.initialize_section_size(cb_sect, new_capacity[n]);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   786
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   787
    assert(cb_sect->capacity() >= new_capacity[n], "big enough");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   788
    address cb_start = cb_sect->start();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   789
    cb_sect->set_end(cb_start + this_sect->size());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   790
    if (this_sect->mark() == NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   791
      cb_sect->clear_mark();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   792
    } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   793
      cb_sect->set_mark(cb_start + this_sect->mark_off());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   794
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   795
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   796
489c9b5090e2 Initial load
duke
parents:
diff changeset
   797
  // Move all the code and relocations to the new blob:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   798
  relocate_code_to(&cb);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   799
489c9b5090e2 Initial load
duke
parents:
diff changeset
   800
  // Copy the temporary code buffer into the current code buffer.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   801
  // Basically, do {*this = cb}, except for some control information.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   802
  this->take_over_code_from(&cb);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   803
  cb.set_blob(NULL);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   804
489c9b5090e2 Initial load
duke
parents:
diff changeset
   805
  // Zap the old code buffer contents, to avoid mistakenly using them.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   806
  debug_only(Copy::fill_to_bytes(bxp->_total_start, bxp->_total_size,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   807
                                 badCodeHeapFreeVal));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   808
489c9b5090e2 Initial load
duke
parents:
diff changeset
   809
  _decode_begin = NULL;  // sanity
489c9b5090e2 Initial load
duke
parents:
diff changeset
   810
489c9b5090e2 Initial load
duke
parents:
diff changeset
   811
  // Make certain that the new sections are all snugly inside the new blob.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   812
  assert(verify_section_allocation(), "expanded allocation is ship-shape");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   813
489c9b5090e2 Initial load
duke
parents:
diff changeset
   814
#ifndef PRODUCT
489c9b5090e2 Initial load
duke
parents:
diff changeset
   815
  if (PrintNMethods && (WizardMode || Verbose)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   816
    tty->print("expanded CodeBuffer:");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   817
    this->print();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   818
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   819
#endif //PRODUCT
489c9b5090e2 Initial load
duke
parents:
diff changeset
   820
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   821
489c9b5090e2 Initial load
duke
parents:
diff changeset
   822
void CodeBuffer::take_over_code_from(CodeBuffer* cb) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   823
  // Must already have disposed of the old blob somehow.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   824
  assert(blob() == NULL, "must be empty");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   825
#ifdef ASSERT
489c9b5090e2 Initial load
duke
parents:
diff changeset
   826
489c9b5090e2 Initial load
duke
parents:
diff changeset
   827
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
   828
  // Take the new blob away from cb.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   829
  set_blob(cb->blob());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   830
  // Take over all the section pointers.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   831
  for (int n = 0; n < (int)SECT_LIMIT; n++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   832
    CodeSection* cb_sect   = cb->code_section(n);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   833
    CodeSection* this_sect = code_section(n);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   834
    this_sect->take_over_code_from(cb_sect);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   835
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   836
  _overflow_arena = cb->_overflow_arena;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   837
  // Make sure the old cb won't try to use it or free it.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   838
  DEBUG_ONLY(cb->_blob = (BufferBlob*)badAddress);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   839
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   840
489c9b5090e2 Initial load
duke
parents:
diff changeset
   841
#ifdef ASSERT
489c9b5090e2 Initial load
duke
parents:
diff changeset
   842
bool CodeBuffer::verify_section_allocation() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   843
  address tstart = _total_start;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   844
  if (tstart == badAddress)  return true;  // smashed by set_blob(NULL)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   845
  address tend   = tstart + _total_size;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   846
  if (_blob != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   847
    assert(tstart >= _blob->instructions_begin(), "sanity");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   848
    assert(tend   <= _blob->instructions_end(),   "sanity");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   849
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   850
  address tcheck = tstart;  // advancing pointer to verify disjointness
489c9b5090e2 Initial load
duke
parents:
diff changeset
   851
  for (int n = 0; n < (int)SECT_LIMIT; n++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   852
    CodeSection* sect = code_section(n);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   853
    if (!sect->is_allocated())  continue;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   854
    assert(sect->start() >= tcheck, "sanity");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   855
    tcheck = sect->start();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   856
    assert((intptr_t)tcheck % sect->alignment() == 0
489c9b5090e2 Initial load
duke
parents:
diff changeset
   857
           || sect->is_empty() || _blob == NULL,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   858
           "start is aligned");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   859
    assert(sect->end()   >= tcheck, "sanity");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   860
    assert(sect->end()   <= tend,   "sanity");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   861
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   862
  return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   863
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   864
#endif //ASSERT
489c9b5090e2 Initial load
duke
parents:
diff changeset
   865
489c9b5090e2 Initial load
duke
parents:
diff changeset
   866
#ifndef PRODUCT
489c9b5090e2 Initial load
duke
parents:
diff changeset
   867
489c9b5090e2 Initial load
duke
parents:
diff changeset
   868
void CodeSection::dump() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   869
  address ptr = start();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   870
  for (csize_t step; ptr < end(); ptr += step) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   871
    step = end() - ptr;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   872
    if (step > jintSize * 4)  step = jintSize * 4;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   873
    tty->print(PTR_FORMAT ": ", ptr);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   874
    while (step > 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   875
      tty->print(" " PTR32_FORMAT, *(jint*)ptr);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   876
      ptr += jintSize;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   877
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   878
    tty->cr();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   879
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   880
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   881
489c9b5090e2 Initial load
duke
parents:
diff changeset
   882
489c9b5090e2 Initial load
duke
parents:
diff changeset
   883
void CodeSection::decode() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   884
  Disassembler::decode(start(), end());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   885
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   886
489c9b5090e2 Initial load
duke
parents:
diff changeset
   887
489c9b5090e2 Initial load
duke
parents:
diff changeset
   888
void CodeBuffer::block_comment(intptr_t offset, const char * comment) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   889
  _comments.add_comment(offset, comment);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   890
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   891
489c9b5090e2 Initial load
duke
parents:
diff changeset
   892
489c9b5090e2 Initial load
duke
parents:
diff changeset
   893
class CodeComment: public CHeapObj {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   894
 private:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   895
  friend class CodeComments;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   896
  intptr_t     _offset;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   897
  const char * _comment;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   898
  CodeComment* _next;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   899
489c9b5090e2 Initial load
duke
parents:
diff changeset
   900
  ~CodeComment() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   901
    assert(_next == NULL, "wrong interface for freeing list");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   902
    os::free((void*)_comment);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   903
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   904
489c9b5090e2 Initial load
duke
parents:
diff changeset
   905
 public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   906
  CodeComment(intptr_t offset, const char * comment) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   907
    _offset = offset;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   908
    _comment = os::strdup(comment);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   909
    _next = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   910
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   911
489c9b5090e2 Initial load
duke
parents:
diff changeset
   912
  intptr_t     offset()  const { return _offset;  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   913
  const char * comment() const { return _comment; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   914
  CodeComment* next()          { return _next; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   915
489c9b5090e2 Initial load
duke
parents:
diff changeset
   916
  void set_next(CodeComment* next) { _next = next; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   917
489c9b5090e2 Initial load
duke
parents:
diff changeset
   918
  CodeComment* find(intptr_t offset) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   919
    CodeComment* a = this;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   920
    while (a != NULL && a->_offset != offset) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   921
      a = a->_next;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   922
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   923
    return a;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   924
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   925
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
   926
489c9b5090e2 Initial load
duke
parents:
diff changeset
   927
489c9b5090e2 Initial load
duke
parents:
diff changeset
   928
void CodeComments::add_comment(intptr_t offset, const char * comment) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   929
  CodeComment* c = new CodeComment(offset, comment);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   930
  CodeComment* insert = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   931
  if (_comments != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   932
    CodeComment* c = _comments->find(offset);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   933
    insert = c;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   934
    while (c && c->offset() == offset) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   935
      insert = c;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   936
      c = c->next();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   937
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   938
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   939
  if (insert) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   940
    // insert after comments with same offset
489c9b5090e2 Initial load
duke
parents:
diff changeset
   941
    c->set_next(insert->next());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   942
    insert->set_next(c);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   943
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   944
    c->set_next(_comments);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   945
    _comments = c;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   946
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   947
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   948
489c9b5090e2 Initial load
duke
parents:
diff changeset
   949
489c9b5090e2 Initial load
duke
parents:
diff changeset
   950
void CodeComments::assign(CodeComments& other) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   951
  assert(_comments == NULL, "don't overwrite old value");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   952
  _comments = other._comments;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   953
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   954
489c9b5090e2 Initial load
duke
parents:
diff changeset
   955
489c9b5090e2 Initial load
duke
parents:
diff changeset
   956
void CodeComments::print_block_comment(outputStream* stream, intptr_t offset) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   957
  if (_comments != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   958
    CodeComment* c = _comments->find(offset);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   959
    while (c && c->offset() == offset) {
347
df859fcca515 6667042: PrintAssembly option does not work without special plugin
jrose
parents: 1
diff changeset
   960
      stream->bol();
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   961
      stream->print("  ;; ");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   962
      stream->print_cr(c->comment());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   963
      c = c->next();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   964
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   965
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   966
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   967
489c9b5090e2 Initial load
duke
parents:
diff changeset
   968
489c9b5090e2 Initial load
duke
parents:
diff changeset
   969
void CodeComments::free() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   970
  CodeComment* n = _comments;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   971
  while (n) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   972
    // unlink the node from the list saving a pointer to the next
489c9b5090e2 Initial load
duke
parents:
diff changeset
   973
    CodeComment* p = n->_next;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   974
    n->_next = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   975
    delete n;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   976
    n = p;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   977
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   978
  _comments = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   979
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   980
489c9b5090e2 Initial load
duke
parents:
diff changeset
   981
489c9b5090e2 Initial load
duke
parents:
diff changeset
   982
489c9b5090e2 Initial load
duke
parents:
diff changeset
   983
void CodeBuffer::decode() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   984
  Disassembler::decode(decode_begin(), code_end());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   985
  _decode_begin = code_end();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   986
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   987
489c9b5090e2 Initial load
duke
parents:
diff changeset
   988
489c9b5090e2 Initial load
duke
parents:
diff changeset
   989
void CodeBuffer::skip_decode() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   990
  _decode_begin = code_end();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   991
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   992
489c9b5090e2 Initial load
duke
parents:
diff changeset
   993
489c9b5090e2 Initial load
duke
parents:
diff changeset
   994
void CodeBuffer::decode_all() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   995
  for (int n = 0; n < (int)SECT_LIMIT; n++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   996
    // dump contents of each section
489c9b5090e2 Initial load
duke
parents:
diff changeset
   997
    CodeSection* cs = code_section(n);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   998
    tty->print_cr("! %s:", code_section_name(n));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   999
    if (cs != consts())
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1000
      cs->decode();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1001
    else
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1002
      cs->dump();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1003
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1004
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1005
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1006
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1007
void CodeSection::print(const char* name) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1008
  csize_t locs_size = locs_end() - locs_start();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1009
  tty->print_cr(" %7s.code = " PTR_FORMAT " : " PTR_FORMAT " : " PTR_FORMAT " (%d of %d)%s",
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1010
                name, start(), end(), limit(), size(), capacity(),
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1011
                is_frozen()? " [frozen]": "");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1012
  tty->print_cr(" %7s.locs = " PTR_FORMAT " : " PTR_FORMAT " : " PTR_FORMAT " (%d of %d) point=%d",
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1013
                name, locs_start(), locs_end(), locs_limit(), locs_size, locs_capacity(), locs_point_off());
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1014
  if (PrintRelocations) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1015
    RelocIterator iter(this);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1016
    iter.print();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1017
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1018
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1019
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1020
void CodeBuffer::print() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1021
  if (this == NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1022
    tty->print_cr("NULL CodeBuffer pointer");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1023
    return;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1024
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1025
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1026
  tty->print_cr("CodeBuffer:");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1027
  for (int n = 0; n < (int)SECT_LIMIT; n++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1028
    // print each section
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1029
    CodeSection* cs = code_section(n);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1030
    cs->print(code_section_name(n));
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1031
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1032
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1033
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1034
#endif // PRODUCT