annotate src/share/vm/asm/codeBuffer.cpp @ 3672:b31471cdc53e

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