src/share/vm/asm/codeBuffer.cpp

Wed, 03 Jul 2019 20:42:37 +0800

author
aoqi
date
Wed, 03 Jul 2019 20:42:37 +0800
changeset 9637
eef07cd490d4
parent 9468
a5cbd2fef2b1
permissions
-rw-r--r--

Merge

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

mercurial