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