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 #include "asm/codeBuffer.hpp"
26 #include "code/compiledIC.hpp"
27 #include "code/oopRecorder.inline.hpp"
28 #include "compiler/disassembler.hpp"
29 #include "logging/log.hpp"
30 #include "oops/klass.inline.hpp"
31 #include "oops/methodData.hpp"
32 #include "oops/oop.inline.hpp"
33 #include "runtime/icache.hpp"
34 #include "runtime/safepointVerifiers.hpp"
35 #include "utilities/align.hpp"
36 #include "utilities/copy.hpp"
37 #include "utilities/powerOfTwo.hpp"
38 #include "utilities/xmlstream.hpp"
39
40 // The structure of a CodeSection:
41 //
42 // _start -> +----------------+
43 // | machine code...|
44 // _end -> |----------------|
45 // | |
46 // | (empty) |
47 // | |
48 // | |
49 // +----------------+
50 // _limit -> | |
519 void CodeBuffer::finalize_oop_references(const methodHandle& mh) {
520 NoSafepointVerifier nsv;
521
522 GrowableArray<oop> oops;
523
524 // Make sure that immediate metadata records something in the OopRecorder
525 for (int n = (int) SECT_FIRST; n < (int) SECT_LIMIT; n++) {
526 // pull code out of each section
527 CodeSection* cs = code_section(n);
528 if (cs->is_empty() || (cs->locs_count() == 0)) continue; // skip trivial section
529 RelocIterator iter(cs);
530 while (iter.next()) {
531 if (iter.type() == relocInfo::metadata_type) {
532 metadata_Relocation* md = iter.metadata_reloc();
533 if (md->metadata_is_immediate()) {
534 Metadata* m = md->metadata_value();
535 if (oop_recorder()->is_real(m)) {
536 if (m->is_methodData()) {
537 m = ((MethodData*)m)->method();
538 }
539 if (m->is_method()) {
540 m = ((Method*)m)->method_holder();
541 }
542 if (m->is_klass()) {
543 append_oop_references(&oops, (Klass*)m);
544 } else {
545 // XXX This will currently occur for MDO which don't
546 // have a backpointer. This has to be fixed later.
547 m->print();
548 ShouldNotReachHere();
549 }
550 }
551 }
552 }
553 }
554 }
555
556 if (!oop_recorder()->is_unused()) {
557 for (int i = 0; i < oop_recorder()->metadata_count(); i++) {
558 Metadata* m = oop_recorder()->metadata_at(i);
559 if (oop_recorder()->is_real(m)) {
560 if (m->is_methodData()) {
561 m = ((MethodData*)m)->method();
562 }
563 if (m->is_method()) {
564 m = ((Method*)m)->method_holder();
565 }
566 if (m->is_klass()) {
567 append_oop_references(&oops, (Klass*)m);
568 } else {
569 m->print();
570 ShouldNotReachHere();
571 }
572 }
573 }
574
575 }
576
577 // Add the class loader of Method* for the nmethod itself
578 append_oop_references(&oops, mh->method_holder());
579
580 // Add any oops that we've found
581 Thread* thread = Thread::current();
582 for (int i = 0; i < oops.length(); i++) {
705 csize_t CodeBuffer::copy_relocations_to(CodeBlob* dest) const {
706 address buf = nullptr;
707 csize_t buf_offset = 0;
708 csize_t buf_limit = 0;
709
710 if (dest != nullptr) {
711 buf = (address)dest->relocation_begin();
712 buf_limit = (address)dest->relocation_end() - buf;
713 }
714 // if dest is null, this is just the sizing pass
715 //
716 buf_offset = copy_relocations_to(buf, buf_limit, false);
717
718 return buf_offset;
719 }
720
721 void CodeBuffer::copy_code_to(CodeBlob* dest_blob) {
722 #ifndef PRODUCT
723 if (PrintNMethods && (WizardMode || Verbose)) {
724 tty->print("done with CodeBuffer:");
725 ((CodeBuffer*)this)->print();
726 }
727 #endif //PRODUCT
728
729 CodeBuffer dest(dest_blob);
730 assert(dest_blob->content_size() >= total_content_size(), "good sizing");
731 this->compute_final_layout(&dest);
732
733 // Set beginning of constant table before relocating.
734 dest_blob->set_ctable_begin(dest.consts()->start());
735
736 relocate_code_to(&dest);
737
738 // Share assembly remarks and debug strings with the blob.
739 NOT_PRODUCT(dest_blob->use_remarks(_asm_remarks));
740 NOT_PRODUCT(dest_blob->use_strings(_dbg_strings));
741
742 // Done moving code bytes; were they the right size?
743 assert((int)align_up(dest.total_content_size(), oopSize) == dest_blob->content_size(), "sanity");
744
745 // Flush generated code
844 exp = 0;
845 }
846 // Allow for inter-section slop:
847 exp += CodeSection::end_slop();
848 csize_t new_cap = sect->size() + exp;
849 if (new_cap < sect->capacity()) {
850 // No need to expand after all.
851 new_cap = sect->capacity();
852 }
853 new_capacity[n] = new_cap;
854 new_total_cap += new_cap;
855 }
856
857 return new_total_cap;
858 }
859
860 void CodeBuffer::expand(CodeSection* which_cs, csize_t amount) {
861 #ifndef PRODUCT
862 if (PrintNMethods && (WizardMode || Verbose)) {
863 tty->print("expanding CodeBuffer:");
864 this->print();
865 }
866
867 if (StressCodeBuffers && blob() != nullptr) {
868 static int expand_count = 0;
869 if (expand_count >= 0) expand_count += 1;
870 if (expand_count > 100 && is_power_of_2(expand_count)) {
871 tty->print_cr("StressCodeBuffers: have expanded %d times", expand_count);
872 // simulate an occasional allocation failure:
873 free_blob();
874 }
875 }
876 #endif //PRODUCT
877
878 // Resizing must be allowed
879 {
880 if (blob() == nullptr) return; // caller must check if blob is null
881 }
882
883 // Figure new capacity for each section.
884 csize_t new_capacity[SECT_LIMIT];
932 // some internal addresses, _last_insn _last_label, are used during code emission,
933 // adjust them in expansion
934 adjust_internal_address(insts_begin(), cb.insts_begin());
935
936 // Copy the temporary code buffer into the current code buffer.
937 // Basically, do {*this = cb}, except for some control information.
938 this->take_over_code_from(&cb);
939 cb.set_blob(nullptr);
940
941 // Zap the old code buffer contents, to avoid mistakenly using them.
942 debug_only(Copy::fill_to_bytes(bxp->_total_start, bxp->_total_size,
943 badCodeHeapFreeVal);)
944
945 // Make certain that the new sections are all snugly inside the new blob.
946 debug_only(verify_section_allocation();)
947
948 #ifndef PRODUCT
949 _decode_begin = nullptr; // sanity
950 if (PrintNMethods && (WizardMode || Verbose)) {
951 tty->print("expanded CodeBuffer:");
952 this->print();
953 }
954 #endif //PRODUCT
955 }
956
957 void CodeBuffer::adjust_internal_address(address from, address to) {
958 if (_last_insn != nullptr) {
959 _last_insn += to - from;
960 }
961 if (_last_label != nullptr) {
962 _last_label += to - from;
963 }
964 }
965
966 void CodeBuffer::take_over_code_from(CodeBuffer* cb) {
967 // Must already have disposed of the old blob somehow.
968 assert(blob() == nullptr, "must be empty");
969 // Take the new blob away from cb.
970 set_blob(cb->blob());
971 // Take over all the section pointers.
972 for (int n = 0; n < (int)SECT_LIMIT; n++) {
985 address tend = tstart + _total_size;
986 if (_blob != nullptr) {
987 guarantee(tstart >= _blob->content_begin(), "sanity");
988 guarantee(tend <= _blob->content_end(), "sanity");
989 }
990 // Verify disjointness.
991 for (int n = (int) SECT_FIRST; n < (int) SECT_LIMIT; n++) {
992 CodeSection* sect = code_section(n);
993 if (!sect->is_allocated() || sect->is_empty()) {
994 continue;
995 }
996 guarantee(_blob == nullptr || is_aligned(sect->start(), sect->alignment()),
997 "start is aligned");
998 for (int m = n + 1; m < (int) SECT_LIMIT; m++) {
999 CodeSection* other = code_section(m);
1000 if (!other->is_allocated() || other == sect) {
1001 continue;
1002 }
1003 guarantee(other->disjoint(sect), "sanity");
1004 }
1005 guarantee(sect->end() <= tend, "sanity");
1006 guarantee(sect->end() <= sect->limit(), "sanity");
1007 }
1008 }
1009
1010 void CodeBuffer::log_section_sizes(const char* name) {
1011 if (xtty != nullptr) {
1012 ttyLocker ttyl;
1013 // log info about buffer usage
1014 xtty->print_cr("<blob name='%s' total_size='%d'>", name, _total_size);
1015 for (int n = (int) CodeBuffer::SECT_FIRST; n < (int) CodeBuffer::SECT_LIMIT; n++) {
1016 CodeSection* sect = code_section(n);
1017 if (!sect->is_allocated() || sect->is_empty()) continue;
1018 xtty->print_cr("<sect index='%d' capacity='%d' size='%d' remaining='%d'/>",
1019 n, sect->capacity(), sect->size(), sect->remaining());
1020 }
1021 xtty->print_cr("</blob>");
1022 }
1023 }
1024
1025 bool CodeBuffer::finalize_stubs() {
1026 if (_finalize_stubs && !pd_finalize_stubs()) {
1027 // stub allocation failure
1028 return false;
1029 }
1030 _finalize_stubs = false;
1031 return true;
1032 }
1033
1034 void CodeBuffer::shared_stub_to_interp_for(ciMethod* callee, csize_t call_offset) {
1035 if (_shared_stub_to_interp_requests == nullptr) {
1036 _shared_stub_to_interp_requests = new SharedStubToInterpRequests(8);
1037 }
1038 SharedStubToInterpRequest request(callee, call_offset);
1039 _shared_stub_to_interp_requests->push(request);
1040 _finalize_stubs = true;
1041 }
1043 #ifndef PRODUCT
1044 void CodeBuffer::block_comment(ptrdiff_t offset, const char* comment) {
1045 if (_collect_comments) {
1046 const char* str = _asm_remarks.insert(offset, comment);
1047 postcond(str != comment);
1048 }
1049 }
1050
1051 const char* CodeBuffer::code_string(const char* str) {
1052 const char* tmp = _dbg_strings.insert(str);
1053 postcond(tmp != str);
1054 return tmp;
1055 }
1056
1057 void CodeBuffer::decode() {
1058 ttyLocker ttyl;
1059 Disassembler::decode(decode_begin(), insts_end(), tty NOT_PRODUCT(COMMA &asm_remarks()));
1060 _decode_begin = insts_end();
1061 }
1062
1063 void CodeSection::print(const char* name) {
1064 csize_t locs_size = locs_end() - locs_start();
1065 tty->print_cr(" %7s.code = " PTR_FORMAT " : " PTR_FORMAT " : " PTR_FORMAT " (%d of %d)",
1066 name, p2i(start()), p2i(end()), p2i(limit()), size(), capacity());
1067 tty->print_cr(" %7s.locs = " PTR_FORMAT " : " PTR_FORMAT " : " PTR_FORMAT " (%d of %d) point=%d",
1068 name, p2i(locs_start()), p2i(locs_end()), p2i(locs_limit()), locs_size, locs_capacity(), locs_point_off());
1069 if (PrintRelocations && (locs_size != 0)) {
1070 RelocIterator iter(this);
1071 iter.print();
1072 }
1073 }
1074
1075 void CodeBuffer::print() {
1076 tty->print_cr("CodeBuffer:");
1077 for (int n = 0; n < (int)SECT_LIMIT; n++) {
1078 // print each section
1079 CodeSection* cs = code_section(n);
1080 cs->print(code_section_name(n));
1081 }
1082 }
1083
1084 // ----- CHeapString -----------------------------------------------------------
1085
1086 class CHeapString : public CHeapObj<mtCode> {
1087 public:
1088 CHeapString(const char* str) : _string(os::strdup(str)) {}
1089 ~CHeapString() {
1090 os::free((void*)_string);
1091 _string = nullptr;
1092 }
1093 const char* string() const { return _string; }
1094
1095 private:
1096 const char* _string;
1097 };
1098
1099 // ----- AsmRemarkCollection ---------------------------------------------------
1100
1101 class AsmRemarkCollection : public CHeapObj<mtCode> {
1102 public:
1103 AsmRemarkCollection() : _ref_cnt(1), _remarks(nullptr), _next(nullptr) {}
1104 ~AsmRemarkCollection() {
1105 assert(is_empty(), "Must 'clear()' before deleting!");
1106 assert(_ref_cnt == 0, "No uses must remain when deleting!");
1107 }
1108 AsmRemarkCollection* reuse() {
1109 precond(_ref_cnt > 0);
1110 return _ref_cnt++, this;
1111 }
1112
1113 const char* insert(uint offset, const char* remark);
1114 const char* lookup(uint offset) const;
1115 const char* next(uint offset) const;
1116
1117 bool is_empty() const { return _remarks == nullptr; }
1118 uint clear();
1119
1120 private:
1121 struct Cell : CHeapString {
1122 Cell(const char* remark, uint offset) :
1123 CHeapString(remark), offset(offset), prev(nullptr), next(nullptr) {}
1124 void push_back(Cell* cell) {
1125 Cell* head = this;
1126 Cell* tail = prev;
1127 tail->next = cell;
1128 cell->next = head;
1129 cell->prev = tail;
1130 prev = cell;
1131 }
1132 uint offset;
1133 Cell* prev;
1134 Cell* next;
1135 };
1136 uint _ref_cnt;
1137 Cell* _remarks;
1138 // Using a 'mutable' iteration pointer to allow 'const' on lookup/next (that
1139 // does not change the state of the list per se), supportig a simplistic
1140 // iteration scheme.
1141 mutable Cell* _next;
1142 };
1143
1144 // ----- DbgStringCollection ---------------------------------------------------
1145
1146 class DbgStringCollection : public CHeapObj<mtCode> {
1147 public:
1148 DbgStringCollection() : _ref_cnt(1), _strings(nullptr) {}
1149 ~DbgStringCollection() {
1150 assert(is_empty(), "Must 'clear()' before deleting!");
1151 assert(_ref_cnt == 0, "No uses must remain when deleting!");
1152 }
1153 DbgStringCollection* reuse() {
1154 precond(_ref_cnt > 0);
1155 return _ref_cnt++, this;
1156 }
1157
1158 const char* insert(const char* str);
1159 const char* lookup(const char* str) const;
1160
1161 bool is_empty() const { return _strings == nullptr; }
1162 uint clear();
1163
1164 private:
1165 struct Cell : CHeapString {
1166 Cell(const char* dbgstr) :
1167 CHeapString(dbgstr), prev(nullptr), next(nullptr) {}
1168 void push_back(Cell* cell) {
1169 Cell* head = this;
1170 Cell* tail = prev;
1171 tail->next = cell;
1172 cell->next = head;
1173 cell->prev = tail;
1174 prev = cell;
1175 }
1176 Cell* prev;
1177 Cell* next;
1178 };
1179 uint _ref_cnt;
1180 Cell* _strings;
1181 };
1182
1183 // ----- AsmRemarks ------------------------------------------------------------
1184 //
1185 // Acting as interface to reference counted mapping [offset -> remark], where
1186 // offset is a byte offset into an instruction stream (CodeBuffer, CodeBlob or
1187 // other memory buffer) and remark is a string (comment).
1188 //
1189 AsmRemarks::AsmRemarks() : _remarks(new AsmRemarkCollection()) {
1190 assert(_remarks != nullptr, "Allocation failure!");
1191 }
1192
1193 AsmRemarks::~AsmRemarks() {
1194 assert(_remarks == nullptr, "Must 'clear()' before deleting!");
1195 }
1196
1197 const char* AsmRemarks::insert(uint offset, const char* remstr) {
1198 precond(remstr != nullptr);
1199 return _remarks->insert(offset, remstr);
1200 }
1201
1202 bool AsmRemarks::is_empty() const {
1203 return _remarks->is_empty();
1204 }
1205
1206 void AsmRemarks::share(const AsmRemarks &src) {
1207 precond(is_empty());
1208 clear();
1209 _remarks = src._remarks->reuse();
1210 }
1211
1212 void AsmRemarks::clear() {
1213 if (_remarks->clear() == 0) {
1214 delete _remarks;
1215 }
1216 _remarks = nullptr;
1217 }
1218
1219 uint AsmRemarks::print(uint offset, outputStream* strm) const {
1220 uint count = 0;
1221 const char* prefix = " ;; ";
1222 const char* remstr = _remarks->lookup(offset);
1223 while (remstr != nullptr) {
1224 strm->bol();
1225 strm->print("%s", prefix);
1226 // Don't interpret as format strings since it could contain '%'.
1227 strm->print_raw(remstr);
1228 // Advance to next line iff string didn't contain a cr() at the end.
1229 strm->bol();
1230 remstr = _remarks->next(offset);
1231 count++;
1232 }
1233 return count;
1234 }
1235
1236 // ----- DbgStrings ------------------------------------------------------------
1237 //
1238 // Acting as interface to reference counted collection of (debug) strings used
1239 // in the code generated, and thus requiring a fixed address.
1240 //
1241 DbgStrings::DbgStrings() : _strings(new DbgStringCollection()) {
1242 assert(_strings != nullptr, "Allocation failure!");
1243 }
1244
1245 DbgStrings::~DbgStrings() {
1246 assert(_strings == nullptr, "Must 'clear()' before deleting!");
1247 }
1248
1249 const char* DbgStrings::insert(const char* dbgstr) {
1250 const char* str = _strings->lookup(dbgstr);
1251 return str != nullptr ? str : _strings->insert(dbgstr);
1252 }
1253
1254 bool DbgStrings::is_empty() const {
1255 return _strings->is_empty();
1256 }
1257
1258 void DbgStrings::share(const DbgStrings &src) {
1259 precond(is_empty());
1260 clear();
1261 _strings = src._strings->reuse();
1262 }
1263
1264 void DbgStrings::clear() {
1265 if (_strings->clear() == 0) {
1266 delete _strings;
1267 }
1268 _strings = nullptr;
1269 }
1270
1271 // ----- AsmRemarkCollection ---------------------------------------------------
1272
1273 const char* AsmRemarkCollection::insert(uint offset, const char* remstr) {
1274 precond(remstr != nullptr);
1275 Cell* cell = new Cell { remstr, offset };
1276 if (is_empty()) {
1277 cell->prev = cell;
1278 cell->next = cell;
1279 _remarks = cell;
1280 } else {
1281 _remarks->push_back(cell);
1282 }
1283 return cell->string();
1284 }
|
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 #include "asm/codeBuffer.hpp"
26 #include "code/compiledIC.hpp"
27 #include "code/oopRecorder.inline.hpp"
28 #include "compiler/disassembler.hpp"
29 #include "logging/log.hpp"
30 #include "oops/klass.inline.hpp"
31 #include "oops/methodCounters.hpp"
32 #include "oops/methodData.hpp"
33 #include "oops/oop.inline.hpp"
34 #include "runtime/icache.hpp"
35 #include "runtime/safepointVerifiers.hpp"
36 #include "utilities/align.hpp"
37 #include "utilities/copy.hpp"
38 #include "utilities/powerOfTwo.hpp"
39 #include "utilities/xmlstream.hpp"
40
41 // The structure of a CodeSection:
42 //
43 // _start -> +----------------+
44 // | machine code...|
45 // _end -> |----------------|
46 // | |
47 // | (empty) |
48 // | |
49 // | |
50 // +----------------+
51 // _limit -> | |
520 void CodeBuffer::finalize_oop_references(const methodHandle& mh) {
521 NoSafepointVerifier nsv;
522
523 GrowableArray<oop> oops;
524
525 // Make sure that immediate metadata records something in the OopRecorder
526 for (int n = (int) SECT_FIRST; n < (int) SECT_LIMIT; n++) {
527 // pull code out of each section
528 CodeSection* cs = code_section(n);
529 if (cs->is_empty() || (cs->locs_count() == 0)) continue; // skip trivial section
530 RelocIterator iter(cs);
531 while (iter.next()) {
532 if (iter.type() == relocInfo::metadata_type) {
533 metadata_Relocation* md = iter.metadata_reloc();
534 if (md->metadata_is_immediate()) {
535 Metadata* m = md->metadata_value();
536 if (oop_recorder()->is_real(m)) {
537 if (m->is_methodData()) {
538 m = ((MethodData*)m)->method();
539 }
540 if (m->is_methodCounters()) {
541 m = ((MethodCounters*)m)->method();
542 }
543 if (m->is_method()) {
544 m = ((Method*)m)->method_holder();
545 }
546 if (m->is_klass()) {
547 append_oop_references(&oops, (Klass*)m);
548 } else {
549 // XXX This will currently occur for MDO which don't
550 // have a backpointer. This has to be fixed later.
551 m->print();
552 ShouldNotReachHere();
553 }
554 }
555 }
556 }
557 }
558 }
559
560 if (!oop_recorder()->is_unused()) {
561 for (int i = 0; i < oop_recorder()->metadata_count(); i++) {
562 Metadata* m = oop_recorder()->metadata_at(i);
563 if (oop_recorder()->is_real(m)) {
564 if (m->is_methodData()) {
565 m = ((MethodData*)m)->method();
566 }
567 if (m->is_methodCounters()) {
568 m = ((MethodCounters*)m)->method();
569 }
570 if (m->is_method()) {
571 m = ((Method*)m)->method_holder();
572 }
573 if (m->is_klass()) {
574 append_oop_references(&oops, (Klass*)m);
575 } else {
576 m->print();
577 ShouldNotReachHere();
578 }
579 }
580 }
581
582 }
583
584 // Add the class loader of Method* for the nmethod itself
585 append_oop_references(&oops, mh->method_holder());
586
587 // Add any oops that we've found
588 Thread* thread = Thread::current();
589 for (int i = 0; i < oops.length(); i++) {
712 csize_t CodeBuffer::copy_relocations_to(CodeBlob* dest) const {
713 address buf = nullptr;
714 csize_t buf_offset = 0;
715 csize_t buf_limit = 0;
716
717 if (dest != nullptr) {
718 buf = (address)dest->relocation_begin();
719 buf_limit = (address)dest->relocation_end() - buf;
720 }
721 // if dest is null, this is just the sizing pass
722 //
723 buf_offset = copy_relocations_to(buf, buf_limit, false);
724
725 return buf_offset;
726 }
727
728 void CodeBuffer::copy_code_to(CodeBlob* dest_blob) {
729 #ifndef PRODUCT
730 if (PrintNMethods && (WizardMode || Verbose)) {
731 tty->print("done with CodeBuffer:");
732 ((CodeBuffer*)this)->print_on(tty);
733 }
734 #endif //PRODUCT
735
736 CodeBuffer dest(dest_blob);
737 assert(dest_blob->content_size() >= total_content_size(), "good sizing");
738 this->compute_final_layout(&dest);
739
740 // Set beginning of constant table before relocating.
741 dest_blob->set_ctable_begin(dest.consts()->start());
742
743 relocate_code_to(&dest);
744
745 // Share assembly remarks and debug strings with the blob.
746 NOT_PRODUCT(dest_blob->use_remarks(_asm_remarks));
747 NOT_PRODUCT(dest_blob->use_strings(_dbg_strings));
748
749 // Done moving code bytes; were they the right size?
750 assert((int)align_up(dest.total_content_size(), oopSize) == dest_blob->content_size(), "sanity");
751
752 // Flush generated code
851 exp = 0;
852 }
853 // Allow for inter-section slop:
854 exp += CodeSection::end_slop();
855 csize_t new_cap = sect->size() + exp;
856 if (new_cap < sect->capacity()) {
857 // No need to expand after all.
858 new_cap = sect->capacity();
859 }
860 new_capacity[n] = new_cap;
861 new_total_cap += new_cap;
862 }
863
864 return new_total_cap;
865 }
866
867 void CodeBuffer::expand(CodeSection* which_cs, csize_t amount) {
868 #ifndef PRODUCT
869 if (PrintNMethods && (WizardMode || Verbose)) {
870 tty->print("expanding CodeBuffer:");
871 this->print_on(tty);
872 }
873
874 if (StressCodeBuffers && blob() != nullptr) {
875 static int expand_count = 0;
876 if (expand_count >= 0) expand_count += 1;
877 if (expand_count > 100 && is_power_of_2(expand_count)) {
878 tty->print_cr("StressCodeBuffers: have expanded %d times", expand_count);
879 // simulate an occasional allocation failure:
880 free_blob();
881 }
882 }
883 #endif //PRODUCT
884
885 // Resizing must be allowed
886 {
887 if (blob() == nullptr) return; // caller must check if blob is null
888 }
889
890 // Figure new capacity for each section.
891 csize_t new_capacity[SECT_LIMIT];
939 // some internal addresses, _last_insn _last_label, are used during code emission,
940 // adjust them in expansion
941 adjust_internal_address(insts_begin(), cb.insts_begin());
942
943 // Copy the temporary code buffer into the current code buffer.
944 // Basically, do {*this = cb}, except for some control information.
945 this->take_over_code_from(&cb);
946 cb.set_blob(nullptr);
947
948 // Zap the old code buffer contents, to avoid mistakenly using them.
949 debug_only(Copy::fill_to_bytes(bxp->_total_start, bxp->_total_size,
950 badCodeHeapFreeVal);)
951
952 // Make certain that the new sections are all snugly inside the new blob.
953 debug_only(verify_section_allocation();)
954
955 #ifndef PRODUCT
956 _decode_begin = nullptr; // sanity
957 if (PrintNMethods && (WizardMode || Verbose)) {
958 tty->print("expanded CodeBuffer:");
959 this->print_on(tty);
960 }
961 #endif //PRODUCT
962 }
963
964 void CodeBuffer::adjust_internal_address(address from, address to) {
965 if (_last_insn != nullptr) {
966 _last_insn += to - from;
967 }
968 if (_last_label != nullptr) {
969 _last_label += to - from;
970 }
971 }
972
973 void CodeBuffer::take_over_code_from(CodeBuffer* cb) {
974 // Must already have disposed of the old blob somehow.
975 assert(blob() == nullptr, "must be empty");
976 // Take the new blob away from cb.
977 set_blob(cb->blob());
978 // Take over all the section pointers.
979 for (int n = 0; n < (int)SECT_LIMIT; n++) {
992 address tend = tstart + _total_size;
993 if (_blob != nullptr) {
994 guarantee(tstart >= _blob->content_begin(), "sanity");
995 guarantee(tend <= _blob->content_end(), "sanity");
996 }
997 // Verify disjointness.
998 for (int n = (int) SECT_FIRST; n < (int) SECT_LIMIT; n++) {
999 CodeSection* sect = code_section(n);
1000 if (!sect->is_allocated() || sect->is_empty()) {
1001 continue;
1002 }
1003 guarantee(_blob == nullptr || is_aligned(sect->start(), sect->alignment()),
1004 "start is aligned");
1005 for (int m = n + 1; m < (int) SECT_LIMIT; m++) {
1006 CodeSection* other = code_section(m);
1007 if (!other->is_allocated() || other == sect) {
1008 continue;
1009 }
1010 guarantee(other->disjoint(sect), "sanity");
1011 }
1012 guarantee(sect->end() <= tend, "sanity, sect_end: " PTR_FORMAT " tend: " PTR_FORMAT " size: %d", p2i(sect->end()), p2i(tend), (int)_total_size);
1013 guarantee(sect->end() <= sect->limit(), "sanity, sect_end: " PTR_FORMAT " sect_limit: " PTR_FORMAT, p2i(sect->end()), p2i(sect->limit()));
1014 }
1015 }
1016
1017 void CodeBuffer::log_section_sizes(const char* name) {
1018 if (xtty != nullptr) {
1019 ttyLocker ttyl;
1020 // log info about buffer usage
1021 xtty->head("blob name='%s' total_size='%d'", name, _total_size);
1022 for (int n = (int) CodeBuffer::SECT_FIRST; n < (int) CodeBuffer::SECT_LIMIT; n++) {
1023 CodeSection* sect = code_section(n);
1024 if (!sect->is_allocated() || sect->is_empty()) continue;
1025 xtty->elem("sect index='%d' capacity='%d' size='%d' remaining='%d'",
1026 n, sect->capacity(), sect->size(), sect->remaining());
1027 }
1028 xtty->tail("blob");
1029 }
1030 }
1031
1032 bool CodeBuffer::finalize_stubs() {
1033 if (_finalize_stubs && !pd_finalize_stubs()) {
1034 // stub allocation failure
1035 return false;
1036 }
1037 _finalize_stubs = false;
1038 return true;
1039 }
1040
1041 void CodeBuffer::shared_stub_to_interp_for(ciMethod* callee, csize_t call_offset) {
1042 if (_shared_stub_to_interp_requests == nullptr) {
1043 _shared_stub_to_interp_requests = new SharedStubToInterpRequests(8);
1044 }
1045 SharedStubToInterpRequest request(callee, call_offset);
1046 _shared_stub_to_interp_requests->push(request);
1047 _finalize_stubs = true;
1048 }
1050 #ifndef PRODUCT
1051 void CodeBuffer::block_comment(ptrdiff_t offset, const char* comment) {
1052 if (_collect_comments) {
1053 const char* str = _asm_remarks.insert(offset, comment);
1054 postcond(str != comment);
1055 }
1056 }
1057
1058 const char* CodeBuffer::code_string(const char* str) {
1059 const char* tmp = _dbg_strings.insert(str);
1060 postcond(tmp != str);
1061 return tmp;
1062 }
1063
1064 void CodeBuffer::decode() {
1065 ttyLocker ttyl;
1066 Disassembler::decode(decode_begin(), insts_end(), tty NOT_PRODUCT(COMMA &asm_remarks()));
1067 _decode_begin = insts_end();
1068 }
1069
1070 void CodeSection::print_on(outputStream* st, const char* name) {
1071 csize_t locs_size = locs_end() - locs_start();
1072 st->print_cr(" %7s.code = " PTR_FORMAT " : " PTR_FORMAT " : " PTR_FORMAT " (%d of %d)",
1073 name, p2i(start()), p2i(end()), p2i(limit()), size(), capacity());
1074 st->print_cr(" %7s.locs = " PTR_FORMAT " : " PTR_FORMAT " : " PTR_FORMAT " (%d of %d) point=%d",
1075 name, p2i(locs_start()), p2i(locs_end()), p2i(locs_limit()), locs_size, locs_capacity(), locs_point_off());
1076 if (PrintRelocations && (locs_size != 0)) {
1077 RelocIterator iter(this);
1078 iter.print_on(st);
1079 }
1080 }
1081
1082 void CodeBuffer::print_on(outputStream* st) {
1083 #if 0
1084 if (this == nullptr) { // gcc complains 'nonnull' argument 'this' compared to nullptr
1085 st->print_cr("null CodeBuffer pointer");
1086 return;
1087 }
1088 #endif
1089
1090 st->print_cr("CodeBuffer:%s", name());
1091 for (int n = 0; n < (int)SECT_LIMIT; n++) {
1092 // print each section
1093 CodeSection* cs = code_section(n);
1094 cs->print_on(st, code_section_name(n));
1095 }
1096 }
1097
1098 // ----- AsmRemarks ------------------------------------------------------------
1099 //
1100 // Acting as interface to reference counted mapping [offset -> remark], where
1101 // offset is a byte offset into an instruction stream (CodeBuffer, CodeBlob or
1102 // other memory buffer) and remark is a string (comment).
1103 //
1104 AsmRemarks::AsmRemarks() : _remarks(new AsmRemarkCollection()) {
1105 assert(_remarks != nullptr, "Allocation failure!");
1106 }
1107
1108 AsmRemarks::~AsmRemarks() {
1109 if (_remarks != nullptr) {
1110 clear();
1111 }
1112 assert(_remarks == nullptr, "Must 'clear()' before deleting!");
1113 }
1114
1115 void AsmRemarks::init(AsmRemarks& asm_remarks) {
1116 asm_remarks._remarks = new AsmRemarkCollection();
1117 }
1118
1119 const char* AsmRemarks::insert(uint offset, const char* remstr) {
1120 precond(remstr != nullptr);
1121 return _remarks->insert(offset, remstr);
1122 }
1123
1124 bool AsmRemarks::is_empty() const {
1125 return _remarks->is_empty();
1126 }
1127
1128 void AsmRemarks::share(const AsmRemarks &src) {
1129 precond(is_empty());
1130 clear();
1131 _remarks = src._remarks->reuse();
1132 }
1133
1134 void AsmRemarks::clear() {
1135 assert(_remarks != nullptr, "sanity check");
1136 if (_remarks->clear() == 0) {
1137 delete _remarks;
1138 }
1139 _remarks = nullptr;
1140 }
1141
1142 uint AsmRemarks::print(uint offset, outputStream* strm) const {
1143 uint count = 0;
1144 const char* prefix = " ;; ";
1145 const char* remstr = _remarks->lookup(offset);
1146 while (remstr != nullptr) {
1147 strm->bol();
1148 strm->print("%s", prefix);
1149 // Don't interpret as format strings since it could contain '%'.
1150 strm->print_raw(remstr);
1151 // Advance to next line iff string didn't contain a cr() at the end.
1152 strm->bol();
1153 remstr = _remarks->next(offset);
1154 count++;
1155 }
1156 return count;
1157 }
1158
1159 // ----- DbgStrings ------------------------------------------------------------
1160 //
1161 // Acting as interface to reference counted collection of (debug) strings used
1162 // in the code generated, and thus requiring a fixed address.
1163 //
1164 DbgStrings::DbgStrings() : _strings(new DbgStringCollection()) {
1165 assert(_strings != nullptr, "Allocation failure!");
1166 }
1167
1168 DbgStrings::~DbgStrings() {
1169 if (_strings != nullptr) {
1170 clear();
1171 }
1172 assert(_strings == nullptr, "Must 'clear()' before deleting!");
1173 }
1174
1175 void DbgStrings::init(DbgStrings& dbg_strings) {
1176 dbg_strings._strings = new DbgStringCollection();
1177 }
1178
1179 const char* DbgStrings::insert(const char* dbgstr) {
1180 const char* str = _strings->lookup(dbgstr);
1181 return str != nullptr ? str : _strings->insert(dbgstr);
1182 }
1183
1184 bool DbgStrings::is_empty() const {
1185 return _strings->is_empty();
1186 }
1187
1188 void DbgStrings::share(const DbgStrings &src) {
1189 precond(is_empty());
1190 clear();
1191 _strings = src._strings->reuse();
1192 }
1193
1194 void DbgStrings::clear() {
1195 assert(_strings != nullptr, "sanity check");
1196 if (_strings->clear() == 0) {
1197 delete _strings;
1198 }
1199 _strings = nullptr;
1200 }
1201
1202 // ----- AsmRemarkCollection ---------------------------------------------------
1203
1204 const char* AsmRemarkCollection::insert(uint offset, const char* remstr) {
1205 precond(remstr != nullptr);
1206 Cell* cell = new Cell { remstr, offset };
1207 if (is_empty()) {
1208 cell->prev = cell;
1209 cell->next = cell;
1210 _remarks = cell;
1211 } else {
1212 _remarks->push_back(cell);
1213 }
1214 return cell->string();
1215 }
|