src/share/vm/asm/codeBuffer.cpp

Tue, 27 Nov 2012 17:41:38 -0800

author
kvn
date
Tue, 27 Nov 2012 17:41:38 -0800
changeset 4316
1acccb7c0b01
parent 4237
a3e2f723f2a5
child 4318
cd3d6a6b95d9
permissions
-rw-r--r--

8003850: add support for constants in stub code
Summary: remember the code section and switch back to the proper one when adding constants.
Reviewed-by: twisti, kvn
Contributed-by: goetz.lindenmaier@sap.com

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

mercurial