src/share/vm/asm/codeBuffer.cpp

Thu, 06 Dec 2012 09:57:41 -0800

author
twisti
date
Thu, 06 Dec 2012 09:57:41 -0800
changeset 4323
f0c2369fda5a
parent 4318
cd3d6a6b95d9
child 4325
d2f8c38e543d
permissions
-rw-r--r--

8003250: SPARC: move MacroAssembler into separate file
Reviewed-by: jrose, kvn

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
twisti@4318 257 bool CodeBuffer::is_backward_branch(Label& L) {
twisti@4318 258 return L.is_bound() && insts_end() <= locator_address(L.loc());
twisti@4318 259 }
twisti@4318 260
duke@435 261 address CodeBuffer::decode_begin() {
duke@435 262 address begin = _insts.start();
duke@435 263 if (_decode_begin != NULL && _decode_begin > begin)
duke@435 264 begin = _decode_begin;
duke@435 265 return begin;
duke@435 266 }
duke@435 267
duke@435 268
duke@435 269 GrowableArray<int>* CodeBuffer::create_patch_overflow() {
duke@435 270 if (_overflow_arena == NULL) {
zgu@3900 271 _overflow_arena = new (mtCode) Arena();
duke@435 272 }
duke@435 273 return new (_overflow_arena) GrowableArray<int>(_overflow_arena, 8, 0, 0);
duke@435 274 }
duke@435 275
duke@435 276
duke@435 277 // Helper function for managing labels and their target addresses.
duke@435 278 // Returns a sensible address, and if it is not the label's final
duke@435 279 // address, notes the dependency (at 'branch_pc') on the label.
duke@435 280 address CodeSection::target(Label& L, address branch_pc) {
duke@435 281 if (L.is_bound()) {
duke@435 282 int loc = L.loc();
duke@435 283 if (index() == CodeBuffer::locator_sect(loc)) {
duke@435 284 return start() + CodeBuffer::locator_pos(loc);
duke@435 285 } else {
duke@435 286 return outer()->locator_address(loc);
duke@435 287 }
duke@435 288 } else {
duke@435 289 assert(allocates2(branch_pc), "sanity");
duke@435 290 address base = start();
duke@435 291 int patch_loc = CodeBuffer::locator(branch_pc - base, index());
duke@435 292 L.add_patch_at(outer(), patch_loc);
duke@435 293
duke@435 294 // Need to return a pc, doesn't matter what it is since it will be
duke@435 295 // replaced during resolution later.
coleenp@548 296 // Don't return NULL or badAddress, since branches shouldn't overflow.
coleenp@548 297 // Don't return base either because that could overflow displacements
coleenp@548 298 // for shorter branches. It will get checked when bound.
coleenp@548 299 return branch_pc;
duke@435 300 }
duke@435 301 }
duke@435 302
duke@435 303 void CodeSection::relocate(address at, RelocationHolder const& spec, int format) {
duke@435 304 Relocation* reloc = spec.reloc();
duke@435 305 relocInfo::relocType rtype = (relocInfo::relocType) reloc->type();
duke@435 306 if (rtype == relocInfo::none) return;
duke@435 307
duke@435 308 // The assertion below has been adjusted, to also work for
duke@435 309 // relocation for fixup. Sometimes we want to put relocation
duke@435 310 // information for the next instruction, since it will be patched
duke@435 311 // with a call.
duke@435 312 assert(start() <= at && at <= end()+1,
duke@435 313 "cannot relocate data outside code boundaries");
duke@435 314
duke@435 315 if (!has_locs()) {
duke@435 316 // no space for relocation information provided => code cannot be
duke@435 317 // relocated. Make sure that relocate is only called with rtypes
duke@435 318 // that can be ignored for this kind of code.
duke@435 319 assert(rtype == relocInfo::none ||
duke@435 320 rtype == relocInfo::runtime_call_type ||
duke@435 321 rtype == relocInfo::internal_word_type||
duke@435 322 rtype == relocInfo::section_word_type ||
duke@435 323 rtype == relocInfo::external_word_type,
duke@435 324 "code needs relocation information");
duke@435 325 // leave behind an indication that we attempted a relocation
duke@435 326 DEBUG_ONLY(_locs_start = _locs_limit = (relocInfo*)badAddress);
duke@435 327 return;
duke@435 328 }
duke@435 329
duke@435 330 // Advance the point, noting the offset we'll have to record.
duke@435 331 csize_t offset = at - locs_point();
duke@435 332 set_locs_point(at);
duke@435 333
duke@435 334 // Test for a couple of overflow conditions; maybe expand the buffer.
duke@435 335 relocInfo* end = locs_end();
duke@435 336 relocInfo* req = end + relocInfo::length_limit;
duke@435 337 // Check for (potential) overflow
duke@435 338 if (req >= locs_limit() || offset >= relocInfo::offset_limit()) {
duke@435 339 req += (uint)offset / (uint)relocInfo::offset_limit();
duke@435 340 if (req >= locs_limit()) {
duke@435 341 // Allocate or reallocate.
duke@435 342 expand_locs(locs_count() + (req - end));
duke@435 343 // reload pointer
duke@435 344 end = locs_end();
duke@435 345 }
duke@435 346 }
duke@435 347
duke@435 348 // If the offset is giant, emit filler relocs, of type 'none', but
duke@435 349 // each carrying the largest possible offset, to advance the locs_point.
duke@435 350 while (offset >= relocInfo::offset_limit()) {
duke@435 351 assert(end < locs_limit(), "adjust previous paragraph of code");
duke@435 352 *end++ = filler_relocInfo();
duke@435 353 offset -= filler_relocInfo().addr_offset();
duke@435 354 }
duke@435 355
duke@435 356 // If it's a simple reloc with no data, we'll just write (rtype | offset).
duke@435 357 (*end) = relocInfo(rtype, offset, format);
duke@435 358
duke@435 359 // If it has data, insert the prefix, as (data_prefix_tag | data1), data2.
duke@435 360 end->initialize(this, reloc);
duke@435 361 }
duke@435 362
duke@435 363 void CodeSection::initialize_locs(int locs_capacity) {
duke@435 364 assert(_locs_start == NULL, "only one locs init step, please");
duke@435 365 // Apply a priori lower limits to relocation size:
duke@435 366 csize_t min_locs = MAX2(size() / 16, (csize_t)4);
duke@435 367 if (locs_capacity < min_locs) locs_capacity = min_locs;
duke@435 368 relocInfo* locs_start = NEW_RESOURCE_ARRAY(relocInfo, locs_capacity);
duke@435 369 _locs_start = locs_start;
duke@435 370 _locs_end = locs_start;
duke@435 371 _locs_limit = locs_start + locs_capacity;
duke@435 372 _locs_own = true;
duke@435 373 }
duke@435 374
duke@435 375 void CodeSection::initialize_shared_locs(relocInfo* buf, int length) {
duke@435 376 assert(_locs_start == NULL, "do this before locs are allocated");
duke@435 377 // Internal invariant: locs buf must be fully aligned.
duke@435 378 // See copy_relocations_to() below.
duke@435 379 while ((uintptr_t)buf % HeapWordSize != 0 && length > 0) {
duke@435 380 ++buf; --length;
duke@435 381 }
duke@435 382 if (length > 0) {
duke@435 383 _locs_start = buf;
duke@435 384 _locs_end = buf;
duke@435 385 _locs_limit = buf + length;
duke@435 386 _locs_own = false;
duke@435 387 }
duke@435 388 }
duke@435 389
duke@435 390 void CodeSection::initialize_locs_from(const CodeSection* source_cs) {
duke@435 391 int lcount = source_cs->locs_count();
duke@435 392 if (lcount != 0) {
duke@435 393 initialize_shared_locs(source_cs->locs_start(), lcount);
duke@435 394 _locs_end = _locs_limit = _locs_start + lcount;
duke@435 395 assert(is_allocated(), "must have copied code already");
duke@435 396 set_locs_point(start() + source_cs->locs_point_off());
duke@435 397 }
duke@435 398 assert(this->locs_count() == source_cs->locs_count(), "sanity");
duke@435 399 }
duke@435 400
duke@435 401 void CodeSection::expand_locs(int new_capacity) {
duke@435 402 if (_locs_start == NULL) {
duke@435 403 initialize_locs(new_capacity);
duke@435 404 return;
duke@435 405 } else {
duke@435 406 int old_count = locs_count();
duke@435 407 int old_capacity = locs_capacity();
duke@435 408 if (new_capacity < old_capacity * 2)
duke@435 409 new_capacity = old_capacity * 2;
duke@435 410 relocInfo* locs_start;
duke@435 411 if (_locs_own) {
duke@435 412 locs_start = REALLOC_RESOURCE_ARRAY(relocInfo, _locs_start, old_capacity, new_capacity);
duke@435 413 } else {
duke@435 414 locs_start = NEW_RESOURCE_ARRAY(relocInfo, new_capacity);
kvn@1958 415 Copy::conjoint_jbytes(_locs_start, locs_start, old_capacity * sizeof(relocInfo));
duke@435 416 _locs_own = true;
duke@435 417 }
duke@435 418 _locs_start = locs_start;
duke@435 419 _locs_end = locs_start + old_count;
duke@435 420 _locs_limit = locs_start + new_capacity;
duke@435 421 }
duke@435 422 }
duke@435 423
duke@435 424
duke@435 425 /// Support for emitting the code to its final location.
duke@435 426 /// The pattern is the same for all functions.
duke@435 427 /// We iterate over all the sections, padding each to alignment.
duke@435 428
twisti@2103 429 csize_t CodeBuffer::total_content_size() const {
twisti@2103 430 csize_t size_so_far = 0;
duke@435 431 for (int n = 0; n < (int)SECT_LIMIT; n++) {
duke@435 432 const CodeSection* cs = code_section(n);
duke@435 433 if (cs->is_empty()) continue; // skip trivial section
twisti@2103 434 size_so_far = cs->align_at_start(size_so_far);
twisti@2103 435 size_so_far += cs->size();
duke@435 436 }
twisti@2103 437 return size_so_far;
duke@435 438 }
duke@435 439
duke@435 440 void CodeBuffer::compute_final_layout(CodeBuffer* dest) const {
duke@435 441 address buf = dest->_total_start;
duke@435 442 csize_t buf_offset = 0;
twisti@2103 443 assert(dest->_total_size >= total_content_size(), "must be big enough");
duke@435 444
duke@435 445 {
duke@435 446 // not sure why this is here, but why not...
duke@435 447 int alignSize = MAX2((intx) sizeof(jdouble), CodeEntryAlignment);
duke@435 448 assert( (dest->_total_start - _insts.start()) % alignSize == 0, "copy must preserve alignment");
duke@435 449 }
duke@435 450
duke@435 451 const CodeSection* prev_cs = NULL;
duke@435 452 CodeSection* prev_dest_cs = NULL;
twisti@2117 453
twisti@2117 454 for (int n = (int) SECT_FIRST; n < (int) SECT_LIMIT; n++) {
duke@435 455 // figure compact layout of each section
duke@435 456 const CodeSection* cs = code_section(n);
twisti@2117 457 csize_t csize = cs->size();
duke@435 458
duke@435 459 CodeSection* dest_cs = dest->code_section(n);
duke@435 460 if (!cs->is_empty()) {
duke@435 461 // Compute initial padding; assign it to the previous non-empty guy.
duke@435 462 // Cf. figure_expanded_capacities.
duke@435 463 csize_t padding = cs->align_at_start(buf_offset) - buf_offset;
duke@435 464 if (padding != 0) {
duke@435 465 buf_offset += padding;
duke@435 466 assert(prev_dest_cs != NULL, "sanity");
duke@435 467 prev_dest_cs->_limit += padding;
duke@435 468 }
duke@435 469 #ifdef ASSERT
twisti@2117 470 if (prev_cs != NULL && prev_cs->is_frozen() && n < (SECT_LIMIT - 1)) {
duke@435 471 // Make sure the ends still match up.
duke@435 472 // This is important because a branch in a frozen section
duke@435 473 // might target code in a following section, via a Label,
duke@435 474 // and without a relocation record. See Label::patch_instructions.
duke@435 475 address dest_start = buf+buf_offset;
duke@435 476 csize_t start2start = cs->start() - prev_cs->start();
duke@435 477 csize_t dest_start2start = dest_start - prev_dest_cs->start();
duke@435 478 assert(start2start == dest_start2start, "cannot stretch frozen sect");
duke@435 479 }
duke@435 480 #endif //ASSERT
duke@435 481 prev_dest_cs = dest_cs;
duke@435 482 prev_cs = cs;
duke@435 483 }
duke@435 484
duke@435 485 debug_only(dest_cs->_start = NULL); // defeat double-initialization assert
duke@435 486 dest_cs->initialize(buf+buf_offset, csize);
duke@435 487 dest_cs->set_end(buf+buf_offset+csize);
duke@435 488 assert(dest_cs->is_allocated(), "must always be allocated");
duke@435 489 assert(cs->is_empty() == dest_cs->is_empty(), "sanity");
duke@435 490
duke@435 491 buf_offset += csize;
duke@435 492 }
duke@435 493
duke@435 494 // Done calculating sections; did it come out to the right end?
twisti@2103 495 assert(buf_offset == total_content_size(), "sanity");
never@3255 496 dest->verify_section_allocation();
duke@435 497 }
duke@435 498
coleenp@4037 499 void CodeBuffer::finalize_oop_references(methodHandle mh) {
coleenp@4037 500 No_Safepoint_Verifier nsv;
coleenp@4037 501
coleenp@4037 502 GrowableArray<oop> oops;
coleenp@4037 503
coleenp@4037 504 // Make sure that immediate metadata records something in the OopRecorder
coleenp@4037 505 for (int n = (int) SECT_FIRST; n < (int) SECT_LIMIT; n++) {
coleenp@4037 506 // pull code out of each section
coleenp@4037 507 CodeSection* cs = code_section(n);
coleenp@4037 508 if (cs->is_empty()) continue; // skip trivial section
coleenp@4037 509 RelocIterator iter(cs);
coleenp@4037 510 while (iter.next()) {
coleenp@4037 511 if (iter.type() == relocInfo::metadata_type) {
coleenp@4037 512 metadata_Relocation* md = iter.metadata_reloc();
coleenp@4037 513 if (md->metadata_is_immediate()) {
coleenp@4037 514 Metadata* m = md->metadata_value();
coleenp@4037 515 if (oop_recorder()->is_real(m)) {
coleenp@4037 516 oop o = NULL;
coleenp@4037 517 if (m->is_methodData()) {
coleenp@4037 518 m = ((MethodData*)m)->method();
coleenp@4037 519 }
coleenp@4037 520 if (m->is_method()) {
coleenp@4037 521 m = ((Method*)m)->method_holder();
coleenp@4037 522 }
coleenp@4037 523 if (m->is_klass()) {
coleenp@4037 524 o = ((Klass*)m)->class_loader();
coleenp@4037 525 } else {
coleenp@4037 526 // XXX This will currently occur for MDO which don't
coleenp@4037 527 // have a backpointer. This has to be fixed later.
coleenp@4037 528 m->print();
coleenp@4037 529 ShouldNotReachHere();
coleenp@4037 530 }
coleenp@4037 531 if (o != NULL && oops.find(o) == -1) {
coleenp@4037 532 oops.append(o);
coleenp@4037 533 }
coleenp@4037 534 }
coleenp@4037 535 }
coleenp@4037 536 }
coleenp@4037 537 }
coleenp@4037 538 }
coleenp@4037 539
coleenp@4037 540 if (!oop_recorder()->is_unused()) {
coleenp@4037 541 for (int i = 0; i < oop_recorder()->metadata_count(); i++) {
coleenp@4037 542 Metadata* m = oop_recorder()->metadata_at(i);
coleenp@4037 543 if (oop_recorder()->is_real(m)) {
coleenp@4037 544 oop o = NULL;
coleenp@4037 545 if (m->is_methodData()) {
coleenp@4037 546 m = ((MethodData*)m)->method();
coleenp@4037 547 }
coleenp@4037 548 if (m->is_method()) {
coleenp@4037 549 m = ((Method*)m)->method_holder();
coleenp@4037 550 }
coleenp@4037 551 if (m->is_klass()) {
coleenp@4037 552 o = ((Klass*)m)->class_loader();
coleenp@4037 553 } else {
coleenp@4037 554 m->print();
coleenp@4037 555 ShouldNotReachHere();
coleenp@4037 556 }
coleenp@4037 557 if (o != NULL && oops.find(o) == -1) {
coleenp@4037 558 oops.append(o);
coleenp@4037 559 }
coleenp@4037 560 }
coleenp@4037 561 }
coleenp@4037 562
coleenp@4037 563 }
coleenp@4037 564
coleenp@4037 565 // Add the class loader of Method* for the nmethod itself
coleenp@4037 566 oop cl = mh->method_holder()->class_loader();
coleenp@4037 567 if (cl != NULL) {
coleenp@4037 568 oops.append(cl);
coleenp@4037 569 }
coleenp@4037 570
coleenp@4037 571 // Add any oops that we've found
coleenp@4037 572 Thread* thread = Thread::current();
coleenp@4037 573 for (int i = 0; i < oops.length(); i++) {
coleenp@4037 574 oop_recorder()->find_index((jobject)thread->handle_area()->allocate_handle(oops.at(i)));
coleenp@4037 575 }
coleenp@4037 576 }
coleenp@4037 577
coleenp@4037 578
coleenp@4037 579
twisti@2117 580 csize_t CodeBuffer::total_offset_of(CodeSection* cs) const {
twisti@2117 581 csize_t size_so_far = 0;
twisti@2117 582 for (int n = (int) SECT_FIRST; n < (int) SECT_LIMIT; n++) {
twisti@2117 583 const CodeSection* cur_cs = code_section(n);
twisti@2117 584 if (!cur_cs->is_empty()) {
twisti@2117 585 size_so_far = cur_cs->align_at_start(size_so_far);
duke@435 586 }
twisti@2117 587 if (cur_cs->index() == cs->index()) {
twisti@2117 588 return size_so_far;
duke@435 589 }
twisti@2117 590 size_so_far += cur_cs->size();
duke@435 591 }
duke@435 592 ShouldNotReachHere();
duke@435 593 return -1;
duke@435 594 }
duke@435 595
duke@435 596 csize_t CodeBuffer::total_relocation_size() const {
duke@435 597 csize_t lsize = copy_relocations_to(NULL); // dry run only
twisti@2103 598 csize_t csize = total_content_size();
duke@435 599 csize_t total = RelocIterator::locs_and_index_size(csize, lsize);
duke@435 600 return (csize_t) align_size_up(total, HeapWordSize);
duke@435 601 }
duke@435 602
duke@435 603 csize_t CodeBuffer::copy_relocations_to(CodeBlob* dest) const {
duke@435 604 address buf = NULL;
duke@435 605 csize_t buf_offset = 0;
duke@435 606 csize_t buf_limit = 0;
duke@435 607 if (dest != NULL) {
duke@435 608 buf = (address)dest->relocation_begin();
duke@435 609 buf_limit = (address)dest->relocation_end() - buf;
duke@435 610 assert((uintptr_t)buf % HeapWordSize == 0, "buf must be fully aligned");
duke@435 611 assert(buf_limit % HeapWordSize == 0, "buf must be evenly sized");
duke@435 612 }
duke@435 613 // if dest == NULL, this is just the sizing pass
duke@435 614
duke@435 615 csize_t code_end_so_far = 0;
duke@435 616 csize_t code_point_so_far = 0;
twisti@2117 617 for (int n = (int) SECT_FIRST; n < (int)SECT_LIMIT; n++) {
duke@435 618 // pull relocs out of each section
duke@435 619 const CodeSection* cs = code_section(n);
duke@435 620 assert(!(cs->is_empty() && cs->locs_count() > 0), "sanity");
duke@435 621 if (cs->is_empty()) continue; // skip trivial section
duke@435 622 relocInfo* lstart = cs->locs_start();
duke@435 623 relocInfo* lend = cs->locs_end();
duke@435 624 csize_t lsize = (csize_t)( (address)lend - (address)lstart );
duke@435 625 csize_t csize = cs->size();
duke@435 626 code_end_so_far = cs->align_at_start(code_end_so_far);
duke@435 627
duke@435 628 if (lsize > 0) {
duke@435 629 // Figure out how to advance the combined relocation point
duke@435 630 // first to the beginning of this section.
duke@435 631 // We'll insert one or more filler relocs to span that gap.
duke@435 632 // (Don't bother to improve this by editing the first reloc's offset.)
duke@435 633 csize_t new_code_point = code_end_so_far;
duke@435 634 for (csize_t jump;
duke@435 635 code_point_so_far < new_code_point;
duke@435 636 code_point_so_far += jump) {
duke@435 637 jump = new_code_point - code_point_so_far;
duke@435 638 relocInfo filler = filler_relocInfo();
duke@435 639 if (jump >= filler.addr_offset()) {
duke@435 640 jump = filler.addr_offset();
duke@435 641 } else { // else shrink the filler to fit
duke@435 642 filler = relocInfo(relocInfo::none, jump);
duke@435 643 }
duke@435 644 if (buf != NULL) {
duke@435 645 assert(buf_offset + (csize_t)sizeof(filler) <= buf_limit, "filler in bounds");
duke@435 646 *(relocInfo*)(buf+buf_offset) = filler;
duke@435 647 }
duke@435 648 buf_offset += sizeof(filler);
duke@435 649 }
duke@435 650
duke@435 651 // Update code point and end to skip past this section:
duke@435 652 csize_t last_code_point = code_end_so_far + cs->locs_point_off();
duke@435 653 assert(code_point_so_far <= last_code_point, "sanity");
duke@435 654 code_point_so_far = last_code_point; // advance past this guy's relocs
duke@435 655 }
duke@435 656 code_end_so_far += csize; // advance past this guy's instructions too
duke@435 657
duke@435 658 // Done with filler; emit the real relocations:
duke@435 659 if (buf != NULL && lsize != 0) {
duke@435 660 assert(buf_offset + lsize <= buf_limit, "target in bounds");
duke@435 661 assert((uintptr_t)lstart % HeapWordSize == 0, "sane start");
duke@435 662 if (buf_offset % HeapWordSize == 0) {
duke@435 663 // Use wordwise copies if possible:
duke@435 664 Copy::disjoint_words((HeapWord*)lstart,
duke@435 665 (HeapWord*)(buf+buf_offset),
duke@435 666 (lsize + HeapWordSize-1) / HeapWordSize);
duke@435 667 } else {
kvn@1958 668 Copy::conjoint_jbytes(lstart, buf+buf_offset, lsize);
duke@435 669 }
duke@435 670 }
duke@435 671 buf_offset += lsize;
duke@435 672 }
duke@435 673
duke@435 674 // Align end of relocation info in target.
duke@435 675 while (buf_offset % HeapWordSize != 0) {
duke@435 676 if (buf != NULL) {
duke@435 677 relocInfo padding = relocInfo(relocInfo::none, 0);
duke@435 678 assert(buf_offset + (csize_t)sizeof(padding) <= buf_limit, "padding in bounds");
duke@435 679 *(relocInfo*)(buf+buf_offset) = padding;
duke@435 680 }
duke@435 681 buf_offset += sizeof(relocInfo);
duke@435 682 }
duke@435 683
twisti@2103 684 assert(code_end_so_far == total_content_size(), "sanity");
duke@435 685
duke@435 686 // Account for index:
duke@435 687 if (buf != NULL) {
duke@435 688 RelocIterator::create_index(dest->relocation_begin(),
duke@435 689 buf_offset / sizeof(relocInfo),
duke@435 690 dest->relocation_end());
duke@435 691 }
duke@435 692
duke@435 693 return buf_offset;
duke@435 694 }
duke@435 695
duke@435 696 void CodeBuffer::copy_code_to(CodeBlob* dest_blob) {
duke@435 697 #ifndef PRODUCT
duke@435 698 if (PrintNMethods && (WizardMode || Verbose)) {
duke@435 699 tty->print("done with CodeBuffer:");
duke@435 700 ((CodeBuffer*)this)->print();
duke@435 701 }
duke@435 702 #endif //PRODUCT
duke@435 703
twisti@2103 704 CodeBuffer dest(dest_blob);
twisti@2103 705 assert(dest_blob->content_size() >= total_content_size(), "good sizing");
duke@435 706 this->compute_final_layout(&dest);
duke@435 707 relocate_code_to(&dest);
duke@435 708
duke@435 709 // transfer comments from buffer to blob
duke@435 710 dest_blob->set_comments(_comments);
duke@435 711
duke@435 712 // Done moving code bytes; were they the right size?
twisti@2103 713 assert(round_to(dest.total_content_size(), oopSize) == dest_blob->content_size(), "sanity");
duke@435 714
duke@435 715 // Flush generated code
twisti@2103 716 ICache::invalidate_range(dest_blob->code_begin(), dest_blob->code_size());
duke@435 717 }
duke@435 718
twisti@2117 719 // Move all my code into another code buffer. Consult applicable
twisti@2117 720 // relocs to repair embedded addresses. The layout in the destination
twisti@2117 721 // CodeBuffer is different to the source CodeBuffer: the destination
twisti@2117 722 // CodeBuffer gets the final layout (consts, insts, stubs in order of
twisti@2117 723 // ascending address).
duke@435 724 void CodeBuffer::relocate_code_to(CodeBuffer* dest) const {
never@3236 725 address dest_end = dest->_total_start + dest->_total_size;
never@3236 726 address dest_filled = NULL;
twisti@2117 727 for (int n = (int) SECT_FIRST; n < (int) SECT_LIMIT; n++) {
duke@435 728 // pull code out of each section
duke@435 729 const CodeSection* cs = code_section(n);
duke@435 730 if (cs->is_empty()) continue; // skip trivial section
duke@435 731 CodeSection* dest_cs = dest->code_section(n);
duke@435 732 assert(cs->size() == dest_cs->size(), "sanity");
duke@435 733 csize_t usize = dest_cs->size();
duke@435 734 csize_t wsize = align_size_up(usize, HeapWordSize);
duke@435 735 assert(dest_cs->start() + wsize <= dest_end, "no overflow");
duke@435 736 // Copy the code as aligned machine words.
duke@435 737 // This may also include an uninitialized partial word at the end.
duke@435 738 Copy::disjoint_words((HeapWord*)cs->start(),
duke@435 739 (HeapWord*)dest_cs->start(),
duke@435 740 wsize / HeapWordSize);
duke@435 741
duke@435 742 if (dest->blob() == NULL) {
duke@435 743 // Destination is a final resting place, not just another buffer.
duke@435 744 // Normalize uninitialized bytes in the final padding.
duke@435 745 Copy::fill_to_bytes(dest_cs->end(), dest_cs->remaining(),
duke@435 746 Assembler::code_fill_byte());
duke@435 747 }
never@3236 748 // Keep track of the highest filled address
never@3236 749 dest_filled = MAX2(dest_filled, dest_cs->end() + dest_cs->remaining());
duke@435 750
duke@435 751 assert(cs->locs_start() != (relocInfo*)badAddress,
duke@435 752 "this section carries no reloc storage, but reloc was attempted");
duke@435 753
duke@435 754 // Make the new code copy use the old copy's relocations:
duke@435 755 dest_cs->initialize_locs_from(cs);
kvn@4316 756 }
duke@435 757
kvn@4316 758 // Do relocation after all sections are copied.
kvn@4316 759 // This is necessary if the code uses constants in stubs, which are
kvn@4316 760 // relocated when the corresponding instruction in the code (e.g., a
kvn@4316 761 // call) is relocated. Stubs are placed behind the main code
kvn@4316 762 // section, so that section has to be copied before relocating.
kvn@4316 763 for (int n = (int) SECT_FIRST; n < (int)SECT_LIMIT; n++) {
kvn@4316 764 // pull code out of each section
kvn@4316 765 const CodeSection* cs = code_section(n);
kvn@4316 766 if (cs->is_empty()) continue; // skip trivial section
kvn@4316 767 CodeSection* dest_cs = dest->code_section(n);
duke@435 768 { // Repair the pc relative information in the code after the move
duke@435 769 RelocIterator iter(dest_cs);
duke@435 770 while (iter.next()) {
duke@435 771 iter.reloc()->fix_relocation_after_move(this, dest);
duke@435 772 }
duke@435 773 }
duke@435 774 }
never@3236 775
twisti@4237 776 if (dest->blob() == NULL && dest_filled != NULL) {
never@3236 777 // Destination is a final resting place, not just another buffer.
never@3236 778 // Normalize uninitialized bytes in the final padding.
never@3236 779 Copy::fill_to_bytes(dest_filled, dest_end - dest_filled,
never@3236 780 Assembler::code_fill_byte());
never@3236 781
never@3236 782 }
duke@435 783 }
duke@435 784
duke@435 785 csize_t CodeBuffer::figure_expanded_capacities(CodeSection* which_cs,
duke@435 786 csize_t amount,
duke@435 787 csize_t* new_capacity) {
duke@435 788 csize_t new_total_cap = 0;
duke@435 789
twisti@2117 790 for (int n = (int) SECT_FIRST; n < (int) SECT_LIMIT; n++) {
duke@435 791 const CodeSection* sect = code_section(n);
duke@435 792
duke@435 793 if (!sect->is_empty()) {
twisti@2117 794 // Compute initial padding; assign it to the previous section,
twisti@2117 795 // even if it's empty (e.g. consts section can be empty).
twisti@2117 796 // Cf. compute_final_layout
duke@435 797 csize_t padding = sect->align_at_start(new_total_cap) - new_total_cap;
duke@435 798 if (padding != 0) {
duke@435 799 new_total_cap += padding;
twisti@2117 800 assert(n - 1 >= SECT_FIRST, "sanity");
twisti@2117 801 new_capacity[n - 1] += padding;
duke@435 802 }
duke@435 803 }
duke@435 804
duke@435 805 csize_t exp = sect->size(); // 100% increase
duke@435 806 if ((uint)exp < 4*K) exp = 4*K; // minimum initial increase
duke@435 807 if (sect == which_cs) {
duke@435 808 if (exp < amount) exp = amount;
duke@435 809 if (StressCodeBuffers) exp = amount; // expand only slightly
duke@435 810 } else if (n == SECT_INSTS) {
duke@435 811 // scale down inst increases to a more modest 25%
duke@435 812 exp = 4*K + ((exp - 4*K) >> 2);
duke@435 813 if (StressCodeBuffers) exp = amount / 2; // expand only slightly
duke@435 814 } else if (sect->is_empty()) {
duke@435 815 // do not grow an empty secondary section
duke@435 816 exp = 0;
duke@435 817 }
duke@435 818 // Allow for inter-section slop:
duke@435 819 exp += CodeSection::end_slop();
duke@435 820 csize_t new_cap = sect->size() + exp;
duke@435 821 if (new_cap < sect->capacity()) {
duke@435 822 // No need to expand after all.
duke@435 823 new_cap = sect->capacity();
duke@435 824 }
duke@435 825 new_capacity[n] = new_cap;
duke@435 826 new_total_cap += new_cap;
duke@435 827 }
duke@435 828
duke@435 829 return new_total_cap;
duke@435 830 }
duke@435 831
duke@435 832 void CodeBuffer::expand(CodeSection* which_cs, csize_t amount) {
duke@435 833 #ifndef PRODUCT
duke@435 834 if (PrintNMethods && (WizardMode || Verbose)) {
duke@435 835 tty->print("expanding CodeBuffer:");
duke@435 836 this->print();
duke@435 837 }
duke@435 838
duke@435 839 if (StressCodeBuffers && blob() != NULL) {
duke@435 840 static int expand_count = 0;
duke@435 841 if (expand_count >= 0) expand_count += 1;
duke@435 842 if (expand_count > 100 && is_power_of_2(expand_count)) {
duke@435 843 tty->print_cr("StressCodeBuffers: have expanded %d times", expand_count);
duke@435 844 // simulate an occasional allocation failure:
duke@435 845 free_blob();
duke@435 846 }
duke@435 847 }
duke@435 848 #endif //PRODUCT
duke@435 849
duke@435 850 // Resizing must be allowed
duke@435 851 {
duke@435 852 if (blob() == NULL) return; // caller must check for blob == NULL
duke@435 853 for (int n = 0; n < (int)SECT_LIMIT; n++) {
duke@435 854 guarantee(!code_section(n)->is_frozen(), "resizing not allowed when frozen");
duke@435 855 }
duke@435 856 }
duke@435 857
duke@435 858 // Figure new capacity for each section.
duke@435 859 csize_t new_capacity[SECT_LIMIT];
duke@435 860 csize_t new_total_cap
duke@435 861 = figure_expanded_capacities(which_cs, amount, new_capacity);
duke@435 862
duke@435 863 // Create a new (temporary) code buffer to hold all the new data
duke@435 864 CodeBuffer cb(name(), new_total_cap, 0);
duke@435 865 if (cb.blob() == NULL) {
duke@435 866 // Failed to allocate in code cache.
duke@435 867 free_blob();
duke@435 868 return;
duke@435 869 }
duke@435 870
duke@435 871 // Create an old code buffer to remember which addresses used to go where.
duke@435 872 // This will be useful when we do final assembly into the code cache,
duke@435 873 // because we will need to know how to warp any internal address that
duke@435 874 // has been created at any time in this CodeBuffer's past.
duke@435 875 CodeBuffer* bxp = new CodeBuffer(_total_start, _total_size);
duke@435 876 bxp->take_over_code_from(this); // remember the old undersized blob
duke@435 877 DEBUG_ONLY(this->_blob = NULL); // silence a later assert
duke@435 878 bxp->_before_expand = this->_before_expand;
duke@435 879 this->_before_expand = bxp;
duke@435 880
duke@435 881 // Give each section its required (expanded) capacity.
twisti@2117 882 for (int n = (int)SECT_LIMIT-1; n >= SECT_FIRST; n--) {
duke@435 883 CodeSection* cb_sect = cb.code_section(n);
duke@435 884 CodeSection* this_sect = code_section(n);
duke@435 885 if (new_capacity[n] == 0) continue; // already nulled out
twisti@2117 886 if (n != SECT_INSTS) {
duke@435 887 cb.initialize_section_size(cb_sect, new_capacity[n]);
duke@435 888 }
duke@435 889 assert(cb_sect->capacity() >= new_capacity[n], "big enough");
duke@435 890 address cb_start = cb_sect->start();
duke@435 891 cb_sect->set_end(cb_start + this_sect->size());
duke@435 892 if (this_sect->mark() == NULL) {
duke@435 893 cb_sect->clear_mark();
duke@435 894 } else {
duke@435 895 cb_sect->set_mark(cb_start + this_sect->mark_off());
duke@435 896 }
duke@435 897 }
duke@435 898
duke@435 899 // Move all the code and relocations to the new blob:
duke@435 900 relocate_code_to(&cb);
duke@435 901
duke@435 902 // Copy the temporary code buffer into the current code buffer.
duke@435 903 // Basically, do {*this = cb}, except for some control information.
duke@435 904 this->take_over_code_from(&cb);
duke@435 905 cb.set_blob(NULL);
duke@435 906
duke@435 907 // Zap the old code buffer contents, to avoid mistakenly using them.
duke@435 908 debug_only(Copy::fill_to_bytes(bxp->_total_start, bxp->_total_size,
duke@435 909 badCodeHeapFreeVal));
duke@435 910
duke@435 911 _decode_begin = NULL; // sanity
duke@435 912
duke@435 913 // Make certain that the new sections are all snugly inside the new blob.
never@3255 914 verify_section_allocation();
duke@435 915
duke@435 916 #ifndef PRODUCT
duke@435 917 if (PrintNMethods && (WizardMode || Verbose)) {
duke@435 918 tty->print("expanded CodeBuffer:");
duke@435 919 this->print();
duke@435 920 }
duke@435 921 #endif //PRODUCT
duke@435 922 }
duke@435 923
duke@435 924 void CodeBuffer::take_over_code_from(CodeBuffer* cb) {
duke@435 925 // Must already have disposed of the old blob somehow.
duke@435 926 assert(blob() == NULL, "must be empty");
duke@435 927 #ifdef ASSERT
duke@435 928
duke@435 929 #endif
duke@435 930 // Take the new blob away from cb.
duke@435 931 set_blob(cb->blob());
duke@435 932 // Take over all the section pointers.
duke@435 933 for (int n = 0; n < (int)SECT_LIMIT; n++) {
duke@435 934 CodeSection* cb_sect = cb->code_section(n);
duke@435 935 CodeSection* this_sect = code_section(n);
duke@435 936 this_sect->take_over_code_from(cb_sect);
duke@435 937 }
duke@435 938 _overflow_arena = cb->_overflow_arena;
duke@435 939 // Make sure the old cb won't try to use it or free it.
duke@435 940 DEBUG_ONLY(cb->_blob = (BufferBlob*)badAddress);
duke@435 941 }
duke@435 942
never@3255 943 void CodeBuffer::verify_section_allocation() {
duke@435 944 address tstart = _total_start;
never@3255 945 if (tstart == badAddress) return; // smashed by set_blob(NULL)
duke@435 946 address tend = tstart + _total_size;
duke@435 947 if (_blob != NULL) {
never@3255 948
never@3255 949 guarantee(tstart >= _blob->content_begin(), "sanity");
never@3255 950 guarantee(tend <= _blob->content_end(), "sanity");
duke@435 951 }
twisti@2117 952 // Verify disjointness.
twisti@2117 953 for (int n = (int) SECT_FIRST; n < (int) SECT_LIMIT; n++) {
duke@435 954 CodeSection* sect = code_section(n);
twisti@2117 955 if (!sect->is_allocated() || sect->is_empty()) continue;
never@3255 956 guarantee((intptr_t)sect->start() % sect->alignment() == 0
duke@435 957 || sect->is_empty() || _blob == NULL,
duke@435 958 "start is aligned");
twisti@2117 959 for (int m = (int) SECT_FIRST; m < (int) SECT_LIMIT; m++) {
twisti@2117 960 CodeSection* other = code_section(m);
twisti@2117 961 if (!other->is_allocated() || other == sect) continue;
never@3255 962 guarantee(!other->contains(sect->start() ), "sanity");
twisti@2117 963 // limit is an exclusive address and can be the start of another
twisti@2117 964 // section.
never@3255 965 guarantee(!other->contains(sect->limit() - 1), "sanity");
twisti@2117 966 }
never@3255 967 guarantee(sect->end() <= tend, "sanity");
never@3255 968 guarantee(sect->end() <= sect->limit(), "sanity");
duke@435 969 }
duke@435 970 }
never@3255 971
never@3255 972 void CodeBuffer::log_section_sizes(const char* name) {
never@3255 973 if (xtty != NULL) {
never@3255 974 // log info about buffer usage
never@3255 975 xtty->print_cr("<blob name='%s' size='%d'>", name, _total_size);
never@3255 976 for (int n = (int) CodeBuffer::SECT_FIRST; n < (int) CodeBuffer::SECT_LIMIT; n++) {
never@3255 977 CodeSection* sect = code_section(n);
never@3255 978 if (!sect->is_allocated() || sect->is_empty()) continue;
never@3255 979 xtty->print_cr("<sect index='%d' size='" SIZE_FORMAT "' free='" SIZE_FORMAT "'/>",
never@3255 980 n, sect->limit() - sect->start(), sect->limit() - sect->end());
never@3255 981 }
never@3255 982 xtty->print_cr("</blob>");
never@3255 983 }
never@3255 984 }
duke@435 985
duke@435 986 #ifndef PRODUCT
duke@435 987
duke@435 988 void CodeSection::dump() {
duke@435 989 address ptr = start();
duke@435 990 for (csize_t step; ptr < end(); ptr += step) {
duke@435 991 step = end() - ptr;
duke@435 992 if (step > jintSize * 4) step = jintSize * 4;
duke@435 993 tty->print(PTR_FORMAT ": ", ptr);
duke@435 994 while (step > 0) {
duke@435 995 tty->print(" " PTR32_FORMAT, *(jint*)ptr);
duke@435 996 ptr += jintSize;
duke@435 997 }
duke@435 998 tty->cr();
duke@435 999 }
duke@435 1000 }
duke@435 1001
duke@435 1002
duke@435 1003 void CodeSection::decode() {
duke@435 1004 Disassembler::decode(start(), end());
duke@435 1005 }
duke@435 1006
duke@435 1007
duke@435 1008 void CodeBuffer::block_comment(intptr_t offset, const char * comment) {
duke@435 1009 _comments.add_comment(offset, comment);
duke@435 1010 }
duke@435 1011
zgu@3900 1012 class CodeComment: public CHeapObj<mtCode> {
duke@435 1013 private:
duke@435 1014 friend class CodeComments;
duke@435 1015 intptr_t _offset;
duke@435 1016 const char * _comment;
duke@435 1017 CodeComment* _next;
duke@435 1018
duke@435 1019 ~CodeComment() {
duke@435 1020 assert(_next == NULL, "wrong interface for freeing list");
zgu@3900 1021 os::free((void*)_comment, mtCode);
duke@435 1022 }
duke@435 1023
duke@435 1024 public:
duke@435 1025 CodeComment(intptr_t offset, const char * comment) {
duke@435 1026 _offset = offset;
zgu@3900 1027 _comment = os::strdup(comment, mtCode);
duke@435 1028 _next = NULL;
duke@435 1029 }
duke@435 1030
duke@435 1031 intptr_t offset() const { return _offset; }
duke@435 1032 const char * comment() const { return _comment; }
duke@435 1033 CodeComment* next() { return _next; }
duke@435 1034
duke@435 1035 void set_next(CodeComment* next) { _next = next; }
duke@435 1036
duke@435 1037 CodeComment* find(intptr_t offset) {
duke@435 1038 CodeComment* a = this;
duke@435 1039 while (a != NULL && a->_offset != offset) {
duke@435 1040 a = a->_next;
duke@435 1041 }
duke@435 1042 return a;
duke@435 1043 }
kvn@4107 1044
kvn@4107 1045 // Convenience for add_comment.
kvn@4107 1046 CodeComment* find_last(intptr_t offset) {
kvn@4107 1047 CodeComment* a = find(offset);
kvn@4107 1048 if (a != NULL) {
kvn@4107 1049 while ((a->_next != NULL) && (a->_next->_offset == offset)) {
kvn@4107 1050 a = a->_next;
kvn@4107 1051 }
kvn@4107 1052 }
kvn@4107 1053 return a;
kvn@4107 1054 }
duke@435 1055 };
duke@435 1056
duke@435 1057
duke@435 1058 void CodeComments::add_comment(intptr_t offset, const char * comment) {
kvn@4107 1059 CodeComment* c = new CodeComment(offset, comment);
kvn@4107 1060 CodeComment* inspos = (_comments == NULL) ? NULL : _comments->find_last(offset);
kvn@4107 1061
kvn@4107 1062 if (inspos) {
kvn@4107 1063 // insert after already existing comments with same offset
kvn@4107 1064 c->set_next(inspos->next());
kvn@4107 1065 inspos->set_next(c);
duke@435 1066 } else {
kvn@4107 1067 // no comments with such offset, yet. Insert before anything else.
duke@435 1068 c->set_next(_comments);
duke@435 1069 _comments = c;
duke@435 1070 }
duke@435 1071 }
duke@435 1072
duke@435 1073
duke@435 1074 void CodeComments::assign(CodeComments& other) {
duke@435 1075 _comments = other._comments;
duke@435 1076 }
duke@435 1077
duke@435 1078
kvn@4107 1079 void CodeComments::print_block_comment(outputStream* stream, intptr_t offset) const {
duke@435 1080 if (_comments != NULL) {
duke@435 1081 CodeComment* c = _comments->find(offset);
duke@435 1082 while (c && c->offset() == offset) {
jrose@535 1083 stream->bol();
duke@435 1084 stream->print(" ;; ");
duke@435 1085 stream->print_cr(c->comment());
duke@435 1086 c = c->next();
duke@435 1087 }
duke@435 1088 }
duke@435 1089 }
duke@435 1090
duke@435 1091
duke@435 1092 void CodeComments::free() {
duke@435 1093 CodeComment* n = _comments;
duke@435 1094 while (n) {
duke@435 1095 // unlink the node from the list saving a pointer to the next
duke@435 1096 CodeComment* p = n->_next;
duke@435 1097 n->_next = NULL;
duke@435 1098 delete n;
duke@435 1099 n = p;
duke@435 1100 }
duke@435 1101 _comments = NULL;
duke@435 1102 }
duke@435 1103
duke@435 1104
duke@435 1105
duke@435 1106 void CodeBuffer::decode() {
kvn@4107 1107 ttyLocker ttyl;
twisti@2103 1108 Disassembler::decode(decode_begin(), insts_end());
twisti@2103 1109 _decode_begin = insts_end();
duke@435 1110 }
duke@435 1111
duke@435 1112
duke@435 1113 void CodeBuffer::skip_decode() {
twisti@2103 1114 _decode_begin = insts_end();
duke@435 1115 }
duke@435 1116
duke@435 1117
duke@435 1118 void CodeBuffer::decode_all() {
kvn@4107 1119 ttyLocker ttyl;
duke@435 1120 for (int n = 0; n < (int)SECT_LIMIT; n++) {
duke@435 1121 // dump contents of each section
duke@435 1122 CodeSection* cs = code_section(n);
duke@435 1123 tty->print_cr("! %s:", code_section_name(n));
duke@435 1124 if (cs != consts())
duke@435 1125 cs->decode();
duke@435 1126 else
duke@435 1127 cs->dump();
duke@435 1128 }
duke@435 1129 }
duke@435 1130
duke@435 1131
duke@435 1132 void CodeSection::print(const char* name) {
duke@435 1133 csize_t locs_size = locs_end() - locs_start();
duke@435 1134 tty->print_cr(" %7s.code = " PTR_FORMAT " : " PTR_FORMAT " : " PTR_FORMAT " (%d of %d)%s",
duke@435 1135 name, start(), end(), limit(), size(), capacity(),
duke@435 1136 is_frozen()? " [frozen]": "");
duke@435 1137 tty->print_cr(" %7s.locs = " PTR_FORMAT " : " PTR_FORMAT " : " PTR_FORMAT " (%d of %d) point=%d",
duke@435 1138 name, locs_start(), locs_end(), locs_limit(), locs_size, locs_capacity(), locs_point_off());
duke@435 1139 if (PrintRelocations) {
duke@435 1140 RelocIterator iter(this);
duke@435 1141 iter.print();
duke@435 1142 }
duke@435 1143 }
duke@435 1144
duke@435 1145 void CodeBuffer::print() {
duke@435 1146 if (this == NULL) {
duke@435 1147 tty->print_cr("NULL CodeBuffer pointer");
duke@435 1148 return;
duke@435 1149 }
duke@435 1150
duke@435 1151 tty->print_cr("CodeBuffer:");
duke@435 1152 for (int n = 0; n < (int)SECT_LIMIT; n++) {
duke@435 1153 // print each section
duke@435 1154 CodeSection* cs = code_section(n);
duke@435 1155 cs->print(code_section_name(n));
duke@435 1156 }
duke@435 1157 }
duke@435 1158
duke@435 1159 #endif // PRODUCT

mercurial