Mercurial > hg > truffle
annotate src/share/vm/asm/codeBuffer.cpp @ 497:7aadaf46ecd7
Merge
author | jmasa |
---|---|
date | Fri, 19 Dec 2008 12:15:34 -0800 |
parents | d1605aabd0a1 |
children | 5bfdb08ea692 |
rev | line source |
---|---|
0 | 1 /* |
196 | 2 * Copyright 1997-2008 Sun Microsystems, Inc. All Rights Reserved. |
0 | 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, | |
20 * CA 95054 USA or visit www.sun.com if you need additional information or | |
21 * have any questions. | |
22 * | |
23 */ | |
24 | |
25 # include "incls/_precompiled.incl" | |
26 # include "incls/_codeBuffer.cpp.incl" | |
27 | |
28 // The structure of a CodeSection: | |
29 // | |
30 // _start -> +----------------+ | |
31 // | machine code...| | |
32 // _end -> |----------------| | |
33 // | | | |
34 // | (empty) | | |
35 // | | | |
36 // | | | |
37 // +----------------+ | |
38 // _limit -> | | | |
39 // | |
40 // _locs_start -> +----------------+ | |
41 // |reloc records...| | |
42 // |----------------| | |
43 // _locs_end -> | | | |
44 // | | | |
45 // | (empty) | | |
46 // | | | |
47 // | | | |
48 // +----------------+ | |
49 // _locs_limit -> | | | |
50 // The _end (resp. _limit) pointer refers to the first | |
51 // unused (resp. unallocated) byte. | |
52 | |
53 // The structure of the CodeBuffer while code is being accumulated: | |
54 // | |
55 // _total_start -> \ | |
56 // _insts._start -> +----------------+ | |
57 // | | | |
58 // | Code | | |
59 // | | | |
60 // _stubs._start -> |----------------| | |
61 // | | | |
62 // | Stubs | (also handlers for deopt/exception) | |
63 // | | | |
64 // _consts._start -> |----------------| | |
65 // | | | |
66 // | Constants | | |
67 // | | | |
68 // +----------------+ | |
69 // + _total_size -> | | | |
70 // | |
71 // When the code and relocations are copied to the code cache, | |
72 // the empty parts of each section are removed, and everything | |
73 // is copied into contiguous locations. | |
74 | |
75 typedef CodeBuffer::csize_t csize_t; // file-local definition | |
76 | |
77 // external buffer, in a predefined CodeBlob or other buffer area | |
78 // Important: The code_start must be taken exactly, and not realigned. | |
79 CodeBuffer::CodeBuffer(address code_start, csize_t code_size) { | |
80 assert(code_start != NULL, "sanity"); | |
81 initialize_misc("static buffer"); | |
82 initialize(code_start, code_size); | |
83 assert(verify_section_allocation(), "initial use of buffer OK"); | |
84 } | |
85 | |
86 void CodeBuffer::initialize(csize_t code_size, csize_t locs_size) { | |
87 // Compute maximal alignment. | |
88 int align = _insts.alignment(); | |
89 // Always allow for empty slop around each section. | |
90 int slop = (int) CodeSection::end_slop(); | |
91 | |
92 assert(blob() == NULL, "only once"); | |
93 set_blob(BufferBlob::create(_name, code_size + (align+slop) * (SECT_LIMIT+1))); | |
94 if (blob() == NULL) { | |
95 // The assembler constructor will throw a fatal on an empty CodeBuffer. | |
96 return; // caller must test this | |
97 } | |
98 | |
99 // Set up various pointers into the blob. | |
100 initialize(_total_start, _total_size); | |
101 | |
102 assert((uintptr_t)code_begin() % CodeEntryAlignment == 0, "instruction start not code entry aligned"); | |
103 | |
104 pd_initialize(); | |
105 | |
106 if (locs_size != 0) { | |
107 _insts.initialize_locs(locs_size / sizeof(relocInfo)); | |
108 } | |
109 | |
110 assert(verify_section_allocation(), "initial use of blob is OK"); | |
111 } | |
112 | |
113 | |
114 CodeBuffer::~CodeBuffer() { | |
115 // If we allocate our code buffer from the CodeCache | |
116 // via a BufferBlob, and it's not permanent, then | |
117 // free the BufferBlob. | |
118 // The rest of the memory will be freed when the ResourceObj | |
119 // is released. | |
120 assert(verify_section_allocation(), "final storage configuration still OK"); | |
121 for (CodeBuffer* cb = this; cb != NULL; cb = cb->before_expand()) { | |
122 // Previous incarnations of this buffer are held live, so that internal | |
123 // addresses constructed before expansions will not be confused. | |
124 cb->free_blob(); | |
125 } | |
126 #ifdef ASSERT | |
127 Copy::fill_to_bytes(this, sizeof(*this), badResourceValue); | |
128 #endif | |
129 } | |
130 | |
131 void CodeBuffer::initialize_oop_recorder(OopRecorder* r) { | |
132 assert(_oop_recorder == &_default_oop_recorder && _default_oop_recorder.is_unused(), "do this once"); | |
133 DEBUG_ONLY(_default_oop_recorder.oop_size()); // force unused OR to be frozen | |
134 _oop_recorder = r; | |
135 } | |
136 | |
137 void CodeBuffer::initialize_section_size(CodeSection* cs, csize_t size) { | |
138 assert(cs != &_insts, "insts is the memory provider, not the consumer"); | |
139 #ifdef ASSERT | |
140 for (int n = (int)SECT_INSTS+1; n < (int)SECT_LIMIT; n++) { | |
141 CodeSection* prevCS = code_section(n); | |
142 if (prevCS == cs) break; | |
143 assert(!prevCS->is_allocated(), "section allocation must be in reverse order"); | |
144 } | |
145 #endif | |
146 csize_t slop = CodeSection::end_slop(); // margin between sections | |
147 int align = cs->alignment(); | |
148 assert(is_power_of_2(align), "sanity"); | |
149 address start = _insts._start; | |
150 address limit = _insts._limit; | |
151 address middle = limit - size; | |
152 middle -= (intptr_t)middle & (align-1); // align the division point downward | |
153 guarantee(middle - slop > start, "need enough space to divide up"); | |
154 _insts._limit = middle - slop; // subtract desired space, plus slop | |
155 cs->initialize(middle, limit - middle); | |
156 assert(cs->start() == middle, "sanity"); | |
157 assert(cs->limit() == limit, "sanity"); | |
158 // give it some relocations to start with, if the main section has them | |
159 if (_insts.has_locs()) cs->initialize_locs(1); | |
160 } | |
161 | |
162 void CodeBuffer::freeze_section(CodeSection* cs) { | |
163 CodeSection* next_cs = (cs == consts())? NULL: code_section(cs->index()+1); | |
164 csize_t frozen_size = cs->size(); | |
165 if (next_cs != NULL) { | |
166 frozen_size = next_cs->align_at_start(frozen_size); | |
167 } | |
168 address old_limit = cs->limit(); | |
169 address new_limit = cs->start() + frozen_size; | |
170 relocInfo* old_locs_limit = cs->locs_limit(); | |
171 relocInfo* new_locs_limit = cs->locs_end(); | |
172 // Patch the limits. | |
173 cs->_limit = new_limit; | |
174 cs->_locs_limit = new_locs_limit; | |
175 cs->_frozen = true; | |
176 if (!next_cs->is_allocated() && !next_cs->is_frozen()) { | |
177 // Give remaining buffer space to the following section. | |
178 next_cs->initialize(new_limit, old_limit - new_limit); | |
179 next_cs->initialize_shared_locs(new_locs_limit, | |
180 old_locs_limit - new_locs_limit); | |
181 } | |
182 } | |
183 | |
184 void CodeBuffer::set_blob(BufferBlob* blob) { | |
185 _blob = blob; | |
186 if (blob != NULL) { | |
187 address start = blob->instructions_begin(); | |
188 address end = blob->instructions_end(); | |
189 // Round up the starting address. | |
190 int align = _insts.alignment(); | |
191 start += (-(intptr_t)start) & (align-1); | |
192 _total_start = start; | |
193 _total_size = end - start; | |
194 } else { | |
195 #ifdef ASSERT | |
196 // Clean out dangling pointers. | |
197 _total_start = badAddress; | |
198 _insts._start = _insts._end = badAddress; | |
199 _stubs._start = _stubs._end = badAddress; | |
200 _consts._start = _consts._end = badAddress; | |
201 #endif //ASSERT | |
202 } | |
203 } | |
204 | |
205 void CodeBuffer::free_blob() { | |
206 if (_blob != NULL) { | |
207 BufferBlob::free(_blob); | |
208 set_blob(NULL); | |
209 } | |
210 } | |
211 | |
212 const char* CodeBuffer::code_section_name(int n) { | |
213 #ifdef PRODUCT | |
214 return NULL; | |
215 #else //PRODUCT | |
216 switch (n) { | |
217 case SECT_INSTS: return "insts"; | |
218 case SECT_STUBS: return "stubs"; | |
219 case SECT_CONSTS: return "consts"; | |
220 default: return NULL; | |
221 } | |
222 #endif //PRODUCT | |
223 } | |
224 | |
225 int CodeBuffer::section_index_of(address addr) const { | |
226 for (int n = 0; n < (int)SECT_LIMIT; n++) { | |
227 const CodeSection* cs = code_section(n); | |
228 if (cs->allocates(addr)) return n; | |
229 } | |
230 return SECT_NONE; | |
231 } | |
232 | |
233 int CodeBuffer::locator(address addr) const { | |
234 for (int n = 0; n < (int)SECT_LIMIT; n++) { | |
235 const CodeSection* cs = code_section(n); | |
236 if (cs->allocates(addr)) { | |
237 return locator(addr - cs->start(), n); | |
238 } | |
239 } | |
240 return -1; | |
241 } | |
242 | |
243 address CodeBuffer::locator_address(int locator) const { | |
244 if (locator < 0) return NULL; | |
245 address start = code_section(locator_sect(locator))->start(); | |
246 return start + locator_pos(locator); | |
247 } | |
248 | |
249 address CodeBuffer::decode_begin() { | |
250 address begin = _insts.start(); | |
251 if (_decode_begin != NULL && _decode_begin > begin) | |
252 begin = _decode_begin; | |
253 return begin; | |
254 } | |
255 | |
256 | |
257 GrowableArray<int>* CodeBuffer::create_patch_overflow() { | |
258 if (_overflow_arena == NULL) { | |
259 _overflow_arena = new Arena(); | |
260 } | |
261 return new (_overflow_arena) GrowableArray<int>(_overflow_arena, 8, 0, 0); | |
262 } | |
263 | |
264 | |
265 // Helper function for managing labels and their target addresses. | |
266 // Returns a sensible address, and if it is not the label's final | |
267 // address, notes the dependency (at 'branch_pc') on the label. | |
268 address CodeSection::target(Label& L, address branch_pc) { | |
269 if (L.is_bound()) { | |
270 int loc = L.loc(); | |
271 if (index() == CodeBuffer::locator_sect(loc)) { | |
272 return start() + CodeBuffer::locator_pos(loc); | |
273 } else { | |
274 return outer()->locator_address(loc); | |
275 } | |
276 } else { | |
277 assert(allocates2(branch_pc), "sanity"); | |
278 address base = start(); | |
279 int patch_loc = CodeBuffer::locator(branch_pc - base, index()); | |
280 L.add_patch_at(outer(), patch_loc); | |
281 | |
282 // Need to return a pc, doesn't matter what it is since it will be | |
283 // replaced during resolution later. | |
113
ba764ed4b6f2
6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents:
100
diff
changeset
|
284 // Don't return NULL or badAddress, since branches shouldn't overflow. |
ba764ed4b6f2
6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents:
100
diff
changeset
|
285 // Don't return base either because that could overflow displacements |
ba764ed4b6f2
6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents:
100
diff
changeset
|
286 // for shorter branches. It will get checked when bound. |
ba764ed4b6f2
6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents:
100
diff
changeset
|
287 return branch_pc; |
0 | 288 } |
289 } | |
290 | |
291 void CodeSection::relocate(address at, RelocationHolder const& spec, int format) { | |
292 Relocation* reloc = spec.reloc(); | |
293 relocInfo::relocType rtype = (relocInfo::relocType) reloc->type(); | |
294 if (rtype == relocInfo::none) return; | |
295 | |
296 // The assertion below has been adjusted, to also work for | |
297 // relocation for fixup. Sometimes we want to put relocation | |
298 // information for the next instruction, since it will be patched | |
299 // with a call. | |
300 assert(start() <= at && at <= end()+1, | |
301 "cannot relocate data outside code boundaries"); | |
302 | |
303 if (!has_locs()) { | |
304 // no space for relocation information provided => code cannot be | |
305 // relocated. Make sure that relocate is only called with rtypes | |
306 // that can be ignored for this kind of code. | |
307 assert(rtype == relocInfo::none || | |
308 rtype == relocInfo::runtime_call_type || | |
309 rtype == relocInfo::internal_word_type|| | |
310 rtype == relocInfo::section_word_type || | |
311 rtype == relocInfo::external_word_type, | |
312 "code needs relocation information"); | |
313 // leave behind an indication that we attempted a relocation | |
314 DEBUG_ONLY(_locs_start = _locs_limit = (relocInfo*)badAddress); | |
315 return; | |
316 } | |
317 | |
318 // Advance the point, noting the offset we'll have to record. | |
319 csize_t offset = at - locs_point(); | |
320 set_locs_point(at); | |
321 | |
322 // Test for a couple of overflow conditions; maybe expand the buffer. | |
323 relocInfo* end = locs_end(); | |
324 relocInfo* req = end + relocInfo::length_limit; | |
325 // Check for (potential) overflow | |
326 if (req >= locs_limit() || offset >= relocInfo::offset_limit()) { | |
327 req += (uint)offset / (uint)relocInfo::offset_limit(); | |
328 if (req >= locs_limit()) { | |
329 // Allocate or reallocate. | |
330 expand_locs(locs_count() + (req - end)); | |
331 // reload pointer | |
332 end = locs_end(); | |
333 } | |
334 } | |
335 | |
336 // If the offset is giant, emit filler relocs, of type 'none', but | |
337 // each carrying the largest possible offset, to advance the locs_point. | |
338 while (offset >= relocInfo::offset_limit()) { | |
339 assert(end < locs_limit(), "adjust previous paragraph of code"); | |
340 *end++ = filler_relocInfo(); | |
341 offset -= filler_relocInfo().addr_offset(); | |
342 } | |
343 | |
344 // If it's a simple reloc with no data, we'll just write (rtype | offset). | |
345 (*end) = relocInfo(rtype, offset, format); | |
346 | |
347 // If it has data, insert the prefix, as (data_prefix_tag | data1), data2. | |
348 end->initialize(this, reloc); | |
349 } | |
350 | |
351 void CodeSection::initialize_locs(int locs_capacity) { | |
352 assert(_locs_start == NULL, "only one locs init step, please"); | |
353 // Apply a priori lower limits to relocation size: | |
354 csize_t min_locs = MAX2(size() / 16, (csize_t)4); | |
355 if (locs_capacity < min_locs) locs_capacity = min_locs; | |
356 relocInfo* locs_start = NEW_RESOURCE_ARRAY(relocInfo, locs_capacity); | |
357 _locs_start = locs_start; | |
358 _locs_end = locs_start; | |
359 _locs_limit = locs_start + locs_capacity; | |
360 _locs_own = true; | |
361 } | |
362 | |
363 void CodeSection::initialize_shared_locs(relocInfo* buf, int length) { | |
364 assert(_locs_start == NULL, "do this before locs are allocated"); | |
365 // Internal invariant: locs buf must be fully aligned. | |
366 // See copy_relocations_to() below. | |
367 while ((uintptr_t)buf % HeapWordSize != 0 && length > 0) { | |
368 ++buf; --length; | |
369 } | |
370 if (length > 0) { | |
371 _locs_start = buf; | |
372 _locs_end = buf; | |
373 _locs_limit = buf + length; | |
374 _locs_own = false; | |
375 } | |
376 } | |
377 | |
378 void CodeSection::initialize_locs_from(const CodeSection* source_cs) { | |
379 int lcount = source_cs->locs_count(); | |
380 if (lcount != 0) { | |
381 initialize_shared_locs(source_cs->locs_start(), lcount); | |
382 _locs_end = _locs_limit = _locs_start + lcount; | |
383 assert(is_allocated(), "must have copied code already"); | |
384 set_locs_point(start() + source_cs->locs_point_off()); | |
385 } | |
386 assert(this->locs_count() == source_cs->locs_count(), "sanity"); | |
387 } | |
388 | |
389 void CodeSection::expand_locs(int new_capacity) { | |
390 if (_locs_start == NULL) { | |
391 initialize_locs(new_capacity); | |
392 return; | |
393 } else { | |
394 int old_count = locs_count(); | |
395 int old_capacity = locs_capacity(); | |
396 if (new_capacity < old_capacity * 2) | |
397 new_capacity = old_capacity * 2; | |
398 relocInfo* locs_start; | |
399 if (_locs_own) { | |
400 locs_start = REALLOC_RESOURCE_ARRAY(relocInfo, _locs_start, old_capacity, new_capacity); | |
401 } else { | |
402 locs_start = NEW_RESOURCE_ARRAY(relocInfo, new_capacity); | |
403 Copy::conjoint_bytes(_locs_start, locs_start, old_capacity * sizeof(relocInfo)); | |
404 _locs_own = true; | |
405 } | |
406 _locs_start = locs_start; | |
407 _locs_end = locs_start + old_count; | |
408 _locs_limit = locs_start + new_capacity; | |
409 } | |
410 } | |
411 | |
412 | |
413 /// Support for emitting the code to its final location. | |
414 /// The pattern is the same for all functions. | |
415 /// We iterate over all the sections, padding each to alignment. | |
416 | |
417 csize_t CodeBuffer::total_code_size() const { | |
418 csize_t code_size_so_far = 0; | |
419 for (int n = 0; n < (int)SECT_LIMIT; n++) { | |
420 const CodeSection* cs = code_section(n); | |
421 if (cs->is_empty()) continue; // skip trivial section | |
422 code_size_so_far = cs->align_at_start(code_size_so_far); | |
423 code_size_so_far += cs->size(); | |
424 } | |
425 return code_size_so_far; | |
426 } | |
427 | |
428 void CodeBuffer::compute_final_layout(CodeBuffer* dest) const { | |
429 address buf = dest->_total_start; | |
430 csize_t buf_offset = 0; | |
431 assert(dest->_total_size >= total_code_size(), "must be big enough"); | |
432 | |
433 { | |
434 // not sure why this is here, but why not... | |
435 int alignSize = MAX2((intx) sizeof(jdouble), CodeEntryAlignment); | |
436 assert( (dest->_total_start - _insts.start()) % alignSize == 0, "copy must preserve alignment"); | |
437 } | |
438 | |
439 const CodeSection* prev_cs = NULL; | |
440 CodeSection* prev_dest_cs = NULL; | |
441 for (int n = 0; n < (int)SECT_LIMIT; n++) { | |
442 // figure compact layout of each section | |
443 const CodeSection* cs = code_section(n); | |
444 address cstart = cs->start(); | |
445 address cend = cs->end(); | |
446 csize_t csize = cend - cstart; | |
447 | |
448 CodeSection* dest_cs = dest->code_section(n); | |
449 if (!cs->is_empty()) { | |
450 // Compute initial padding; assign it to the previous non-empty guy. | |
451 // Cf. figure_expanded_capacities. | |
452 csize_t padding = cs->align_at_start(buf_offset) - buf_offset; | |
453 if (padding != 0) { | |
454 buf_offset += padding; | |
455 assert(prev_dest_cs != NULL, "sanity"); | |
456 prev_dest_cs->_limit += padding; | |
457 } | |
458 #ifdef ASSERT | |
459 if (prev_cs != NULL && prev_cs->is_frozen() && n < SECT_CONSTS) { | |
460 // Make sure the ends still match up. | |
461 // This is important because a branch in a frozen section | |
462 // might target code in a following section, via a Label, | |
463 // and without a relocation record. See Label::patch_instructions. | |
464 address dest_start = buf+buf_offset; | |
465 csize_t start2start = cs->start() - prev_cs->start(); | |
466 csize_t dest_start2start = dest_start - prev_dest_cs->start(); | |
467 assert(start2start == dest_start2start, "cannot stretch frozen sect"); | |
468 } | |
469 #endif //ASSERT | |
470 prev_dest_cs = dest_cs; | |
471 prev_cs = cs; | |
472 } | |
473 | |
474 debug_only(dest_cs->_start = NULL); // defeat double-initialization assert | |
475 dest_cs->initialize(buf+buf_offset, csize); | |
476 dest_cs->set_end(buf+buf_offset+csize); | |
477 assert(dest_cs->is_allocated(), "must always be allocated"); | |
478 assert(cs->is_empty() == dest_cs->is_empty(), "sanity"); | |
479 | |
480 buf_offset += csize; | |
481 } | |
482 | |
483 // Done calculating sections; did it come out to the right end? | |
484 assert(buf_offset == total_code_size(), "sanity"); | |
485 assert(dest->verify_section_allocation(), "final configuration works"); | |
486 } | |
487 | |
488 csize_t CodeBuffer::total_offset_of(address addr) const { | |
489 csize_t code_size_so_far = 0; | |
490 for (int n = 0; n < (int)SECT_LIMIT; n++) { | |
491 const CodeSection* cs = code_section(n); | |
492 if (!cs->is_empty()) { | |
493 code_size_so_far = cs->align_at_start(code_size_so_far); | |
494 } | |
495 if (cs->contains2(addr)) { | |
496 return code_size_so_far + (addr - cs->start()); | |
497 } | |
498 code_size_so_far += cs->size(); | |
499 } | |
500 #ifndef PRODUCT | |
501 tty->print_cr("Dangling address " PTR_FORMAT " in:", addr); | |
502 ((CodeBuffer*)this)->print(); | |
503 #endif | |
504 ShouldNotReachHere(); | |
505 return -1; | |
506 } | |
507 | |
508 csize_t CodeBuffer::total_relocation_size() const { | |
509 csize_t lsize = copy_relocations_to(NULL); // dry run only | |
510 csize_t csize = total_code_size(); | |
511 csize_t total = RelocIterator::locs_and_index_size(csize, lsize); | |
512 return (csize_t) align_size_up(total, HeapWordSize); | |
513 } | |
514 | |
515 csize_t CodeBuffer::copy_relocations_to(CodeBlob* dest) const { | |
516 address buf = NULL; | |
517 csize_t buf_offset = 0; | |
518 csize_t buf_limit = 0; | |
519 if (dest != NULL) { | |
520 buf = (address)dest->relocation_begin(); | |
521 buf_limit = (address)dest->relocation_end() - buf; | |
522 assert((uintptr_t)buf % HeapWordSize == 0, "buf must be fully aligned"); | |
523 assert(buf_limit % HeapWordSize == 0, "buf must be evenly sized"); | |
524 } | |
525 // if dest == NULL, this is just the sizing pass | |
526 | |
527 csize_t code_end_so_far = 0; | |
528 csize_t code_point_so_far = 0; | |
529 for (int n = 0; n < (int)SECT_LIMIT; n++) { | |
530 // pull relocs out of each section | |
531 const CodeSection* cs = code_section(n); | |
532 assert(!(cs->is_empty() && cs->locs_count() > 0), "sanity"); | |
533 if (cs->is_empty()) continue; // skip trivial section | |
534 relocInfo* lstart = cs->locs_start(); | |
535 relocInfo* lend = cs->locs_end(); | |
536 csize_t lsize = (csize_t)( (address)lend - (address)lstart ); | |
537 csize_t csize = cs->size(); | |
538 code_end_so_far = cs->align_at_start(code_end_so_far); | |
539 | |
540 if (lsize > 0) { | |
541 // Figure out how to advance the combined relocation point | |
542 // first to the beginning of this section. | |
543 // We'll insert one or more filler relocs to span that gap. | |
544 // (Don't bother to improve this by editing the first reloc's offset.) | |
545 csize_t new_code_point = code_end_so_far; | |
546 for (csize_t jump; | |
547 code_point_so_far < new_code_point; | |
548 code_point_so_far += jump) { | |
549 jump = new_code_point - code_point_so_far; | |
550 relocInfo filler = filler_relocInfo(); | |
551 if (jump >= filler.addr_offset()) { | |
552 jump = filler.addr_offset(); | |
553 } else { // else shrink the filler to fit | |
554 filler = relocInfo(relocInfo::none, jump); | |
555 } | |
556 if (buf != NULL) { | |
557 assert(buf_offset + (csize_t)sizeof(filler) <= buf_limit, "filler in bounds"); | |
558 *(relocInfo*)(buf+buf_offset) = filler; | |
559 } | |
560 buf_offset += sizeof(filler); | |
561 } | |
562 | |
563 // Update code point and end to skip past this section: | |
564 csize_t last_code_point = code_end_so_far + cs->locs_point_off(); | |
565 assert(code_point_so_far <= last_code_point, "sanity"); | |
566 code_point_so_far = last_code_point; // advance past this guy's relocs | |
567 } | |
568 code_end_so_far += csize; // advance past this guy's instructions too | |
569 | |
570 // Done with filler; emit the real relocations: | |
571 if (buf != NULL && lsize != 0) { | |
572 assert(buf_offset + lsize <= buf_limit, "target in bounds"); | |
573 assert((uintptr_t)lstart % HeapWordSize == 0, "sane start"); | |
574 if (buf_offset % HeapWordSize == 0) { | |
575 // Use wordwise copies if possible: | |
576 Copy::disjoint_words((HeapWord*)lstart, | |
577 (HeapWord*)(buf+buf_offset), | |
578 (lsize + HeapWordSize-1) / HeapWordSize); | |
579 } else { | |
580 Copy::conjoint_bytes(lstart, buf+buf_offset, lsize); | |
581 } | |
582 } | |
583 buf_offset += lsize; | |
584 } | |
585 | |
586 // Align end of relocation info in target. | |
587 while (buf_offset % HeapWordSize != 0) { | |
588 if (buf != NULL) { | |
589 relocInfo padding = relocInfo(relocInfo::none, 0); | |
590 assert(buf_offset + (csize_t)sizeof(padding) <= buf_limit, "padding in bounds"); | |
591 *(relocInfo*)(buf+buf_offset) = padding; | |
592 } | |
593 buf_offset += sizeof(relocInfo); | |
594 } | |
595 | |
596 assert(code_end_so_far == total_code_size(), "sanity"); | |
597 | |
598 // Account for index: | |
599 if (buf != NULL) { | |
600 RelocIterator::create_index(dest->relocation_begin(), | |
601 buf_offset / sizeof(relocInfo), | |
602 dest->relocation_end()); | |
603 } | |
604 | |
605 return buf_offset; | |
606 } | |
607 | |
608 void CodeBuffer::copy_code_to(CodeBlob* dest_blob) { | |
609 #ifndef PRODUCT | |
610 if (PrintNMethods && (WizardMode || Verbose)) { | |
611 tty->print("done with CodeBuffer:"); | |
612 ((CodeBuffer*)this)->print(); | |
613 } | |
614 #endif //PRODUCT | |
615 | |
616 CodeBuffer dest(dest_blob->instructions_begin(), | |
617 dest_blob->instructions_size()); | |
618 assert(dest_blob->instructions_size() >= total_code_size(), "good sizing"); | |
619 this->compute_final_layout(&dest); | |
620 relocate_code_to(&dest); | |
621 | |
622 // transfer comments from buffer to blob | |
623 dest_blob->set_comments(_comments); | |
624 | |
625 // Done moving code bytes; were they the right size? | |
626 assert(round_to(dest.total_code_size(), oopSize) == dest_blob->instructions_size(), "sanity"); | |
627 | |
628 // Flush generated code | |
629 ICache::invalidate_range(dest_blob->instructions_begin(), | |
630 dest_blob->instructions_size()); | |
631 } | |
632 | |
633 // Move all my code into another code buffer. | |
634 // Consult applicable relocs to repair embedded addresses. | |
635 void CodeBuffer::relocate_code_to(CodeBuffer* dest) const { | |
636 DEBUG_ONLY(address dest_end = dest->_total_start + dest->_total_size); | |
637 for (int n = 0; n < (int)SECT_LIMIT; n++) { | |
638 // pull code out of each section | |
639 const CodeSection* cs = code_section(n); | |
640 if (cs->is_empty()) continue; // skip trivial section | |
641 CodeSection* dest_cs = dest->code_section(n); | |
642 assert(cs->size() == dest_cs->size(), "sanity"); | |
643 csize_t usize = dest_cs->size(); | |
644 csize_t wsize = align_size_up(usize, HeapWordSize); | |
645 assert(dest_cs->start() + wsize <= dest_end, "no overflow"); | |
646 // Copy the code as aligned machine words. | |
647 // This may also include an uninitialized partial word at the end. | |
648 Copy::disjoint_words((HeapWord*)cs->start(), | |
649 (HeapWord*)dest_cs->start(), | |
650 wsize / HeapWordSize); | |
651 | |
652 if (dest->blob() == NULL) { | |
653 // Destination is a final resting place, not just another buffer. | |
654 // Normalize uninitialized bytes in the final padding. | |
655 Copy::fill_to_bytes(dest_cs->end(), dest_cs->remaining(), | |
656 Assembler::code_fill_byte()); | |
657 } | |
658 | |
659 assert(cs->locs_start() != (relocInfo*)badAddress, | |
660 "this section carries no reloc storage, but reloc was attempted"); | |
661 | |
662 // Make the new code copy use the old copy's relocations: | |
663 dest_cs->initialize_locs_from(cs); | |
664 | |
665 { // Repair the pc relative information in the code after the move | |
666 RelocIterator iter(dest_cs); | |
667 while (iter.next()) { | |
668 iter.reloc()->fix_relocation_after_move(this, dest); | |
669 } | |
670 } | |
671 } | |
672 } | |
673 | |
674 csize_t CodeBuffer::figure_expanded_capacities(CodeSection* which_cs, | |
675 csize_t amount, | |
676 csize_t* new_capacity) { | |
677 csize_t new_total_cap = 0; | |
678 | |
679 int prev_n = -1; | |
680 for (int n = 0; n < (int)SECT_LIMIT; n++) { | |
681 const CodeSection* sect = code_section(n); | |
682 | |
683 if (!sect->is_empty()) { | |
684 // Compute initial padding; assign it to the previous non-empty guy. | |
685 // Cf. compute_final_layout. | |
686 csize_t padding = sect->align_at_start(new_total_cap) - new_total_cap; | |
687 if (padding != 0) { | |
688 new_total_cap += padding; | |
689 assert(prev_n >= 0, "sanity"); | |
690 new_capacity[prev_n] += padding; | |
691 } | |
692 prev_n = n; | |
693 } | |
694 | |
695 csize_t exp = sect->size(); // 100% increase | |
696 if ((uint)exp < 4*K) exp = 4*K; // minimum initial increase | |
697 if (sect == which_cs) { | |
698 if (exp < amount) exp = amount; | |
699 if (StressCodeBuffers) exp = amount; // expand only slightly | |
700 } else if (n == SECT_INSTS) { | |
701 // scale down inst increases to a more modest 25% | |
702 exp = 4*K + ((exp - 4*K) >> 2); | |
703 if (StressCodeBuffers) exp = amount / 2; // expand only slightly | |
704 } else if (sect->is_empty()) { | |
705 // do not grow an empty secondary section | |
706 exp = 0; | |
707 } | |
708 // Allow for inter-section slop: | |
709 exp += CodeSection::end_slop(); | |
710 csize_t new_cap = sect->size() + exp; | |
711 if (new_cap < sect->capacity()) { | |
712 // No need to expand after all. | |
713 new_cap = sect->capacity(); | |
714 } | |
715 new_capacity[n] = new_cap; | |
716 new_total_cap += new_cap; | |
717 } | |
718 | |
719 return new_total_cap; | |
720 } | |
721 | |
722 void CodeBuffer::expand(CodeSection* which_cs, csize_t amount) { | |
723 #ifndef PRODUCT | |
724 if (PrintNMethods && (WizardMode || Verbose)) { | |
725 tty->print("expanding CodeBuffer:"); | |
726 this->print(); | |
727 } | |
728 | |
729 if (StressCodeBuffers && blob() != NULL) { | |
730 static int expand_count = 0; | |
731 if (expand_count >= 0) expand_count += 1; | |
732 if (expand_count > 100 && is_power_of_2(expand_count)) { | |
733 tty->print_cr("StressCodeBuffers: have expanded %d times", expand_count); | |
734 // simulate an occasional allocation failure: | |
735 free_blob(); | |
736 } | |
737 } | |
738 #endif //PRODUCT | |
739 | |
740 // Resizing must be allowed | |
741 { | |
742 if (blob() == NULL) return; // caller must check for blob == NULL | |
743 for (int n = 0; n < (int)SECT_LIMIT; n++) { | |
744 guarantee(!code_section(n)->is_frozen(), "resizing not allowed when frozen"); | |
745 } | |
746 } | |
747 | |
748 // Figure new capacity for each section. | |
749 csize_t new_capacity[SECT_LIMIT]; | |
750 csize_t new_total_cap | |
751 = figure_expanded_capacities(which_cs, amount, new_capacity); | |
752 | |
753 // Create a new (temporary) code buffer to hold all the new data | |
754 CodeBuffer cb(name(), new_total_cap, 0); | |
755 if (cb.blob() == NULL) { | |
756 // Failed to allocate in code cache. | |
757 free_blob(); | |
758 return; | |
759 } | |
760 | |
761 // Create an old code buffer to remember which addresses used to go where. | |
762 // This will be useful when we do final assembly into the code cache, | |
763 // because we will need to know how to warp any internal address that | |
764 // has been created at any time in this CodeBuffer's past. | |
765 CodeBuffer* bxp = new CodeBuffer(_total_start, _total_size); | |
766 bxp->take_over_code_from(this); // remember the old undersized blob | |
767 DEBUG_ONLY(this->_blob = NULL); // silence a later assert | |
768 bxp->_before_expand = this->_before_expand; | |
769 this->_before_expand = bxp; | |
770 | |
771 // Give each section its required (expanded) capacity. | |
772 for (int n = (int)SECT_LIMIT-1; n >= SECT_INSTS; n--) { | |
773 CodeSection* cb_sect = cb.code_section(n); | |
774 CodeSection* this_sect = code_section(n); | |
775 if (new_capacity[n] == 0) continue; // already nulled out | |
776 if (n > SECT_INSTS) { | |
777 cb.initialize_section_size(cb_sect, new_capacity[n]); | |
778 } | |
779 assert(cb_sect->capacity() >= new_capacity[n], "big enough"); | |
780 address cb_start = cb_sect->start(); | |
781 cb_sect->set_end(cb_start + this_sect->size()); | |
782 if (this_sect->mark() == NULL) { | |
783 cb_sect->clear_mark(); | |
784 } else { | |
785 cb_sect->set_mark(cb_start + this_sect->mark_off()); | |
786 } | |
787 } | |
788 | |
789 // Move all the code and relocations to the new blob: | |
790 relocate_code_to(&cb); | |
791 | |
792 // Copy the temporary code buffer into the current code buffer. | |
793 // Basically, do {*this = cb}, except for some control information. | |
794 this->take_over_code_from(&cb); | |
795 cb.set_blob(NULL); | |
796 | |
797 // Zap the old code buffer contents, to avoid mistakenly using them. | |
798 debug_only(Copy::fill_to_bytes(bxp->_total_start, bxp->_total_size, | |
799 badCodeHeapFreeVal)); | |
800 | |
801 _decode_begin = NULL; // sanity | |
802 | |
803 // Make certain that the new sections are all snugly inside the new blob. | |
804 assert(verify_section_allocation(), "expanded allocation is ship-shape"); | |
805 | |
806 #ifndef PRODUCT | |
807 if (PrintNMethods && (WizardMode || Verbose)) { | |
808 tty->print("expanded CodeBuffer:"); | |
809 this->print(); | |
810 } | |
811 #endif //PRODUCT | |
812 } | |
813 | |
814 void CodeBuffer::take_over_code_from(CodeBuffer* cb) { | |
815 // Must already have disposed of the old blob somehow. | |
816 assert(blob() == NULL, "must be empty"); | |
817 #ifdef ASSERT | |
818 | |
819 #endif | |
820 // Take the new blob away from cb. | |
821 set_blob(cb->blob()); | |
822 // Take over all the section pointers. | |
823 for (int n = 0; n < (int)SECT_LIMIT; n++) { | |
824 CodeSection* cb_sect = cb->code_section(n); | |
825 CodeSection* this_sect = code_section(n); | |
826 this_sect->take_over_code_from(cb_sect); | |
827 } | |
828 _overflow_arena = cb->_overflow_arena; | |
829 // Make sure the old cb won't try to use it or free it. | |
830 DEBUG_ONLY(cb->_blob = (BufferBlob*)badAddress); | |
831 } | |
832 | |
833 #ifdef ASSERT | |
834 bool CodeBuffer::verify_section_allocation() { | |
835 address tstart = _total_start; | |
836 if (tstart == badAddress) return true; // smashed by set_blob(NULL) | |
837 address tend = tstart + _total_size; | |
838 if (_blob != NULL) { | |
839 assert(tstart >= _blob->instructions_begin(), "sanity"); | |
840 assert(tend <= _blob->instructions_end(), "sanity"); | |
841 } | |
842 address tcheck = tstart; // advancing pointer to verify disjointness | |
843 for (int n = 0; n < (int)SECT_LIMIT; n++) { | |
844 CodeSection* sect = code_section(n); | |
845 if (!sect->is_allocated()) continue; | |
846 assert(sect->start() >= tcheck, "sanity"); | |
847 tcheck = sect->start(); | |
848 assert((intptr_t)tcheck % sect->alignment() == 0 | |
849 || sect->is_empty() || _blob == NULL, | |
850 "start is aligned"); | |
851 assert(sect->end() >= tcheck, "sanity"); | |
852 assert(sect->end() <= tend, "sanity"); | |
853 } | |
854 return true; | |
855 } | |
856 #endif //ASSERT | |
857 | |
858 #ifndef PRODUCT | |
859 | |
860 void CodeSection::dump() { | |
861 address ptr = start(); | |
862 for (csize_t step; ptr < end(); ptr += step) { | |
863 step = end() - ptr; | |
864 if (step > jintSize * 4) step = jintSize * 4; | |
865 tty->print(PTR_FORMAT ": ", ptr); | |
866 while (step > 0) { | |
867 tty->print(" " PTR32_FORMAT, *(jint*)ptr); | |
868 ptr += jintSize; | |
869 } | |
870 tty->cr(); | |
871 } | |
872 } | |
873 | |
874 | |
875 void CodeSection::decode() { | |
876 Disassembler::decode(start(), end()); | |
877 } | |
878 | |
879 | |
880 void CodeBuffer::block_comment(intptr_t offset, const char * comment) { | |
881 _comments.add_comment(offset, comment); | |
882 } | |
883 | |
884 | |
885 class CodeComment: public CHeapObj { | |
886 private: | |
887 friend class CodeComments; | |
888 intptr_t _offset; | |
889 const char * _comment; | |
890 CodeComment* _next; | |
891 | |
892 ~CodeComment() { | |
893 assert(_next == NULL, "wrong interface for freeing list"); | |
894 os::free((void*)_comment); | |
895 } | |
896 | |
897 public: | |
898 CodeComment(intptr_t offset, const char * comment) { | |
899 _offset = offset; | |
900 _comment = os::strdup(comment); | |
901 _next = NULL; | |
902 } | |
903 | |
904 intptr_t offset() const { return _offset; } | |
905 const char * comment() const { return _comment; } | |
906 CodeComment* next() { return _next; } | |
907 | |
908 void set_next(CodeComment* next) { _next = next; } | |
909 | |
910 CodeComment* find(intptr_t offset) { | |
911 CodeComment* a = this; | |
912 while (a != NULL && a->_offset != offset) { | |
913 a = a->_next; | |
914 } | |
915 return a; | |
916 } | |
917 }; | |
918 | |
919 | |
920 void CodeComments::add_comment(intptr_t offset, const char * comment) { | |
921 CodeComment* c = new CodeComment(offset, comment); | |
922 CodeComment* insert = NULL; | |
923 if (_comments != NULL) { | |
924 CodeComment* c = _comments->find(offset); | |
925 insert = c; | |
926 while (c && c->offset() == offset) { | |
927 insert = c; | |
928 c = c->next(); | |
929 } | |
930 } | |
931 if (insert) { | |
932 // insert after comments with same offset | |
933 c->set_next(insert->next()); | |
934 insert->set_next(c); | |
935 } else { | |
936 c->set_next(_comments); | |
937 _comments = c; | |
938 } | |
939 } | |
940 | |
941 | |
942 void CodeComments::assign(CodeComments& other) { | |
943 assert(_comments == NULL, "don't overwrite old value"); | |
944 _comments = other._comments; | |
945 } | |
946 | |
947 | |
948 void CodeComments::print_block_comment(outputStream* stream, intptr_t offset) { | |
949 if (_comments != NULL) { | |
950 CodeComment* c = _comments->find(offset); | |
951 while (c && c->offset() == offset) { | |
100
c7c777385a15
6667042: PrintAssembly option does not work without special plugin
jrose
parents:
0
diff
changeset
|
952 stream->bol(); |
0 | 953 stream->print(" ;; "); |
954 stream->print_cr(c->comment()); | |
955 c = c->next(); | |
956 } | |
957 } | |
958 } | |
959 | |
960 | |
961 void CodeComments::free() { | |
962 CodeComment* n = _comments; | |
963 while (n) { | |
964 // unlink the node from the list saving a pointer to the next | |
965 CodeComment* p = n->_next; | |
966 n->_next = NULL; | |
967 delete n; | |
968 n = p; | |
969 } | |
970 _comments = NULL; | |
971 } | |
972 | |
973 | |
974 | |
975 void CodeBuffer::decode() { | |
976 Disassembler::decode(decode_begin(), code_end()); | |
977 _decode_begin = code_end(); | |
978 } | |
979 | |
980 | |
981 void CodeBuffer::skip_decode() { | |
982 _decode_begin = code_end(); | |
983 } | |
984 | |
985 | |
986 void CodeBuffer::decode_all() { | |
987 for (int n = 0; n < (int)SECT_LIMIT; n++) { | |
988 // dump contents of each section | |
989 CodeSection* cs = code_section(n); | |
990 tty->print_cr("! %s:", code_section_name(n)); | |
991 if (cs != consts()) | |
992 cs->decode(); | |
993 else | |
994 cs->dump(); | |
995 } | |
996 } | |
997 | |
998 | |
999 void CodeSection::print(const char* name) { | |
1000 csize_t locs_size = locs_end() - locs_start(); | |
1001 tty->print_cr(" %7s.code = " PTR_FORMAT " : " PTR_FORMAT " : " PTR_FORMAT " (%d of %d)%s", | |
1002 name, start(), end(), limit(), size(), capacity(), | |
1003 is_frozen()? " [frozen]": ""); | |
1004 tty->print_cr(" %7s.locs = " PTR_FORMAT " : " PTR_FORMAT " : " PTR_FORMAT " (%d of %d) point=%d", | |
1005 name, locs_start(), locs_end(), locs_limit(), locs_size, locs_capacity(), locs_point_off()); | |
1006 if (PrintRelocations) { | |
1007 RelocIterator iter(this); | |
1008 iter.print(); | |
1009 } | |
1010 } | |
1011 | |
1012 void CodeBuffer::print() { | |
1013 if (this == NULL) { | |
1014 tty->print_cr("NULL CodeBuffer pointer"); | |
1015 return; | |
1016 } | |
1017 | |
1018 tty->print_cr("CodeBuffer:"); | |
1019 for (int n = 0; n < (int)SECT_LIMIT; n++) { | |
1020 // print each section | |
1021 CodeSection* cs = code_section(n); | |
1022 cs->print(code_section_name(n)); | |
1023 } | |
1024 } | |
1025 | |
1026 #endif // PRODUCT |