src/share/vm/asm/codeBuffer.cpp

Wed, 27 Aug 2014 08:19:12 -0400

author
zgu
date
Wed, 27 Aug 2014 08:19:12 -0400
changeset 7074
833b0f92429a
parent 6680
78bbf4d43a14
child 7149
094cbdffa87d
permissions
-rw-r--r--

8046598: Scalable Native memory tracking development
Summary: Enhance scalability of native memory tracking
Reviewed-by: coleenp, ctornqvi, gtriantafill

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

mercurial