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 */
24
25 #ifndef SHARE_ASM_CODEBUFFER_HPP
26 #define SHARE_ASM_CODEBUFFER_HPP
27
28 #include "code/oopRecorder.hpp"
29 #include "code/relocInfo.hpp"
30 #include "compiler/compiler_globals.hpp"
31 #include "utilities/align.hpp"
32 #include "utilities/debug.hpp"
33 #include "utilities/growableArray.hpp"
34 #include "utilities/linkedlist.hpp"
35 #include "utilities/resizeableResourceHash.hpp"
36 #include "utilities/macros.hpp"
37
38 template <typename T>
39 static inline void put_native(address p, T x) {
40 memcpy((void*)p, &x, sizeof x);
41 }
42
43 class PhaseCFG;
44 class Compile;
45 class BufferBlob;
46 class CodeBuffer;
47 class Label;
48 class ciMethod;
49 class SharedStubToInterpRequest;
50
72 CodeOffsets() {
73 _values[Entry ] = 0;
74 _values[Verified_Entry] = 0;
75 _values[Frame_Complete] = frame_never_safe;
76 _values[OSR_Entry ] = 0;
77 _values[Exceptions ] = -1;
78 _values[Deopt ] = -1;
79 _values[DeoptMH ] = -1;
80 _values[UnwindHandler ] = -1;
81 }
82
83 int value(Entries e) { return _values[e]; }
84 void set_value(Entries e, int val) { _values[e] = val; }
85 };
86
87 // This class represents a stream of code and associated relocations.
88 // There are a few in each CodeBuffer.
89 // They are filled concurrently, and concatenated at the end.
90 class CodeSection {
91 friend class CodeBuffer;
92 public:
93 typedef int csize_t; // code size type; would be size_t except for history
94
95 private:
96 address _start; // first byte of contents (instructions)
97 address _mark; // user mark, usually an instruction beginning
98 address _end; // current end address
99 address _limit; // last possible (allocated) end address
100 relocInfo* _locs_start; // first byte of relocation information
101 relocInfo* _locs_end; // first byte after relocation information
102 relocInfo* _locs_limit; // first byte after relocation information buf
103 address _locs_point; // last relocated position (grows upward)
104 bool _locs_own; // did I allocate the locs myself?
105 bool _scratch_emit; // Buffer is used for scratch emit, don't relocate.
106 int _skipped_instructions_size;
107 int8_t _index; // my section number (SECT_INST, etc.)
108 CodeBuffer* _outer; // enclosing CodeBuffer
109
110 // (Note: _locs_point used to be called _last_reloc_offset.)
111
266
267 // Emit a relocation.
268 void relocate(address at, RelocationHolder const& rspec, int format = 0);
269 void relocate(address at, relocInfo::relocType rtype, int format = 0, jint method_index = 0);
270
271 int alignment() const;
272
273 // Slop between sections, used only when allocating temporary BufferBlob buffers.
274 static csize_t end_slop() { return MAX2((int)sizeof(jdouble), (int)CodeEntryAlignment); }
275
276 csize_t align_at_start(csize_t off) const {
277 return (csize_t) align_up(off, alignment());
278 }
279
280 // Ensure there's enough space left in the current section.
281 // Return true if there was an expansion.
282 bool maybe_expand_to_ensure_remaining(csize_t amount);
283
284 #ifndef PRODUCT
285 void decode();
286 void print(const char* name);
287 #endif //PRODUCT
288 };
289
290
291 #ifndef PRODUCT
292
293 class AsmRemarkCollection;
294 class DbgStringCollection;
295
296 // The assumption made here is that most code remarks (or comments) added to
297 // the generated assembly code are unique, i.e. there is very little gain in
298 // trying to share the strings between the different offsets tracked in a
299 // buffer (or blob).
300
301 class AsmRemarks {
302 public:
303 AsmRemarks();
304 ~AsmRemarks();
305
306 const char* insert(uint offset, const char* remstr);
307
308 bool is_empty() const;
309
310 void share(const AsmRemarks &src);
311 void clear();
312 uint print(uint offset, outputStream* strm = tty) const;
313
314 // For testing purposes only.
315 const AsmRemarkCollection* ref() const { return _remarks; }
316
317 private:
318 AsmRemarkCollection* _remarks;
319 };
320
321 // The assumption made here is that the number of debug strings (with a fixed
322 // address requirement) is a rather small set per compilation unit.
323
324 class DbgStrings {
325 public:
326 DbgStrings();
327 ~DbgStrings();
328
329 const char* insert(const char* dbgstr);
330
331 bool is_empty() const;
332
333 void share(const DbgStrings &src);
334 void clear();
335
336 // For testing purposes only.
337 const DbgStringCollection* ref() const { return _strings; }
338
339 private:
340 DbgStringCollection* _strings;
341 };
342 #endif // not PRODUCT
343
344
345 #ifdef ASSERT
346 #include "utilities/copy.hpp"
347
348 class Scrubber {
349 public:
350 Scrubber(void* addr, size_t size) : _addr(addr), _size(size) {}
351 ~Scrubber() {
352 Copy::fill_to_bytes(_addr, _size, badResourceValue);
353 }
354 private:
355 void* _addr;
356 size_t _size;
357 };
358 #endif // ASSERT
369 // (1) A CodeBuffer referring to an already allocated piece of memory:
370 // This is used to direct 'static' code generation (e.g. for interpreter
371 // or stubroutine generation, etc.). This code comes with NO relocation
372 // information.
373 //
374 // (2) A CodeBuffer referring to a piece of memory allocated when the
375 // CodeBuffer is allocated. This is used for nmethod generation.
376 //
377 // The memory can be divided up into several parts called sections.
378 // Each section independently accumulates code (or data) an relocations.
379 // Sections can grow (at the expense of a reallocation of the BufferBlob
380 // and recopying of all active sections). When the buffered code is finally
381 // written to an nmethod (or other CodeBlob), the contents (code, data,
382 // and relocations) of the sections are padded to an alignment and concatenated.
383 // Instructions and data in one section can contain relocatable references to
384 // addresses in a sibling section.
385
386 class CodeBuffer: public StackObj DEBUG_ONLY(COMMA private Scrubber) {
387 friend class CodeSection;
388 friend class StubCodeGenerator;
389
390 private:
391 // CodeBuffers must be allocated on the stack except for a single
392 // special case during expansion which is handled internally. This
393 // is done to guarantee proper cleanup of resources.
394 void* operator new(size_t size) throw() { return resource_allocate_bytes(size); }
395 void operator delete(void* p) { ShouldNotCallThis(); }
396
397 public:
398 typedef int csize_t; // code size type; would be size_t except for history
399 enum : int8_t {
400 // Here is the list of all possible sections. The order reflects
401 // the final layout.
402 SECT_FIRST = 0,
403 SECT_CONSTS = SECT_FIRST, // Non-instruction data: Floats, jump tables, etc.
404 SECT_INSTS, // Executable instructions.
405 SECT_STUBS, // Outbound trampolines for supporting call sites.
406 SECT_LIMIT, SECT_NONE = -1
407 };
408
725 const char* code_string(const char* str) PRODUCT_RETURN_(return nullptr;);
726
727 // Log a little info about section usage in the CodeBuffer
728 void log_section_sizes(const char* name);
729
730 // Make a set of stubs final. It can create/optimize stubs.
731 bool finalize_stubs();
732
733 // Request for a shared stub to the interpreter
734 void shared_stub_to_interp_for(ciMethod* callee, csize_t call_offset);
735
736 void set_const_section_alignment(int align) {
737 _const_section_alignment = align_up(align, HeapWordSize);
738 }
739
740 #ifndef PRODUCT
741 public:
742 // Printing / Decoding
743 // decodes from decode_begin() to code_end() and sets decode_begin to end
744 void decode();
745 void print();
746 #endif
747 // Directly disassemble code buffer.
748 void decode(address start, address end);
749
750 // The following header contains architecture-specific implementations
751 #include CPU_HEADER(codeBuffer)
752
753 };
754
755 // A Java method can have calls of Java methods which can be statically bound.
756 // Calls of Java methods need stubs to the interpreter. Calls sharing the same Java method
757 // can share a stub to the interpreter.
758 // A SharedStubToInterpRequest is a request for a shared stub to the interpreter.
759 class SharedStubToInterpRequest : public ResourceObj {
760 private:
761 ciMethod* _shared_method;
762 CodeBuffer::csize_t _call_offset; // The offset of the call in CodeBuffer
763
764 public:
765 SharedStubToInterpRequest(ciMethod* method = nullptr, CodeBuffer::csize_t call_offset = -1) : _shared_method(method),
|
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 */
24
25 #ifndef SHARE_ASM_CODEBUFFER_HPP
26 #define SHARE_ASM_CODEBUFFER_HPP
27
28 #include "code/oopRecorder.hpp"
29 #include "code/relocInfo.hpp"
30 #include "compiler/compiler_globals.hpp"
31 #include "runtime/os.hpp"
32 #include "utilities/align.hpp"
33 #include "utilities/debug.hpp"
34 #include "utilities/growableArray.hpp"
35 #include "utilities/linkedlist.hpp"
36 #include "utilities/resizeableResourceHash.hpp"
37 #include "utilities/macros.hpp"
38
39 template <typename T>
40 static inline void put_native(address p, T x) {
41 memcpy((void*)p, &x, sizeof x);
42 }
43
44 class PhaseCFG;
45 class Compile;
46 class BufferBlob;
47 class CodeBuffer;
48 class Label;
49 class ciMethod;
50 class SharedStubToInterpRequest;
51
73 CodeOffsets() {
74 _values[Entry ] = 0;
75 _values[Verified_Entry] = 0;
76 _values[Frame_Complete] = frame_never_safe;
77 _values[OSR_Entry ] = 0;
78 _values[Exceptions ] = -1;
79 _values[Deopt ] = -1;
80 _values[DeoptMH ] = -1;
81 _values[UnwindHandler ] = -1;
82 }
83
84 int value(Entries e) { return _values[e]; }
85 void set_value(Entries e, int val) { _values[e] = val; }
86 };
87
88 // This class represents a stream of code and associated relocations.
89 // There are a few in each CodeBuffer.
90 // They are filled concurrently, and concatenated at the end.
91 class CodeSection {
92 friend class CodeBuffer;
93 friend class SCCReader;
94 public:
95 typedef int csize_t; // code size type; would be size_t except for history
96
97 private:
98 address _start; // first byte of contents (instructions)
99 address _mark; // user mark, usually an instruction beginning
100 address _end; // current end address
101 address _limit; // last possible (allocated) end address
102 relocInfo* _locs_start; // first byte of relocation information
103 relocInfo* _locs_end; // first byte after relocation information
104 relocInfo* _locs_limit; // first byte after relocation information buf
105 address _locs_point; // last relocated position (grows upward)
106 bool _locs_own; // did I allocate the locs myself?
107 bool _scratch_emit; // Buffer is used for scratch emit, don't relocate.
108 int _skipped_instructions_size;
109 int8_t _index; // my section number (SECT_INST, etc.)
110 CodeBuffer* _outer; // enclosing CodeBuffer
111
112 // (Note: _locs_point used to be called _last_reloc_offset.)
113
268
269 // Emit a relocation.
270 void relocate(address at, RelocationHolder const& rspec, int format = 0);
271 void relocate(address at, relocInfo::relocType rtype, int format = 0, jint method_index = 0);
272
273 int alignment() const;
274
275 // Slop between sections, used only when allocating temporary BufferBlob buffers.
276 static csize_t end_slop() { return MAX2((int)sizeof(jdouble), (int)CodeEntryAlignment); }
277
278 csize_t align_at_start(csize_t off) const {
279 return (csize_t) align_up(off, alignment());
280 }
281
282 // Ensure there's enough space left in the current section.
283 // Return true if there was an expansion.
284 bool maybe_expand_to_ensure_remaining(csize_t amount);
285
286 #ifndef PRODUCT
287 void decode();
288 void print_on(outputStream* st, const char* name);
289 #endif //PRODUCT
290 };
291
292
293 #ifndef PRODUCT
294
295 class CHeapString : public CHeapObj<mtCode> {
296 public:
297 CHeapString(const char* str) : _string(os::strdup(str)) {}
298 ~CHeapString() {
299 os::free((void*)_string);
300 _string = nullptr;
301 }
302 const char* string() const { return _string; }
303
304 private:
305 const char* _string;
306 };
307
308 class AsmRemarkCollection : public CHeapObj<mtCode> {
309 public:
310 AsmRemarkCollection() : _ref_cnt(1), _remarks(nullptr), _next(nullptr) {}
311 ~AsmRemarkCollection() {
312 assert(is_empty(), "Must 'clear()' before deleting!");
313 assert(_ref_cnt == 0, "No uses must remain when deleting!");
314 }
315 AsmRemarkCollection* reuse() {
316 precond(_ref_cnt > 0);
317 return _ref_cnt++, this;
318 }
319
320 const char* insert(uint offset, const char* remark);
321 const char* lookup(uint offset) const;
322 const char* next(uint offset) const;
323
324 bool is_empty() const { return _remarks == nullptr; }
325 uint clear();
326
327 template<typename Function>
328 bool iterate(Function function) const { // lambda enabled API
329 if (_remarks != nullptr) {
330 Cell* tmp = _remarks;
331 do {
332 if(!function(tmp->offset, tmp->string())) {
333 return false;
334 }
335 tmp = tmp->next;
336 } while (tmp != _remarks);
337 }
338 return true;
339 }
340
341 private:
342 struct Cell : CHeapString {
343 Cell(const char* remark, uint offset) :
344 CHeapString(remark), offset(offset), prev(nullptr), next(nullptr) {}
345 void push_back(Cell* cell) {
346 Cell* head = this;
347 Cell* tail = prev;
348 tail->next = cell;
349 cell->next = head;
350 cell->prev = tail;
351 prev = cell;
352 }
353 uint offset;
354 Cell* prev;
355 Cell* next;
356 };
357 uint _ref_cnt;
358 Cell* _remarks;
359 // Using a 'mutable' iteration pointer to allow 'const' on lookup/next (that
360 // does not change the state of the list per se), supportig a simplistic
361 // iteration scheme.
362 mutable Cell* _next;
363 };
364
365 // The assumption made here is that most code remarks (or comments) added to
366 // the generated assembly code are unique, i.e. there is very little gain in
367 // trying to share the strings between the different offsets tracked in a
368 // buffer (or blob).
369
370 class AsmRemarks {
371 public:
372 AsmRemarks();
373 ~AsmRemarks();
374
375 static void init(AsmRemarks& asm_remarks);
376
377 const char* insert(uint offset, const char* remstr);
378
379 bool is_empty() const;
380
381 void share(const AsmRemarks &src);
382 void clear();
383 uint print(uint offset, outputStream* strm = tty) const;
384
385 // For testing purposes only.
386 const AsmRemarkCollection* ref() const { return _remarks; }
387
388 template<typename Function>
389 bool iterate(Function function) const { return _remarks->iterate(function); }
390
391 private:
392 AsmRemarkCollection* _remarks;
393 };
394
395 class DbgStringCollection : public CHeapObj<mtCode> {
396 public:
397 DbgStringCollection() : _ref_cnt(1), _strings(nullptr) {}
398 ~DbgStringCollection() {
399 assert(is_empty(), "Must 'clear()' before deleting!");
400 assert(_ref_cnt == 0, "No uses must remain when deleting!");
401 }
402 DbgStringCollection* reuse() {
403 precond(_ref_cnt > 0);
404 return _ref_cnt++, this;
405 }
406
407 const char* insert(const char* str);
408 const char* lookup(const char* str) const;
409
410 bool is_empty() const { return _strings == nullptr; }
411 uint clear();
412
413 template<typename Function>
414 bool iterate(Function function) const { // lambda enabled API
415 if (_strings != nullptr) {
416 Cell* tmp = _strings;
417 do {
418 if (!function(tmp->string())) {
419 return false;
420 }
421 tmp = tmp->next;
422 } while (tmp != _strings);
423 }
424 return true;
425 }
426
427 private:
428 struct Cell : CHeapString {
429 Cell(const char* dbgstr) :
430 CHeapString(dbgstr), prev(nullptr), next(nullptr) {}
431 void push_back(Cell* cell) {
432 Cell* head = this;
433 Cell* tail = prev;
434 tail->next = cell;
435 cell->next = head;
436 cell->prev = tail;
437 prev = cell;
438 }
439 Cell* prev;
440 Cell* next;
441 };
442 uint _ref_cnt;
443 Cell* _strings;
444 };
445
446 // The assumption made here is that the number of debug strings (with a fixed
447 // address requirement) is a rather small set per compilation unit.
448
449 class DbgStrings {
450 public:
451 DbgStrings();
452 ~DbgStrings();
453
454 static void init(DbgStrings& dbg_strings);
455
456 const char* insert(const char* dbgstr);
457
458 bool is_empty() const;
459
460 void share(const DbgStrings &src);
461 void clear();
462
463 // For testing purposes only.
464 const DbgStringCollection* ref() const { return _strings; }
465
466 template<typename Function>
467 bool iterate(Function function) const { return _strings->iterate(function); }
468
469 private:
470 DbgStringCollection* _strings;
471 };
472 #endif // not PRODUCT
473
474
475 #ifdef ASSERT
476 #include "utilities/copy.hpp"
477
478 class Scrubber {
479 public:
480 Scrubber(void* addr, size_t size) : _addr(addr), _size(size) {}
481 ~Scrubber() {
482 Copy::fill_to_bytes(_addr, _size, badResourceValue);
483 }
484 private:
485 void* _addr;
486 size_t _size;
487 };
488 #endif // ASSERT
499 // (1) A CodeBuffer referring to an already allocated piece of memory:
500 // This is used to direct 'static' code generation (e.g. for interpreter
501 // or stubroutine generation, etc.). This code comes with NO relocation
502 // information.
503 //
504 // (2) A CodeBuffer referring to a piece of memory allocated when the
505 // CodeBuffer is allocated. This is used for nmethod generation.
506 //
507 // The memory can be divided up into several parts called sections.
508 // Each section independently accumulates code (or data) an relocations.
509 // Sections can grow (at the expense of a reallocation of the BufferBlob
510 // and recopying of all active sections). When the buffered code is finally
511 // written to an nmethod (or other CodeBlob), the contents (code, data,
512 // and relocations) of the sections are padded to an alignment and concatenated.
513 // Instructions and data in one section can contain relocatable references to
514 // addresses in a sibling section.
515
516 class CodeBuffer: public StackObj DEBUG_ONLY(COMMA private Scrubber) {
517 friend class CodeSection;
518 friend class StubCodeGenerator;
519 friend class SCCReader;
520
521 private:
522 // CodeBuffers must be allocated on the stack except for a single
523 // special case during expansion which is handled internally. This
524 // is done to guarantee proper cleanup of resources.
525 void* operator new(size_t size) throw() { return resource_allocate_bytes(size); }
526 void operator delete(void* p) { ShouldNotCallThis(); }
527
528 public:
529 typedef int csize_t; // code size type; would be size_t except for history
530 enum : int8_t {
531 // Here is the list of all possible sections. The order reflects
532 // the final layout.
533 SECT_FIRST = 0,
534 SECT_CONSTS = SECT_FIRST, // Non-instruction data: Floats, jump tables, etc.
535 SECT_INSTS, // Executable instructions.
536 SECT_STUBS, // Outbound trampolines for supporting call sites.
537 SECT_LIMIT, SECT_NONE = -1
538 };
539
856 const char* code_string(const char* str) PRODUCT_RETURN_(return nullptr;);
857
858 // Log a little info about section usage in the CodeBuffer
859 void log_section_sizes(const char* name);
860
861 // Make a set of stubs final. It can create/optimize stubs.
862 bool finalize_stubs();
863
864 // Request for a shared stub to the interpreter
865 void shared_stub_to_interp_for(ciMethod* callee, csize_t call_offset);
866
867 void set_const_section_alignment(int align) {
868 _const_section_alignment = align_up(align, HeapWordSize);
869 }
870
871 #ifndef PRODUCT
872 public:
873 // Printing / Decoding
874 // decodes from decode_begin() to code_end() and sets decode_begin to end
875 void decode();
876 void print_on(outputStream* st);
877 #endif
878 // Directly disassemble code buffer.
879 void decode(address start, address end);
880
881 // The following header contains architecture-specific implementations
882 #include CPU_HEADER(codeBuffer)
883
884 };
885
886 // A Java method can have calls of Java methods which can be statically bound.
887 // Calls of Java methods need stubs to the interpreter. Calls sharing the same Java method
888 // can share a stub to the interpreter.
889 // A SharedStubToInterpRequest is a request for a shared stub to the interpreter.
890 class SharedStubToInterpRequest : public ResourceObj {
891 private:
892 ciMethod* _shared_method;
893 CodeBuffer::csize_t _call_offset; // The offset of the call in CodeBuffer
894
895 public:
896 SharedStubToInterpRequest(ciMethod* method = nullptr, CodeBuffer::csize_t call_offset = -1) : _shared_method(method),
|