src/share/vm/asm/assembler.cpp

Fri, 23 Dec 2011 15:24:36 -0800

author
kvn
date
Fri, 23 Dec 2011 15:24:36 -0800
changeset 3395
40c2484c09e1
parent 2708
1d1603768966
child 3969
1d7922586cf6
permissions
-rw-r--r--

7110832: ctw/.../org_apache_avalon_composition_util_StringHelper crashes the VM
Summary: Distance is too large for one short branch in string_indexofC8().
Reviewed-by: iveresov

duke@435 1 /*
trims@2708 2 * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
duke@435 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@435 4 *
duke@435 5 * This code is free software; you can redistribute it and/or modify it
duke@435 6 * under the terms of the GNU General Public License version 2 only, as
duke@435 7 * published by the Free Software Foundation.
duke@435 8 *
duke@435 9 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@435 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@435 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@435 12 * version 2 for more details (a copy is included in the LICENSE file that
duke@435 13 * accompanied this code).
duke@435 14 *
duke@435 15 * You should have received a copy of the GNU General Public License version
duke@435 16 * 2 along with this work; if not, write to the Free Software Foundation,
duke@435 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@435 18 *
trims@1907 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
trims@1907 20 * or visit www.oracle.com if you need additional information or have any
trims@1907 21 * questions.
duke@435 22 *
duke@435 23 */
duke@435 24
stefank@2314 25 #include "precompiled.hpp"
stefank@2314 26 #include "asm/assembler.hpp"
stefank@2314 27 #include "asm/assembler.inline.hpp"
stefank@2314 28 #include "asm/codeBuffer.hpp"
stefank@2314 29 #include "runtime/icache.hpp"
stefank@2314 30 #include "runtime/os.hpp"
stefank@2314 31 #ifdef TARGET_ARCH_x86
stefank@2314 32 # include "assembler_x86.inline.hpp"
stefank@2314 33 #endif
stefank@2314 34 #ifdef TARGET_ARCH_sparc
stefank@2314 35 # include "assembler_sparc.inline.hpp"
stefank@2314 36 #endif
stefank@2314 37 #ifdef TARGET_ARCH_zero
stefank@2314 38 # include "assembler_zero.inline.hpp"
stefank@2314 39 #endif
bobv@2508 40 #ifdef TARGET_ARCH_arm
bobv@2508 41 # include "assembler_arm.inline.hpp"
bobv@2508 42 #endif
bobv@2508 43 #ifdef TARGET_ARCH_ppc
bobv@2508 44 # include "assembler_ppc.inline.hpp"
bobv@2508 45 #endif
duke@435 46
duke@435 47
duke@435 48 // Implementation of AbstractAssembler
duke@435 49 //
duke@435 50 // The AbstractAssembler is generating code into a CodeBuffer. To make code generation faster,
duke@435 51 // the assembler keeps a copy of the code buffers boundaries & modifies them when
duke@435 52 // emitting bytes rather than using the code buffers accessor functions all the time.
twisti@1040 53 // The code buffer is updated via set_code_end(...) after emitting a whole instruction.
duke@435 54
duke@435 55 AbstractAssembler::AbstractAssembler(CodeBuffer* code) {
duke@435 56 if (code == NULL) return;
duke@435 57 CodeSection* cs = code->insts();
duke@435 58 cs->clear_mark(); // new assembler kills old mark
duke@435 59 _code_section = cs;
duke@435 60 _code_begin = cs->start();
duke@435 61 _code_limit = cs->limit();
duke@435 62 _code_pos = cs->end();
duke@435 63 _oop_recorder= code->oop_recorder();
kvn@3395 64 DEBUG_ONLY( _short_branch_delta = 0; )
duke@435 65 if (_code_begin == NULL) {
jcoomes@1845 66 vm_exit_out_of_memory(0, err_msg("CodeCache: no room for %s",
jcoomes@1845 67 code->name()));
duke@435 68 }
duke@435 69 }
duke@435 70
duke@435 71 void AbstractAssembler::set_code_section(CodeSection* cs) {
duke@435 72 assert(cs->outer() == code_section()->outer(), "sanity");
duke@435 73 assert(cs->is_allocated(), "need to pre-allocate this section");
duke@435 74 cs->clear_mark(); // new assembly into this section kills old mark
duke@435 75 _code_section = cs;
duke@435 76 _code_begin = cs->start();
duke@435 77 _code_limit = cs->limit();
duke@435 78 _code_pos = cs->end();
duke@435 79 }
duke@435 80
duke@435 81 // Inform CodeBuffer that incoming code and relocation will be for stubs
duke@435 82 address AbstractAssembler::start_a_stub(int required_space) {
duke@435 83 CodeBuffer* cb = code();
duke@435 84 CodeSection* cs = cb->stubs();
duke@435 85 assert(_code_section == cb->insts(), "not in insts?");
duke@435 86 sync();
duke@435 87 if (cs->maybe_expand_to_ensure_remaining(required_space)
duke@435 88 && cb->blob() == NULL) {
duke@435 89 return NULL;
duke@435 90 }
duke@435 91 set_code_section(cs);
duke@435 92 return pc();
duke@435 93 }
duke@435 94
duke@435 95 // Inform CodeBuffer that incoming code and relocation will be code
duke@435 96 // Should not be called if start_a_stub() returned NULL
duke@435 97 void AbstractAssembler::end_a_stub() {
duke@435 98 assert(_code_section == code()->stubs(), "not in stubs?");
duke@435 99 sync();
duke@435 100 set_code_section(code()->insts());
duke@435 101 }
duke@435 102
duke@435 103 // Inform CodeBuffer that incoming code and relocation will be for stubs
duke@435 104 address AbstractAssembler::start_a_const(int required_space, int required_align) {
duke@435 105 CodeBuffer* cb = code();
duke@435 106 CodeSection* cs = cb->consts();
duke@435 107 assert(_code_section == cb->insts(), "not in insts?");
duke@435 108 sync();
duke@435 109 address end = cs->end();
duke@435 110 int pad = -(intptr_t)end & (required_align-1);
duke@435 111 if (cs->maybe_expand_to_ensure_remaining(pad + required_space)) {
duke@435 112 if (cb->blob() == NULL) return NULL;
duke@435 113 end = cs->end(); // refresh pointer
duke@435 114 }
duke@435 115 if (pad > 0) {
duke@435 116 while (--pad >= 0) { *end++ = 0; }
duke@435 117 cs->set_end(end);
duke@435 118 }
duke@435 119 set_code_section(cs);
duke@435 120 return end;
duke@435 121 }
duke@435 122
duke@435 123 // Inform CodeBuffer that incoming code and relocation will be code
duke@435 124 // Should not be called if start_a_const() returned NULL
duke@435 125 void AbstractAssembler::end_a_const() {
duke@435 126 assert(_code_section == code()->consts(), "not in consts?");
duke@435 127 sync();
duke@435 128 set_code_section(code()->insts());
duke@435 129 }
duke@435 130
duke@435 131
duke@435 132 void AbstractAssembler::flush() {
duke@435 133 sync();
duke@435 134 ICache::invalidate_range(addr_at(0), offset());
duke@435 135 }
duke@435 136
duke@435 137
duke@435 138 void AbstractAssembler::a_byte(int x) {
duke@435 139 emit_byte(x);
duke@435 140 }
duke@435 141
duke@435 142
duke@435 143 void AbstractAssembler::a_long(jint x) {
duke@435 144 emit_long(x);
duke@435 145 }
duke@435 146
duke@435 147 // Labels refer to positions in the (to be) generated code. There are bound
duke@435 148 // and unbound
duke@435 149 //
duke@435 150 // Bound labels refer to known positions in the already generated code.
duke@435 151 // offset() is the position the label refers to.
duke@435 152 //
duke@435 153 // Unbound labels refer to unknown positions in the code to be generated; it
duke@435 154 // may contain a list of unresolved displacements that refer to it
duke@435 155 #ifndef PRODUCT
duke@435 156 void AbstractAssembler::print(Label& L) {
duke@435 157 if (L.is_bound()) {
duke@435 158 tty->print_cr("bound label to %d|%d", L.loc_pos(), L.loc_sect());
duke@435 159 } else if (L.is_unbound()) {
duke@435 160 L.print_instructions((MacroAssembler*)this);
duke@435 161 } else {
duke@435 162 tty->print_cr("label in inconsistent state (loc = %d)", L.loc());
duke@435 163 }
duke@435 164 }
duke@435 165 #endif // PRODUCT
duke@435 166
duke@435 167
duke@435 168 void AbstractAssembler::bind(Label& L) {
duke@435 169 if (L.is_bound()) {
duke@435 170 // Assembler can bind a label more than once to the same place.
duke@435 171 guarantee(L.loc() == locator(), "attempt to redefine label");
duke@435 172 return;
duke@435 173 }
duke@435 174 L.bind_loc(locator());
duke@435 175 L.patch_instructions((MacroAssembler*)this);
duke@435 176 }
duke@435 177
duke@435 178 void AbstractAssembler::generate_stack_overflow_check( int frame_size_in_bytes) {
duke@435 179 if (UseStackBanging) {
duke@435 180 // Each code entry causes one stack bang n pages down the stack where n
duke@435 181 // is configurable by StackBangPages. The setting depends on the maximum
duke@435 182 // depth of VM call stack or native before going back into java code,
duke@435 183 // since only java code can raise a stack overflow exception using the
duke@435 184 // stack banging mechanism. The VM and native code does not detect stack
duke@435 185 // overflow.
duke@435 186 // The code in JavaCalls::call() checks that there is at least n pages
duke@435 187 // available, so all entry code needs to do is bang once for the end of
duke@435 188 // this shadow zone.
duke@435 189 // The entry code may need to bang additional pages if the framesize
duke@435 190 // is greater than a page.
duke@435 191
duke@435 192 const int page_size = os::vm_page_size();
duke@435 193 int bang_end = StackShadowPages*page_size;
duke@435 194
duke@435 195 // This is how far the previous frame's stack banging extended.
duke@435 196 const int bang_end_safe = bang_end;
duke@435 197
duke@435 198 if (frame_size_in_bytes > page_size) {
duke@435 199 bang_end += frame_size_in_bytes;
duke@435 200 }
duke@435 201
duke@435 202 int bang_offset = bang_end_safe;
duke@435 203 while (bang_offset <= bang_end) {
duke@435 204 // Need at least one stack bang at end of shadow zone.
duke@435 205 bang_stack_with_offset(bang_offset);
duke@435 206 bang_offset += page_size;
duke@435 207 }
duke@435 208 } // end (UseStackBanging)
duke@435 209 }
duke@435 210
duke@435 211 void Label::add_patch_at(CodeBuffer* cb, int branch_loc) {
duke@435 212 assert(_loc == -1, "Label is unbound");
duke@435 213 if (_patch_index < PatchCacheSize) {
duke@435 214 _patches[_patch_index] = branch_loc;
duke@435 215 } else {
duke@435 216 if (_patch_overflow == NULL) {
duke@435 217 _patch_overflow = cb->create_patch_overflow();
duke@435 218 }
duke@435 219 _patch_overflow->push(branch_loc);
duke@435 220 }
duke@435 221 ++_patch_index;
duke@435 222 }
duke@435 223
duke@435 224 void Label::patch_instructions(MacroAssembler* masm) {
duke@435 225 assert(is_bound(), "Label is bound");
duke@435 226 CodeBuffer* cb = masm->code();
duke@435 227 int target_sect = CodeBuffer::locator_sect(loc());
duke@435 228 address target = cb->locator_address(loc());
duke@435 229 while (_patch_index > 0) {
duke@435 230 --_patch_index;
duke@435 231 int branch_loc;
duke@435 232 if (_patch_index >= PatchCacheSize) {
duke@435 233 branch_loc = _patch_overflow->pop();
duke@435 234 } else {
duke@435 235 branch_loc = _patches[_patch_index];
duke@435 236 }
duke@435 237 int branch_sect = CodeBuffer::locator_sect(branch_loc);
duke@435 238 address branch = cb->locator_address(branch_loc);
duke@435 239 if (branch_sect == CodeBuffer::SECT_CONSTS) {
duke@435 240 // The thing to patch is a constant word.
duke@435 241 *(address*)branch = target;
duke@435 242 continue;
duke@435 243 }
duke@435 244
duke@435 245 #ifdef ASSERT
duke@435 246 // Cross-section branches only work if the
duke@435 247 // intermediate section boundaries are frozen.
duke@435 248 if (target_sect != branch_sect) {
duke@435 249 for (int n = MIN2(target_sect, branch_sect),
duke@435 250 nlimit = (target_sect + branch_sect) - n;
duke@435 251 n < nlimit; n++) {
duke@435 252 CodeSection* cs = cb->code_section(n);
duke@435 253 assert(cs->is_frozen(), "cross-section branch needs stable offsets");
duke@435 254 }
duke@435 255 }
duke@435 256 #endif //ASSERT
duke@435 257
duke@435 258 // Push the target offset into the branch instruction.
duke@435 259 masm->pd_patch_instruction(branch, target);
duke@435 260 }
duke@435 261 }
duke@435 262
jrose@1057 263 struct DelayedConstant {
jrose@1057 264 typedef void (*value_fn_t)();
jrose@1057 265 BasicType type;
jrose@1057 266 intptr_t value;
jrose@1057 267 value_fn_t value_fn;
jrose@1057 268 // This limit of 20 is generous for initial uses.
jrose@1057 269 // The limit needs to be large enough to store the field offsets
jrose@1057 270 // into classes which do not have statically fixed layouts.
jrose@1057 271 // (Initial use is for method handle object offsets.)
jrose@1057 272 // Look for uses of "delayed_value" in the source code
jrose@1057 273 // and make sure this number is generous enough to handle all of them.
jrose@1057 274 enum { DC_LIMIT = 20 };
jrose@1057 275 static DelayedConstant delayed_constants[DC_LIMIT];
jrose@1057 276 static DelayedConstant* add(BasicType type, value_fn_t value_fn);
jrose@1057 277 bool match(BasicType t, value_fn_t cfn) {
jrose@1057 278 return type == t && value_fn == cfn;
jrose@1057 279 }
jrose@1057 280 static void update_all();
jrose@1057 281 };
jrose@1057 282
jrose@1057 283 DelayedConstant DelayedConstant::delayed_constants[DC_LIMIT];
jrose@1057 284 // Default C structure initialization rules have the following effect here:
jrose@1057 285 // = { { (BasicType)0, (intptr_t)NULL }, ... };
jrose@1057 286
jrose@1057 287 DelayedConstant* DelayedConstant::add(BasicType type,
jrose@1057 288 DelayedConstant::value_fn_t cfn) {
jrose@1057 289 for (int i = 0; i < DC_LIMIT; i++) {
jrose@1057 290 DelayedConstant* dcon = &delayed_constants[i];
jrose@1057 291 if (dcon->match(type, cfn))
jrose@1057 292 return dcon;
jrose@1057 293 if (dcon->value_fn == NULL) {
jrose@1057 294 // (cmpxchg not because this is multi-threaded but because I'm paranoid)
jrose@1057 295 if (Atomic::cmpxchg_ptr(CAST_FROM_FN_PTR(void*, cfn), &dcon->value_fn, NULL) == NULL) {
jrose@1057 296 dcon->type = type;
jrose@1057 297 return dcon;
jrose@1057 298 }
jrose@1057 299 }
jrose@1057 300 }
jrose@1057 301 // If this assert is hit (in pre-integration testing!) then re-evaluate
jrose@1057 302 // the comment on the definition of DC_LIMIT.
jrose@1057 303 guarantee(false, "too many delayed constants");
jrose@1057 304 return NULL;
jrose@1057 305 }
jrose@1057 306
jrose@1057 307 void DelayedConstant::update_all() {
jrose@1057 308 for (int i = 0; i < DC_LIMIT; i++) {
jrose@1057 309 DelayedConstant* dcon = &delayed_constants[i];
jrose@1057 310 if (dcon->value_fn != NULL && dcon->value == 0) {
jrose@1057 311 typedef int (*int_fn_t)();
jrose@1057 312 typedef address (*address_fn_t)();
jrose@1057 313 switch (dcon->type) {
jrose@1057 314 case T_INT: dcon->value = (intptr_t) ((int_fn_t) dcon->value_fn)(); break;
jrose@1057 315 case T_ADDRESS: dcon->value = (intptr_t) ((address_fn_t)dcon->value_fn)(); break;
jrose@1057 316 }
jrose@1057 317 }
jrose@1057 318 }
jrose@1057 319 }
jrose@1057 320
jrose@1057 321 intptr_t* AbstractAssembler::delayed_value_addr(int(*value_fn)()) {
jrose@1057 322 DelayedConstant* dcon = DelayedConstant::add(T_INT, (DelayedConstant::value_fn_t) value_fn);
jrose@1057 323 return &dcon->value;
jrose@1057 324 }
jrose@1057 325 intptr_t* AbstractAssembler::delayed_value_addr(address(*value_fn)()) {
jrose@1057 326 DelayedConstant* dcon = DelayedConstant::add(T_ADDRESS, (DelayedConstant::value_fn_t) value_fn);
jrose@1057 327 return &dcon->value;
jrose@1057 328 }
jrose@1057 329 void AbstractAssembler::update_delayed_values() {
jrose@1057 330 DelayedConstant::update_all();
jrose@1057 331 }
jrose@1057 332
jrose@1057 333
jrose@1057 334
duke@435 335
duke@435 336 void AbstractAssembler::block_comment(const char* comment) {
duke@435 337 if (sect() == CodeBuffer::SECT_INSTS) {
duke@435 338 code_section()->outer()->block_comment(offset(), comment);
duke@435 339 }
duke@435 340 }
duke@435 341
coleenp@672 342 bool MacroAssembler::needs_explicit_null_check(intptr_t offset) {
coleenp@672 343 // Exception handler checks the nmethod's implicit null checks table
coleenp@672 344 // only when this method returns false.
kvn@1077 345 #ifdef _LP64
kvn@1077 346 if (UseCompressedOops && Universe::narrow_oop_base() != NULL) {
kvn@1077 347 assert (Universe::heap() != NULL, "java heap should be initialized");
coleenp@672 348 // The first page after heap_base is unmapped and
coleenp@672 349 // the 'offset' is equal to [heap_base + offset] for
coleenp@672 350 // narrow oop implicit null checks.
kvn@1077 351 uintptr_t base = (uintptr_t)Universe::narrow_oop_base();
kvn@1077 352 if ((uintptr_t)offset >= base) {
coleenp@672 353 // Normalize offset for the next check.
kvn@1077 354 offset = (intptr_t)(pointer_delta((void*)offset, (void*)base, 1));
coleenp@672 355 }
coleenp@672 356 }
kvn@1077 357 #endif
coleenp@672 358 return offset < 0 || os::vm_page_size() <= offset;
coleenp@672 359 }
duke@435 360
duke@435 361 #ifndef PRODUCT
duke@435 362 void Label::print_instructions(MacroAssembler* masm) const {
duke@435 363 CodeBuffer* cb = masm->code();
duke@435 364 for (int i = 0; i < _patch_index; ++i) {
duke@435 365 int branch_loc;
duke@435 366 if (i >= PatchCacheSize) {
duke@435 367 branch_loc = _patch_overflow->at(i - PatchCacheSize);
duke@435 368 } else {
duke@435 369 branch_loc = _patches[i];
duke@435 370 }
duke@435 371 int branch_pos = CodeBuffer::locator_pos(branch_loc);
duke@435 372 int branch_sect = CodeBuffer::locator_sect(branch_loc);
duke@435 373 address branch = cb->locator_address(branch_loc);
duke@435 374 tty->print_cr("unbound label");
duke@435 375 tty->print("@ %d|%d ", branch_pos, branch_sect);
duke@435 376 if (branch_sect == CodeBuffer::SECT_CONSTS) {
duke@435 377 tty->print_cr(PTR_FORMAT, *(address*)branch);
duke@435 378 continue;
duke@435 379 }
duke@435 380 masm->pd_print_patched_instruction(branch);
duke@435 381 tty->cr();
duke@435 382 }
duke@435 383 }
duke@435 384 #endif // ndef PRODUCT

mercurial