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-2007 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 return base; 289 } 290 } 291 292 void CodeSection::relocate(address at, RelocationHolder const& spec, int format) { 293 Relocation* reloc = spec.reloc(); 294 relocInfo::relocType rtype = (relocInfo::relocType) reloc->type(); 295 if (rtype == relocInfo::none) return; 296 297 // The assertion below has been adjusted, to also work for 298 // relocation for fixup. Sometimes we want to put relocation 299 // information for the next instruction, since it will be patched 300 // with a call. 301 assert(start() <= at && at <= end()+1, 302 "cannot relocate data outside code boundaries"); 303 304 if (!has_locs()) { 305 // no space for relocation information provided => code cannot be 306 // relocated. Make sure that relocate is only called with rtypes 307 // that can be ignored for this kind of code. 308 assert(rtype == relocInfo::none || 309 rtype == relocInfo::runtime_call_type || 310 rtype == relocInfo::internal_word_type|| 311 rtype == relocInfo::section_word_type || 312 rtype == relocInfo::external_word_type, 313 "code needs relocation information"); 314 // leave behind an indication that we attempted a relocation 315 DEBUG_ONLY(_locs_start = _locs_limit = (relocInfo*)badAddress); 316 return; 317 } 318 319 // Advance the point, noting the offset we'll have to record. 320 csize_t offset = at - locs_point(); 321 set_locs_point(at); 322 323 // Test for a couple of overflow conditions; maybe expand the buffer. 324 relocInfo* end = locs_end(); 325 relocInfo* req = end + relocInfo::length_limit; 326 // Check for (potential) overflow 327 if (req >= locs_limit() || offset >= relocInfo::offset_limit()) { 328 req += (uint)offset / (uint)relocInfo::offset_limit(); 329 if (req >= locs_limit()) { 330 // Allocate or reallocate. 331 expand_locs(locs_count() + (req - end)); 332 // reload pointer 333 end = locs_end(); 334 } 335 } 336 337 // If the offset is giant, emit filler relocs, of type 'none', but 338 // each carrying the largest possible offset, to advance the locs_point. 339 while (offset >= relocInfo::offset_limit()) { 340 assert(end < locs_limit(), "adjust previous paragraph of code"); 341 *end++ = filler_relocInfo(); 342 offset -= filler_relocInfo().addr_offset(); 343 } 344 345 // If it's a simple reloc with no data, we'll just write (rtype | offset). 346 (*end) = relocInfo(rtype, offset, format); 347 348 // If it has data, insert the prefix, as (data_prefix_tag | data1), data2. 349 end->initialize(this, reloc); 350 } 351 352 void CodeSection::initialize_locs(int locs_capacity) { 353 assert(_locs_start == NULL, "only one locs init step, please"); 354 // Apply a priori lower limits to relocation size: 355 csize_t min_locs = MAX2(size() / 16, (csize_t)4); 356 if (locs_capacity < min_locs) locs_capacity = min_locs; 357 relocInfo* locs_start = NEW_RESOURCE_ARRAY(relocInfo, locs_capacity); 358 _locs_start = locs_start; 359 _locs_end = locs_start; 360 _locs_limit = locs_start + locs_capacity; 361 _locs_own = true; 362 } 363 364 void CodeSection::initialize_shared_locs(relocInfo* buf, int length) { 365 assert(_locs_start == NULL, "do this before locs are allocated"); 366 // Internal invariant: locs buf must be fully aligned. 367 // See copy_relocations_to() below. 368 while ((uintptr_t)buf % HeapWordSize != 0 && length > 0) { 369 ++buf; --length; 370 } 371 if (length > 0) { 372 _locs_start = buf; 373 _locs_end = buf; 374 _locs_limit = buf + length; 375 _locs_own = false; 376 } 377 } 378 379 void CodeSection::initialize_locs_from(const CodeSection* source_cs) { 380 int lcount = source_cs->locs_count(); 381 if (lcount != 0) { 382 initialize_shared_locs(source_cs->locs_start(), lcount); 383 _locs_end = _locs_limit = _locs_start + lcount; 384 assert(is_allocated(), "must have copied code already"); 385 set_locs_point(start() + source_cs->locs_point_off()); 386 } 387 assert(this->locs_count() == source_cs->locs_count(), "sanity"); 388 } 389 390 void CodeSection::expand_locs(int new_capacity) { 391 if (_locs_start == NULL) { 392 initialize_locs(new_capacity); 393 return; 394 } else { 395 int old_count = locs_count(); 396 int old_capacity = locs_capacity(); 397 if (new_capacity < old_capacity * 2) 398 new_capacity = old_capacity * 2; 399 relocInfo* locs_start; 400 if (_locs_own) { 401 locs_start = REALLOC_RESOURCE_ARRAY(relocInfo, _locs_start, old_capacity, new_capacity); 402 } else { 403 locs_start = NEW_RESOURCE_ARRAY(relocInfo, new_capacity); 404 Copy::conjoint_bytes(_locs_start, locs_start, old_capacity * sizeof(relocInfo)); 405 _locs_own = true; 406 } 407 _locs_start = locs_start; 408 _locs_end = locs_start + old_count; 409 _locs_limit = locs_start + new_capacity; 410 } 411 } 412 413 414 /// Support for emitting the code to its final location. 415 /// The pattern is the same for all functions. 416 /// We iterate over all the sections, padding each to alignment. 417 418 csize_t CodeBuffer::total_code_size() const { 419 csize_t code_size_so_far = 0; 420 for (int n = 0; n < (int)SECT_LIMIT; n++) { 421 const CodeSection* cs = code_section(n); 422 if (cs->is_empty()) continue; // skip trivial section 423 code_size_so_far = cs->align_at_start(code_size_so_far); 424 code_size_so_far += cs->size(); 425 } 426 return code_size_so_far; 427 } 428 429 void CodeBuffer::compute_final_layout(CodeBuffer* dest) const { 430 address buf = dest->_total_start; 431 csize_t buf_offset = 0; 432 assert(dest->_total_size >= total_code_size(), "must be big enough"); 433 434 { 435 // not sure why this is here, but why not... 436 int alignSize = MAX2((intx) sizeof(jdouble), CodeEntryAlignment); 437 assert( (dest->_total_start - _insts.start()) % alignSize == 0, "copy must preserve alignment"); 438 } 439 440 const CodeSection* prev_cs = NULL; 441 CodeSection* prev_dest_cs = NULL; 442 for (int n = 0; n < (int)SECT_LIMIT; n++) { 443 // figure compact layout of each section 444 const CodeSection* cs = code_section(n); 445 address cstart = cs->start(); 446 address cend = cs->end(); 447 csize_t csize = cend - cstart; 448 449 CodeSection* dest_cs = dest->code_section(n); 450 if (!cs->is_empty()) { 451 // Compute initial padding; assign it to the previous non-empty guy. 452 // Cf. figure_expanded_capacities. 453 csize_t padding = cs->align_at_start(buf_offset) - buf_offset; 454 if (padding != 0) { 455 buf_offset += padding; 456 assert(prev_dest_cs != NULL, "sanity"); 457 prev_dest_cs->_limit += padding; 458 } 459 #ifdef ASSERT 460 if (prev_cs != NULL && prev_cs->is_frozen() && n < SECT_CONSTS) { 461 // Make sure the ends still match up. 462 // This is important because a branch in a frozen section 463 // might target code in a following section, via a Label, 464 // and without a relocation record. See Label::patch_instructions. 465 address dest_start = buf+buf_offset; 466 csize_t start2start = cs->start() - prev_cs->start(); 467 csize_t dest_start2start = dest_start - prev_dest_cs->start(); 468 assert(start2start == dest_start2start, "cannot stretch frozen sect"); 469 } 470 #endif //ASSERT 471 prev_dest_cs = dest_cs; 472 prev_cs = cs; 473 } 474 475 debug_only(dest_cs->_start = NULL); // defeat double-initialization assert 476 dest_cs->initialize(buf+buf_offset, csize); 477 dest_cs->set_end(buf+buf_offset+csize); 478 assert(dest_cs->is_allocated(), "must always be allocated"); 479 assert(cs->is_empty() == dest_cs->is_empty(), "sanity"); 480 481 buf_offset += csize; 482 } 483 484 // Done calculating sections; did it come out to the right end? 485 assert(buf_offset == total_code_size(), "sanity"); 486 assert(dest->verify_section_allocation(), "final configuration works"); 487 } 488 489 csize_t CodeBuffer::total_offset_of(address addr) const { 490 csize_t code_size_so_far = 0; 491 for (int n = 0; n < (int)SECT_LIMIT; n++) { 492 const CodeSection* cs = code_section(n); 493 if (!cs->is_empty()) { 494 code_size_so_far = cs->align_at_start(code_size_so_far); 495 } 496 if (cs->contains2(addr)) { 497 return code_size_so_far + (addr - cs->start()); 498 } 499 code_size_so_far += cs->size(); 500 } 501 #ifndef PRODUCT 502 tty->print_cr("Dangling address " PTR_FORMAT " in:", addr); 503 ((CodeBuffer*)this)->print(); 504 #endif 505 ShouldNotReachHere(); 506 return -1; 507 } 508 509 csize_t CodeBuffer::total_relocation_size() const { 510 csize_t lsize = copy_relocations_to(NULL); // dry run only 511 csize_t csize = total_code_size(); 512 csize_t total = RelocIterator::locs_and_index_size(csize, lsize); 513 return (csize_t) align_size_up(total, HeapWordSize); 514 } 515 516 csize_t CodeBuffer::copy_relocations_to(CodeBlob* dest) const { 517 address buf = NULL; 518 csize_t buf_offset = 0; 519 csize_t buf_limit = 0; 520 if (dest != NULL) { 521 buf = (address)dest->relocation_begin(); 522 buf_limit = (address)dest->relocation_end() - buf; 523 assert((uintptr_t)buf % HeapWordSize == 0, "buf must be fully aligned"); 524 assert(buf_limit % HeapWordSize == 0, "buf must be evenly sized"); 525 } 526 // if dest == NULL, this is just the sizing pass 527 528 csize_t code_end_so_far = 0; 529 csize_t code_point_so_far = 0; 530 for (int n = 0; n < (int)SECT_LIMIT; n++) { 531 // pull relocs out of each section 532 const CodeSection* cs = code_section(n); 533 assert(!(cs->is_empty() && cs->locs_count() > 0), "sanity"); 534 if (cs->is_empty()) continue; // skip trivial section 535 relocInfo* lstart = cs->locs_start(); 536 relocInfo* lend = cs->locs_end(); 537 csize_t lsize = (csize_t)( (address)lend - (address)lstart ); 538 csize_t csize = cs->size(); 539 code_end_so_far = cs->align_at_start(code_end_so_far); 540 541 if (lsize > 0) { 542 // Figure out how to advance the combined relocation point 543 // first to the beginning of this section. 544 // We'll insert one or more filler relocs to span that gap. 545 // (Don't bother to improve this by editing the first reloc's offset.) 546 csize_t new_code_point = code_end_so_far; 547 for (csize_t jump; 548 code_point_so_far < new_code_point; 549 code_point_so_far += jump) { 550 jump = new_code_point - code_point_so_far; 551 relocInfo filler = filler_relocInfo(); 552 if (jump >= filler.addr_offset()) { 553 jump = filler.addr_offset(); 554 } else { // else shrink the filler to fit 555 filler = relocInfo(relocInfo::none, jump); 556 } 557 if (buf != NULL) { 558 assert(buf_offset + (csize_t)sizeof(filler) <= buf_limit, "filler in bounds"); 559 *(relocInfo*)(buf+buf_offset) = filler; 560 } 561 buf_offset += sizeof(filler); 562 } 563 564 // Update code point and end to skip past this section: 565 csize_t last_code_point = code_end_so_far + cs->locs_point_off(); 566 assert(code_point_so_far <= last_code_point, "sanity"); 567 code_point_so_far = last_code_point; // advance past this guy's relocs 568 } 569 code_end_so_far += csize; // advance past this guy's instructions too 570 571 // Done with filler; emit the real relocations: 572 if (buf != NULL && lsize != 0) { 573 assert(buf_offset + lsize <= buf_limit, "target in bounds"); 574 assert((uintptr_t)lstart % HeapWordSize == 0, "sane start"); 575 if (buf_offset % HeapWordSize == 0) { 576 // Use wordwise copies if possible: 577 Copy::disjoint_words((HeapWord*)lstart, 578 (HeapWord*)(buf+buf_offset), 579 (lsize + HeapWordSize-1) / HeapWordSize); 580 } else { 581 Copy::conjoint_bytes(lstart, buf+buf_offset, lsize); 582 } 583 } 584 buf_offset += lsize; 585 } 586 587 // Align end of relocation info in target. 588 while (buf_offset % HeapWordSize != 0) { 589 if (buf != NULL) { 590 relocInfo padding = relocInfo(relocInfo::none, 0); 591 assert(buf_offset + (csize_t)sizeof(padding) <= buf_limit, "padding in bounds"); 592 *(relocInfo*)(buf+buf_offset) = padding; 593 } 594 buf_offset += sizeof(relocInfo); 595 } 596 597 assert(code_end_so_far == total_code_size(), "sanity"); 598 599 // Account for index: 600 if (buf != NULL) { 601 RelocIterator::create_index(dest->relocation_begin(), 602 buf_offset / sizeof(relocInfo), 603 dest->relocation_end()); 604 } 605 606 return buf_offset; 607 } 608 609 void CodeBuffer::copy_code_to(CodeBlob* dest_blob) { 610 #ifndef PRODUCT 611 if (PrintNMethods && (WizardMode || Verbose)) { 612 tty->print("done with CodeBuffer:"); 613 ((CodeBuffer*)this)->print(); 614 } 615 #endif //PRODUCT 616 617 CodeBuffer dest(dest_blob->instructions_begin(), 618 dest_blob->instructions_size()); 619 assert(dest_blob->instructions_size() >= total_code_size(), "good sizing"); 620 this->compute_final_layout(&dest); 621 relocate_code_to(&dest); 622 623 // transfer comments from buffer to blob 624 dest_blob->set_comments(_comments); 625 626 // Done moving code bytes; were they the right size? 627 assert(round_to(dest.total_code_size(), oopSize) == dest_blob->instructions_size(), "sanity"); 628 629 // Flush generated code 630 ICache::invalidate_range(dest_blob->instructions_begin(), 631 dest_blob->instructions_size()); 632 } 633 634 // Move all my code into another code buffer. 635 // Consult applicable relocs to repair embedded addresses. 636 void CodeBuffer::relocate_code_to(CodeBuffer* dest) const { 637 DEBUG_ONLY(address dest_end = dest->_total_start + dest->_total_size); 638 for (int n = 0; n < (int)SECT_LIMIT; n++) { 639 // pull code out of each section 640 const CodeSection* cs = code_section(n); 641 if (cs->is_empty()) continue; // skip trivial section 642 CodeSection* dest_cs = dest->code_section(n); 643 assert(cs->size() == dest_cs->size(), "sanity"); 644 csize_t usize = dest_cs->size(); 645 csize_t wsize = align_size_up(usize, HeapWordSize); 646 assert(dest_cs->start() + wsize <= dest_end, "no overflow"); 647 // Copy the code as aligned machine words. 648 // This may also include an uninitialized partial word at the end. 649 Copy::disjoint_words((HeapWord*)cs->start(), 650 (HeapWord*)dest_cs->start(), 651 wsize / HeapWordSize); 652 653 if (dest->blob() == NULL) { 654 // Destination is a final resting place, not just another buffer. 655 // Normalize uninitialized bytes in the final padding. 656 Copy::fill_to_bytes(dest_cs->end(), dest_cs->remaining(), 657 Assembler::code_fill_byte()); 658 } 659 660 assert(cs->locs_start() != (relocInfo*)badAddress, 661 "this section carries no reloc storage, but reloc was attempted"); 662 663 // Make the new code copy use the old copy's relocations: 664 dest_cs->initialize_locs_from(cs); 665 666 { // Repair the pc relative information in the code after the move 667 RelocIterator iter(dest_cs); 668 while (iter.next()) { 669 iter.reloc()->fix_relocation_after_move(this, dest); 670 } 671 } 672 } 673 } 674 675 csize_t CodeBuffer::figure_expanded_capacities(CodeSection* which_cs, 676 csize_t amount, 677 csize_t* new_capacity) { 678 csize_t new_total_cap = 0; 679 680 int prev_n = -1; 681 for (int n = 0; n < (int)SECT_LIMIT; n++) { 682 const CodeSection* sect = code_section(n); 683 684 if (!sect->is_empty()) { 685 // Compute initial padding; assign it to the previous non-empty guy. 686 // Cf. compute_final_layout. 687 csize_t padding = sect->align_at_start(new_total_cap) - new_total_cap; 688 if (padding != 0) { 689 new_total_cap += padding; 690 assert(prev_n >= 0, "sanity"); 691 new_capacity[prev_n] += padding; 692 } 693 prev_n = n; 694 } 695 696 csize_t exp = sect->size(); // 100% increase 697 if ((uint)exp < 4*K) exp = 4*K; // minimum initial increase 698 if (sect == which_cs) { 699 if (exp < amount) exp = amount; 700 if (StressCodeBuffers) exp = amount; // expand only slightly 701 } else if (n == SECT_INSTS) { 702 // scale down inst increases to a more modest 25% 703 exp = 4*K + ((exp - 4*K) >> 2); 704 if (StressCodeBuffers) exp = amount / 2; // expand only slightly 705 } else if (sect->is_empty()) { 706 // do not grow an empty secondary section 707 exp = 0; 708 } 709 // Allow for inter-section slop: 710 exp += CodeSection::end_slop(); 711 csize_t new_cap = sect->size() + exp; 712 if (new_cap < sect->capacity()) { 713 // No need to expand after all. 714 new_cap = sect->capacity(); 715 } 716 new_capacity[n] = new_cap; 717 new_total_cap += new_cap; 718 } 719 720 return new_total_cap; 721 } 722 723 void CodeBuffer::expand(CodeSection* which_cs, csize_t amount) { 724 #ifndef PRODUCT 725 if (PrintNMethods && (WizardMode || Verbose)) { 726 tty->print("expanding CodeBuffer:"); 727 this->print(); 728 } 729 730 if (StressCodeBuffers && blob() != NULL) { 731 static int expand_count = 0; 732 if (expand_count >= 0) expand_count += 1; 733 if (expand_count > 100 && is_power_of_2(expand_count)) { 734 tty->print_cr("StressCodeBuffers: have expanded %d times", expand_count); 735 // simulate an occasional allocation failure: 736 free_blob(); 737 } 738 } 739 #endif //PRODUCT 740 741 // Resizing must be allowed 742 { 743 if (blob() == NULL) return; // caller must check for blob == NULL 744 for (int n = 0; n < (int)SECT_LIMIT; n++) { 745 guarantee(!code_section(n)->is_frozen(), "resizing not allowed when frozen"); 746 } 747 } 748 749 // Figure new capacity for each section. 750 csize_t new_capacity[SECT_LIMIT]; 751 csize_t new_total_cap 752 = figure_expanded_capacities(which_cs, amount, new_capacity); 753 754 // Create a new (temporary) code buffer to hold all the new data 755 CodeBuffer cb(name(), new_total_cap, 0); 756 if (cb.blob() == NULL) { 757 // Failed to allocate in code cache. 758 free_blob(); 759 return; 760 } 761 762 // Create an old code buffer to remember which addresses used to go where. 763 // This will be useful when we do final assembly into the code cache, 764 // because we will need to know how to warp any internal address that 765 // has been created at any time in this CodeBuffer's past. 766 CodeBuffer* bxp = new CodeBuffer(_total_start, _total_size); 767 bxp->take_over_code_from(this); // remember the old undersized blob 768 DEBUG_ONLY(this->_blob = NULL); // silence a later assert 769 bxp->_before_expand = this->_before_expand; 770 this->_before_expand = bxp; 771 772 // Give each section its required (expanded) capacity. 773 for (int n = (int)SECT_LIMIT-1; n >= SECT_INSTS; n--) { 774 CodeSection* cb_sect = cb.code_section(n); 775 CodeSection* this_sect = code_section(n); 776 if (new_capacity[n] == 0) continue; // already nulled out 777 if (n > SECT_INSTS) { 778 cb.initialize_section_size(cb_sect, new_capacity[n]); 779 } 780 assert(cb_sect->capacity() >= new_capacity[n], "big enough"); 781 address cb_start = cb_sect->start(); 782 cb_sect->set_end(cb_start + this_sect->size()); 783 if (this_sect->mark() == NULL) { 784 cb_sect->clear_mark(); 785 } else { 786 cb_sect->set_mark(cb_start + this_sect->mark_off()); 787 } 788 } 789 790 // Move all the code and relocations to the new blob: 791 relocate_code_to(&cb); 792 793 // Copy the temporary code buffer into the current code buffer. 794 // Basically, do {*this = cb}, except for some control information. 795 this->take_over_code_from(&cb); 796 cb.set_blob(NULL); 797 798 // Zap the old code buffer contents, to avoid mistakenly using them. 799 debug_only(Copy::fill_to_bytes(bxp->_total_start, bxp->_total_size, 800 badCodeHeapFreeVal)); 801 802 _decode_begin = NULL; // sanity 803 804 // Make certain that the new sections are all snugly inside the new blob. 805 assert(verify_section_allocation(), "expanded allocation is ship-shape"); 806 807 #ifndef PRODUCT 808 if (PrintNMethods && (WizardMode || Verbose)) { 809 tty->print("expanded CodeBuffer:"); 810 this->print(); 811 } 812 #endif //PRODUCT 813 } 814 815 void CodeBuffer::take_over_code_from(CodeBuffer* cb) { 816 // Must already have disposed of the old blob somehow. 817 assert(blob() == NULL, "must be empty"); 818 #ifdef ASSERT 819 820 #endif 821 // Take the new blob away from cb. 822 set_blob(cb->blob()); 823 // Take over all the section pointers. 824 for (int n = 0; n < (int)SECT_LIMIT; n++) { 825 CodeSection* cb_sect = cb->code_section(n); 826 CodeSection* this_sect = code_section(n); 827 this_sect->take_over_code_from(cb_sect); 828 } 829 _overflow_arena = cb->_overflow_arena; 830 // Make sure the old cb won't try to use it or free it. 831 DEBUG_ONLY(cb->_blob = (BufferBlob*)badAddress); 832 } 833 834 #ifdef ASSERT 835 bool CodeBuffer::verify_section_allocation() { 836 address tstart = _total_start; 837 if (tstart == badAddress) return true; // smashed by set_blob(NULL) 838 address tend = tstart + _total_size; 839 if (_blob != NULL) { 840 assert(tstart >= _blob->instructions_begin(), "sanity"); 841 assert(tend <= _blob->instructions_end(), "sanity"); 842 } 843 address tcheck = tstart; // advancing pointer to verify disjointness 844 for (int n = 0; n < (int)SECT_LIMIT; n++) { 845 CodeSection* sect = code_section(n); 846 if (!sect->is_allocated()) continue; 847 assert(sect->start() >= tcheck, "sanity"); 848 tcheck = sect->start(); 849 assert((intptr_t)tcheck % sect->alignment() == 0 850 || sect->is_empty() || _blob == NULL, 851 "start is aligned"); 852 assert(sect->end() >= tcheck, "sanity"); 853 assert(sect->end() <= tend, "sanity"); 854 } 855 return true; 856 } 857 #endif //ASSERT 858 859 #ifndef PRODUCT 860 861 void CodeSection::dump() { 862 address ptr = start(); 863 for (csize_t step; ptr < end(); ptr += step) { 864 step = end() - ptr; 865 if (step > jintSize * 4) step = jintSize * 4; 866 tty->print(PTR_FORMAT ": ", ptr); 867 while (step > 0) { 868 tty->print(" " PTR32_FORMAT, *(jint*)ptr); 869 ptr += jintSize; 870 } 871 tty->cr(); 872 } 873 } 874 875 876 void CodeSection::decode() { 877 Disassembler::decode(start(), end()); 878 } 879 880 881 void CodeBuffer::block_comment(intptr_t offset, const char * comment) { 882 _comments.add_comment(offset, comment); 883 } 884 885 886 class CodeComment: public CHeapObj { 887 private: 888 friend class CodeComments; 889 intptr_t _offset; 890 const char * _comment; 891 CodeComment* _next; 892 893 ~CodeComment() { 894 assert(_next == NULL, "wrong interface for freeing list"); 895 os::free((void*)_comment); 896 } 897 898 public: 899 CodeComment(intptr_t offset, const char * comment) { 900 _offset = offset; 901 _comment = os::strdup(comment); 902 _next = NULL; 903 } 904 905 intptr_t offset() const { return _offset; } 906 const char * comment() const { return _comment; } 907 CodeComment* next() { return _next; } 908 909 void set_next(CodeComment* next) { _next = next; } 910 911 CodeComment* find(intptr_t offset) { 912 CodeComment* a = this; 913 while (a != NULL && a->_offset != offset) { 914 a = a->_next; 915 } 916 return a; 917 } 918 }; 919 920 921 void CodeComments::add_comment(intptr_t offset, const char * comment) { 922 CodeComment* c = new CodeComment(offset, comment); 923 CodeComment* insert = NULL; 924 if (_comments != NULL) { 925 CodeComment* c = _comments->find(offset); 926 insert = c; 927 while (c && c->offset() == offset) { 928 insert = c; 929 c = c->next(); 930 } 931 } 932 if (insert) { 933 // insert after comments with same offset 934 c->set_next(insert->next()); 935 insert->set_next(c); 936 } else { 937 c->set_next(_comments); 938 _comments = c; 939 } 940 } 941 942 943 void CodeComments::assign(CodeComments& other) { 944 assert(_comments == NULL, "don't overwrite old value"); 945 _comments = other._comments; 946 } 947 948 949 void CodeComments::print_block_comment(outputStream* stream, intptr_t offset) { 950 if (_comments != NULL) { 951 CodeComment* c = _comments->find(offset); 952 while (c && c->offset() == offset) { 953 stream->print(" ;; "); 954 stream->print_cr(c->comment()); 955 c = c->next(); 956 } 957 } 958 } 959 960 961 void CodeComments::free() { 962 CodeComment* n = _comments; 963 while (n) { 964 // unlink the node from the list saving a pointer to the next 965 CodeComment* p = n->_next; 966 n->_next = NULL; 967 delete n; 968 n = p; 969 } 970 _comments = NULL; 971 } 972 973 974 975 void CodeBuffer::decode() { 976 Disassembler::decode(decode_begin(), code_end()); 977 _decode_begin = code_end(); 978 } 979 980 981 void CodeBuffer::skip_decode() { 982 _decode_begin = code_end(); 983 } 984 985 986 void CodeBuffer::decode_all() { 987 for (int n = 0; n < (int)SECT_LIMIT; n++) { 988 // dump contents of each section 989 CodeSection* cs = code_section(n); 990 tty->print_cr("! %s:", code_section_name(n)); 991 if (cs != consts()) 992 cs->decode(); 993 else 994 cs->dump(); 995 } 996 } 997 998 999 void CodeSection::print(const char* name) { 1000 csize_t locs_size = locs_end() - locs_start(); 1001 tty->print_cr(" %7s.code = " PTR_FORMAT " : " PTR_FORMAT " : " PTR_FORMAT " (%d of %d)%s", 1002 name, start(), end(), limit(), size(), capacity(), 1003 is_frozen()? " [frozen]": ""); 1004 tty->print_cr(" %7s.locs = " PTR_FORMAT " : " PTR_FORMAT " : " PTR_FORMAT " (%d of %d) point=%d", 1005 name, locs_start(), locs_end(), locs_limit(), locs_size, locs_capacity(), locs_point_off()); 1006 if (PrintRelocations) { 1007 RelocIterator iter(this); 1008 iter.print(); 1009 } 1010 } 1011 1012 void CodeBuffer::print() { 1013 if (this == NULL) { 1014 tty->print_cr("NULL CodeBuffer pointer"); 1015 return; 1016 } 1017 1018 tty->print_cr("CodeBuffer:"); 1019 for (int n = 0; n < (int)SECT_LIMIT; n++) { 1020 // print each section 1021 CodeSection* cs = code_section(n); 1022 cs->print(code_section_name(n)); 1023 } 1024 } 1025 1026 #endif // PRODUCT