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