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