1 /* |
1 /* |
2 * Copyright (c) 1997, 2009, Oracle and/or its affiliates. All rights reserved. |
2 * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 * |
4 * |
5 * This code is free software; you can redistribute it and/or modify it |
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 |
6 * under the terms of the GNU General Public License version 2 only, as |
7 * published by the Free Software Foundation. |
7 * published by the Free Software Foundation. |
72 // the empty parts of each section are removed, and everything |
72 // the empty parts of each section are removed, and everything |
73 // is copied into contiguous locations. |
73 // is copied into contiguous locations. |
74 |
74 |
75 typedef CodeBuffer::csize_t csize_t; // file-local definition |
75 typedef CodeBuffer::csize_t csize_t; // file-local definition |
76 |
76 |
77 // external buffer, in a predefined CodeBlob or other buffer area |
77 // External buffer, in a predefined CodeBlob. |
78 // Important: The code_start must be taken exactly, and not realigned. |
78 // Important: The code_start must be taken exactly, and not realigned. |
79 CodeBuffer::CodeBuffer(address code_start, csize_t code_size) { |
79 CodeBuffer::CodeBuffer(CodeBlob* blob) { |
80 assert(code_start != NULL, "sanity"); |
|
81 initialize_misc("static buffer"); |
80 initialize_misc("static buffer"); |
82 initialize(code_start, code_size); |
81 initialize(blob->content_begin(), blob->content_size()); |
83 assert(verify_section_allocation(), "initial use of buffer OK"); |
82 assert(verify_section_allocation(), "initial use of buffer OK"); |
84 } |
83 } |
85 |
84 |
86 void CodeBuffer::initialize(csize_t code_size, csize_t locs_size) { |
85 void CodeBuffer::initialize(csize_t code_size, csize_t locs_size) { |
87 // Compute maximal alignment. |
86 // Compute maximal alignment. |
97 } |
96 } |
98 |
97 |
99 // Set up various pointers into the blob. |
98 // Set up various pointers into the blob. |
100 initialize(_total_start, _total_size); |
99 initialize(_total_start, _total_size); |
101 |
100 |
102 assert((uintptr_t)code_begin() % CodeEntryAlignment == 0, "instruction start not code entry aligned"); |
101 assert((uintptr_t)insts_begin() % CodeEntryAlignment == 0, "instruction start not code entry aligned"); |
103 |
102 |
104 pd_initialize(); |
103 pd_initialize(); |
105 |
104 |
106 if (locs_size != 0) { |
105 if (locs_size != 0) { |
107 _insts.initialize_locs(locs_size / sizeof(relocInfo)); |
106 _insts.initialize_locs(locs_size / sizeof(relocInfo)); |
190 } |
189 } |
191 |
190 |
192 void CodeBuffer::set_blob(BufferBlob* blob) { |
191 void CodeBuffer::set_blob(BufferBlob* blob) { |
193 _blob = blob; |
192 _blob = blob; |
194 if (blob != NULL) { |
193 if (blob != NULL) { |
195 address start = blob->instructions_begin(); |
194 address start = blob->content_begin(); |
196 address end = blob->instructions_end(); |
195 address end = blob->content_end(); |
197 // Round up the starting address. |
196 // Round up the starting address. |
198 int align = _insts.alignment(); |
197 int align = _insts.alignment(); |
199 start += (-(intptr_t)start) & (align-1); |
198 start += (-(intptr_t)start) & (align-1); |
200 _total_start = start; |
199 _total_start = start; |
201 _total_size = end - start; |
200 _total_size = end - start; |
420 |
419 |
421 /// Support for emitting the code to its final location. |
420 /// Support for emitting the code to its final location. |
422 /// The pattern is the same for all functions. |
421 /// The pattern is the same for all functions. |
423 /// We iterate over all the sections, padding each to alignment. |
422 /// We iterate over all the sections, padding each to alignment. |
424 |
423 |
425 csize_t CodeBuffer::total_code_size() const { |
424 csize_t CodeBuffer::total_content_size() const { |
426 csize_t code_size_so_far = 0; |
425 csize_t size_so_far = 0; |
427 for (int n = 0; n < (int)SECT_LIMIT; n++) { |
426 for (int n = 0; n < (int)SECT_LIMIT; n++) { |
428 const CodeSection* cs = code_section(n); |
427 const CodeSection* cs = code_section(n); |
429 if (cs->is_empty()) continue; // skip trivial section |
428 if (cs->is_empty()) continue; // skip trivial section |
430 code_size_so_far = cs->align_at_start(code_size_so_far); |
429 size_so_far = cs->align_at_start(size_so_far); |
431 code_size_so_far += cs->size(); |
430 size_so_far += cs->size(); |
432 } |
431 } |
433 return code_size_so_far; |
432 return size_so_far; |
434 } |
433 } |
435 |
434 |
436 void CodeBuffer::compute_final_layout(CodeBuffer* dest) const { |
435 void CodeBuffer::compute_final_layout(CodeBuffer* dest) const { |
437 address buf = dest->_total_start; |
436 address buf = dest->_total_start; |
438 csize_t buf_offset = 0; |
437 csize_t buf_offset = 0; |
439 assert(dest->_total_size >= total_code_size(), "must be big enough"); |
438 assert(dest->_total_size >= total_content_size(), "must be big enough"); |
440 |
439 |
441 { |
440 { |
442 // not sure why this is here, but why not... |
441 // not sure why this is here, but why not... |
443 int alignSize = MAX2((intx) sizeof(jdouble), CodeEntryAlignment); |
442 int alignSize = MAX2((intx) sizeof(jdouble), CodeEntryAlignment); |
444 assert( (dest->_total_start - _insts.start()) % alignSize == 0, "copy must preserve alignment"); |
443 assert( (dest->_total_start - _insts.start()) % alignSize == 0, "copy must preserve alignment"); |
487 |
486 |
488 buf_offset += csize; |
487 buf_offset += csize; |
489 } |
488 } |
490 |
489 |
491 // Done calculating sections; did it come out to the right end? |
490 // Done calculating sections; did it come out to the right end? |
492 assert(buf_offset == total_code_size(), "sanity"); |
491 assert(buf_offset == total_content_size(), "sanity"); |
493 assert(dest->verify_section_allocation(), "final configuration works"); |
492 assert(dest->verify_section_allocation(), "final configuration works"); |
494 } |
493 } |
495 |
494 |
496 csize_t CodeBuffer::total_offset_of(address addr) const { |
495 csize_t CodeBuffer::total_offset_of(address addr) const { |
497 csize_t code_size_so_far = 0; |
496 csize_t code_size_so_far = 0; |
513 return -1; |
512 return -1; |
514 } |
513 } |
515 |
514 |
516 csize_t CodeBuffer::total_relocation_size() const { |
515 csize_t CodeBuffer::total_relocation_size() const { |
517 csize_t lsize = copy_relocations_to(NULL); // dry run only |
516 csize_t lsize = copy_relocations_to(NULL); // dry run only |
518 csize_t csize = total_code_size(); |
517 csize_t csize = total_content_size(); |
519 csize_t total = RelocIterator::locs_and_index_size(csize, lsize); |
518 csize_t total = RelocIterator::locs_and_index_size(csize, lsize); |
520 return (csize_t) align_size_up(total, HeapWordSize); |
519 return (csize_t) align_size_up(total, HeapWordSize); |
521 } |
520 } |
522 |
521 |
523 csize_t CodeBuffer::copy_relocations_to(CodeBlob* dest) const { |
522 csize_t CodeBuffer::copy_relocations_to(CodeBlob* dest) const { |
599 *(relocInfo*)(buf+buf_offset) = padding; |
598 *(relocInfo*)(buf+buf_offset) = padding; |
600 } |
599 } |
601 buf_offset += sizeof(relocInfo); |
600 buf_offset += sizeof(relocInfo); |
602 } |
601 } |
603 |
602 |
604 assert(code_end_so_far == total_code_size(), "sanity"); |
603 assert(code_end_so_far == total_content_size(), "sanity"); |
605 |
604 |
606 // Account for index: |
605 // Account for index: |
607 if (buf != NULL) { |
606 if (buf != NULL) { |
608 RelocIterator::create_index(dest->relocation_begin(), |
607 RelocIterator::create_index(dest->relocation_begin(), |
609 buf_offset / sizeof(relocInfo), |
608 buf_offset / sizeof(relocInfo), |
619 tty->print("done with CodeBuffer:"); |
618 tty->print("done with CodeBuffer:"); |
620 ((CodeBuffer*)this)->print(); |
619 ((CodeBuffer*)this)->print(); |
621 } |
620 } |
622 #endif //PRODUCT |
621 #endif //PRODUCT |
623 |
622 |
624 CodeBuffer dest(dest_blob->instructions_begin(), |
623 CodeBuffer dest(dest_blob); |
625 dest_blob->instructions_size()); |
624 assert(dest_blob->content_size() >= total_content_size(), "good sizing"); |
626 assert(dest_blob->instructions_size() >= total_code_size(), "good sizing"); |
|
627 this->compute_final_layout(&dest); |
625 this->compute_final_layout(&dest); |
628 relocate_code_to(&dest); |
626 relocate_code_to(&dest); |
629 |
627 |
630 // transfer comments from buffer to blob |
628 // transfer comments from buffer to blob |
631 dest_blob->set_comments(_comments); |
629 dest_blob->set_comments(_comments); |
632 |
630 |
633 // Done moving code bytes; were they the right size? |
631 // Done moving code bytes; were they the right size? |
634 assert(round_to(dest.total_code_size(), oopSize) == dest_blob->instructions_size(), "sanity"); |
632 assert(round_to(dest.total_content_size(), oopSize) == dest_blob->content_size(), "sanity"); |
635 |
633 |
636 // Flush generated code |
634 // Flush generated code |
637 ICache::invalidate_range(dest_blob->instructions_begin(), |
635 ICache::invalidate_range(dest_blob->code_begin(), dest_blob->code_size()); |
638 dest_blob->instructions_size()); |
|
639 } |
636 } |
640 |
637 |
641 // Move all my code into another code buffer. |
638 // Move all my code into another code buffer. |
642 // Consult applicable relocs to repair embedded addresses. |
639 // Consult applicable relocs to repair embedded addresses. |
643 void CodeBuffer::relocate_code_to(CodeBuffer* dest) const { |
640 void CodeBuffer::relocate_code_to(CodeBuffer* dest) const { |
842 bool CodeBuffer::verify_section_allocation() { |
839 bool CodeBuffer::verify_section_allocation() { |
843 address tstart = _total_start; |
840 address tstart = _total_start; |
844 if (tstart == badAddress) return true; // smashed by set_blob(NULL) |
841 if (tstart == badAddress) return true; // smashed by set_blob(NULL) |
845 address tend = tstart + _total_size; |
842 address tend = tstart + _total_size; |
846 if (_blob != NULL) { |
843 if (_blob != NULL) { |
847 assert(tstart >= _blob->instructions_begin(), "sanity"); |
844 assert(tstart >= _blob->content_begin(), "sanity"); |
848 assert(tend <= _blob->instructions_end(), "sanity"); |
845 assert(tend <= _blob->content_end(), "sanity"); |
849 } |
846 } |
850 address tcheck = tstart; // advancing pointer to verify disjointness |
847 address tcheck = tstart; // advancing pointer to verify disjointness |
851 for (int n = 0; n < (int)SECT_LIMIT; n++) { |
848 for (int n = 0; n < (int)SECT_LIMIT; n++) { |
852 CodeSection* sect = code_section(n); |
849 CodeSection* sect = code_section(n); |
853 if (!sect->is_allocated()) continue; |
850 if (!sect->is_allocated()) continue; |
979 } |
976 } |
980 |
977 |
981 |
978 |
982 |
979 |
983 void CodeBuffer::decode() { |
980 void CodeBuffer::decode() { |
984 Disassembler::decode(decode_begin(), code_end()); |
981 Disassembler::decode(decode_begin(), insts_end()); |
985 _decode_begin = code_end(); |
982 _decode_begin = insts_end(); |
986 } |
983 } |
987 |
984 |
988 |
985 |
989 void CodeBuffer::skip_decode() { |
986 void CodeBuffer::skip_decode() { |
990 _decode_begin = code_end(); |
987 _decode_begin = insts_end(); |
991 } |
988 } |
992 |
989 |
993 |
990 |
994 void CodeBuffer::decode_all() { |
991 void CodeBuffer::decode_all() { |
995 for (int n = 0; n < (int)SECT_LIMIT; n++) { |
992 for (int n = 0; n < (int)SECT_LIMIT; n++) { |