src/share/vm/asm/codeBuffer.hpp

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

author
aoqi
date
Wed, 03 Jul 2019 20:42:37 +0800
changeset 9637
eef07cd490d4
parent 7535
7ae4e26cb1e0
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 #ifndef SHARE_VM_ASM_CODEBUFFER_HPP
aoqi@0 32 #define SHARE_VM_ASM_CODEBUFFER_HPP
aoqi@0 33
aoqi@0 34 #include "code/oopRecorder.hpp"
aoqi@0 35 #include "code/relocInfo.hpp"
drchase@7149 36 #include "utilities/debug.hpp"
aoqi@0 37
aoqi@0 38 class CodeStrings;
aoqi@0 39 class PhaseCFG;
aoqi@0 40 class Compile;
aoqi@0 41 class BufferBlob;
aoqi@0 42 class CodeBuffer;
aoqi@0 43 class Label;
aoqi@0 44
aoqi@0 45 class CodeOffsets: public StackObj {
aoqi@0 46 public:
aoqi@0 47 enum Entries { Entry,
aoqi@0 48 Verified_Entry,
aoqi@0 49 Frame_Complete, // Offset in the code where the frame setup is (for forte stackwalks) is complete
aoqi@0 50 OSR_Entry,
aoqi@0 51 Dtrace_trap = OSR_Entry, // dtrace probes can never have an OSR entry so reuse it
aoqi@0 52 Exceptions, // Offset where exception handler lives
aoqi@0 53 Deopt, // Offset where deopt handler lives
aoqi@0 54 DeoptMH, // Offset where MethodHandle deopt handler lives
aoqi@0 55 UnwindHandler, // Offset to default unwind handler
aoqi@0 56 max_Entries };
aoqi@0 57
aoqi@0 58 // special value to note codeBlobs where profile (forte) stack walking is
aoqi@0 59 // always dangerous and suspect.
aoqi@0 60
aoqi@0 61 enum { frame_never_safe = -1 };
aoqi@0 62
aoqi@0 63 private:
aoqi@0 64 int _values[max_Entries];
aoqi@0 65
aoqi@0 66 public:
aoqi@0 67 CodeOffsets() {
aoqi@0 68 _values[Entry ] = 0;
aoqi@0 69 _values[Verified_Entry] = 0;
aoqi@0 70 _values[Frame_Complete] = frame_never_safe;
aoqi@0 71 _values[OSR_Entry ] = 0;
aoqi@0 72 _values[Exceptions ] = -1;
aoqi@0 73 _values[Deopt ] = -1;
aoqi@0 74 _values[DeoptMH ] = -1;
aoqi@0 75 _values[UnwindHandler ] = -1;
aoqi@0 76 }
aoqi@0 77
aoqi@0 78 int value(Entries e) { return _values[e]; }
aoqi@0 79 void set_value(Entries e, int val) { _values[e] = val; }
aoqi@0 80 };
aoqi@0 81
aoqi@0 82 // This class represents a stream of code and associated relocations.
aoqi@0 83 // There are a few in each CodeBuffer.
aoqi@0 84 // They are filled concurrently, and concatenated at the end.
aoqi@0 85 class CodeSection VALUE_OBJ_CLASS_SPEC {
aoqi@0 86 friend class CodeBuffer;
aoqi@0 87 public:
aoqi@0 88 typedef int csize_t; // code size type; would be size_t except for history
aoqi@0 89
aoqi@0 90 private:
aoqi@0 91 address _start; // first byte of contents (instructions)
aoqi@0 92 address _mark; // user mark, usually an instruction beginning
aoqi@0 93 address _end; // current end address
aoqi@0 94 address _limit; // last possible (allocated) end address
aoqi@0 95 relocInfo* _locs_start; // first byte of relocation information
aoqi@0 96 relocInfo* _locs_end; // first byte after relocation information
aoqi@0 97 relocInfo* _locs_limit; // first byte after relocation information buf
aoqi@0 98 address _locs_point; // last relocated position (grows upward)
aoqi@0 99 bool _locs_own; // did I allocate the locs myself?
aoqi@0 100 bool _frozen; // no more expansion of this section
aoqi@0 101 char _index; // my section number (SECT_INST, etc.)
aoqi@0 102 CodeBuffer* _outer; // enclosing CodeBuffer
aoqi@0 103
aoqi@0 104 // (Note: _locs_point used to be called _last_reloc_offset.)
aoqi@0 105
aoqi@0 106 CodeSection() {
aoqi@0 107 _start = NULL;
aoqi@0 108 _mark = NULL;
aoqi@0 109 _end = NULL;
aoqi@0 110 _limit = NULL;
aoqi@0 111 _locs_start = NULL;
aoqi@0 112 _locs_end = NULL;
aoqi@0 113 _locs_limit = NULL;
aoqi@0 114 _locs_point = NULL;
aoqi@0 115 _locs_own = false;
aoqi@0 116 _frozen = false;
aoqi@0 117 debug_only(_index = (char)-1);
aoqi@0 118 debug_only(_outer = (CodeBuffer*)badAddress);
aoqi@0 119 }
aoqi@0 120
aoqi@0 121 void initialize_outer(CodeBuffer* outer, int index) {
aoqi@0 122 _outer = outer;
aoqi@0 123 _index = index;
aoqi@0 124 }
aoqi@0 125
aoqi@0 126 void initialize(address start, csize_t size = 0) {
aoqi@0 127 assert(_start == NULL, "only one init step, please");
aoqi@0 128 _start = start;
aoqi@0 129 _mark = NULL;
aoqi@0 130 _end = start;
aoqi@0 131
aoqi@0 132 _limit = start + size;
aoqi@0 133 _locs_point = start;
aoqi@0 134 }
aoqi@0 135
aoqi@0 136 void initialize_locs(int locs_capacity);
aoqi@0 137 void expand_locs(int new_capacity);
aoqi@0 138 void initialize_locs_from(const CodeSection* source_cs);
aoqi@0 139
aoqi@0 140 // helper for CodeBuffer::expand()
aoqi@0 141 void take_over_code_from(CodeSection* cs) {
aoqi@0 142 _start = cs->_start;
aoqi@0 143 _mark = cs->_mark;
aoqi@0 144 _end = cs->_end;
aoqi@0 145 _limit = cs->_limit;
aoqi@0 146 _locs_point = cs->_locs_point;
aoqi@0 147 }
aoqi@0 148
aoqi@0 149 public:
aoqi@0 150 address start() const { return _start; }
aoqi@0 151 address mark() const { return _mark; }
aoqi@0 152 address end() const { return _end; }
aoqi@0 153 address limit() const { return _limit; }
aoqi@0 154 csize_t size() const { return (csize_t)(_end - _start); }
aoqi@0 155 csize_t mark_off() const { assert(_mark != NULL, "not an offset");
aoqi@0 156 return (csize_t)(_mark - _start); }
aoqi@0 157 csize_t capacity() const { return (csize_t)(_limit - _start); }
aoqi@0 158 csize_t remaining() const { return (csize_t)(_limit - _end); }
aoqi@0 159
aoqi@0 160 relocInfo* locs_start() const { return _locs_start; }
aoqi@0 161 relocInfo* locs_end() const { return _locs_end; }
aoqi@0 162 int locs_count() const { return (int)(_locs_end - _locs_start); }
aoqi@0 163 relocInfo* locs_limit() const { return _locs_limit; }
aoqi@0 164 address locs_point() const { return _locs_point; }
aoqi@0 165 csize_t locs_point_off() const{ return (csize_t)(_locs_point - _start); }
aoqi@0 166 csize_t locs_capacity() const { return (csize_t)(_locs_limit - _locs_start); }
aoqi@0 167 csize_t locs_remaining()const { return (csize_t)(_locs_limit - _locs_end); }
aoqi@0 168
aoqi@0 169 int index() const { return _index; }
aoqi@0 170 bool is_allocated() const { return _start != NULL; }
aoqi@0 171 bool is_empty() const { return _start == _end; }
aoqi@0 172 bool is_frozen() const { return _frozen; }
aoqi@0 173 bool has_locs() const { return _locs_end != NULL; }
aoqi@0 174
aoqi@0 175 CodeBuffer* outer() const { return _outer; }
aoqi@0 176
aoqi@0 177 // is a given address in this section? (2nd version is end-inclusive)
aoqi@0 178 bool contains(address pc) const { return pc >= _start && pc < _end; }
aoqi@0 179 bool contains2(address pc) const { return pc >= _start && pc <= _end; }
aoqi@0 180 bool allocates(address pc) const { return pc >= _start && pc < _limit; }
aoqi@0 181 bool allocates2(address pc) const { return pc >= _start && pc <= _limit; }
aoqi@0 182
aoqi@0 183 void set_end(address pc) { assert(allocates2(pc), err_msg("not in CodeBuffer memory: " PTR_FORMAT " <= " PTR_FORMAT " <= " INTPTR_FORMAT, p2i(_start), p2i(pc), p2i(_limit))); _end = pc; }
aoqi@0 184 void set_mark(address pc) { assert(contains2(pc), "not in codeBuffer");
aoqi@0 185 _mark = pc; }
aoqi@0 186 void set_mark_off(int offset) { assert(contains2(offset+_start),"not in codeBuffer");
aoqi@0 187 _mark = offset + _start; }
aoqi@0 188 void set_mark() { _mark = _end; }
aoqi@0 189 void clear_mark() { _mark = NULL; }
aoqi@0 190
aoqi@0 191 void set_locs_end(relocInfo* p) {
aoqi@0 192 assert(p <= locs_limit(), "locs data fits in allocated buffer");
aoqi@0 193 _locs_end = p;
aoqi@0 194 }
aoqi@0 195 void set_locs_point(address pc) {
aoqi@0 196 assert(pc >= locs_point(), "relocation addr may not decrease");
aoqi@0 197 assert(allocates2(pc), "relocation addr must be in this section");
aoqi@0 198 _locs_point = pc;
aoqi@0 199 }
aoqi@0 200
aoqi@0 201 // Code emission
aoqi@0 202 void emit_int8 ( int8_t x) { *((int8_t*) end()) = x; set_end(end() + sizeof(int8_t)); }
aoqi@0 203 void emit_int16( int16_t x) { *((int16_t*) end()) = x; set_end(end() + sizeof(int16_t)); }
aoqi@0 204 void emit_int32( int32_t x) { *((int32_t*) end()) = x; set_end(end() + sizeof(int32_t)); }
aoqi@0 205 void emit_int64( int64_t x) { *((int64_t*) end()) = x; set_end(end() + sizeof(int64_t)); }
aoqi@0 206
aoqi@0 207 void emit_float( jfloat x) { *((jfloat*) end()) = x; set_end(end() + sizeof(jfloat)); }
aoqi@0 208 void emit_double(jdouble x) { *((jdouble*) end()) = x; set_end(end() + sizeof(jdouble)); }
aoqi@0 209 void emit_address(address x) { *((address*) end()) = x; set_end(end() + sizeof(address)); }
aoqi@0 210
aoqi@0 211 // Share a scratch buffer for relocinfo. (Hacky; saves a resource allocation.)
aoqi@0 212 void initialize_shared_locs(relocInfo* buf, int length);
aoqi@0 213
aoqi@0 214 // Manage labels and their addresses.
aoqi@0 215 address target(Label& L, address branch_pc);
aoqi@0 216
aoqi@0 217 // Emit a relocation.
aoqi@0 218 void relocate(address at, RelocationHolder const& rspec, int format = 0);
aoqi@0 219 void relocate(address at, relocInfo::relocType rtype, int format = 0) {
aoqi@0 220 if (rtype != relocInfo::none)
aoqi@0 221 relocate(at, Relocation::spec_simple(rtype), format);
aoqi@0 222 }
aoqi@0 223
aoqi@0 224 // alignment requirement for starting offset
aoqi@0 225 // Requirements are that the instruction area and the
aoqi@0 226 // stubs area must start on CodeEntryAlignment, and
aoqi@0 227 // the ctable on sizeof(jdouble)
aoqi@0 228 int alignment() const { return MAX2((int)sizeof(jdouble), (int)CodeEntryAlignment); }
aoqi@0 229
aoqi@0 230 // Slop between sections, used only when allocating temporary BufferBlob buffers.
aoqi@0 231 static csize_t end_slop() { return MAX2((int)sizeof(jdouble), (int)CodeEntryAlignment); }
aoqi@0 232
aoqi@0 233 csize_t align_at_start(csize_t off) const { return (csize_t) align_size_up(off, alignment()); }
aoqi@0 234
aoqi@0 235 // Mark a section frozen. Assign its remaining space to
aoqi@0 236 // the following section. It will never expand after this point.
aoqi@0 237 inline void freeze(); // { _outer->freeze_section(this); }
aoqi@0 238
aoqi@0 239 // Ensure there's enough space left in the current section.
aoqi@0 240 // Return true if there was an expansion.
aoqi@0 241 bool maybe_expand_to_ensure_remaining(csize_t amount);
aoqi@0 242
aoqi@0 243 #ifndef PRODUCT
aoqi@0 244 void decode();
aoqi@0 245 void dump();
aoqi@0 246 void print(const char* name);
aoqi@0 247 #endif //PRODUCT
aoqi@0 248 };
aoqi@0 249
aoqi@0 250 class CodeString;
aoqi@0 251 class CodeStrings VALUE_OBJ_CLASS_SPEC {
aoqi@0 252 private:
aoqi@0 253 #ifndef PRODUCT
aoqi@0 254 CodeString* _strings;
drchase@7149 255 #ifdef ASSERT
drchase@7149 256 // Becomes true after copy-out, forbids further use.
drchase@7149 257 bool _defunct; // Zero bit pattern is "valid", see memset call in decode_env::decode_env
drchase@7149 258 #endif
aoqi@0 259 #endif
aoqi@0 260
aoqi@0 261 CodeString* find(intptr_t offset) const;
aoqi@0 262 CodeString* find_last(intptr_t offset) const;
aoqi@0 263
drchase@7149 264 void set_null_and_invalidate() {
drchase@7149 265 #ifndef PRODUCT
drchase@7149 266 _strings = NULL;
drchase@7149 267 #ifdef ASSERT
drchase@7149 268 _defunct = true;
drchase@7149 269 #endif
drchase@7149 270 #endif
drchase@7149 271 }
drchase@7149 272
aoqi@0 273 public:
aoqi@0 274 CodeStrings() {
aoqi@0 275 #ifndef PRODUCT
aoqi@0 276 _strings = NULL;
drchase@7149 277 #ifdef ASSERT
drchase@7149 278 _defunct = false;
drchase@7149 279 #endif
drchase@7149 280 #endif
drchase@7149 281 }
drchase@7149 282
drchase@7149 283 bool is_null() {
drchase@7149 284 #ifdef ASSERT
drchase@7149 285 return _strings == NULL;
drchase@7149 286 #else
drchase@7149 287 return true;
aoqi@0 288 #endif
aoqi@0 289 }
aoqi@0 290
aoqi@0 291 const char* add_string(const char * string) PRODUCT_RETURN_(return NULL;);
aoqi@0 292
aoqi@0 293 void add_comment(intptr_t offset, const char * comment) PRODUCT_RETURN;
aoqi@0 294 void print_block_comment(outputStream* stream, intptr_t offset) const PRODUCT_RETURN;
drchase@7149 295 // MOVE strings from other to this; invalidate other.
aoqi@0 296 void assign(CodeStrings& other) PRODUCT_RETURN;
drchase@7149 297 // COPY strings from other to this; leave other valid.
drchase@7149 298 void copy(CodeStrings& other) PRODUCT_RETURN;
aoqi@0 299 void free() PRODUCT_RETURN;
drchase@7149 300 // Guarantee that _strings are used at most once; assign invalidates a buffer.
drchase@7149 301 inline void check_valid() const {
drchase@7149 302 #ifdef ASSERT
drchase@7149 303 assert(!_defunct, "Use of invalid CodeStrings");
drchase@7149 304 #endif
drchase@7149 305 }
aoqi@0 306 };
aoqi@0 307
aoqi@0 308 // A CodeBuffer describes a memory space into which assembly
aoqi@0 309 // code is generated. This memory space usually occupies the
aoqi@0 310 // interior of a single BufferBlob, but in some cases it may be
aoqi@0 311 // an arbitrary span of memory, even outside the code cache.
aoqi@0 312 //
aoqi@0 313 // A code buffer comes in two variants:
aoqi@0 314 //
aoqi@0 315 // (1) A CodeBuffer referring to an already allocated piece of memory:
aoqi@0 316 // This is used to direct 'static' code generation (e.g. for interpreter
aoqi@0 317 // or stubroutine generation, etc.). This code comes with NO relocation
aoqi@0 318 // information.
aoqi@0 319 //
aoqi@0 320 // (2) A CodeBuffer referring to a piece of memory allocated when the
aoqi@0 321 // CodeBuffer is allocated. This is used for nmethod generation.
aoqi@0 322 //
aoqi@0 323 // The memory can be divided up into several parts called sections.
aoqi@0 324 // Each section independently accumulates code (or data) an relocations.
aoqi@0 325 // Sections can grow (at the expense of a reallocation of the BufferBlob
aoqi@0 326 // and recopying of all active sections). When the buffered code is finally
aoqi@0 327 // written to an nmethod (or other CodeBlob), the contents (code, data,
aoqi@0 328 // and relocations) of the sections are padded to an alignment and concatenated.
aoqi@0 329 // Instructions and data in one section can contain relocatable references to
aoqi@0 330 // addresses in a sibling section.
aoqi@0 331
aoqi@0 332 class CodeBuffer: public StackObj {
aoqi@0 333 friend class CodeSection;
aoqi@0 334
aoqi@0 335 private:
aoqi@0 336 // CodeBuffers must be allocated on the stack except for a single
aoqi@0 337 // special case during expansion which is handled internally. This
aoqi@0 338 // is done to guarantee proper cleanup of resources.
aoqi@0 339 void* operator new(size_t size) throw() { return ResourceObj::operator new(size); }
aoqi@0 340 void operator delete(void* p) { ShouldNotCallThis(); }
aoqi@0 341
aoqi@0 342 public:
aoqi@0 343 typedef int csize_t; // code size type; would be size_t except for history
aoqi@0 344 enum {
aoqi@0 345 // Here is the list of all possible sections. The order reflects
aoqi@0 346 // the final layout.
aoqi@0 347 SECT_FIRST = 0,
aoqi@0 348 SECT_CONSTS = SECT_FIRST, // Non-instruction data: Floats, jump tables, etc.
aoqi@0 349 SECT_INSTS, // Executable instructions.
aoqi@0 350 SECT_STUBS, // Outbound trampolines for supporting call sites.
aoqi@0 351 SECT_LIMIT, SECT_NONE = -1
aoqi@0 352 };
aoqi@0 353
aoqi@0 354 private:
aoqi@0 355 enum {
aoqi@0 356 sect_bits = 2, // assert (SECT_LIMIT <= (1<<sect_bits))
aoqi@0 357 sect_mask = (1<<sect_bits)-1
aoqi@0 358 };
aoqi@0 359
aoqi@0 360 const char* _name;
aoqi@0 361
aoqi@0 362 CodeSection _consts; // constants, jump tables
aoqi@0 363 CodeSection _insts; // instructions (the main section)
aoqi@0 364 CodeSection _stubs; // stubs (call site support), deopt, exception handling
aoqi@0 365
aoqi@0 366 CodeBuffer* _before_expand; // dead buffer, from before the last expansion
aoqi@0 367
aoqi@0 368 BufferBlob* _blob; // optional buffer in CodeCache for generated code
aoqi@0 369 address _total_start; // first address of combined memory buffer
aoqi@0 370 csize_t _total_size; // size in bytes of combined memory buffer
aoqi@0 371
aoqi@0 372 OopRecorder* _oop_recorder;
drchase@7149 373 CodeStrings _code_strings;
aoqi@0 374 OopRecorder _default_oop_recorder; // override with initialize_oop_recorder
aoqi@0 375 Arena* _overflow_arena;
aoqi@0 376
aoqi@0 377 address _decode_begin; // start address for decode
aoqi@0 378 address decode_begin();
aoqi@0 379
aoqi@0 380 void initialize_misc(const char * name) {
aoqi@0 381 // all pointers other than code_start/end and those inside the sections
aoqi@0 382 assert(name != NULL, "must have a name");
aoqi@0 383 _name = name;
aoqi@0 384 _before_expand = NULL;
aoqi@0 385 _blob = NULL;
aoqi@0 386 _oop_recorder = NULL;
aoqi@0 387 _decode_begin = NULL;
aoqi@0 388 _overflow_arena = NULL;
aoqi@0 389 }
aoqi@0 390
aoqi@0 391 void initialize(address code_start, csize_t code_size) {
aoqi@0 392 _consts.initialize_outer(this, SECT_CONSTS);
aoqi@0 393 _insts.initialize_outer(this, SECT_INSTS);
aoqi@0 394 _stubs.initialize_outer(this, SECT_STUBS);
aoqi@0 395 _total_start = code_start;
aoqi@0 396 _total_size = code_size;
aoqi@0 397 // Initialize the main section:
aoqi@0 398 _insts.initialize(code_start, code_size);
aoqi@0 399 assert(!_stubs.is_allocated(), "no garbage here");
aoqi@0 400 assert(!_consts.is_allocated(), "no garbage here");
aoqi@0 401 _oop_recorder = &_default_oop_recorder;
aoqi@0 402 }
aoqi@0 403
aoqi@0 404 void initialize_section_size(CodeSection* cs, csize_t size);
aoqi@0 405
aoqi@0 406 void freeze_section(CodeSection* cs);
aoqi@0 407
aoqi@0 408 // helper for CodeBuffer::expand()
aoqi@0 409 void take_over_code_from(CodeBuffer* cs);
aoqi@0 410
aoqi@0 411 // ensure sections are disjoint, ordered, and contained in the blob
aoqi@0 412 void verify_section_allocation();
aoqi@0 413
aoqi@0 414 // copies combined relocations to the blob, returns bytes copied
aoqi@0 415 // (if target is null, it is a dry run only, just for sizing)
aoqi@0 416 csize_t copy_relocations_to(CodeBlob* blob) const;
aoqi@0 417
aoqi@0 418 // copies combined code to the blob (assumes relocs are already in there)
aoqi@0 419 void copy_code_to(CodeBlob* blob);
aoqi@0 420
aoqi@0 421 // moves code sections to new buffer (assumes relocs are already in there)
aoqi@0 422 void relocate_code_to(CodeBuffer* cb) const;
aoqi@0 423
aoqi@0 424 // set up a model of the final layout of my contents
aoqi@0 425 void compute_final_layout(CodeBuffer* dest) const;
aoqi@0 426
aoqi@0 427 // Expand the given section so at least 'amount' is remaining.
aoqi@0 428 // Creates a new, larger BufferBlob, and rewrites the code & relocs.
aoqi@0 429 void expand(CodeSection* which_cs, csize_t amount);
aoqi@0 430
aoqi@0 431 // Helper for expand.
aoqi@0 432 csize_t figure_expanded_capacities(CodeSection* which_cs, csize_t amount, csize_t* new_capacity);
aoqi@0 433
aoqi@0 434 public:
aoqi@0 435 // (1) code buffer referring to pre-allocated instruction memory
aoqi@0 436 CodeBuffer(address code_start, csize_t code_size) {
aoqi@0 437 assert(code_start != NULL, "sanity");
aoqi@0 438 initialize_misc("static buffer");
aoqi@0 439 initialize(code_start, code_size);
aoqi@0 440 verify_section_allocation();
aoqi@0 441 }
aoqi@0 442
aoqi@0 443 // (2) CodeBuffer referring to pre-allocated CodeBlob.
aoqi@0 444 CodeBuffer(CodeBlob* blob);
aoqi@0 445
aoqi@0 446 // (3) code buffer allocating codeBlob memory for code & relocation
aoqi@0 447 // info but with lazy initialization. The name must be something
aoqi@0 448 // informative.
aoqi@0 449 CodeBuffer(const char* name) {
aoqi@0 450 initialize_misc(name);
aoqi@0 451 }
aoqi@0 452
aoqi@0 453
aoqi@0 454 // (4) code buffer allocating codeBlob memory for code & relocation
aoqi@0 455 // info. The name must be something informative and code_size must
aoqi@0 456 // include both code and stubs sizes.
aoqi@0 457 CodeBuffer(const char* name, csize_t code_size, csize_t locs_size) {
aoqi@0 458 initialize_misc(name);
aoqi@0 459 initialize(code_size, locs_size);
aoqi@0 460 }
aoqi@0 461
aoqi@0 462 ~CodeBuffer();
aoqi@0 463
aoqi@0 464 // Initialize a CodeBuffer constructed using constructor 3. Using
aoqi@0 465 // constructor 4 is equivalent to calling constructor 3 and then
aoqi@0 466 // calling this method. It's been factored out for convenience of
aoqi@0 467 // construction.
aoqi@0 468 void initialize(csize_t code_size, csize_t locs_size);
aoqi@0 469
aoqi@0 470 CodeSection* consts() { return &_consts; }
aoqi@0 471 CodeSection* insts() { return &_insts; }
aoqi@0 472 CodeSection* stubs() { return &_stubs; }
aoqi@0 473
aoqi@0 474 // present sections in order; return NULL at end; consts is #0, etc.
aoqi@0 475 CodeSection* code_section(int n) {
aoqi@0 476 // This makes the slightly questionable but portable assumption
aoqi@0 477 // that the various members (_consts, _insts, _stubs, etc.) are
aoqi@0 478 // adjacent in the layout of CodeBuffer.
aoqi@0 479 CodeSection* cs = &_consts + n;
aoqi@0 480 assert(cs->index() == n || !cs->is_allocated(), "sanity");
aoqi@0 481 return cs;
aoqi@0 482 }
aoqi@0 483 const CodeSection* code_section(int n) const { // yucky const stuff
aoqi@0 484 return ((CodeBuffer*)this)->code_section(n);
aoqi@0 485 }
aoqi@0 486 static const char* code_section_name(int n);
aoqi@0 487 int section_index_of(address addr) const;
aoqi@0 488 bool contains(address addr) const {
aoqi@0 489 // handy for debugging
aoqi@0 490 return section_index_of(addr) > SECT_NONE;
aoqi@0 491 }
aoqi@0 492
aoqi@0 493 // A stable mapping between 'locators' (small ints) and addresses.
aoqi@0 494 static int locator_pos(int locator) { return locator >> sect_bits; }
aoqi@0 495 static int locator_sect(int locator) { return locator & sect_mask; }
aoqi@0 496 static int locator(int pos, int sect) { return (pos << sect_bits) | sect; }
aoqi@0 497 int locator(address addr) const;
aoqi@0 498 address locator_address(int locator) const;
aoqi@0 499
aoqi@0 500 // Heuristic for pre-packing the taken/not-taken bit of a predicted branch.
aoqi@0 501 bool is_backward_branch(Label& L);
aoqi@0 502
aoqi@0 503 // Properties
aoqi@0 504 const char* name() const { return _name; }
aoqi@0 505 CodeBuffer* before_expand() const { return _before_expand; }
aoqi@0 506 BufferBlob* blob() const { return _blob; }
aoqi@0 507 void set_blob(BufferBlob* blob);
aoqi@0 508 void free_blob(); // Free the blob, if we own one.
aoqi@0 509
aoqi@0 510 // Properties relative to the insts section:
aoqi@0 511 address insts_begin() const { return _insts.start(); }
aoqi@0 512 address insts_end() const { return _insts.end(); }
aoqi@0 513 void set_insts_end(address end) { _insts.set_end(end); }
aoqi@0 514 address insts_limit() const { return _insts.limit(); }
aoqi@0 515 address insts_mark() const { return _insts.mark(); }
aoqi@0 516 void set_insts_mark() { _insts.set_mark(); }
aoqi@0 517 void clear_insts_mark() { _insts.clear_mark(); }
aoqi@0 518
aoqi@0 519 // is there anything in the buffer other than the current section?
aoqi@0 520 bool is_pure() const { return insts_size() == total_content_size(); }
aoqi@0 521
aoqi@0 522 // size in bytes of output so far in the insts sections
aoqi@0 523 csize_t insts_size() const { return _insts.size(); }
aoqi@0 524
aoqi@0 525 // same as insts_size(), except that it asserts there is no non-code here
aoqi@0 526 csize_t pure_insts_size() const { assert(is_pure(), "no non-code");
aoqi@0 527 return insts_size(); }
aoqi@0 528 // capacity in bytes of the insts sections
aoqi@0 529 csize_t insts_capacity() const { return _insts.capacity(); }
aoqi@0 530
aoqi@0 531 // number of bytes remaining in the insts section
aoqi@0 532 csize_t insts_remaining() const { return _insts.remaining(); }
aoqi@0 533
aoqi@0 534 // is a given address in the insts section? (2nd version is end-inclusive)
aoqi@0 535 bool insts_contains(address pc) const { return _insts.contains(pc); }
aoqi@0 536 bool insts_contains2(address pc) const { return _insts.contains2(pc); }
aoqi@0 537
aoqi@0 538 // Record any extra oops required to keep embedded metadata alive
aoqi@0 539 void finalize_oop_references(methodHandle method);
aoqi@0 540
aoqi@0 541 // Allocated size in all sections, when aligned and concatenated
aoqi@0 542 // (this is the eventual state of the content in its final
aoqi@0 543 // CodeBlob).
aoqi@0 544 csize_t total_content_size() const;
aoqi@0 545
aoqi@0 546 // Combined offset (relative to start of first section) of given
aoqi@0 547 // section, as eventually found in the final CodeBlob.
aoqi@0 548 csize_t total_offset_of(CodeSection* cs) const;
aoqi@0 549
aoqi@0 550 // allocated size of all relocation data, including index, rounded up
aoqi@0 551 csize_t total_relocation_size() const;
aoqi@0 552
aoqi@0 553 // allocated size of any and all recorded oops
aoqi@0 554 csize_t total_oop_size() const {
aoqi@0 555 OopRecorder* recorder = oop_recorder();
aoqi@0 556 return (recorder == NULL)? 0: recorder->oop_size();
aoqi@0 557 }
aoqi@0 558
aoqi@0 559 // allocated size of any and all recorded metadata
aoqi@0 560 csize_t total_metadata_size() const {
aoqi@0 561 OopRecorder* recorder = oop_recorder();
aoqi@0 562 return (recorder == NULL)? 0: recorder->metadata_size();
aoqi@0 563 }
aoqi@0 564
aoqi@0 565 // Configuration functions, called immediately after the CB is constructed.
aoqi@0 566 // The section sizes are subtracted from the original insts section.
aoqi@0 567 // Note: Call them in reverse section order, because each steals from insts.
aoqi@0 568 void initialize_consts_size(csize_t size) { initialize_section_size(&_consts, size); }
aoqi@0 569 void initialize_stubs_size(csize_t size) { initialize_section_size(&_stubs, size); }
aoqi@0 570 // Override default oop recorder.
aoqi@0 571 void initialize_oop_recorder(OopRecorder* r);
aoqi@0 572
aoqi@0 573 OopRecorder* oop_recorder() const { return _oop_recorder; }
drchase@7149 574 CodeStrings& strings() { return _code_strings; }
drchase@7149 575
drchase@7149 576 void free_strings() {
drchase@7149 577 if (!_code_strings.is_null()) {
drchase@7149 578 _code_strings.free(); // sets _strings Null as a side-effect.
drchase@7149 579 }
drchase@7149 580 }
aoqi@0 581
aoqi@0 582 // Code generation
aoqi@0 583 void relocate(address at, RelocationHolder const& rspec, int format = 0) {
aoqi@0 584 _insts.relocate(at, rspec, format);
aoqi@0 585 }
aoqi@0 586 void relocate(address at, relocInfo::relocType rtype, int format = 0) {
aoqi@0 587 _insts.relocate(at, rtype, format);
aoqi@0 588 }
aoqi@0 589
aoqi@0 590 // Management of overflow storage for binding of Labels.
aoqi@0 591 GrowableArray<int>* create_patch_overflow();
aoqi@0 592
aoqi@0 593 // NMethod generation
aoqi@0 594 void copy_code_and_locs_to(CodeBlob* blob) {
aoqi@0 595 assert(blob != NULL, "sane");
aoqi@0 596 copy_relocations_to(blob);
aoqi@0 597 copy_code_to(blob);
aoqi@0 598 }
aoqi@0 599 void copy_values_to(nmethod* nm) {
aoqi@0 600 if (!oop_recorder()->is_unused()) {
aoqi@0 601 oop_recorder()->copy_values_to(nm);
aoqi@0 602 }
aoqi@0 603 }
aoqi@0 604
aoqi@0 605 // Transform an address from the code in this code buffer to a specified code buffer
aoqi@0 606 address transform_address(const CodeBuffer &cb, address addr) const;
aoqi@0 607
aoqi@0 608 void block_comment(intptr_t offset, const char * comment) PRODUCT_RETURN;
aoqi@0 609 const char* code_string(const char* str) PRODUCT_RETURN_(return NULL;);
aoqi@0 610
aoqi@0 611 // Log a little info about section usage in the CodeBuffer
aoqi@0 612 void log_section_sizes(const char* name);
aoqi@0 613
aoqi@0 614 #ifndef PRODUCT
aoqi@0 615 public:
aoqi@0 616 // Printing / Decoding
aoqi@0 617 // decodes from decode_begin() to code_end() and sets decode_begin to end
aoqi@0 618 void decode();
aoqi@0 619 void decode_all(); // decodes all the code
aoqi@0 620 void skip_decode(); // sets decode_begin to code_end();
aoqi@0 621 void print();
aoqi@0 622 #endif
aoqi@0 623
aoqi@0 624
aoqi@0 625 // The following header contains architecture-specific implementations
aoqi@0 626 #ifdef TARGET_ARCH_x86
aoqi@0 627 # include "codeBuffer_x86.hpp"
aoqi@0 628 #endif
aoqi@0 629 #ifdef TARGET_ARCH_sparc
aoqi@0 630 # include "codeBuffer_sparc.hpp"
aoqi@0 631 #endif
aoqi@0 632 #ifdef TARGET_ARCH_zero
aoqi@0 633 # include "codeBuffer_zero.hpp"
aoqi@0 634 #endif
aoqi@0 635 #ifdef TARGET_ARCH_arm
aoqi@0 636 # include "codeBuffer_arm.hpp"
aoqi@0 637 #endif
aoqi@0 638 #ifdef TARGET_ARCH_ppc
aoqi@0 639 # include "codeBuffer_ppc.hpp"
aoqi@0 640 #endif
aoqi@1 641 #ifdef TARGET_ARCH_mips
aoqi@1 642 # include "codeBuffer_mips.hpp"
aoqi@1 643 #endif
aoqi@0 644
aoqi@0 645 };
aoqi@0 646
aoqi@0 647
aoqi@0 648 inline void CodeSection::freeze() {
aoqi@0 649 _outer->freeze_section(this);
aoqi@0 650 }
aoqi@0 651
aoqi@0 652 inline bool CodeSection::maybe_expand_to_ensure_remaining(csize_t amount) {
aoqi@0 653 if (remaining() < amount) { _outer->expand(this, amount); return true; }
aoqi@0 654 return false;
aoqi@0 655 }
aoqi@0 656
aoqi@0 657 #endif // SHARE_VM_ASM_CODEBUFFER_HPP

mercurial