1 /* 2 * Copyright (c) 1997, 2005, Oracle and/or its affiliates. All rights reserved. 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 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 "incls/_precompiled.incl" 26 # include "incls/_relocator.cpp.incl" 27 28 #define MAX_METHOD_LENGTH 65535 29 30 #define MAX_SHORT ((1 << 15) - 1) 31 #define MIN_SHORT (- (1 << 15)) 32 33 // Encapsulates a code change request. There are 3 types. 34 // General instruction, jump instruction, and table/lookup switches 35 // 36 class ChangeItem : public ResourceObj { 37 int _bci; 38 public: 39 ChangeItem(int bci) { _bci = bci; } 40 virtual bool handle_code_change(Relocator *r) = 0; 41 42 // type info 43 virtual bool is_widen() { return false; } 44 virtual bool is_jump_widen() { return false; } 45 virtual bool is_switch_pad() { return false; } 46 47 // accessors 48 int bci() { return _bci; } 49 void relocate(int break_bci, int delta) { if (_bci > break_bci) { _bci += delta; } } 50 51 virtual bool adjust(int bci, int delta) { return false; } 52 53 // debug 54 virtual void print() = 0; 55 }; 56 57 class ChangeWiden : public ChangeItem { 58 int _new_ilen; // New length of instruction at bci 59 u_char* _inst_buffer; // New bytecodes 60 public: 61 ChangeWiden(int bci, int new_ilen, u_char* inst_buffer) : ChangeItem(bci) { 62 _new_ilen = new_ilen; 63 _inst_buffer = inst_buffer; 64 } 65 66 // Callback to do instruction 67 bool handle_code_change(Relocator *r) { return r->handle_widen(bci(), _new_ilen, _inst_buffer); }; 68 69 bool is_widen() { return true; } 70 71 void print() { tty->print_cr("ChangeWiden. bci: %d New_ilen: %d", bci(), _new_ilen); } 72 }; 73 74 class ChangeJumpWiden : public ChangeItem { 75 int _delta; // New length of instruction at bci 76 public: 77 ChangeJumpWiden(int bci, int delta) : ChangeItem(bci) { _delta = delta; } 78 79 // Callback to do instruction 80 bool handle_code_change(Relocator *r) { return r->handle_jump_widen(bci(), _delta); }; 81 82 bool is_jump_widen() { return true; } 83 84 // If the bci matches, adjust the delta in the change jump request. 85 bool adjust(int jump_bci, int delta) { 86 if (bci() == jump_bci) { 87 if (_delta > 0) 88 _delta += delta; 89 else 90 _delta -= delta; 91 return true; 92 } 93 return false; 94 } 95 96 void print() { tty->print_cr("ChangeJumpWiden. bci: %d Delta: %d", bci(), _delta); } 97 }; 98 99 class ChangeSwitchPad : public ChangeItem { 100 int _padding; 101 bool _is_lookup_switch; 102 public: 103 ChangeSwitchPad(int bci, int padding, bool is_lookup_switch) : ChangeItem(bci) { 104 _padding = padding; 105 _is_lookup_switch = is_lookup_switch; 106 } 107 108 // Callback to do instruction 109 bool handle_code_change(Relocator *r) { return r->handle_switch_pad(bci(), _padding, _is_lookup_switch); }; 110 111 bool is_switch_pad() { return true; } 112 int padding() { return _padding; } 113 bool is_lookup_switch() { return _is_lookup_switch; } 114 115 void print() { tty->print_cr("ChangeSwitchPad. bci: %d Padding: %d IsLookupSwitch: %d", bci(), _padding, _is_lookup_switch); } 116 }; 117 118 //----------------------------------------------------------------------------------------------------------- 119 // Relocator code 120 121 Relocator::Relocator(methodHandle m, RelocatorListener* listener) { 122 set_method(m); 123 set_code_length(method()->code_size()); 124 set_code_array(NULL); 125 // Allocate code array and copy bytecodes 126 if (!expand_code_array(0)) { 127 // Should have at least MAX_METHOD_LENGTH available or the verifier 128 // would have failed. 129 ShouldNotReachHere(); 130 } 131 set_compressed_line_number_table(NULL); 132 set_compressed_line_number_table_size(0); 133 _listener = listener; 134 } 135 136 // size is the new size of the instruction at bci. Hence, if size is less than the current 137 // instruction sice, we will shrink the code. 138 methodHandle Relocator::insert_space_at(int bci, int size, u_char inst_buffer[], TRAPS) { 139 _changes = new GrowableArray<ChangeItem*> (10); 140 _changes->push(new ChangeWiden(bci, size, inst_buffer)); 141 142 if (TraceRelocator) { 143 tty->print_cr("Space at: %d Size: %d", bci, size); 144 _method->print(); 145 _method->print_codes(); 146 tty->print_cr("-------------------------------------------------"); 147 } 148 149 if (!handle_code_changes()) return methodHandle(); 150 151 // Construct the new method 152 methodHandle new_method = methodOopDesc::clone_with_new_data(method(), 153 code_array(), code_length(), 154 compressed_line_number_table(), 155 compressed_line_number_table_size(), 156 CHECK_(methodHandle())); 157 set_method(new_method); 158 159 if (TraceRelocator) { 160 tty->print_cr("-------------------------------------------------"); 161 tty->print_cr("new method"); 162 _method->print_codes(); 163 } 164 165 return new_method; 166 } 167 168 169 bool Relocator::handle_code_changes() { 170 assert(_changes != NULL, "changes vector must be initialized"); 171 172 while (!_changes->is_empty()) { 173 // Inv: everything is aligned. 174 ChangeItem* ci = _changes->first(); 175 176 if (TraceRelocator) { 177 ci->print(); 178 } 179 180 // Execute operation 181 if (!ci->handle_code_change(this)) return false; 182 183 // Shuffel items up 184 for (int index = 1; index < _changes->length(); index++) { 185 _changes->at_put(index-1, _changes->at(index)); 186 } 187 _changes->pop(); 188 } 189 return true; 190 } 191 192 193 bool Relocator::is_opcode_lookupswitch(Bytecodes::Code bc) { 194 switch (bc) { 195 case Bytecodes::_tableswitch: return false; 196 case Bytecodes::_lookupswitch: // not rewritten on ia64 197 case Bytecodes::_fast_linearswitch: // rewritten _lookupswitch 198 case Bytecodes::_fast_binaryswitch: return true; // rewritten _lookupswitch 199 default: ShouldNotReachHere(); 200 } 201 return true; // dummy 202 } 203 204 // We need a special instruction size method, since lookupswitches and tableswitches might not be 205 // properly alligned during relocation 206 int Relocator::rc_instr_len(int bci) { 207 Bytecodes::Code bc= code_at(bci); 208 switch (bc) { 209 // In the case of switch instructions, see if we have the original 210 // padding recorded. 211 case Bytecodes::_tableswitch: 212 case Bytecodes::_lookupswitch: 213 case Bytecodes::_fast_linearswitch: 214 case Bytecodes::_fast_binaryswitch: 215 { 216 int pad = get_orig_switch_pad(bci, is_opcode_lookupswitch(bc)); 217 if (pad == -1) { 218 return instruction_length_at(bci); 219 } 220 // Otherwise, depends on the switch type. 221 switch (bc) { 222 case Bytecodes::_tableswitch: { 223 int lo = int_at(bci + 1 + pad + 4 * 1); 224 int hi = int_at(bci + 1 + pad + 4 * 2); 225 int n = hi - lo + 1; 226 return 1 + pad + 4*(3 + n); 227 } 228 case Bytecodes::_lookupswitch: 229 case Bytecodes::_fast_linearswitch: 230 case Bytecodes::_fast_binaryswitch: { 231 int npairs = int_at(bci + 1 + pad + 4 * 1); 232 return 1 + pad + 4*(2 + 2*npairs); 233 } 234 default: 235 ShouldNotReachHere(); 236 } 237 } 238 } 239 return instruction_length_at(bci); 240 } 241 242 // If a change item is recorded for "pc", with type "ct", returns the 243 // associated padding, else -1. 244 int Relocator::get_orig_switch_pad(int bci, bool is_lookup_switch) { 245 for (int k = 0; k < _changes->length(); k++) { 246 ChangeItem* ci = _changes->at(k); 247 if (ci->is_switch_pad()) { 248 ChangeSwitchPad* csp = (ChangeSwitchPad*)ci; 249 if (csp->is_lookup_switch() == is_lookup_switch && csp->bci() == bci) { 250 return csp->padding(); 251 } 252 } 253 } 254 return -1; 255 } 256 257 258 // Push a ChangeJumpWiden if it doesn't already exist on the work queue, 259 // otherwise adjust the item already there by delta. The calculation for 260 // new_delta is wrong for this because it uses the offset stored in the 261 // code stream itself which wasn't fixed when item was pushed on the work queue. 262 void Relocator::push_jump_widen(int bci, int delta, int new_delta) { 263 for (int j = 0; j < _changes->length(); j++) { 264 ChangeItem* ci = _changes->at(j); 265 if (ci->adjust(bci, delta)) return; 266 } 267 _changes->push(new ChangeJumpWiden(bci, new_delta)); 268 } 269 270 271 // The current instruction of "c" is a jump; one of its offset starts 272 // at "offset" and is a short if "isShort" is "TRUE", 273 // and an integer otherwise. If the jump crosses "breakPC", change 274 // the span of the jump by "delta". 275 void Relocator::change_jump(int bci, int offset, bool is_short, int break_bci, int delta) { 276 int bci_delta = (is_short) ? short_at(offset) : int_at(offset); 277 int targ = bci + bci_delta; 278 279 if ((bci <= break_bci && targ > break_bci) || 280 (bci > break_bci && targ <= break_bci)) { 281 int new_delta; 282 if (bci_delta > 0) 283 new_delta = bci_delta + delta; 284 else 285 new_delta = bci_delta - delta; 286 287 if (is_short && ((new_delta > MAX_SHORT) || new_delta < MIN_SHORT)) { 288 push_jump_widen(bci, delta, new_delta); 289 } else if (is_short) { 290 short_at_put(offset, new_delta); 291 } else { 292 int_at_put(offset, new_delta); 293 } 294 } 295 } 296 297 298 // Changes all jumps crossing "break_bci" by "delta". May enqueue things 299 // on "rc->changes" 300 void Relocator::change_jumps(int break_bci, int delta) { 301 int bci = 0; 302 Bytecodes::Code bc; 303 // Now, adjust any affected instructions. 304 while (bci < code_length()) { 305 switch (bc= code_at(bci)) { 306 case Bytecodes::_ifeq: 307 case Bytecodes::_ifne: 308 case Bytecodes::_iflt: 309 case Bytecodes::_ifge: 310 case Bytecodes::_ifgt: 311 case Bytecodes::_ifle: 312 case Bytecodes::_if_icmpeq: 313 case Bytecodes::_if_icmpne: 314 case Bytecodes::_if_icmplt: 315 case Bytecodes::_if_icmpge: 316 case Bytecodes::_if_icmpgt: 317 case Bytecodes::_if_icmple: 318 case Bytecodes::_if_acmpeq: 319 case Bytecodes::_if_acmpne: 320 case Bytecodes::_ifnull: 321 case Bytecodes::_ifnonnull: 322 case Bytecodes::_goto: 323 case Bytecodes::_jsr: 324 change_jump(bci, bci+1, true, break_bci, delta); 325 break; 326 case Bytecodes::_goto_w: 327 case Bytecodes::_jsr_w: 328 change_jump(bci, bci+1, false, break_bci, delta); 329 break; 330 case Bytecodes::_tableswitch: 331 case Bytecodes::_lookupswitch: 332 case Bytecodes::_fast_linearswitch: 333 case Bytecodes::_fast_binaryswitch: { 334 int recPad = get_orig_switch_pad(bci, (bc != Bytecodes::_tableswitch)); 335 int oldPad = (recPad != -1) ? recPad : align(bci+1) - (bci+1); 336 if (bci > break_bci) { 337 int new_bci = bci + delta; 338 int newPad = align(new_bci+1) - (new_bci+1); 339 // Do we need to check the padding? 340 if (newPad != oldPad) { 341 if (recPad == -1) { 342 _changes->push(new ChangeSwitchPad(bci, oldPad, (bc != Bytecodes::_tableswitch))); 343 } 344 } 345 } 346 347 // Then the rest, which depend on the kind of switch. 348 switch (bc) { 349 case Bytecodes::_tableswitch: { 350 change_jump(bci, bci +1 + oldPad, false, break_bci, delta); 351 // We cannot use the Bytecode_tableswitch abstraction, since the padding might not be correct. 352 int lo = int_at(bci + 1 + oldPad + 4 * 1); 353 int hi = int_at(bci + 1 + oldPad + 4 * 2); 354 int n = hi - lo + 1; 355 for (int k = 0; k < n; k++) { 356 change_jump(bci, bci +1 + oldPad + 4*(k+3), false, break_bci, delta); 357 } 358 // Special next-bci calculation here... 359 bci += 1 + oldPad + (n+3)*4; 360 continue; 361 } 362 case Bytecodes::_lookupswitch: 363 case Bytecodes::_fast_linearswitch: 364 case Bytecodes::_fast_binaryswitch: { 365 change_jump(bci, bci +1 + oldPad, false, break_bci, delta); 366 // We cannot use the Bytecode_lookupswitch abstraction, since the padding might not be correct. 367 int npairs = int_at(bci + 1 + oldPad + 4 * 1); 368 for (int k = 0; k < npairs; k++) { 369 change_jump(bci, bci + 1 + oldPad + 4*(2 + 2*k + 1), false, break_bci, delta); 370 } 371 /* Special next-bci calculation here... */ 372 bci += 1 + oldPad + (2 + (npairs*2))*4; 373 continue; 374 } 375 default: 376 ShouldNotReachHere(); 377 } 378 } 379 default: 380 break; 381 } 382 bci += rc_instr_len(bci); 383 } 384 } 385 386 // The width of instruction at "pc" is changing by "delta". Adjust the 387 // exception table, if any, of "rc->mb". 388 void Relocator::adjust_exception_table(int bci, int delta) { 389 typeArrayOop table = method()->exception_table(); 390 for (int index = 0; index < table->length(); index +=4) { 391 if (table->int_at(index) > bci) { 392 table->int_at_put(index+0, table->int_at(index+0) + delta); 393 table->int_at_put(index+1, table->int_at(index+1) + delta); 394 } else if (bci < table->int_at(index+1)) { 395 table->int_at_put(index+1, table->int_at(index+1) + delta); 396 } 397 if (table->int_at(index+2) > bci) 398 table->int_at_put(index+2, table->int_at(index+2) + delta); 399 } 400 } 401 402 403 // The width of instruction at "bci" is changing by "delta". Adjust the line number table. 404 void Relocator::adjust_line_no_table(int bci, int delta) { 405 if (method()->has_linenumber_table()) { 406 CompressedLineNumberReadStream reader(method()->compressed_linenumber_table()); 407 CompressedLineNumberWriteStream writer(64); // plenty big for most line number tables 408 while (reader.read_pair()) { 409 int adjustment = (reader.bci() > bci) ? delta : 0; 410 writer.write_pair(reader.bci() + adjustment, reader.line()); 411 } 412 writer.write_terminator(); 413 set_compressed_line_number_table(writer.buffer()); 414 set_compressed_line_number_table_size(writer.position()); 415 } 416 } 417 418 419 // The width of instruction at "bci" is changing by "delta". Adjust the local variable table. 420 void Relocator::adjust_local_var_table(int bci, int delta) { 421 int localvariable_table_length = method()->localvariable_table_length(); 422 if (localvariable_table_length > 0) { 423 LocalVariableTableElement* table = method()->localvariable_table_start(); 424 for (int i = 0; i < localvariable_table_length; i++) { 425 u2 current_bci = table[i].start_bci; 426 if (current_bci > bci) { 427 table[i].start_bci = current_bci + delta; 428 } else { 429 u2 current_length = table[i].length; 430 if (current_bci + current_length > bci) { 431 table[i].length = current_length + delta; 432 } 433 } 434 } 435 } 436 } 437 438 // Create a new array, copying the src array but adding a hole at 439 // the specified location 440 static typeArrayOop insert_hole_at( 441 size_t where, int hole_sz, typeArrayOop src) { 442 Thread* THREAD = Thread::current(); 443 Handle src_hnd(THREAD, src); 444 typeArrayOop dst = 445 oopFactory::new_permanent_byteArray(src->length() + hole_sz, CHECK_NULL); 446 src = (typeArrayOop)src_hnd(); 447 448 address src_addr = (address)src->byte_at_addr(0); 449 address dst_addr = (address)dst->byte_at_addr(0); 450 451 memcpy(dst_addr, src_addr, where); 452 memcpy(dst_addr + where + hole_sz, 453 src_addr + where, src->length() - where); 454 return dst; 455 } 456 457 // The width of instruction at "bci" is changing by "delta". Adjust the stack 458 // map frames. 459 void Relocator::adjust_stack_map_table(int bci, int delta) { 460 if (method()->has_stackmap_table()) { 461 typeArrayOop data = method()->stackmap_data(); 462 // The data in the array is a classfile representation of the stackmap 463 // table attribute, less the initial u2 tag and u4 attribute_length fields. 464 stack_map_table_attribute* attr = stack_map_table_attribute::at( 465 (address)data->byte_at_addr(0) - (sizeof(u2) + sizeof(u4))); 466 467 int count = attr->number_of_entries(); 468 stack_map_frame* frame = attr->entries(); 469 int bci_iter = -1; 470 bool offset_adjusted = false; // only need to adjust one offset 471 472 for (int i = 0; i < count; ++i) { 473 int offset_delta = frame->offset_delta(); 474 bci_iter += offset_delta; 475 476 if (!offset_adjusted && bci_iter > bci) { 477 int new_offset_delta = offset_delta + delta; 478 479 if (frame->is_valid_offset(new_offset_delta)) { 480 frame->set_offset_delta(new_offset_delta); 481 } else { 482 assert(frame->is_same_frame() || 483 frame->is_same_frame_1_stack_item_frame(), 484 "Frame must be one of the compressed forms"); 485 // The new delta exceeds the capacity of the 'same_frame' or 486 // 'same_frame_1_stack_item_frame' frame types. We need to 487 // convert these frames to the extended versions, but the extended 488 // version is bigger and requires more room. So we allocate a 489 // new array and copy the data, being sure to leave u2-sized hole 490 // right after the 'frame_type' for the new offset field. 491 // 492 // We can safely ignore the reverse situation as a small delta 493 // can still be used in an extended version of the frame. 494 495 size_t frame_offset = (address)frame - (address)data->byte_at_addr(0); 496 497 data = insert_hole_at(frame_offset + 1, 2, data); 498 if (data == NULL) { 499 return; // out-of-memory? 500 } 501 502 address frame_addr = (address)(data->byte_at_addr(0) + frame_offset); 503 frame = stack_map_frame::at(frame_addr); 504 505 506 // Now convert the frames in place 507 if (frame->is_same_frame()) { 508 same_frame_extended::create_at(frame_addr, new_offset_delta); 509 } else { 510 same_frame_1_stack_item_extended::create_at( 511 frame_addr, new_offset_delta, NULL); 512 // the verification_info_type should already be at the right spot 513 } 514 } 515 offset_adjusted = true; // needs to be done only once, since subsequent 516 // values are offsets from the current 517 } 518 519 // The stack map frame may contain verification types, if so we need to 520 // check and update any Uninitialized type's bci (no matter where it is). 521 int number_of_types = frame->number_of_types(); 522 verification_type_info* types = frame->types(); 523 524 for (int i = 0; i < number_of_types; ++i) { 525 if (types->is_uninitialized() && types->bci() > bci) { 526 types->set_bci(types->bci() + delta); 527 } 528 types = types->next(); 529 } 530 531 // Full frame has stack values too 532 full_frame* ff = frame->as_full_frame(); 533 if (ff != NULL) { 534 address eol = (address)types; 535 number_of_types = ff->stack_slots(eol); 536 types = ff->stack(eol); 537 for (int i = 0; i < number_of_types; ++i) { 538 if (types->is_uninitialized() && types->bci() > bci) { 539 types->set_bci(types->bci() + delta); 540 } 541 types = types->next(); 542 } 543 } 544 545 frame = frame->next(); 546 } 547 548 method()->set_stackmap_data(data); // in case it has changed 549 } 550 } 551 552 553 bool Relocator::expand_code_array(int delta) { 554 int length = MAX2(code_length() + delta, code_length() * (100+code_slop_pct()) / 100); 555 556 if (length > MAX_METHOD_LENGTH) { 557 if (delta == 0 && code_length() <= MAX_METHOD_LENGTH) { 558 length = MAX_METHOD_LENGTH; 559 } else { 560 return false; 561 } 562 } 563 564 unsigned char* new_code_array = NEW_RESOURCE_ARRAY(unsigned char, length); 565 if (!new_code_array) return false; 566 567 // Expanding current array 568 if (code_array() != NULL) { 569 memcpy(new_code_array, code_array(), code_length()); 570 } else { 571 // Initial copy. Copy directly from methodOop 572 memcpy(new_code_array, method()->code_base(), code_length()); 573 } 574 575 set_code_array(new_code_array); 576 set_code_array_length(length); 577 578 return true; 579 } 580 581 582 // The instruction at "bci", whose size is "ilen", is changing size by 583 // "delta". Reallocate, move code, recalculate jumps, and enqueue 584 // change items as necessary. 585 bool Relocator::relocate_code(int bci, int ilen, int delta) { 586 int next_bci = bci + ilen; 587 if (delta > 0 && code_length() + delta > code_array_length()) { 588 // Expand allocated code space, if necessary. 589 if (!expand_code_array(delta)) { 590 return false; 591 } 592 } 593 594 // We require 4-byte alignment of code arrays. 595 assert(((intptr_t)code_array() & 3) == 0, "check code alignment"); 596 // Change jumps before doing the copying; this routine requires aligned switches. 597 change_jumps(bci, delta); 598 599 // In case we have shrunken a tableswitch/lookupswitch statement, we store the last 600 // bytes that get overwritten. We have to copy the bytes after the change_jumps method 601 // has been called, since it is likly to update last offset in a tableswitch/lookupswitch 602 if (delta < 0) { 603 assert(delta>=-3, "we cannot overwrite more than 3 bytes"); 604 memcpy(_overwrite, addr_at(bci + ilen + delta), -delta); 605 } 606 607 memmove(addr_at(next_bci + delta), addr_at(next_bci), code_length() - next_bci); 608 set_code_length(code_length() + delta); 609 // Also adjust exception tables... 610 adjust_exception_table(bci, delta); 611 // Line number tables... 612 adjust_line_no_table(bci, delta); 613 // And local variable table... 614 adjust_local_var_table(bci, delta); 615 616 // Adjust stack maps 617 adjust_stack_map_table(bci, delta); 618 619 // Relocate the pending change stack... 620 for (int j = 0; j < _changes->length(); j++) { 621 ChangeItem* ci = _changes->at(j); 622 ci->relocate(bci, delta); 623 } 624 625 // Notify any listeners about code relocation 626 notify(bci, delta, code_length()); 627 628 return true; 629 } 630 631 // relocate a general instruction. Called by ChangeWiden class 632 bool Relocator::handle_widen(int bci, int new_ilen, u_char inst_buffer[]) { 633 int ilen = rc_instr_len(bci); 634 if (!relocate_code(bci, ilen, new_ilen - ilen)) 635 return false; 636 637 // Insert new bytecode(s) 638 for(int k = 0; k < new_ilen; k++) { 639 code_at_put(bci + k, (Bytecodes::Code)inst_buffer[k]); 640 } 641 642 return true; 643 } 644 645 // handle jump_widen instruction. Called be ChangeJumpWiden class 646 bool Relocator::handle_jump_widen(int bci, int delta) { 647 int ilen = rc_instr_len(bci); 648 649 Bytecodes::Code bc = code_at(bci); 650 switch (bc) { 651 case Bytecodes::_ifeq: 652 case Bytecodes::_ifne: 653 case Bytecodes::_iflt: 654 case Bytecodes::_ifge: 655 case Bytecodes::_ifgt: 656 case Bytecodes::_ifle: 657 case Bytecodes::_if_icmpeq: 658 case Bytecodes::_if_icmpne: 659 case Bytecodes::_if_icmplt: 660 case Bytecodes::_if_icmpge: 661 case Bytecodes::_if_icmpgt: 662 case Bytecodes::_if_icmple: 663 case Bytecodes::_if_acmpeq: 664 case Bytecodes::_if_acmpne: 665 case Bytecodes::_ifnull: 666 case Bytecodes::_ifnonnull: { 667 const int goto_length = Bytecodes::length_for(Bytecodes::_goto); 668 669 // If 'if' points to the next bytecode after goto, it's already handled. 670 // it shouldn't be. 671 assert (short_at(bci+1) != ilen+goto_length, "if relocation already handled"); 672 assert(ilen == 3, "check length"); 673 674 // Convert to 0 if <cond> goto 6 675 // 3 _goto 11 676 // 6 _goto_w <wide delta offset> 677 // 11 <else code> 678 const int goto_w_length = Bytecodes::length_for(Bytecodes::_goto_w); 679 const int add_bci = goto_length + goto_w_length; 680 681 if (!relocate_code(bci, 3, /*delta*/add_bci)) return false; 682 683 // if bytecode points to goto_w instruction 684 short_at_put(bci + 1, ilen + goto_length); 685 686 int cbci = bci + ilen; 687 // goto around 688 code_at_put(cbci, Bytecodes::_goto); 689 short_at_put(cbci + 1, add_bci); 690 // goto_w <wide delta> 691 cbci = cbci + goto_length; 692 code_at_put(cbci, Bytecodes::_goto_w); 693 if (delta > 0) { 694 delta += 2; // goto_w is 2 bytes more than "if" code 695 } else { 696 delta -= ilen+goto_length; // branch starts at goto_w offset 697 } 698 int_at_put(cbci + 1, delta); 699 break; 700 701 } 702 case Bytecodes::_goto: 703 case Bytecodes::_jsr: 704 assert(ilen == 3, "check length"); 705 706 if (!relocate_code(bci, 3, 2)) return false; 707 if (bc == Bytecodes::_goto) 708 code_at_put(bci, Bytecodes::_goto_w); 709 else 710 code_at_put(bci, Bytecodes::_jsr_w); 711 712 // If it's a forward jump, add 2 for the widening. 713 if (delta > 0) delta += 2; 714 int_at_put(bci + 1, delta); 715 break; 716 717 default: ShouldNotReachHere(); 718 } 719 720 return true; 721 } 722 723 // handle lookup/table switch instructions. Called be ChangeSwitchPad class 724 bool Relocator::handle_switch_pad(int bci, int old_pad, bool is_lookup_switch) { 725 int ilen = rc_instr_len(bci); 726 int new_pad = align(bci+1) - (bci+1); 727 int pad_delta = new_pad - old_pad; 728 if (pad_delta != 0) { 729 int len; 730 if (!is_lookup_switch) { 731 int low = int_at(bci+1+old_pad+4); 732 int high = int_at(bci+1+old_pad+8); 733 len = high-low+1 + 3; // 3 for default, hi, lo. 734 } else { 735 int npairs = int_at(bci+1+old_pad+4); 736 len = npairs*2 + 2; // 2 for default, npairs. 737 } 738 // Because "relocateCode" does a "changeJumps" loop, 739 // which parses instructions to determine their length, 740 // we need to call that before messing with the current 741 // instruction. Since it may also overwrite the current 742 // instruction when moving down, remember the possibly 743 // overwritten part. 744 745 // Move the code following the instruction... 746 if (!relocate_code(bci, ilen, pad_delta)) return false; 747 748 if (pad_delta < 0) { 749 // Move the shrunken instruction down. 750 memmove(addr_at(bci + 1 + new_pad), 751 addr_at(bci + 1 + old_pad), 752 len * 4 + pad_delta); 753 memmove(addr_at(bci + 1 + new_pad + len*4 + pad_delta), 754 _overwrite, -pad_delta); 755 } else { 756 assert(pad_delta > 0, "check"); 757 // Move the expanded instruction up. 758 memmove(addr_at(bci +1 + new_pad), 759 addr_at(bci +1 + old_pad), 760 len * 4); 761 memset(addr_at(bci + 1), 0, new_pad); // pad must be 0 762 } 763 } 764 return true; 765 }