annotate src/share/vm/oops/generateOopMap.cpp @ 5645:fce21ac5968d

8027229: ICCE expected for >=2 maximally specific default methods. Summary: Need to process defaults for interfaces for invokespecial Reviewed-by: lfoltan, hseigel, coleenp, jrose
author acorn
date Wed, 13 Nov 2013 07:31:26 -0800
parents 2eef6d34833b
children 00bcb186fc5a
rev   line source
duke@0 1 /*
coleenp@4208 2 * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
duke@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@0 4 *
duke@0 5 * This code is free software; you can redistribute it and/or modify it
duke@0 6 * under the terms of the GNU General Public License version 2 only, as
duke@0 7 * published by the Free Software Foundation.
duke@0 8 *
duke@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@0 12 * version 2 for more details (a copy is included in the LICENSE file that
duke@0 13 * accompanied this code).
duke@0 14 *
duke@0 15 * You should have received a copy of the GNU General Public License version
duke@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
duke@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@0 18 *
trims@1472 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
trims@1472 20 * or visit www.oracle.com if you need additional information or have any
trims@1472 21 * questions.
duke@0 22 *
duke@0 23 */
duke@0 24
stefank@1879 25 #include "precompiled.hpp"
stefank@1879 26 #include "interpreter/bytecodeStream.hpp"
stefank@1879 27 #include "oops/generateOopMap.hpp"
stefank@1879 28 #include "oops/oop.inline.hpp"
coleenp@2062 29 #include "oops/symbol.hpp"
stefank@1879 30 #include "runtime/handles.inline.hpp"
stefank@1879 31 #include "runtime/java.hpp"
stefank@1879 32 #include "runtime/relocator.hpp"
stefank@1879 33 #include "utilities/bitMap.inline.hpp"
twisti@3534 34 #include "prims/methodHandles.hpp"
stefank@1879 35
duke@0 36 //
duke@0 37 //
duke@0 38 // Compute stack layouts for each instruction in method.
duke@0 39 //
duke@0 40 // Problems:
duke@0 41 // - What to do about jsr with different types of local vars?
duke@0 42 // Need maps that are conditional on jsr path?
duke@0 43 // - Jsr and exceptions should be done more efficiently (the retAddr stuff)
duke@0 44 //
duke@0 45 // Alternative:
duke@0 46 // - Could extend verifier to provide this information.
duke@0 47 // For: one fewer abstract interpreter to maintain. Against: the verifier
duke@0 48 // solves a bigger problem so slower (undesirable to force verification of
duke@0 49 // everything?).
duke@0 50 //
duke@0 51 // Algorithm:
duke@0 52 // Partition bytecodes into basic blocks
duke@0 53 // For each basic block: store entry state (vars, stack). For instructions
duke@0 54 // inside basic blocks we do not store any state (instead we recompute it
duke@0 55 // from state produced by previous instruction).
duke@0 56 //
duke@0 57 // Perform abstract interpretation of bytecodes over this lattice:
duke@0 58 //
duke@0 59 // _--'#'--_
duke@0 60 // / / \ \
duke@0 61 // / / \ \
duke@0 62 // / | | \
duke@0 63 // 'r' 'v' 'p' ' '
duke@0 64 // \ | | /
duke@0 65 // \ \ / /
duke@0 66 // \ \ / /
duke@0 67 // -- '@' --
duke@0 68 //
duke@0 69 // '#' top, result of conflict merge
duke@0 70 // 'r' reference type
duke@0 71 // 'v' value type
duke@0 72 // 'p' pc type for jsr/ret
duke@0 73 // ' ' uninitialized; never occurs on operand stack in Java
duke@0 74 // '@' bottom/unexecuted; initial state each bytecode.
duke@0 75 //
duke@0 76 // Basic block headers are the only merge points. We use this iteration to
duke@0 77 // compute the information:
duke@0 78 //
duke@0 79 // find basic blocks;
duke@0 80 // initialize them with uninitialized state;
duke@0 81 // initialize first BB according to method signature;
duke@0 82 // mark first BB changed
duke@0 83 // while (some BB is changed) do {
duke@0 84 // perform abstract interpration of all bytecodes in BB;
duke@0 85 // merge exit state of BB into entry state of all successor BBs,
duke@0 86 // noting if any of these change;
duke@0 87 // }
duke@0 88 //
duke@0 89 // One additional complication is necessary. The jsr instruction pushes
duke@0 90 // a return PC on the stack (a 'p' type in the abstract interpretation).
duke@0 91 // To be able to process "ret" bytecodes, we keep track of these return
duke@0 92 // PC's in a 'retAddrs' structure in abstract interpreter context (when
duke@0 93 // processing a "ret" bytecodes, it is not sufficient to know that it gets
duke@0 94 // an argument of the right type 'p'; we need to know which address it
duke@0 95 // returns to).
duke@0 96 //
duke@0 97 // (Note this comment is borrowed form the original author of the algorithm)
duke@0 98
duke@0 99 // ComputeCallStack
duke@0 100 //
duke@0 101 // Specialization of SignatureIterator - compute the effects of a call
duke@0 102 //
duke@0 103 class ComputeCallStack : public SignatureIterator {
duke@0 104 CellTypeState *_effect;
duke@0 105 int _idx;
duke@0 106
duke@0 107 void setup();
duke@0 108 void set(CellTypeState state) { _effect[_idx++] = state; }
duke@0 109 int length() { return _idx; };
duke@0 110
duke@0 111 virtual void do_bool () { set(CellTypeState::value); };
duke@0 112 virtual void do_char () { set(CellTypeState::value); };
duke@0 113 virtual void do_float () { set(CellTypeState::value); };
duke@0 114 virtual void do_byte () { set(CellTypeState::value); };
duke@0 115 virtual void do_short () { set(CellTypeState::value); };
duke@0 116 virtual void do_int () { set(CellTypeState::value); };
duke@0 117 virtual void do_void () { set(CellTypeState::bottom);};
duke@0 118 virtual void do_object(int begin, int end) { set(CellTypeState::ref); };
duke@0 119 virtual void do_array (int begin, int end) { set(CellTypeState::ref); };
duke@0 120
duke@0 121 void do_double() { set(CellTypeState::value);
duke@0 122 set(CellTypeState::value); }
duke@0 123 void do_long () { set(CellTypeState::value);
duke@0 124 set(CellTypeState::value); }
duke@0 125
duke@0 126 public:
coleenp@2062 127 ComputeCallStack(Symbol* signature) : SignatureIterator(signature) {};
duke@0 128
duke@0 129 // Compute methods
duke@0 130 int compute_for_parameters(bool is_static, CellTypeState *effect) {
duke@0 131 _idx = 0;
duke@0 132 _effect = effect;
duke@0 133
duke@0 134 if (!is_static)
duke@0 135 effect[_idx++] = CellTypeState::ref;
duke@0 136
duke@0 137 iterate_parameters();
duke@0 138
duke@0 139 return length();
duke@0 140 };
duke@0 141
duke@0 142 int compute_for_returntype(CellTypeState *effect) {
duke@0 143 _idx = 0;
duke@0 144 _effect = effect;
duke@0 145 iterate_returntype();
duke@0 146 set(CellTypeState::bottom); // Always terminate with a bottom state, so ppush works
duke@0 147
duke@0 148 return length();
duke@0 149 }
duke@0 150 };
duke@0 151
duke@0 152 //=========================================================================================
duke@0 153 // ComputeEntryStack
duke@0 154 //
duke@0 155 // Specialization of SignatureIterator - in order to set up first stack frame
duke@0 156 //
duke@0 157 class ComputeEntryStack : public SignatureIterator {
duke@0 158 CellTypeState *_effect;
duke@0 159 int _idx;
duke@0 160
duke@0 161 void setup();
duke@0 162 void set(CellTypeState state) { _effect[_idx++] = state; }
duke@0 163 int length() { return _idx; };
duke@0 164
duke@0 165 virtual void do_bool () { set(CellTypeState::value); };
duke@0 166 virtual void do_char () { set(CellTypeState::value); };
duke@0 167 virtual void do_float () { set(CellTypeState::value); };
duke@0 168 virtual void do_byte () { set(CellTypeState::value); };
duke@0 169 virtual void do_short () { set(CellTypeState::value); };
duke@0 170 virtual void do_int () { set(CellTypeState::value); };
duke@0 171 virtual void do_void () { set(CellTypeState::bottom);};
duke@0 172 virtual void do_object(int begin, int end) { set(CellTypeState::make_slot_ref(_idx)); }
duke@0 173 virtual void do_array (int begin, int end) { set(CellTypeState::make_slot_ref(_idx)); }
duke@0 174
duke@0 175 void do_double() { set(CellTypeState::value);
duke@0 176 set(CellTypeState::value); }
duke@0 177 void do_long () { set(CellTypeState::value);
duke@0 178 set(CellTypeState::value); }
duke@0 179
duke@0 180 public:
coleenp@2062 181 ComputeEntryStack(Symbol* signature) : SignatureIterator(signature) {};
duke@0 182
duke@0 183 // Compute methods
duke@0 184 int compute_for_parameters(bool is_static, CellTypeState *effect) {
duke@0 185 _idx = 0;
duke@0 186 _effect = effect;
duke@0 187
duke@0 188 if (!is_static)
duke@0 189 effect[_idx++] = CellTypeState::make_slot_ref(0);
duke@0 190
duke@0 191 iterate_parameters();
duke@0 192
duke@0 193 return length();
duke@0 194 };
duke@0 195
duke@0 196 int compute_for_returntype(CellTypeState *effect) {
duke@0 197 _idx = 0;
duke@0 198 _effect = effect;
duke@0 199 iterate_returntype();
duke@0 200 set(CellTypeState::bottom); // Always terminate with a bottom state, so ppush works
duke@0 201
duke@0 202 return length();
duke@0 203 }
duke@0 204 };
duke@0 205
duke@0 206 //=====================================================================================
duke@0 207 //
duke@0 208 // Implementation of RetTable/RetTableEntry
duke@0 209 //
duke@0 210 // Contains function to itereate through all bytecodes
duke@0 211 // and find all return entry points
duke@0 212 //
duke@0 213 int RetTable::_init_nof_entries = 10;
duke@0 214 int RetTableEntry::_init_nof_jsrs = 5;
duke@0 215
duke@0 216 void RetTableEntry::add_delta(int bci, int delta) {
duke@0 217 if (_target_bci > bci) _target_bci += delta;
duke@0 218
duke@0 219 for (int k = 0; k < _jsrs->length(); k++) {
duke@0 220 int jsr = _jsrs->at(k);
duke@0 221 if (jsr > bci) _jsrs->at_put(k, jsr+delta);
duke@0 222 }
duke@0 223 }
duke@0 224
duke@0 225 void RetTable::compute_ret_table(methodHandle method) {
duke@0 226 BytecodeStream i(method);
duke@0 227 Bytecodes::Code bytecode;
duke@0 228
duke@0 229 while( (bytecode = i.next()) >= 0) {
duke@0 230 switch (bytecode) {
duke@0 231 case Bytecodes::_jsr:
duke@0 232 add_jsr(i.next_bci(), i.dest());
duke@0 233 break;
duke@0 234 case Bytecodes::_jsr_w:
duke@0 235 add_jsr(i.next_bci(), i.dest_w());
duke@0 236 break;
duke@0 237 }
duke@0 238 }
duke@0 239 }
duke@0 240
duke@0 241 void RetTable::add_jsr(int return_bci, int target_bci) {
duke@0 242 RetTableEntry* entry = _first;
duke@0 243
duke@0 244 // Scan table for entry
duke@0 245 for (;entry && entry->target_bci() != target_bci; entry = entry->next());
duke@0 246
duke@0 247 if (!entry) {
duke@0 248 // Allocate new entry and put in list
duke@0 249 entry = new RetTableEntry(target_bci, _first);
duke@0 250 _first = entry;
duke@0 251 }
duke@0 252
duke@0 253 // Now "entry" is set. Make sure that the entry is initialized
duke@0 254 // and has room for the new jsr.
duke@0 255 entry->add_jsr(return_bci);
duke@0 256 }
duke@0 257
duke@0 258 RetTableEntry* RetTable::find_jsrs_for_target(int targBci) {
duke@0 259 RetTableEntry *cur = _first;
duke@0 260
duke@0 261 while(cur) {
duke@0 262 assert(cur->target_bci() != -1, "sanity check");
duke@0 263 if (cur->target_bci() == targBci) return cur;
duke@0 264 cur = cur->next();
duke@0 265 }
duke@0 266 ShouldNotReachHere();
duke@0 267 return NULL;
duke@0 268 }
duke@0 269
duke@0 270 // The instruction at bci is changing size by "delta". Update the return map.
duke@0 271 void RetTable::update_ret_table(int bci, int delta) {
duke@0 272 RetTableEntry *cur = _first;
duke@0 273 while(cur) {
duke@0 274 cur->add_delta(bci, delta);
duke@0 275 cur = cur->next();
duke@0 276 }
duke@0 277 }
duke@0 278
duke@0 279 //
duke@0 280 // Celltype state
duke@0 281 //
duke@0 282
duke@0 283 CellTypeState CellTypeState::bottom = CellTypeState::make_bottom();
duke@0 284 CellTypeState CellTypeState::uninit = CellTypeState::make_any(uninit_value);
duke@0 285 CellTypeState CellTypeState::ref = CellTypeState::make_any(ref_conflict);
duke@0 286 CellTypeState CellTypeState::value = CellTypeState::make_any(val_value);
duke@0 287 CellTypeState CellTypeState::refUninit = CellTypeState::make_any(ref_conflict | uninit_value);
duke@0 288 CellTypeState CellTypeState::top = CellTypeState::make_top();
duke@0 289 CellTypeState CellTypeState::addr = CellTypeState::make_any(addr_conflict);
duke@0 290
duke@0 291 // Commonly used constants
duke@0 292 static CellTypeState epsilonCTS[1] = { CellTypeState::bottom };
duke@0 293 static CellTypeState refCTS = CellTypeState::ref;
duke@0 294 static CellTypeState valCTS = CellTypeState::value;
duke@0 295 static CellTypeState vCTS[2] = { CellTypeState::value, CellTypeState::bottom };
duke@0 296 static CellTypeState rCTS[2] = { CellTypeState::ref, CellTypeState::bottom };
duke@0 297 static CellTypeState rrCTS[3] = { CellTypeState::ref, CellTypeState::ref, CellTypeState::bottom };
duke@0 298 static CellTypeState vrCTS[3] = { CellTypeState::value, CellTypeState::ref, CellTypeState::bottom };
duke@0 299 static CellTypeState vvCTS[3] = { CellTypeState::value, CellTypeState::value, CellTypeState::bottom };
duke@0 300 static CellTypeState rvrCTS[4] = { CellTypeState::ref, CellTypeState::value, CellTypeState::ref, CellTypeState::bottom };
duke@0 301 static CellTypeState vvrCTS[4] = { CellTypeState::value, CellTypeState::value, CellTypeState::ref, CellTypeState::bottom };
duke@0 302 static CellTypeState vvvCTS[4] = { CellTypeState::value, CellTypeState::value, CellTypeState::value, CellTypeState::bottom };
duke@0 303 static CellTypeState vvvrCTS[5] = { CellTypeState::value, CellTypeState::value, CellTypeState::value, CellTypeState::ref, CellTypeState::bottom };
duke@0 304 static CellTypeState vvvvCTS[5] = { CellTypeState::value, CellTypeState::value, CellTypeState::value, CellTypeState::value, CellTypeState::bottom };
duke@0 305
duke@0 306 char CellTypeState::to_char() const {
duke@0 307 if (can_be_reference()) {
duke@0 308 if (can_be_value() || can_be_address())
duke@0 309 return '#'; // Conflict that needs to be rewritten
duke@0 310 else
duke@0 311 return 'r';
duke@0 312 } else if (can_be_value())
duke@0 313 return 'v';
duke@0 314 else if (can_be_address())
duke@0 315 return 'p';
duke@0 316 else if (can_be_uninit())
duke@0 317 return ' ';
duke@0 318 else
duke@0 319 return '@';
duke@0 320 }
duke@0 321
duke@0 322
duke@0 323 // Print a detailed CellTypeState. Indicate all bits that are set. If
duke@0 324 // the CellTypeState represents an address or a reference, print the
duke@0 325 // value of the additional information.
duke@0 326 void CellTypeState::print(outputStream *os) {
duke@0 327 if (can_be_address()) {
duke@0 328 os->print("(p");
duke@0 329 } else {
duke@0 330 os->print("( ");
duke@0 331 }
duke@0 332 if (can_be_reference()) {
duke@0 333 os->print("r");
duke@0 334 } else {
duke@0 335 os->print(" ");
duke@0 336 }
duke@0 337 if (can_be_value()) {
duke@0 338 os->print("v");
duke@0 339 } else {
duke@0 340 os->print(" ");
duke@0 341 }
duke@0 342 if (can_be_uninit()) {
duke@0 343 os->print("u|");
duke@0 344 } else {
duke@0 345 os->print(" |");
duke@0 346 }
duke@0 347 if (is_info_top()) {
duke@0 348 os->print("Top)");
duke@0 349 } else if (is_info_bottom()) {
duke@0 350 os->print("Bot)");
duke@0 351 } else {
duke@0 352 if (is_reference()) {
duke@0 353 int info = get_info();
duke@0 354 int data = info & ~(ref_not_lock_bit | ref_slot_bit);
duke@0 355 if (info & ref_not_lock_bit) {
duke@0 356 // Not a monitor lock reference.
duke@0 357 if (info & ref_slot_bit) {
duke@0 358 // slot
duke@0 359 os->print("slot%d)", data);
duke@0 360 } else {
duke@0 361 // line
duke@0 362 os->print("line%d)", data);
duke@0 363 }
duke@0 364 } else {
duke@0 365 // lock
duke@0 366 os->print("lock%d)", data);
duke@0 367 }
duke@0 368 } else {
duke@0 369 os->print("%d)", get_info());
duke@0 370 }
duke@0 371 }
duke@0 372 }
duke@0 373
duke@0 374 //
duke@0 375 // Basicblock handling methods
duke@0 376 //
duke@0 377
duke@0 378 void GenerateOopMap ::initialize_bb() {
duke@0 379 _gc_points = 0;
duke@0 380 _bb_count = 0;
ysr@342 381 _bb_hdr_bits.clear();
ysr@342 382 _bb_hdr_bits.resize(method()->code_size());
duke@0 383 }
duke@0 384
duke@0 385 void GenerateOopMap::bb_mark_fct(GenerateOopMap *c, int bci, int *data) {
duke@0 386 assert(bci>= 0 && bci < c->method()->code_size(), "index out of bounds");
duke@0 387 if (c->is_bb_header(bci))
duke@0 388 return;
duke@0 389
duke@0 390 if (TraceNewOopMapGeneration) {
duke@0 391 tty->print_cr("Basicblock#%d begins at: %d", c->_bb_count, bci);
duke@0 392 }
duke@0 393 c->set_bbmark_bit(bci);
duke@0 394 c->_bb_count++;
duke@0 395 }
duke@0 396
duke@0 397
duke@0 398 void GenerateOopMap::mark_bbheaders_and_count_gc_points() {
duke@0 399 initialize_bb();
duke@0 400
duke@0 401 bool fellThrough = false; // False to get first BB marked.
duke@0 402
duke@0 403 // First mark all exception handlers as start of a basic-block
jiangli@3482 404 ExceptionTable excps(method());
jiangli@3482 405 for(int i = 0; i < excps.length(); i ++) {
jiangli@3482 406 bb_mark_fct(this, excps.handler_pc(i), NULL);
duke@0 407 }
duke@0 408
duke@0 409 // Then iterate through the code
duke@0 410 BytecodeStream bcs(_method);
duke@0 411 Bytecodes::Code bytecode;
duke@0 412
duke@0 413 while( (bytecode = bcs.next()) >= 0) {
duke@0 414 int bci = bcs.bci();
duke@0 415
duke@0 416 if (!fellThrough)
duke@0 417 bb_mark_fct(this, bci, NULL);
duke@0 418
duke@0 419 fellThrough = jump_targets_do(&bcs, &GenerateOopMap::bb_mark_fct, NULL);
duke@0 420
duke@0 421 /* We will also mark successors of jsr's as basic block headers. */
duke@0 422 switch (bytecode) {
duke@0 423 case Bytecodes::_jsr:
duke@0 424 assert(!fellThrough, "should not happen");
duke@0 425 bb_mark_fct(this, bci + Bytecodes::length_for(bytecode), NULL);
duke@0 426 break;
duke@0 427 case Bytecodes::_jsr_w:
duke@0 428 assert(!fellThrough, "should not happen");
duke@0 429 bb_mark_fct(this, bci + Bytecodes::length_for(bytecode), NULL);
duke@0 430 break;
duke@0 431 }
duke@0 432
duke@0 433 if (possible_gc_point(&bcs))
duke@0 434 _gc_points++;
duke@0 435 }
duke@0 436 }
duke@0 437
duke@0 438 void GenerateOopMap::reachable_basicblock(GenerateOopMap *c, int bci, int *data) {
duke@0 439 assert(bci>= 0 && bci < c->method()->code_size(), "index out of bounds");
duke@0 440 BasicBlock* bb = c->get_basic_block_at(bci);
duke@0 441 if (bb->is_dead()) {
duke@0 442 bb->mark_as_alive();
duke@0 443 *data = 1; // Mark basicblock as changed
duke@0 444 }
duke@0 445 }
duke@0 446
duke@0 447
duke@0 448 void GenerateOopMap::mark_reachable_code() {
duke@0 449 int change = 1; // int to get function pointers to work
duke@0 450
duke@0 451 // Mark entry basic block as alive and all exception handlers
duke@0 452 _basic_blocks[0].mark_as_alive();
jiangli@3482 453 ExceptionTable excps(method());
jiangli@3482 454 for(int i = 0; i < excps.length(); i++) {
jiangli@3482 455 BasicBlock *bb = get_basic_block_at(excps.handler_pc(i));
duke@0 456 // If block is not already alive (due to multiple exception handlers to same bb), then
duke@0 457 // make it alive
duke@0 458 if (bb->is_dead()) bb->mark_as_alive();
duke@0 459 }
duke@0 460
duke@0 461 BytecodeStream bcs(_method);
duke@0 462
duke@0 463 // Iterate through all basic blocks until we reach a fixpoint
duke@0 464 while (change) {
duke@0 465 change = 0;
duke@0 466
duke@0 467 for (int i = 0; i < _bb_count; i++) {
duke@0 468 BasicBlock *bb = &_basic_blocks[i];
duke@0 469 if (bb->is_alive()) {
duke@0 470 // Position bytecodestream at last bytecode in basicblock
duke@0 471 bcs.set_start(bb->_end_bci);
duke@0 472 bcs.next();
duke@0 473 Bytecodes::Code bytecode = bcs.code();
duke@0 474 int bci = bcs.bci();
duke@0 475 assert(bci == bb->_end_bci, "wrong bci");
duke@0 476
duke@0 477 bool fell_through = jump_targets_do(&bcs, &GenerateOopMap::reachable_basicblock, &change);
duke@0 478
duke@0 479 // We will also mark successors of jsr's as alive.
duke@0 480 switch (bytecode) {
duke@0 481 case Bytecodes::_jsr:
duke@0 482 case Bytecodes::_jsr_w:
duke@0 483 assert(!fell_through, "should not happen");
duke@0 484 reachable_basicblock(this, bci + Bytecodes::length_for(bytecode), &change);
duke@0 485 break;
duke@0 486 }
duke@0 487 if (fell_through) {
duke@0 488 // Mark successor as alive
duke@0 489 if (bb[1].is_dead()) {
duke@0 490 bb[1].mark_as_alive();
duke@0 491 change = 1;
duke@0 492 }
duke@0 493 }
duke@0 494 }
duke@0 495 }
duke@0 496 }
duke@0 497 }
duke@0 498
duke@0 499 /* If the current instruction in "c" has no effect on control flow,
duke@0 500 returns "true". Otherwise, calls "jmpFct" one or more times, with
duke@0 501 "c", an appropriate "pcDelta", and "data" as arguments, then
duke@0 502 returns "false". There is one exception: if the current
duke@0 503 instruction is a "ret", returns "false" without calling "jmpFct".
duke@0 504 Arrangements for tracking the control flow of a "ret" must be made
duke@0 505 externally. */
duke@0 506 bool GenerateOopMap::jump_targets_do(BytecodeStream *bcs, jmpFct_t jmpFct, int *data) {
duke@0 507 int bci = bcs->bci();
duke@0 508
duke@0 509 switch (bcs->code()) {
duke@0 510 case Bytecodes::_ifeq:
duke@0 511 case Bytecodes::_ifne:
duke@0 512 case Bytecodes::_iflt:
duke@0 513 case Bytecodes::_ifge:
duke@0 514 case Bytecodes::_ifgt:
duke@0 515 case Bytecodes::_ifle:
duke@0 516 case Bytecodes::_if_icmpeq:
duke@0 517 case Bytecodes::_if_icmpne:
duke@0 518 case Bytecodes::_if_icmplt:
duke@0 519 case Bytecodes::_if_icmpge:
duke@0 520 case Bytecodes::_if_icmpgt:
duke@0 521 case Bytecodes::_if_icmple:
duke@0 522 case Bytecodes::_if_acmpeq:
duke@0 523 case Bytecodes::_if_acmpne:
duke@0 524 case Bytecodes::_ifnull:
duke@0 525 case Bytecodes::_ifnonnull:
duke@0 526 (*jmpFct)(this, bcs->dest(), data);
duke@0 527 (*jmpFct)(this, bci + 3, data);
duke@0 528 break;
duke@0 529
duke@0 530 case Bytecodes::_goto:
duke@0 531 (*jmpFct)(this, bcs->dest(), data);
duke@0 532 break;
duke@0 533 case Bytecodes::_goto_w:
duke@0 534 (*jmpFct)(this, bcs->dest_w(), data);
duke@0 535 break;
duke@0 536 case Bytecodes::_tableswitch:
never@2027 537 { Bytecode_tableswitch tableswitch(method(), bcs->bcp());
never@2027 538 int len = tableswitch.length();
duke@0 539
never@2027 540 (*jmpFct)(this, bci + tableswitch.default_offset(), data); /* Default. jump address */
duke@0 541 while (--len >= 0) {
never@2027 542 (*jmpFct)(this, bci + tableswitch.dest_offset_at(len), data);
duke@0 543 }
duke@0 544 break;
duke@0 545 }
duke@0 546
duke@0 547 case Bytecodes::_lookupswitch:
never@2027 548 { Bytecode_lookupswitch lookupswitch(method(), bcs->bcp());
never@2027 549 int npairs = lookupswitch.number_of_pairs();
never@2027 550 (*jmpFct)(this, bci + lookupswitch.default_offset(), data); /* Default. */
duke@0 551 while(--npairs >= 0) {
never@2027 552 LookupswitchPair pair = lookupswitch.pair_at(npairs);
never@2027 553 (*jmpFct)(this, bci + pair.offset(), data);
duke@0 554 }
duke@0 555 break;
duke@0 556 }
duke@0 557 case Bytecodes::_jsr:
duke@0 558 assert(bcs->is_wide()==false, "sanity check");
duke@0 559 (*jmpFct)(this, bcs->dest(), data);
duke@0 560
duke@0 561
duke@0 562
duke@0 563 break;
duke@0 564 case Bytecodes::_jsr_w:
duke@0 565 (*jmpFct)(this, bcs->dest_w(), data);
duke@0 566 break;
duke@0 567 case Bytecodes::_wide:
duke@0 568 ShouldNotReachHere();
duke@0 569 return true;
duke@0 570 break;
duke@0 571 case Bytecodes::_athrow:
duke@0 572 case Bytecodes::_ireturn:
duke@0 573 case Bytecodes::_lreturn:
duke@0 574 case Bytecodes::_freturn:
duke@0 575 case Bytecodes::_dreturn:
duke@0 576 case Bytecodes::_areturn:
duke@0 577 case Bytecodes::_return:
duke@0 578 case Bytecodes::_ret:
duke@0 579 break;
duke@0 580 default:
duke@0 581 return true;
duke@0 582 }
duke@0 583 return false;
duke@0 584 }
duke@0 585
duke@0 586 /* Requires "pc" to be the head of a basic block; returns that basic
duke@0 587 block. */
duke@0 588 BasicBlock *GenerateOopMap::get_basic_block_at(int bci) const {
duke@0 589 BasicBlock* bb = get_basic_block_containing(bci);
duke@0 590 assert(bb->_bci == bci, "should have found BB");
duke@0 591 return bb;
duke@0 592 }
duke@0 593
duke@0 594 // Requires "pc" to be the start of an instruction; returns the basic
duke@0 595 // block containing that instruction. */
duke@0 596 BasicBlock *GenerateOopMap::get_basic_block_containing(int bci) const {
duke@0 597 BasicBlock *bbs = _basic_blocks;
duke@0 598 int lo = 0, hi = _bb_count - 1;
duke@0 599
duke@0 600 while (lo <= hi) {
duke@0 601 int m = (lo + hi) / 2;
duke@0 602 int mbci = bbs[m]._bci;
duke@0 603 int nbci;
duke@0 604
duke@0 605 if ( m == _bb_count-1) {
duke@0 606 assert( bci >= mbci && bci < method()->code_size(), "sanity check failed");
duke@0 607 return bbs+m;
duke@0 608 } else {
duke@0 609 nbci = bbs[m+1]._bci;
duke@0 610 }
duke@0 611
duke@0 612 if ( mbci <= bci && bci < nbci) {
duke@0 613 return bbs+m;
duke@0 614 } else if (mbci < bci) {
duke@0 615 lo = m + 1;
duke@0 616 } else {
duke@0 617 assert(mbci > bci, "sanity check");
duke@0 618 hi = m - 1;
duke@0 619 }
duke@0 620 }
duke@0 621
duke@0 622 fatal("should have found BB");
duke@0 623 return NULL;
duke@0 624 }
duke@0 625
duke@0 626 void GenerateOopMap::restore_state(BasicBlock *bb)
duke@0 627 {
duke@0 628 memcpy(_state, bb->_state, _state_len*sizeof(CellTypeState));
duke@0 629 _stack_top = bb->_stack_top;
duke@0 630 _monitor_top = bb->_monitor_top;
duke@0 631 }
duke@0 632
duke@0 633 int GenerateOopMap::next_bb_start_pc(BasicBlock *bb) {
duke@0 634 int bbNum = bb - _basic_blocks + 1;
duke@0 635 if (bbNum == _bb_count)
duke@0 636 return method()->code_size();
duke@0 637
duke@0 638 return _basic_blocks[bbNum]._bci;
duke@0 639 }
duke@0 640
duke@0 641 //
duke@0 642 // CellType handling methods
duke@0 643 //
duke@0 644
hseigel@4806 645 // Allocate memory and throw LinkageError if failure.
hseigel@4806 646 #define ALLOC_RESOURCE_ARRAY(var, type, count) \
hseigel@4806 647 var = NEW_RESOURCE_ARRAY_RETURN_NULL(type, count); \
hseigel@4806 648 if (var == NULL) { \
hseigel@4806 649 report_error("Cannot reserve enough memory to analyze this method"); \
hseigel@4806 650 return; \
hseigel@4806 651 }
hseigel@4806 652
hseigel@4806 653
duke@0 654 void GenerateOopMap::init_state() {
duke@0 655 _state_len = _max_locals + _max_stack + _max_monitors;
hseigel@4806 656 ALLOC_RESOURCE_ARRAY(_state, CellTypeState, _state_len);
duke@0 657 memset(_state, 0, _state_len * sizeof(CellTypeState));
hseigel@4806 658 int count = MAX3(_max_locals, _max_stack, _max_monitors) + 1/*for null terminator char */;
hseigel@4806 659 ALLOC_RESOURCE_ARRAY(_state_vec_buf, char, count);
duke@0 660 }
duke@0 661
duke@0 662 void GenerateOopMap::make_context_uninitialized() {
duke@0 663 CellTypeState* vs = vars();
duke@0 664
duke@0 665 for (int i = 0; i < _max_locals; i++)
duke@0 666 vs[i] = CellTypeState::uninit;
duke@0 667
duke@0 668 _stack_top = 0;
duke@0 669 _monitor_top = 0;
duke@0 670 }
duke@0 671
coleenp@2062 672 int GenerateOopMap::methodsig_to_effect(Symbol* signature, bool is_static, CellTypeState* effect) {
duke@0 673 ComputeEntryStack ces(signature);
duke@0 674 return ces.compute_for_parameters(is_static, effect);
duke@0 675 }
duke@0 676
duke@0 677 // Return result of merging cts1 and cts2.
duke@0 678 CellTypeState CellTypeState::merge(CellTypeState cts, int slot) const {
duke@0 679 CellTypeState result;
duke@0 680
duke@0 681 assert(!is_bottom() && !cts.is_bottom(),
duke@0 682 "merge of bottom values is handled elsewhere");
duke@0 683
duke@0 684 result._state = _state | cts._state;
duke@0 685
duke@0 686 // If the top bit is set, we don't need to do any more work.
duke@0 687 if (!result.is_info_top()) {
duke@0 688 assert((result.can_be_address() || result.can_be_reference()),
duke@0 689 "only addresses and references have non-top info");
duke@0 690
duke@0 691 if (!equal(cts)) {
duke@0 692 // The two values being merged are different. Raise to top.
duke@0 693 if (result.is_reference()) {
duke@0 694 result = CellTypeState::make_slot_ref(slot);
duke@0 695 } else {
duke@0 696 result._state |= info_conflict;
duke@0 697 }
duke@0 698 }
duke@0 699 }
duke@0 700 assert(result.is_valid_state(), "checking that CTS merge maintains legal state");
duke@0 701
duke@0 702 return result;
duke@0 703 }
duke@0 704
duke@0 705 // Merge the variable state for locals and stack from cts into bbts.
duke@0 706 bool GenerateOopMap::merge_local_state_vectors(CellTypeState* cts,
duke@0 707 CellTypeState* bbts) {
duke@0 708 int i;
duke@0 709 int len = _max_locals + _stack_top;
duke@0 710 bool change = false;
duke@0 711
duke@0 712 for (i = len - 1; i >= 0; i--) {
duke@0 713 CellTypeState v = cts[i].merge(bbts[i], i);
duke@0 714 change = change || !v.equal(bbts[i]);
duke@0 715 bbts[i] = v;
duke@0 716 }
duke@0 717
duke@0 718 return change;
duke@0 719 }
duke@0 720
duke@0 721 // Merge the monitor stack state from cts into bbts.
duke@0 722 bool GenerateOopMap::merge_monitor_state_vectors(CellTypeState* cts,
duke@0 723 CellTypeState* bbts) {
duke@0 724 bool change = false;
duke@0 725 if (_max_monitors > 0 && _monitor_top != bad_monitors) {
duke@0 726 // If there are no monitors in the program, or there has been
duke@0 727 // a monitor matching error before this point in the program,
duke@0 728 // then we do not merge in the monitor state.
duke@0 729
duke@0 730 int base = _max_locals + _max_stack;
duke@0 731 int len = base + _monitor_top;
duke@0 732 for (int i = len - 1; i >= base; i--) {
duke@0 733 CellTypeState v = cts[i].merge(bbts[i], i);
duke@0 734
duke@0 735 // Can we prove that, when there has been a change, it will already
duke@0 736 // have been detected at this point? That would make this equal
duke@0 737 // check here unnecessary.
duke@0 738 change = change || !v.equal(bbts[i]);
duke@0 739 bbts[i] = v;
duke@0 740 }
duke@0 741 }
duke@0 742
duke@0 743 return change;
duke@0 744 }
duke@0 745
duke@0 746 void GenerateOopMap::copy_state(CellTypeState *dst, CellTypeState *src) {
duke@0 747 int len = _max_locals + _stack_top;
duke@0 748 for (int i = 0; i < len; i++) {
duke@0 749 if (src[i].is_nonlock_reference()) {
duke@0 750 dst[i] = CellTypeState::make_slot_ref(i);
duke@0 751 } else {
duke@0 752 dst[i] = src[i];
duke@0 753 }
duke@0 754 }
duke@0 755 if (_max_monitors > 0 && _monitor_top != bad_monitors) {
duke@0 756 int base = _max_locals + _max_stack;
duke@0 757 len = base + _monitor_top;
duke@0 758 for (int i = base; i < len; i++) {
duke@0 759 dst[i] = src[i];
duke@0 760 }
duke@0 761 }
duke@0 762 }
duke@0 763
duke@0 764
duke@0 765 // Merge the states for the current block and the next. As long as a
duke@0 766 // block is reachable the locals and stack must be merged. If the
duke@0 767 // stack heights don't match then this is a verification error and
duke@0 768 // it's impossible to interpret the code. Simultaneously monitor
duke@0 769 // states are being check to see if they nest statically. If monitor
duke@0 770 // depths match up then their states are merged. Otherwise the
duke@0 771 // mismatch is simply recorded and interpretation continues since
duke@0 772 // monitor matching is purely informational and doesn't say anything
duke@0 773 // about the correctness of the code.
duke@0 774 void GenerateOopMap::merge_state_into_bb(BasicBlock *bb) {
morris@4338 775 guarantee(bb != NULL, "null basicblock");
duke@0 776 assert(bb->is_alive(), "merging state into a dead basicblock");
duke@0 777
duke@0 778 if (_stack_top == bb->_stack_top) {
duke@0 779 // always merge local state even if monitors don't match.
duke@0 780 if (merge_local_state_vectors(_state, bb->_state)) {
duke@0 781 bb->set_changed(true);
duke@0 782 }
duke@0 783 if (_monitor_top == bb->_monitor_top) {
duke@0 784 // monitors still match so continue merging monitor states.
duke@0 785 if (merge_monitor_state_vectors(_state, bb->_state)) {
duke@0 786 bb->set_changed(true);
duke@0 787 }
duke@0 788 } else {
duke@0 789 if (TraceMonitorMismatch) {
duke@0 790 report_monitor_mismatch("monitor stack height merge conflict");
duke@0 791 }
duke@0 792 // When the monitor stacks are not matched, we set _monitor_top to
duke@0 793 // bad_monitors. This signals that, from here on, the monitor stack cannot
duke@0 794 // be trusted. In particular, monitorexit bytecodes may throw
duke@0 795 // exceptions. We mark this block as changed so that the change
duke@0 796 // propagates properly.
duke@0 797 bb->_monitor_top = bad_monitors;
duke@0 798 bb->set_changed(true);
duke@0 799 _monitor_safe = false;
duke@0 800 }
duke@0 801 } else if (!bb->is_reachable()) {
duke@0 802 // First time we look at this BB
duke@0 803 copy_state(bb->_state, _state);
duke@0 804 bb->_stack_top = _stack_top;
duke@0 805 bb->_monitor_top = _monitor_top;
duke@0 806 bb->set_changed(true);
duke@0 807 } else {
duke@0 808 verify_error("stack height conflict: %d vs. %d", _stack_top, bb->_stack_top);
duke@0 809 }
duke@0 810 }
duke@0 811
duke@0 812 void GenerateOopMap::merge_state(GenerateOopMap *gom, int bci, int* data) {
duke@0 813 gom->merge_state_into_bb(gom->get_basic_block_at(bci));
duke@0 814 }
duke@0 815
duke@0 816 void GenerateOopMap::set_var(int localNo, CellTypeState cts) {
duke@0 817 assert(cts.is_reference() || cts.is_value() || cts.is_address(),
duke@0 818 "wrong celltypestate");
duke@0 819 if (localNo < 0 || localNo > _max_locals) {
duke@0 820 verify_error("variable write error: r%d", localNo);
duke@0 821 return;
duke@0 822 }
duke@0 823 vars()[localNo] = cts;
duke@0 824 }
duke@0 825
duke@0 826 CellTypeState GenerateOopMap::get_var(int localNo) {
jcoomes@1409 827 assert(localNo < _max_locals + _nof_refval_conflicts, "variable read error");
duke@0 828 if (localNo < 0 || localNo > _max_locals) {
duke@0 829 verify_error("variable read error: r%d", localNo);
duke@0 830 return valCTS; // just to pick something;
duke@0 831 }
duke@0 832 return vars()[localNo];
duke@0 833 }
duke@0 834
duke@0 835 CellTypeState GenerateOopMap::pop() {
duke@0 836 if ( _stack_top <= 0) {
duke@0 837 verify_error("stack underflow");
duke@0 838 return valCTS; // just to pick something
duke@0 839 }
duke@0 840 return stack()[--_stack_top];
duke@0 841 }
duke@0 842
duke@0 843 void GenerateOopMap::push(CellTypeState cts) {
duke@0 844 if ( _stack_top >= _max_stack) {
duke@0 845 verify_error("stack overflow");
duke@0 846 return;
duke@0 847 }
duke@0 848 stack()[_stack_top++] = cts;
duke@0 849 }
duke@0 850
duke@0 851 CellTypeState GenerateOopMap::monitor_pop() {
duke@0 852 assert(_monitor_top != bad_monitors, "monitor_pop called on error monitor stack");
duke@0 853 if (_monitor_top == 0) {
duke@0 854 // We have detected a pop of an empty monitor stack.
duke@0 855 _monitor_safe = false;
duke@0 856 _monitor_top = bad_monitors;
duke@0 857
duke@0 858 if (TraceMonitorMismatch) {
duke@0 859 report_monitor_mismatch("monitor stack underflow");
duke@0 860 }
duke@0 861 return CellTypeState::ref; // just to keep the analysis going.
duke@0 862 }
duke@0 863 return monitors()[--_monitor_top];
duke@0 864 }
duke@0 865
duke@0 866 void GenerateOopMap::monitor_push(CellTypeState cts) {
duke@0 867 assert(_monitor_top != bad_monitors, "monitor_push called on error monitor stack");
duke@0 868 if (_monitor_top >= _max_monitors) {
duke@0 869 // Some monitorenter is being executed more than once.
duke@0 870 // This means that the monitor stack cannot be simulated.
duke@0 871 _monitor_safe = false;
duke@0 872 _monitor_top = bad_monitors;
duke@0 873
duke@0 874 if (TraceMonitorMismatch) {
duke@0 875 report_monitor_mismatch("monitor stack overflow");
duke@0 876 }
duke@0 877 return;
duke@0 878 }
duke@0 879 monitors()[_monitor_top++] = cts;
duke@0 880 }
duke@0 881
duke@0 882 //
duke@0 883 // Interpretation handling methods
duke@0 884 //
duke@0 885
duke@0 886 void GenerateOopMap::do_interpretation()
duke@0 887 {
duke@0 888 // "i" is just for debugging, so we can detect cases where this loop is
duke@0 889 // iterated more than once.
duke@0 890 int i = 0;
duke@0 891 do {
duke@0 892 #ifndef PRODUCT
duke@0 893 if (TraceNewOopMapGeneration) {
duke@0 894 tty->print("\n\nIteration #%d of do_interpretation loop, method:\n", i);
duke@0 895 method()->print_name(tty);
duke@0 896 tty->print("\n\n");
duke@0 897 }
duke@0 898 #endif
duke@0 899 _conflict = false;
duke@0 900 _monitor_safe = true;
duke@0 901 // init_state is now called from init_basic_blocks. The length of a
duke@0 902 // state vector cannot be determined until we have made a pass through
duke@0 903 // the bytecodes counting the possible monitor entries.
duke@0 904 if (!_got_error) init_basic_blocks();
duke@0 905 if (!_got_error) setup_method_entry_state();
duke@0 906 if (!_got_error) interp_all();
duke@0 907 if (!_got_error) rewrite_refval_conflicts();
duke@0 908 i++;
duke@0 909 } while (_conflict && !_got_error);
duke@0 910 }
duke@0 911
duke@0 912 void GenerateOopMap::init_basic_blocks() {
duke@0 913 // Note: Could consider reserving only the needed space for each BB's state
duke@0 914 // (entry stack may not be of maximal height for every basic block).
duke@0 915 // But cumbersome since we don't know the stack heights yet. (Nor the
duke@0 916 // monitor stack heights...)
duke@0 917
hseigel@4806 918 ALLOC_RESOURCE_ARRAY(_basic_blocks, BasicBlock, _bb_count);
duke@0 919
duke@0 920 // Make a pass through the bytecodes. Count the number of monitorenters.
duke@0 921 // This can be used an upper bound on the monitor stack depth in programs
duke@0 922 // which obey stack discipline with their monitor usage. Initialize the
duke@0 923 // known information about basic blocks.
duke@0 924 BytecodeStream j(_method);
duke@0 925 Bytecodes::Code bytecode;
duke@0 926
duke@0 927 int bbNo = 0;
duke@0 928 int monitor_count = 0;
duke@0 929 int prev_bci = -1;
duke@0 930 while( (bytecode = j.next()) >= 0) {
duke@0 931 if (j.code() == Bytecodes::_monitorenter) {
duke@0 932 monitor_count++;
duke@0 933 }
duke@0 934
duke@0 935 int bci = j.bci();
duke@0 936 if (is_bb_header(bci)) {
duke@0 937 // Initialize the basicblock structure
duke@0 938 BasicBlock *bb = _basic_blocks + bbNo;
duke@0 939 bb->_bci = bci;
duke@0 940 bb->_max_locals = _max_locals;
duke@0 941 bb->_max_stack = _max_stack;
duke@0 942 bb->set_changed(false);
duke@0 943 bb->_stack_top = BasicBlock::_dead_basic_block; // Initialize all basicblocks are dead.
duke@0 944 bb->_monitor_top = bad_monitors;
duke@0 945
duke@0 946 if (bbNo > 0) {
duke@0 947 _basic_blocks[bbNo - 1]._end_bci = prev_bci;
duke@0 948 }
duke@0 949
duke@0 950 bbNo++;
duke@0 951 }
duke@0 952 // Remember prevous bci.
duke@0 953 prev_bci = bci;
duke@0 954 }
duke@0 955 // Set
duke@0 956 _basic_blocks[bbNo-1]._end_bci = prev_bci;
duke@0 957
duke@0 958
ysr@342 959 // Check that the correct number of basicblocks was found
ysr@342 960 if (bbNo !=_bb_count) {
ysr@342 961 if (bbNo < _bb_count) {
ysr@342 962 verify_error("jump into the middle of instruction?");
ysr@342 963 return;
ysr@342 964 } else {
ysr@342 965 verify_error("extra basic blocks - should not happen?");
ysr@342 966 return;
ysr@342 967 }
ysr@342 968 }
ysr@342 969
duke@0 970 _max_monitors = monitor_count;
duke@0 971
duke@0 972 // Now that we have a bound on the depth of the monitor stack, we can
duke@0 973 // initialize the CellTypeState-related information.
duke@0 974 init_state();
duke@0 975
kamg@2625 976 // We allocate space for all state-vectors for all basicblocks in one huge
kamg@2625 977 // chunk. Then in the next part of the code, we set a pointer in each
kamg@2625 978 // _basic_block that points to each piece.
kamg@2625 979
kamg@2625 980 // The product of bbNo and _state_len can get large if there are lots of
kamg@2625 981 // basic blocks and stack/locals/monitors. Need to check to make sure
kamg@2625 982 // we don't overflow the capacity of a pointer.
kamg@2625 983 if ((unsigned)bbNo > UINTPTR_MAX / sizeof(CellTypeState) / _state_len) {
kamg@2625 984 report_error("The amount of memory required to analyze this method "
kamg@2625 985 "exceeds addressable range");
kamg@2625 986 return;
kamg@2625 987 }
kamg@2625 988
hseigel@4806 989 CellTypeState *basicBlockState;
hseigel@4806 990 ALLOC_RESOURCE_ARRAY(basicBlockState, CellTypeState, bbNo * _state_len);
duke@0 991 memset(basicBlockState, 0, bbNo * _state_len * sizeof(CellTypeState));
duke@0 992
duke@0 993 // Make a pass over the basicblocks and assign their state vectors.
duke@0 994 for (int blockNum=0; blockNum < bbNo; blockNum++) {
duke@0 995 BasicBlock *bb = _basic_blocks + blockNum;
duke@0 996 bb->_state = basicBlockState + blockNum * _state_len;
duke@0 997
duke@0 998 #ifdef ASSERT
duke@0 999 if (blockNum + 1 < bbNo) {
duke@0 1000 address bcp = _method->bcp_from(bb->_end_bci);
never@2027 1001 int bc_len = Bytecodes::java_length_at(_method(), bcp);
duke@0 1002 assert(bb->_end_bci + bc_len == bb[1]._bci, "unmatched bci info in basicblock");
duke@0 1003 }
duke@0 1004 #endif
duke@0 1005 }
duke@0 1006 #ifdef ASSERT
duke@0 1007 { BasicBlock *bb = &_basic_blocks[bbNo-1];
duke@0 1008 address bcp = _method->bcp_from(bb->_end_bci);
never@2027 1009 int bc_len = Bytecodes::java_length_at(_method(), bcp);
duke@0 1010 assert(bb->_end_bci + bc_len == _method->code_size(), "wrong end bci");
duke@0 1011 }
duke@0 1012 #endif
duke@0 1013
duke@0 1014 // Mark all alive blocks
duke@0 1015 mark_reachable_code();
duke@0 1016 }
duke@0 1017
duke@0 1018 void GenerateOopMap::setup_method_entry_state() {
duke@0 1019
duke@0 1020 // Initialize all locals to 'uninit' and set stack-height to 0
duke@0 1021 make_context_uninitialized();
duke@0 1022
duke@0 1023 // Initialize CellState type of arguments
duke@0 1024 methodsig_to_effect(method()->signature(), method()->is_static(), vars());
duke@0 1025
duke@0 1026 // If some references must be pre-assigned to null, then set that up
duke@0 1027 initialize_vars();
duke@0 1028
duke@0 1029 // This is the start state
duke@0 1030 merge_state_into_bb(&_basic_blocks[0]);
duke@0 1031
duke@0 1032 assert(_basic_blocks[0].changed(), "we are not getting off the ground");
duke@0 1033 }
duke@0 1034
duke@0 1035 // The instruction at bci is changing size by "delta". Update the basic blocks.
duke@0 1036 void GenerateOopMap::update_basic_blocks(int bci, int delta,
duke@0 1037 int new_method_size) {
duke@0 1038 assert(new_method_size >= method()->code_size() + delta,
duke@0 1039 "new method size is too small");
duke@0 1040
ysr@342 1041 BitMap::bm_word_t* new_bb_hdr_bits =
ysr@342 1042 NEW_RESOURCE_ARRAY(BitMap::bm_word_t,
ysr@342 1043 BitMap::word_align_up(new_method_size));
ysr@342 1044 _bb_hdr_bits.set_map(new_bb_hdr_bits);
ysr@342 1045 _bb_hdr_bits.set_size(new_method_size);
ysr@342 1046 _bb_hdr_bits.clear();
duke@0 1047
duke@0 1048
duke@0 1049 for(int k = 0; k < _bb_count; k++) {
duke@0 1050 if (_basic_blocks[k]._bci > bci) {
duke@0 1051 _basic_blocks[k]._bci += delta;
duke@0 1052 _basic_blocks[k]._end_bci += delta;
duke@0 1053 }
ysr@342 1054 _bb_hdr_bits.at_put(_basic_blocks[k]._bci, true);
duke@0 1055 }
duke@0 1056 }
duke@0 1057
duke@0 1058 //
duke@0 1059 // Initvars handling
duke@0 1060 //
duke@0 1061
duke@0 1062 void GenerateOopMap::initialize_vars() {
duke@0 1063 for (int k = 0; k < _init_vars->length(); k++)
duke@0 1064 _state[_init_vars->at(k)] = CellTypeState::make_slot_ref(k);
duke@0 1065 }
duke@0 1066
duke@0 1067 void GenerateOopMap::add_to_ref_init_set(int localNo) {
duke@0 1068
duke@0 1069 if (TraceNewOopMapGeneration)
duke@0 1070 tty->print_cr("Added init vars: %d", localNo);
duke@0 1071
duke@0 1072 // Is it already in the set?
duke@0 1073 if (_init_vars->contains(localNo) )
duke@0 1074 return;
duke@0 1075
duke@0 1076 _init_vars->append(localNo);
duke@0 1077 }
duke@0 1078
duke@0 1079 //
duke@0 1080 // Interpreration code
duke@0 1081 //
duke@0 1082
duke@0 1083 void GenerateOopMap::interp_all() {
duke@0 1084 bool change = true;
duke@0 1085
duke@0 1086 while (change && !_got_error) {
duke@0 1087 change = false;
duke@0 1088 for (int i = 0; i < _bb_count && !_got_error; i++) {
duke@0 1089 BasicBlock *bb = &_basic_blocks[i];
duke@0 1090 if (bb->changed()) {
duke@0 1091 if (_got_error) return;
duke@0 1092 change = true;
duke@0 1093 bb->set_changed(false);
duke@0 1094 interp_bb(bb);
duke@0 1095 }
duke@0 1096 }
duke@0 1097 }
duke@0 1098 }
duke@0 1099
duke@0 1100 void GenerateOopMap::interp_bb(BasicBlock *bb) {
duke@0 1101
duke@0 1102 // We do not want to do anything in case the basic-block has not been initialized. This
duke@0 1103 // will happen in the case where there is dead-code hang around in a method.
duke@0 1104 assert(bb->is_reachable(), "should be reachable or deadcode exist");
duke@0 1105 restore_state(bb);
duke@0 1106
duke@0 1107 BytecodeStream itr(_method);
duke@0 1108
duke@0 1109 // Set iterator interval to be the current basicblock
duke@0 1110 int lim_bci = next_bb_start_pc(bb);
duke@0 1111 itr.set_interval(bb->_bci, lim_bci);
duke@0 1112 assert(lim_bci != bb->_bci, "must be at least one instruction in a basicblock");
duke@0 1113 itr.next(); // read first instruction
duke@0 1114
duke@0 1115 // Iterates through all bytecodes except the last in a basic block.
duke@0 1116 // We handle the last one special, since there is controlflow change.
duke@0 1117 while(itr.next_bci() < lim_bci && !_got_error) {
duke@0 1118 if (_has_exceptions || _monitor_top != 0) {
duke@0 1119 // We do not need to interpret the results of exceptional
duke@0 1120 // continuation from this instruction when the method has no
duke@0 1121 // exception handlers and the monitor stack is currently
duke@0 1122 // empty.
duke@0 1123 do_exception_edge(&itr);
duke@0 1124 }
duke@0 1125 interp1(&itr);
duke@0 1126 itr.next();
duke@0 1127 }
duke@0 1128
duke@0 1129 // Handle last instruction.
duke@0 1130 if (!_got_error) {
duke@0 1131 assert(itr.next_bci() == lim_bci, "must point to end");
duke@0 1132 if (_has_exceptions || _monitor_top != 0) {
duke@0 1133 do_exception_edge(&itr);
duke@0 1134 }
duke@0 1135 interp1(&itr);
duke@0 1136
duke@0 1137 bool fall_through = jump_targets_do(&itr, GenerateOopMap::merge_state, NULL);
duke@0 1138 if (_got_error) return;
duke@0 1139
duke@0 1140 if (itr.code() == Bytecodes::_ret) {
duke@0 1141 assert(!fall_through, "cannot be set if ret instruction");
duke@0 1142 // Automatically handles 'wide' ret indicies
duke@0 1143 ret_jump_targets_do(&itr, GenerateOopMap::merge_state, itr.get_index(), NULL);
duke@0 1144 } else if (fall_through) {
duke@0 1145 // Hit end of BB, but the instr. was a fall-through instruction,
duke@0 1146 // so perform transition as if the BB ended in a "jump".
duke@0 1147 if (lim_bci != bb[1]._bci) {
duke@0 1148 verify_error("bytecodes fell through last instruction");
duke@0 1149 return;
duke@0 1150 }
duke@0 1151 merge_state_into_bb(bb + 1);
duke@0 1152 }
duke@0 1153 }
duke@0 1154 }
duke@0 1155
duke@0 1156 void GenerateOopMap::do_exception_edge(BytecodeStream* itr) {
duke@0 1157 // Only check exception edge, if bytecode can trap
duke@0 1158 if (!Bytecodes::can_trap(itr->code())) return;
duke@0 1159 switch (itr->code()) {
duke@0 1160 case Bytecodes::_aload_0:
duke@0 1161 // These bytecodes can trap for rewriting. We need to assume that
duke@0 1162 // they do not throw exceptions to make the monitor analysis work.
duke@0 1163 return;
duke@0 1164
duke@0 1165 case Bytecodes::_ireturn:
duke@0 1166 case Bytecodes::_lreturn:
duke@0 1167 case Bytecodes::_freturn:
duke@0 1168 case Bytecodes::_dreturn:
duke@0 1169 case Bytecodes::_areturn:
duke@0 1170 case Bytecodes::_return:
duke@0 1171 // If the monitor stack height is not zero when we leave the method,
duke@0 1172 // then we are either exiting with a non-empty stack or we have
duke@0 1173 // found monitor trouble earlier in our analysis. In either case,
duke@0 1174 // assume an exception could be taken here.
duke@0 1175 if (_monitor_top == 0) {
duke@0 1176 return;
duke@0 1177 }
duke@0 1178 break;
duke@0 1179
duke@0 1180 case Bytecodes::_monitorexit:
duke@0 1181 // If the monitor stack height is bad_monitors, then we have detected a
duke@0 1182 // monitor matching problem earlier in the analysis. If the
duke@0 1183 // monitor stack height is 0, we are about to pop a monitor
duke@0 1184 // off of an empty stack. In either case, the bytecode
duke@0 1185 // could throw an exception.
duke@0 1186 if (_monitor_top != bad_monitors && _monitor_top != 0) {
duke@0 1187 return;
duke@0 1188 }
duke@0 1189 break;
duke@0 1190 }
duke@0 1191
duke@0 1192 if (_has_exceptions) {
duke@0 1193 int bci = itr->bci();
jiangli@3482 1194 ExceptionTable exct(method());
jiangli@3482 1195 for(int i = 0; i< exct.length(); i++) {
jiangli@3482 1196 int start_pc = exct.start_pc(i);
jiangli@3482 1197 int end_pc = exct.end_pc(i);
jiangli@3482 1198 int handler_pc = exct.handler_pc(i);
jiangli@3482 1199 int catch_type = exct.catch_type_index(i);
duke@0 1200
duke@0 1201 if (start_pc <= bci && bci < end_pc) {
duke@0 1202 BasicBlock *excBB = get_basic_block_at(handler_pc);
morris@4338 1203 guarantee(excBB != NULL, "no basic block for exception");
duke@0 1204 CellTypeState *excStk = excBB->stack();
duke@0 1205 CellTypeState *cOpStck = stack();
duke@0 1206 CellTypeState cOpStck_0 = cOpStck[0];
duke@0 1207 int cOpStackTop = _stack_top;
duke@0 1208
duke@0 1209 // Exception stacks are always the same.
duke@0 1210 assert(method()->max_stack() > 0, "sanity check");
duke@0 1211
duke@0 1212 // We remembered the size and first element of "cOpStck"
duke@0 1213 // above; now we temporarily set them to the appropriate
duke@0 1214 // values for an exception handler. */
duke@0 1215 cOpStck[0] = CellTypeState::make_slot_ref(_max_locals);
duke@0 1216 _stack_top = 1;
duke@0 1217
duke@0 1218 merge_state_into_bb(excBB);
duke@0 1219
duke@0 1220 // Now undo the temporary change.
duke@0 1221 cOpStck[0] = cOpStck_0;
duke@0 1222 _stack_top = cOpStackTop;
duke@0 1223
duke@0 1224 // If this is a "catch all" handler, then we do not need to
duke@0 1225 // consider any additional handlers.
duke@0 1226 if (catch_type == 0) {
duke@0 1227 return;
duke@0 1228 }
duke@0 1229 }
duke@0 1230 }
duke@0 1231 }
duke@0 1232
duke@0 1233 // It is possible that none of the exception handlers would have caught
duke@0 1234 // the exception. In this case, we will exit the method. We must
duke@0 1235 // ensure that the monitor stack is empty in this case.
duke@0 1236 if (_monitor_top == 0) {
duke@0 1237 return;
duke@0 1238 }
duke@0 1239
duke@0 1240 // We pessimistically assume that this exception can escape the
duke@0 1241 // method. (It is possible that it will always be caught, but
duke@0 1242 // we don't care to analyse the types of the catch clauses.)
duke@0 1243
duke@0 1244 // We don't set _monitor_top to bad_monitors because there are no successors
duke@0 1245 // to this exceptional exit.
duke@0 1246
duke@0 1247 if (TraceMonitorMismatch && _monitor_safe) {
duke@0 1248 // We check _monitor_safe so that we only report the first mismatched
duke@0 1249 // exceptional exit.
duke@0 1250 report_monitor_mismatch("non-empty monitor stack at exceptional exit");
duke@0 1251 }
duke@0 1252 _monitor_safe = false;
duke@0 1253
duke@0 1254 }
duke@0 1255
duke@0 1256 void GenerateOopMap::report_monitor_mismatch(const char *msg) {
duke@0 1257 #ifndef PRODUCT
duke@0 1258 tty->print(" Monitor mismatch in method ");
duke@0 1259 method()->print_short_name(tty);
duke@0 1260 tty->print_cr(": %s", msg);
duke@0 1261 #endif
duke@0 1262 }
duke@0 1263
duke@0 1264 void GenerateOopMap::print_states(outputStream *os,
duke@0 1265 CellTypeState* vec, int num) {
duke@0 1266 for (int i = 0; i < num; i++) {
duke@0 1267 vec[i].print(tty);
duke@0 1268 }
duke@0 1269 }
duke@0 1270
duke@0 1271 // Print the state values at the current bytecode.
duke@0 1272 void GenerateOopMap::print_current_state(outputStream *os,
duke@0 1273 BytecodeStream *currentBC,
duke@0 1274 bool detailed) {
duke@0 1275
duke@0 1276 if (detailed) {
duke@0 1277 os->print(" %4d vars = ", currentBC->bci());
duke@0 1278 print_states(os, vars(), _max_locals);
duke@0 1279 os->print(" %s", Bytecodes::name(currentBC->code()));
duke@0 1280 switch(currentBC->code()) {
duke@0 1281 case Bytecodes::_invokevirtual:
duke@0 1282 case Bytecodes::_invokespecial:
duke@0 1283 case Bytecodes::_invokestatic:
jrose@726 1284 case Bytecodes::_invokedynamic:
duke@0 1285 case Bytecodes::_invokeinterface:
jrose@1830 1286 int idx = currentBC->has_index_u4() ? currentBC->get_index_u4() : currentBC->get_index_u2_cpcache();
coleenp@3602 1287 ConstantPool* cp = method()->constants();
duke@0 1288 int nameAndTypeIdx = cp->name_and_type_ref_index_at(idx);
duke@0 1289 int signatureIdx = cp->signature_ref_index_at(nameAndTypeIdx);
coleenp@2062 1290 Symbol* signature = cp->symbol_at(signatureIdx);
duke@0 1291 os->print("%s", signature->as_C_string());
duke@0 1292 }
duke@0 1293 os->cr();
duke@0 1294 os->print(" stack = ");
duke@0 1295 print_states(os, stack(), _stack_top);
duke@0 1296 os->cr();
duke@0 1297 if (_monitor_top != bad_monitors) {
duke@0 1298 os->print(" monitors = ");
duke@0 1299 print_states(os, monitors(), _monitor_top);
duke@0 1300 } else {
duke@0 1301 os->print(" [bad monitor stack]");
duke@0 1302 }
duke@0 1303 os->cr();
duke@0 1304 } else {
duke@0 1305 os->print(" %4d vars = '%s' ", currentBC->bci(), state_vec_to_string(vars(), _max_locals));
duke@0 1306 os->print(" stack = '%s' ", state_vec_to_string(stack(), _stack_top));
duke@0 1307 if (_monitor_top != bad_monitors) {
duke@0 1308 os->print(" monitors = '%s' \t%s", state_vec_to_string(monitors(), _monitor_top), Bytecodes::name(currentBC->code()));
duke@0 1309 } else {
duke@0 1310 os->print(" [bad monitor stack]");
duke@0 1311 }
duke@0 1312 switch(currentBC->code()) {
duke@0 1313 case Bytecodes::_invokevirtual:
duke@0 1314 case Bytecodes::_invokespecial:
duke@0 1315 case Bytecodes::_invokestatic:
jrose@726 1316 case Bytecodes::_invokedynamic:
duke@0 1317 case Bytecodes::_invokeinterface:
jrose@1830 1318 int idx = currentBC->has_index_u4() ? currentBC->get_index_u4() : currentBC->get_index_u2_cpcache();
coleenp@3602 1319 ConstantPool* cp = method()->constants();
duke@0 1320 int nameAndTypeIdx = cp->name_and_type_ref_index_at(idx);
duke@0 1321 int signatureIdx = cp->signature_ref_index_at(nameAndTypeIdx);
coleenp@2062 1322 Symbol* signature = cp->symbol_at(signatureIdx);
duke@0 1323 os->print("%s", signature->as_C_string());
duke@0 1324 }
duke@0 1325 os->cr();
duke@0 1326 }
duke@0 1327 }
duke@0 1328
duke@0 1329 // Sets the current state to be the state after executing the
duke@0 1330 // current instruction, starting in the current state.
duke@0 1331 void GenerateOopMap::interp1(BytecodeStream *itr) {
duke@0 1332 if (TraceNewOopMapGeneration) {
duke@0 1333 print_current_state(tty, itr, TraceNewOopMapGenerationDetailed);
duke@0 1334 }
duke@0 1335
duke@0 1336 // Should we report the results? Result is reported *before* the instruction at the current bci is executed.
duke@0 1337 // However, not for calls. For calls we do not want to include the arguments, so we postpone the reporting until
duke@0 1338 // they have been popped (in method ppl).
duke@0 1339 if (_report_result == true) {
duke@0 1340 switch(itr->code()) {
duke@0 1341 case Bytecodes::_invokevirtual:
duke@0 1342 case Bytecodes::_invokespecial:
duke@0 1343 case Bytecodes::_invokestatic:
jrose@726 1344 case Bytecodes::_invokedynamic:
duke@0 1345 case Bytecodes::_invokeinterface:
duke@0 1346 _itr_send = itr;
duke@0 1347 _report_result_for_send = true;
duke@0 1348 break;
duke@0 1349 default:
duke@0 1350 fill_stackmap_for_opcodes(itr, vars(), stack(), _stack_top);
duke@0 1351 break;
duke@0 1352 }
duke@0 1353 }
duke@0 1354
duke@0 1355 // abstract interpretation of current opcode
duke@0 1356 switch(itr->code()) {
duke@0 1357 case Bytecodes::_nop: break;
duke@0 1358 case Bytecodes::_goto: break;
duke@0 1359 case Bytecodes::_goto_w: break;
duke@0 1360 case Bytecodes::_iinc: break;
duke@0 1361 case Bytecodes::_return: do_return_monitor_check();
duke@0 1362 break;
duke@0 1363
duke@0 1364 case Bytecodes::_aconst_null:
duke@0 1365 case Bytecodes::_new: ppush1(CellTypeState::make_line_ref(itr->bci()));
duke@0 1366 break;
duke@0 1367
duke@0 1368 case Bytecodes::_iconst_m1:
duke@0 1369 case Bytecodes::_iconst_0:
duke@0 1370 case Bytecodes::_iconst_1:
duke@0 1371 case Bytecodes::_iconst_2:
duke@0 1372 case Bytecodes::_iconst_3:
duke@0 1373 case Bytecodes::_iconst_4:
duke@0 1374 case Bytecodes::_iconst_5:
duke@0 1375 case Bytecodes::_fconst_0:
duke@0 1376 case Bytecodes::_fconst_1:
duke@0 1377 case Bytecodes::_fconst_2:
duke@0 1378 case Bytecodes::_bipush:
duke@0 1379 case Bytecodes::_sipush: ppush1(valCTS); break;
duke@0 1380
duke@0 1381 case Bytecodes::_lconst_0:
duke@0 1382 case Bytecodes::_lconst_1:
duke@0 1383 case Bytecodes::_dconst_0:
duke@0 1384 case Bytecodes::_dconst_1: ppush(vvCTS); break;
duke@0 1385
duke@0 1386 case Bytecodes::_ldc2_w: ppush(vvCTS); break;
duke@0 1387
jrose@1830 1388 case Bytecodes::_ldc: // fall through:
jrose@1830 1389 case Bytecodes::_ldc_w: do_ldc(itr->bci()); break;
duke@0 1390
duke@0 1391 case Bytecodes::_iload:
duke@0 1392 case Bytecodes::_fload: ppload(vCTS, itr->get_index()); break;
duke@0 1393
duke@0 1394 case Bytecodes::_lload:
duke@0 1395 case Bytecodes::_dload: ppload(vvCTS,itr->get_index()); break;
duke@0 1396
duke@0 1397 case Bytecodes::_aload: ppload(rCTS, itr->get_index()); break;
duke@0 1398
duke@0 1399 case Bytecodes::_iload_0:
duke@0 1400 case Bytecodes::_fload_0: ppload(vCTS, 0); break;
duke@0 1401 case Bytecodes::_iload_1:
duke@0 1402 case Bytecodes::_fload_1: ppload(vCTS, 1); break;
duke@0 1403 case Bytecodes::_iload_2:
duke@0 1404 case Bytecodes::_fload_2: ppload(vCTS, 2); break;
duke@0 1405 case Bytecodes::_iload_3:
duke@0 1406 case Bytecodes::_fload_3: ppload(vCTS, 3); break;
duke@0 1407
duke@0 1408 case Bytecodes::_lload_0:
duke@0 1409 case Bytecodes::_dload_0: ppload(vvCTS, 0); break;
duke@0 1410 case Bytecodes::_lload_1:
duke@0 1411 case Bytecodes::_dload_1: ppload(vvCTS, 1); break;
duke@0 1412 case Bytecodes::_lload_2:
duke@0 1413 case Bytecodes::_dload_2: ppload(vvCTS, 2); break;
duke@0 1414 case Bytecodes::_lload_3:
duke@0 1415 case Bytecodes::_dload_3: ppload(vvCTS, 3); break;
duke@0 1416
duke@0 1417 case Bytecodes::_aload_0: ppload(rCTS, 0); break;
duke@0 1418 case Bytecodes::_aload_1: ppload(rCTS, 1); break;
duke@0 1419 case Bytecodes::_aload_2: ppload(rCTS, 2); break;
duke@0 1420 case Bytecodes::_aload_3: ppload(rCTS, 3); break;
duke@0 1421
duke@0 1422 case Bytecodes::_iaload:
duke@0 1423 case Bytecodes::_faload:
duke@0 1424 case Bytecodes::_baload:
duke@0 1425 case Bytecodes::_caload:
duke@0 1426 case Bytecodes::_saload: pp(vrCTS, vCTS); break;
duke@0 1427
duke@0 1428 case Bytecodes::_laload: pp(vrCTS, vvCTS); break;
duke@0 1429 case Bytecodes::_daload: pp(vrCTS, vvCTS); break;
duke@0 1430
duke@0 1431 case Bytecodes::_aaload: pp_new_ref(vrCTS, itr->bci()); break;
duke@0 1432
duke@0 1433 case Bytecodes::_istore:
duke@0 1434 case Bytecodes::_fstore: ppstore(vCTS, itr->get_index()); break;
duke@0 1435
duke@0 1436 case Bytecodes::_lstore:
duke@0 1437 case Bytecodes::_dstore: ppstore(vvCTS, itr->get_index()); break;
duke@0 1438
duke@0 1439 case Bytecodes::_astore: do_astore(itr->get_index()); break;
duke@0 1440
duke@0 1441 case Bytecodes::_istore_0:
duke@0 1442 case Bytecodes::_fstore_0: ppstore(vCTS, 0); break;
duke@0 1443 case Bytecodes::_istore_1:
duke@0 1444 case Bytecodes::_fstore_1: ppstore(vCTS, 1); break;
duke@0 1445 case Bytecodes::_istore_2:
duke@0 1446 case Bytecodes::_fstore_2: ppstore(vCTS, 2); break;
duke@0 1447 case Bytecodes::_istore_3:
duke@0 1448 case Bytecodes::_fstore_3: ppstore(vCTS, 3); break;
duke@0 1449
duke@0 1450 case Bytecodes::_lstore_0:
duke@0 1451 case Bytecodes::_dstore_0: ppstore(vvCTS, 0); break;
duke@0 1452 case Bytecodes::_lstore_1:
duke@0 1453 case Bytecodes::_dstore_1: ppstore(vvCTS, 1); break;
duke@0 1454 case Bytecodes::_lstore_2:
duke@0 1455 case Bytecodes::_dstore_2: ppstore(vvCTS, 2); break;
duke@0 1456 case Bytecodes::_lstore_3:
duke@0 1457 case Bytecodes::_dstore_3: ppstore(vvCTS, 3); break;
duke@0 1458
duke@0 1459 case Bytecodes::_astore_0: do_astore(0); break;
duke@0 1460 case Bytecodes::_astore_1: do_astore(1); break;
duke@0 1461 case Bytecodes::_astore_2: do_astore(2); break;
duke@0 1462 case Bytecodes::_astore_3: do_astore(3); break;
duke@0 1463
duke@0 1464 case Bytecodes::_iastore:
duke@0 1465 case Bytecodes::_fastore:
duke@0 1466 case Bytecodes::_bastore:
duke@0 1467 case Bytecodes::_castore:
duke@0 1468 case Bytecodes::_sastore: ppop(vvrCTS); break;
duke@0 1469 case Bytecodes::_lastore:
duke@0 1470 case Bytecodes::_dastore: ppop(vvvrCTS); break;
duke@0 1471 case Bytecodes::_aastore: ppop(rvrCTS); break;
duke@0 1472
duke@0 1473 case Bytecodes::_pop: ppop_any(1); break;
duke@0 1474 case Bytecodes::_pop2: ppop_any(2); break;
duke@0 1475
duke@0 1476 case Bytecodes::_dup: ppdupswap(1, "11"); break;
duke@0 1477 case Bytecodes::_dup_x1: ppdupswap(2, "121"); break;
duke@0 1478 case Bytecodes::_dup_x2: ppdupswap(3, "1321"); break;
duke@0 1479 case Bytecodes::_dup2: ppdupswap(2, "2121"); break;
duke@0 1480 case Bytecodes::_dup2_x1: ppdupswap(3, "21321"); break;
duke@0 1481 case Bytecodes::_dup2_x2: ppdupswap(4, "214321"); break;
duke@0 1482 case Bytecodes::_swap: ppdupswap(2, "12"); break;
duke@0 1483
duke@0 1484 case Bytecodes::_iadd:
duke@0 1485 case Bytecodes::_fadd:
duke@0 1486 case Bytecodes::_isub:
duke@0 1487 case Bytecodes::_fsub:
duke@0 1488 case Bytecodes::_imul:
duke@0 1489 case Bytecodes::_fmul:
duke@0 1490 case Bytecodes::_idiv:
duke@0 1491 case Bytecodes::_fdiv:
duke@0 1492 case Bytecodes::_irem:
duke@0 1493 case Bytecodes::_frem:
duke@0 1494 case Bytecodes::_ishl:
duke@0 1495 case Bytecodes::_ishr:
duke@0 1496 case Bytecodes::_iushr:
duke@0 1497 case Bytecodes::_iand:
duke@0 1498 case Bytecodes::_ior:
duke@0 1499 case Bytecodes::_ixor:
duke@0 1500 case Bytecodes::_l2f:
duke@0 1501 case Bytecodes::_l2i:
duke@0 1502 case Bytecodes::_d2f:
duke@0 1503 case Bytecodes::_d2i:
duke@0 1504 case Bytecodes::_fcmpl:
duke@0 1505 case Bytecodes::_fcmpg: pp(vvCTS, vCTS); break;
duke@0 1506
duke@0 1507 case Bytecodes::_ladd:
duke@0 1508 case Bytecodes::_dadd:
duke@0 1509 case Bytecodes::_lsub:
duke@0 1510 case Bytecodes::_dsub:
duke@0 1511 case Bytecodes::_lmul:
duke@0 1512 case Bytecodes::_dmul:
duke@0 1513 case Bytecodes::_ldiv:
duke@0 1514 case Bytecodes::_ddiv:
duke@0 1515 case Bytecodes::_lrem:
duke@0 1516 case Bytecodes::_drem:
duke@0 1517 case Bytecodes::_land:
duke@0 1518 case Bytecodes::_lor:
duke@0 1519 case Bytecodes::_lxor: pp(vvvvCTS, vvCTS); break;
duke@0 1520
duke@0 1521 case Bytecodes::_ineg:
duke@0 1522 case Bytecodes::_fneg:
duke@0 1523 case Bytecodes::_i2f:
duke@0 1524 case Bytecodes::_f2i:
duke@0 1525 case Bytecodes::_i2c:
duke@0 1526 case Bytecodes::_i2s:
duke@0 1527 case Bytecodes::_i2b: pp(vCTS, vCTS); break;
duke@0 1528
duke@0 1529 case Bytecodes::_lneg:
duke@0 1530 case Bytecodes::_dneg:
duke@0 1531 case Bytecodes::_l2d:
duke@0 1532 case Bytecodes::_d2l: pp(vvCTS, vvCTS); break;
duke@0 1533
duke@0 1534 case Bytecodes::_lshl:
duke@0 1535 case Bytecodes::_lshr:
duke@0 1536 case Bytecodes::_lushr: pp(vvvCTS, vvCTS); break;
duke@0 1537
duke@0 1538 case Bytecodes::_i2l:
duke@0 1539 case Bytecodes::_i2d:
duke@0 1540 case Bytecodes::_f2l:
duke@0 1541 case Bytecodes::_f2d: pp(vCTS, vvCTS); break;
duke@0 1542
duke@0 1543 case Bytecodes::_lcmp: pp(vvvvCTS, vCTS); break;
duke@0 1544 case Bytecodes::_dcmpl:
duke@0 1545 case Bytecodes::_dcmpg: pp(vvvvCTS, vCTS); break;
duke@0 1546
duke@0 1547 case Bytecodes::_ifeq:
duke@0 1548 case Bytecodes::_ifne:
duke@0 1549 case Bytecodes::_iflt:
duke@0 1550 case Bytecodes::_ifge:
duke@0 1551 case Bytecodes::_ifgt:
duke@0 1552 case Bytecodes::_ifle:
duke@0 1553 case Bytecodes::_tableswitch: ppop1(valCTS);
duke@0 1554 break;
duke@0 1555 case Bytecodes::_ireturn:
duke@0 1556 case Bytecodes::_freturn: do_return_monitor_check();
duke@0 1557 ppop1(valCTS);
duke@0 1558 break;
duke@0 1559 case Bytecodes::_if_icmpeq:
duke@0 1560 case Bytecodes::_if_icmpne:
duke@0 1561 case Bytecodes::_if_icmplt:
duke@0 1562 case Bytecodes::_if_icmpge:
duke@0 1563 case Bytecodes::_if_icmpgt:
duke@0 1564 case Bytecodes::_if_icmple: ppop(vvCTS);
duke@0 1565 break;
duke@0 1566
duke@0 1567 case Bytecodes::_lreturn: do_return_monitor_check();
duke@0 1568 ppop(vvCTS);
duke@0 1569 break;
duke@0 1570
duke@0 1571 case Bytecodes::_dreturn: do_return_monitor_check();
duke@0 1572 ppop(vvCTS);
duke@0 1573 break;
duke@0 1574
duke@0 1575 case Bytecodes::_if_acmpeq:
duke@0 1576 case Bytecodes::_if_acmpne: ppop(rrCTS); break;
duke@0 1577
duke@0 1578 case Bytecodes::_jsr: do_jsr(itr->dest()); break;
duke@0 1579 case Bytecodes::_jsr_w: do_jsr(itr->dest_w()); break;
duke@0 1580
never@2601 1581 case Bytecodes::_getstatic: do_field(true, true, itr->get_index_u2_cpcache(), itr->bci()); break;
jrose@1485 1582 case Bytecodes::_putstatic: do_field(false, true, itr->get_index_u2_cpcache(), itr->bci()); break;
jrose@1485 1583 case Bytecodes::_getfield: do_field(true, false, itr->get_index_u2_cpcache(), itr->bci()); break;
jrose@1485 1584 case Bytecodes::_putfield: do_field(false, false, itr->get_index_u2_cpcache(), itr->bci()); break;
duke@0 1585
twisti@1138 1586 case Bytecodes::_invokevirtual:
jrose@1485 1587 case Bytecodes::_invokespecial: do_method(false, false, itr->get_index_u2_cpcache(), itr->bci()); break;
jrose@1485 1588 case Bytecodes::_invokestatic: do_method(true, false, itr->get_index_u2_cpcache(), itr->bci()); break;
jrose@1485 1589 case Bytecodes::_invokedynamic: do_method(true, false, itr->get_index_u4(), itr->bci()); break;
jrose@1485 1590 case Bytecodes::_invokeinterface: do_method(false, true, itr->get_index_u2_cpcache(), itr->bci()); break;
twisti@1138 1591 case Bytecodes::_newarray:
twisti@1138 1592 case Bytecodes::_anewarray: pp_new_ref(vCTS, itr->bci()); break;
duke@0 1593 case Bytecodes::_checkcast: do_checkcast(); break;
duke@0 1594 case Bytecodes::_arraylength:
duke@0 1595 case Bytecodes::_instanceof: pp(rCTS, vCTS); break;
duke@0 1596 case Bytecodes::_monitorenter: do_monitorenter(itr->bci()); break;
duke@0 1597 case Bytecodes::_monitorexit: do_monitorexit(itr->bci()); break;
duke@0 1598
duke@0 1599 case Bytecodes::_athrow: // handled by do_exception_edge() BUT ...
duke@0 1600 // vlh(apple): do_exception_edge() does not get
duke@0 1601 // called if method has no exception handlers
duke@0 1602 if ((!_has_exceptions) && (_monitor_top > 0)) {
duke@0 1603 _monitor_safe = false;
duke@0 1604 }
duke@0 1605 break;
duke@0 1606
duke@0 1607 case Bytecodes::_areturn: do_return_monitor_check();
duke@0 1608 ppop1(refCTS);
duke@0 1609 break;
duke@0 1610 case Bytecodes::_ifnull:
duke@0 1611 case Bytecodes::_ifnonnull: ppop1(refCTS); break;
duke@0 1612 case Bytecodes::_multianewarray: do_multianewarray(*(itr->bcp()+3), itr->bci()); break;
duke@0 1613
duke@0 1614 case Bytecodes::_wide: fatal("Iterator should skip this bytecode"); break;
duke@0 1615 case Bytecodes::_ret: break;
duke@0 1616
duke@0 1617 // Java opcodes
duke@0 1618 case Bytecodes::_lookupswitch: ppop1(valCTS); break;
duke@0 1619
duke@0 1620 default:
duke@0 1621 tty->print("unexpected opcode: %d\n", itr->code());
duke@0 1622 ShouldNotReachHere();
duke@0 1623 break;
duke@0 1624 }
duke@0 1625 }
duke@0 1626
duke@0 1627 void GenerateOopMap::check_type(CellTypeState expected, CellTypeState actual) {
duke@0 1628 if (!expected.equal_kind(actual)) {
duke@0 1629 verify_error("wrong type on stack (found: %c expected: %c)", actual.to_char(), expected.to_char());
duke@0 1630 }
duke@0 1631 }
duke@0 1632
duke@0 1633 void GenerateOopMap::ppstore(CellTypeState *in, int loc_no) {
duke@0 1634 while(!(*in).is_bottom()) {
duke@0 1635 CellTypeState expected =*in++;
duke@0 1636 CellTypeState actual = pop();
duke@0 1637 check_type(expected, actual);
duke@0 1638 assert(loc_no >= 0, "sanity check");
duke@0 1639 set_var(loc_no++, actual);
duke@0 1640 }
duke@0 1641 }
duke@0 1642
duke@0 1643 void GenerateOopMap::ppload(CellTypeState *out, int loc_no) {
duke@0 1644 while(!(*out).is_bottom()) {
duke@0 1645 CellTypeState out1 = *out++;
duke@0 1646 CellTypeState vcts = get_var(loc_no);
duke@0 1647 assert(out1.can_be_reference() || out1.can_be_value(),
duke@0 1648 "can only load refs. and values.");
duke@0 1649 if (out1.is_reference()) {
duke@0 1650 assert(loc_no>=0, "sanity check");
duke@0 1651 if (!vcts.is_reference()) {
duke@0 1652 // We were asked to push a reference, but the type of the
duke@0 1653 // variable can be something else
duke@0 1654 _conflict = true;
duke@0 1655 if (vcts.can_be_uninit()) {
duke@0 1656 // It is a ref-uninit conflict (at least). If there are other
duke@0 1657 // problems, we'll get them in the next round
duke@0 1658 add_to_ref_init_set(loc_no);
duke@0 1659 vcts = out1;
duke@0 1660 } else {
duke@0 1661 // It wasn't a ref-uninit conflict. So must be a
duke@0 1662 // ref-val or ref-pc conflict. Split the variable.
duke@0 1663 record_refval_conflict(loc_no);
duke@0 1664 vcts = out1;
duke@0 1665 }
duke@0 1666 push(out1); // recover...
duke@0 1667 } else {
duke@0 1668 push(vcts); // preserve reference.
duke@0 1669 }
duke@0 1670 // Otherwise it is a conflict, but one that verification would
duke@0 1671 // have caught if illegal. In particular, it can't be a topCTS
duke@0 1672 // resulting from mergeing two difference pcCTS's since the verifier
duke@0 1673 // would have rejected any use of such a merge.
duke@0 1674 } else {
duke@0 1675 push(out1); // handle val/init conflict
duke@0 1676 }
duke@0 1677 loc_no++;
duke@0 1678 }
duke@0 1679 }
duke@0 1680
duke@0 1681 void GenerateOopMap::ppdupswap(int poplen, const char *out) {
duke@0 1682 CellTypeState actual[5];
duke@0 1683 assert(poplen < 5, "this must be less than length of actual vector");
duke@0 1684
duke@0 1685 // pop all arguments
duke@0 1686 for(int i = 0; i < poplen; i++) actual[i] = pop();
duke@0 1687
duke@0 1688 // put them back
duke@0 1689 char push_ch = *out++;
duke@0 1690 while (push_ch != '\0') {
duke@0 1691 int idx = push_ch - '1';
duke@0 1692 assert(idx >= 0 && idx < poplen, "wrong arguments");
duke@0 1693 push(actual[idx]);
duke@0 1694 push_ch = *out++;
duke@0 1695 }
duke@0 1696 }
duke@0 1697
duke@0 1698 void GenerateOopMap::ppop1(CellTypeState out) {
duke@0 1699 CellTypeState actual = pop();
duke@0 1700 check_type(out, actual);
duke@0 1701 }
duke@0 1702
duke@0 1703 void GenerateOopMap::ppop(CellTypeState *out) {
duke@0 1704 while (!(*out).is_bottom()) {
duke@0 1705 ppop1(*out++);
duke@0 1706 }
duke@0 1707 }
duke@0 1708
duke@0 1709 void GenerateOopMap::ppush1(CellTypeState in) {
duke@0 1710 assert(in.is_reference() | in.is_value(), "sanity check");
duke@0 1711 push(in);
duke@0 1712 }
duke@0 1713
duke@0 1714 void GenerateOopMap::ppush(CellTypeState *in) {
duke@0 1715 while (!(*in).is_bottom()) {
duke@0 1716 ppush1(*in++);
duke@0 1717 }
duke@0 1718 }
duke@0 1719
duke@0 1720 void GenerateOopMap::pp(CellTypeState *in, CellTypeState *out) {
duke@0 1721 ppop(in);
duke@0 1722 ppush(out);
duke@0 1723 }
duke@0 1724
duke@0 1725 void GenerateOopMap::pp_new_ref(CellTypeState *in, int bci) {
duke@0 1726 ppop(in);
duke@0 1727 ppush1(CellTypeState::make_line_ref(bci));
duke@0 1728 }
duke@0 1729
duke@0 1730 void GenerateOopMap::ppop_any(int poplen) {
duke@0 1731 if (_stack_top >= poplen) {
duke@0 1732 _stack_top -= poplen;
duke@0 1733 } else {
duke@0 1734 verify_error("stack underflow");
duke@0 1735 }
duke@0 1736 }
duke@0 1737
duke@0 1738 // Replace all occurences of the state 'match' with the state 'replace'
duke@0 1739 // in our current state vector.
duke@0 1740 void GenerateOopMap::replace_all_CTS_matches(CellTypeState match,
duke@0 1741 CellTypeState replace) {
duke@0 1742 int i;
duke@0 1743 int len = _max_locals + _stack_top;
duke@0 1744 bool change = false;
duke@0 1745
duke@0 1746 for (i = len - 1; i >= 0; i--) {
duke@0 1747 if (match.equal(_state[i])) {
duke@0 1748 _state[i] = replace;
duke@0 1749 }
duke@0 1750 }
duke@0 1751
duke@0 1752 if (_monitor_top > 0) {
duke@0 1753 int base = _max_locals + _max_stack;
duke@0 1754 len = base + _monitor_top;
duke@0 1755 for (i = len - 1; i >= base; i--) {
duke@0 1756 if (match.equal(_state[i])) {
duke@0 1757 _state[i] = replace;
duke@0 1758 }
duke@0 1759 }
duke@0 1760 }
duke@0 1761 }
duke@0 1762
duke@0 1763 void GenerateOopMap::do_checkcast() {
duke@0 1764 CellTypeState actual = pop();
duke@0 1765 check_type(refCTS, actual);
duke@0 1766 push(actual);
duke@0 1767 }
duke@0 1768
duke@0 1769 void GenerateOopMap::do_monitorenter(int bci) {
duke@0 1770 CellTypeState actual = pop();
duke@0 1771 if (_monitor_top == bad_monitors) {
duke@0 1772 return;
duke@0 1773 }
duke@0 1774
duke@0 1775 // Bail out when we get repeated locks on an identical monitor. This case
duke@0 1776 // isn't too hard to handle and can be made to work if supporting nested
duke@0 1777 // redundant synchronized statements becomes a priority.
duke@0 1778 //
duke@0 1779 // See also "Note" in do_monitorexit(), below.
duke@0 1780 if (actual.is_lock_reference()) {
duke@0 1781 _monitor_top = bad_monitors;
duke@0 1782 _monitor_safe = false;
duke@0 1783
duke@0 1784 if (TraceMonitorMismatch) {
duke@0 1785 report_monitor_mismatch("nested redundant lock -- bailout...");
duke@0 1786 }
duke@0 1787 return;
duke@0 1788 }
duke@0 1789
duke@0 1790 CellTypeState lock = CellTypeState::make_lock_ref(bci);
duke@0 1791 check_type(refCTS, actual);
duke@0 1792 if (!actual.is_info_top()) {
duke@0 1793 replace_all_CTS_matches(actual, lock);
duke@0 1794 monitor_push(lock);
duke@0 1795 }
duke@0 1796 }
duke@0 1797
duke@0 1798 void GenerateOopMap::do_monitorexit(int bci) {
duke@0 1799 CellTypeState actual = pop();
duke@0 1800 if (_monitor_top == bad_monitors) {
duke@0 1801 return;
duke@0 1802 }
duke@0 1803 check_type(refCTS, actual);
duke@0 1804 CellTypeState expected = monitor_pop();
duke@0 1805 if (!actual.is_lock_reference() || !expected.equal(actual)) {
duke@0 1806 // The monitor we are exiting is not verifiably the one
duke@0 1807 // on the top of our monitor stack. This causes a monitor
duke@0 1808 // mismatch.
duke@0 1809 _monitor_top = bad_monitors;
duke@0 1810 _monitor_safe = false;
duke@0 1811
duke@0 1812 // We need to mark this basic block as changed so that
duke@0 1813 // this monitorexit will be visited again. We need to
duke@0 1814 // do this to ensure that we have accounted for the
duke@0 1815 // possibility that this bytecode will throw an
duke@0 1816 // exception.
duke@0 1817 BasicBlock* bb = get_basic_block_containing(bci);
morris@4338 1818 guarantee(bb != NULL, "no basic block for bci");
duke@0 1819 bb->set_changed(true);
duke@0 1820 bb->_monitor_top = bad_monitors;
duke@0 1821
duke@0 1822 if (TraceMonitorMismatch) {
duke@0 1823 report_monitor_mismatch("improper monitor pair");
duke@0 1824 }
duke@0 1825 } else {
duke@0 1826 // This code is a fix for the case where we have repeated
duke@0 1827 // locking of the same object in straightline code. We clear
duke@0 1828 // out the lock when it is popped from the monitor stack
duke@0 1829 // and replace it with an unobtrusive reference value that can
duke@0 1830 // be locked again.
duke@0 1831 //
duke@0 1832 // Note: when generateOopMap is fixed to properly handle repeated,
duke@0 1833 // nested, redundant locks on the same object, then this
duke@0 1834 // fix will need to be removed at that time.
duke@0 1835 replace_all_CTS_matches(actual, CellTypeState::make_line_ref(bci));
duke@0 1836 }
duke@0 1837 }
duke@0 1838
duke@0 1839 void GenerateOopMap::do_return_monitor_check() {
duke@0 1840 if (_monitor_top > 0) {
duke@0 1841 // The monitor stack must be empty when we leave the method
duke@0 1842 // for the monitors to be properly matched.
duke@0 1843 _monitor_safe = false;
duke@0 1844
duke@0 1845 // Since there are no successors to the *return bytecode, it
duke@0 1846 // isn't necessary to set _monitor_top to bad_monitors.
duke@0 1847
duke@0 1848 if (TraceMonitorMismatch) {
duke@0 1849 report_monitor_mismatch("non-empty monitor stack at return");
duke@0 1850 }
duke@0 1851 }
duke@0 1852 }
duke@0 1853
duke@0 1854 void GenerateOopMap::do_jsr(int targ_bci) {
duke@0 1855 push(CellTypeState::make_addr(targ_bci));
duke@0 1856 }
duke@0 1857
duke@0 1858
duke@0 1859
jrose@1830 1860 void GenerateOopMap::do_ldc(int bci) {
never@2027 1861 Bytecode_loadconstant ldc(method(), bci);
coleenp@3602 1862 ConstantPool* cp = method()->constants();
coleenp@3602 1863 constantTag tag = cp->tag_at(ldc.pool_index()); // idx is index in resolved_references
never@2027 1864 BasicType bt = ldc.result_type();
coleenp@3602 1865 CellTypeState cts;
coleenp@3602 1866 if (tag.is_klass() ||
coleenp@3602 1867 tag.is_unresolved_klass() ||
coleenp@3602 1868 tag.is_string() ||
coleenp@3602 1869 tag.is_method_handle() ||
coleenp@3602 1870 tag.is_method_type()) {
coleenp@3602 1871 assert(bt == T_OBJECT, "Guard is incorrect");
coleenp@3602 1872 cts = CellTypeState::make_line_ref(bci);
coleenp@3602 1873 } else {
coleenp@3602 1874 assert(bt != T_OBJECT, "Guard is incorrect");
coleenp@3602 1875 cts = valCTS;
coleenp@3602 1876 }
duke@0 1877 ppush1(cts);
duke@0 1878 }
duke@0 1879
duke@0 1880 void GenerateOopMap::do_multianewarray(int dims, int bci) {
duke@0 1881 assert(dims >= 1, "sanity check");
duke@0 1882 for(int i = dims -1; i >=0; i--) {
duke@0 1883 ppop1(valCTS);
duke@0 1884 }
duke@0 1885 ppush1(CellTypeState::make_line_ref(bci));
duke@0 1886 }
duke@0 1887
duke@0 1888 void GenerateOopMap::do_astore(int idx) {
duke@0 1889 CellTypeState r_or_p = pop();
duke@0 1890 if (!r_or_p.is_address() && !r_or_p.is_reference()) {
duke@0 1891 // We actually expected ref or pc, but we only report that we expected a ref. It does not
duke@0 1892 // really matter (at least for now)
duke@0 1893 verify_error("wrong type on stack (found: %c, expected: {pr})", r_or_p.to_char());
duke@0 1894 return;
duke@0 1895 }
duke@0 1896 set_var(idx, r_or_p);
duke@0 1897 }
duke@0 1898
duke@0 1899 // Copies bottom/zero terminated CTS string from "src" into "dst".
duke@0 1900 // Does NOT terminate with a bottom. Returns the number of cells copied.
duke@0 1901 int GenerateOopMap::copy_cts(CellTypeState *dst, CellTypeState *src) {
duke@0 1902 int idx = 0;
duke@0 1903 while (!src[idx].is_bottom()) {
duke@0 1904 dst[idx] = src[idx];
duke@0 1905 idx++;
duke@0 1906 }
duke@0 1907 return idx;
duke@0 1908 }
duke@0 1909
duke@0 1910 void GenerateOopMap::do_field(int is_get, int is_static, int idx, int bci) {
duke@0 1911 // Dig up signature for field in constant pool
coleenp@3602 1912 ConstantPool* cp = method()->constants();
duke@0 1913 int nameAndTypeIdx = cp->name_and_type_ref_index_at(idx);
duke@0 1914 int signatureIdx = cp->signature_ref_index_at(nameAndTypeIdx);
coleenp@2062 1915 Symbol* signature = cp->symbol_at(signatureIdx);
duke@0 1916
duke@0 1917 // Parse signature (espcially simple for fields)
duke@0 1918 assert(signature->utf8_length() > 0, "field signatures cannot have zero length");
duke@0 1919 // The signature is UFT8 encoded, but the first char is always ASCII for signatures.
duke@0 1920 char sigch = (char)*(signature->base());
duke@0 1921 CellTypeState temp[4];
duke@0 1922 CellTypeState *eff = sigchar_to_effect(sigch, bci, temp);
duke@0 1923
duke@0 1924 CellTypeState in[4];
duke@0 1925 CellTypeState *out;
duke@0 1926 int i = 0;
duke@0 1927
duke@0 1928 if (is_get) {
duke@0 1929 out = eff;
duke@0 1930 } else {
duke@0 1931 out = epsilonCTS;
duke@0 1932 i = copy_cts(in, eff);
duke@0 1933 }
duke@0 1934 if (!is_static) in[i++] = CellTypeState::ref;
duke@0 1935 in[i] = CellTypeState::bottom;
duke@0 1936 assert(i<=3, "sanity check");
duke@0 1937 pp(in, out);
duke@0 1938 }
duke@0 1939
duke@0 1940 void GenerateOopMap::do_method(int is_static, int is_interface, int idx, int bci) {
jrose@1059 1941 // Dig up signature for field in constant pool
coleenp@3602 1942 ConstantPool* cp = _method->constants();
coleenp@2062 1943 Symbol* signature = cp->signature_ref_at(idx);
duke@0 1944
duke@0 1945 // Parse method signature
duke@0 1946 CellTypeState out[4];
duke@0 1947 CellTypeState in[MAXARGSIZE+1]; // Includes result
duke@0 1948 ComputeCallStack cse(signature);
duke@0 1949
duke@0 1950 // Compute return type
duke@0 1951 int res_length= cse.compute_for_returntype(out);
duke@0 1952
duke@0 1953 // Temporary hack.
duke@0 1954 if (out[0].equal(CellTypeState::ref) && out[1].equal(CellTypeState::bottom)) {
duke@0 1955 out[0] = CellTypeState::make_line_ref(bci);
duke@0 1956 }
duke@0 1957
duke@0 1958 assert(res_length<=4, "max value should be vv");
duke@0 1959
duke@0 1960 // Compute arguments
duke@0 1961 int arg_length = cse.compute_for_parameters(is_static != 0, in);
duke@0 1962 assert(arg_length<=MAXARGSIZE, "too many locals");
duke@0 1963
duke@0 1964 // Pop arguments
duke@0 1965 for (int i = arg_length - 1; i >= 0; i--) ppop1(in[i]);// Do args in reverse order.
duke@0 1966
duke@0 1967 // Report results
duke@0 1968 if (_report_result_for_send == true) {
duke@0 1969 fill_stackmap_for_opcodes(_itr_send, vars(), stack(), _stack_top);
duke@0 1970 _report_result_for_send = false;
duke@0 1971 }
duke@0 1972
duke@0 1973 // Push return address
duke@0 1974 ppush(out);
duke@0 1975 }
duke@0 1976
duke@0 1977 // This is used to parse the signature for fields, since they are very simple...
duke@0 1978 CellTypeState *GenerateOopMap::sigchar_to_effect(char sigch, int bci, CellTypeState *out) {
duke@0 1979 // Object and array
duke@0 1980 if (sigch=='L' || sigch=='[') {
duke@0 1981 out[0] = CellTypeState::make_line_ref(bci);
duke@0 1982 out[1] = CellTypeState::bottom;
duke@0 1983 return out;
duke@0 1984 }
duke@0 1985 if (sigch == 'J' || sigch == 'D' ) return vvCTS; // Long and Double
duke@0 1986 if (sigch == 'V' ) return epsilonCTS; // Void
duke@0 1987 return vCTS; // Otherwise
duke@0 1988 }
duke@0 1989
duke@0 1990 long GenerateOopMap::_total_byte_count = 0;
duke@0 1991 elapsedTimer GenerateOopMap::_total_oopmap_time;
duke@0 1992
duke@0 1993 // This function assumes "bcs" is at a "ret" instruction and that the vars
duke@0 1994 // state is valid for that instruction. Furthermore, the ret instruction
duke@0 1995 // must be the last instruction in "bb" (we store information about the
duke@0 1996 // "ret" in "bb").
duke@0 1997 void GenerateOopMap::ret_jump_targets_do(BytecodeStream *bcs, jmpFct_t jmpFct, int varNo, int *data) {
duke@0 1998 CellTypeState ra = vars()[varNo];
duke@0 1999 if (!ra.is_good_address()) {
duke@0 2000 verify_error("ret returns from two jsr subroutines?");
duke@0 2001 return;
duke@0 2002 }
duke@0 2003 int target = ra.get_info();
duke@0 2004
duke@0 2005 RetTableEntry* rtEnt = _rt.find_jsrs_for_target(target);
duke@0 2006 int bci = bcs->bci();
duke@0 2007 for (int i = 0; i < rtEnt->nof_jsrs(); i++) {
duke@0 2008 int target_bci = rtEnt->jsrs(i);
duke@0 2009 // Make sure a jrtRet does not set the changed bit for dead basicblock.
duke@0 2010 BasicBlock* jsr_bb = get_basic_block_containing(target_bci - 1);
duke@0 2011 debug_only(BasicBlock* target_bb = &jsr_bb[1];)
duke@0 2012 assert(target_bb == get_basic_block_at(target_bci), "wrong calc. of successor basicblock");
duke@0 2013 bool alive = jsr_bb->is_alive();
duke@0 2014 if (TraceNewOopMapGeneration) {
duke@0 2015 tty->print("pc = %d, ret -> %d alive: %s\n", bci, target_bci, alive ? "true" : "false");
duke@0 2016 }
duke@0 2017 if (alive) jmpFct(this, target_bci, data);
duke@0 2018 }
duke@0 2019 }
duke@0 2020
duke@0 2021 //
duke@0 2022 // Debug method
duke@0 2023 //
duke@0 2024 char* GenerateOopMap::state_vec_to_string(CellTypeState* vec, int len) {
duke@0 2025 #ifdef ASSERT
duke@0 2026 int checklen = MAX3(_max_locals, _max_stack, _max_monitors) + 1;
duke@0 2027 assert(len < checklen, "state_vec_buf overflow");
duke@0 2028 #endif
duke@0 2029 for (int i = 0; i < len; i++) _state_vec_buf[i] = vec[i].to_char();
duke@0 2030 _state_vec_buf[len] = 0;
duke@0 2031 return _state_vec_buf;
duke@0 2032 }
duke@0 2033
duke@0 2034 void GenerateOopMap::print_time() {
duke@0 2035 tty->print_cr ("Accumulated oopmap times:");
duke@0 2036 tty->print_cr ("---------------------------");
duke@0 2037 tty->print_cr (" Total : %3.3f sec.", GenerateOopMap::_total_oopmap_time.seconds());
duke@0 2038 tty->print_cr (" (%3.0f bytecodes per sec) ",
duke@0 2039 GenerateOopMap::_total_byte_count / GenerateOopMap::_total_oopmap_time.seconds());
duke@0 2040 }
duke@0 2041
duke@0 2042 //
duke@0 2043 // ============ Main Entry Point ===========
duke@0 2044 //
duke@0 2045 GenerateOopMap::GenerateOopMap(methodHandle method) {
twisti@605 2046 // We have to initialize all variables here, that can be queried directly
duke@0 2047 _method = method;
duke@0 2048 _max_locals=0;
duke@0 2049 _init_vars = NULL;
duke@0 2050
duke@0 2051 #ifndef PRODUCT
duke@0 2052 // If we are doing a detailed trace, include the regular trace information.
duke@0 2053 if (TraceNewOopMapGenerationDetailed) {
duke@0 2054 TraceNewOopMapGeneration = true;
duke@0 2055 }
duke@0 2056 #endif
duke@0 2057 }
duke@0 2058
duke@0 2059 void GenerateOopMap::compute_map(TRAPS) {
duke@0 2060 #ifndef PRODUCT
duke@0 2061 if (TimeOopMap2) {
duke@0 2062 method()->print_short_name(tty);
duke@0 2063 tty->print(" ");
duke@0 2064 }
duke@0 2065 if (TimeOopMap) {
duke@0 2066 _total_byte_count += method()->code_size();
duke@0 2067 }
duke@0 2068 #endif
duke@0 2069 TraceTime t_single("oopmap time", TimeOopMap2);
duke@0 2070 TraceTime t_all(NULL, &_total_oopmap_time, TimeOopMap);
duke@0 2071
duke@0 2072 // Initialize values
duke@0 2073 _got_error = false;
duke@0 2074 _conflict = false;
duke@0 2075 _max_locals = method()->max_locals();
duke@0 2076 _max_stack = method()->max_stack();
jiangli@3482 2077 _has_exceptions = (method()->has_exception_handler());
duke@0 2078 _nof_refval_conflicts = 0;
duke@0 2079 _init_vars = new GrowableArray<intptr_t>(5); // There are seldom more than 5 init_vars
duke@0 2080 _report_result = false;
duke@0 2081 _report_result_for_send = false;
duke@0 2082 _new_var_map = NULL;
duke@0 2083 _ret_adr_tos = new GrowableArray<intptr_t>(5); // 5 seems like a good number;
duke@0 2084 _did_rewriting = false;
duke@0 2085 _did_relocation = false;
duke@0 2086
duke@0 2087 if (TraceNewOopMapGeneration) {
duke@0 2088 tty->print("Method name: %s\n", method()->name()->as_C_string());
duke@0 2089 if (Verbose) {
duke@0 2090 _method->print_codes();
duke@0 2091 tty->print_cr("Exception table:");
jiangli@3482 2092 ExceptionTable excps(method());
jiangli@3482 2093 for(int i = 0; i < excps.length(); i ++) {
jiangli@3482 2094 tty->print_cr("[%d - %d] -> %d",
jiangli@3482 2095 excps.start_pc(i), excps.end_pc(i), excps.handler_pc(i));
duke@0 2096 }
duke@0 2097 }
duke@0 2098 }
duke@0 2099
duke@0 2100 // if no code - do nothing
duke@0 2101 // compiler needs info
duke@0 2102 if (method()->code_size() == 0 || _max_locals + method()->max_stack() == 0) {
duke@0 2103 fill_stackmap_prolog(0);
duke@0 2104 fill_stackmap_epilog();
duke@0 2105 return;
duke@0 2106 }
duke@0 2107 // Step 1: Compute all jump targets and their return value
duke@0 2108 if (!_got_error)
duke@0 2109 _rt.compute_ret_table(_method);
duke@0 2110
duke@0 2111 // Step 2: Find all basic blocks and count GC points
duke@0 2112 if (!_got_error)
duke@0 2113 mark_bbheaders_and_count_gc_points();
duke@0 2114
duke@0 2115 // Step 3: Calculate stack maps
duke@0 2116 if (!_got_error)
duke@0 2117 do_interpretation();
duke@0 2118
duke@0 2119 // Step 4:Return results
duke@0 2120 if (!_got_error && report_results())
duke@0 2121 report_result();
duke@0 2122
duke@0 2123 if (_got_error) {
duke@0 2124 THROW_HANDLE(_exception);
duke@0 2125 }
duke@0 2126 }
duke@0 2127
duke@0 2128 // Error handling methods
duke@0 2129 // These methods create an exception for the current thread which is thrown
duke@0 2130 // at the bottom of the call stack, when it returns to compute_map(). The
duke@0 2131 // _got_error flag controls execution. NOT TODO: The VM exception propagation
duke@0 2132 // mechanism using TRAPS/CHECKs could be used here instead but it would need
duke@0 2133 // to be added as a parameter to every function and checked for every call.
duke@0 2134 // The tons of extra code it would generate didn't seem worth the change.
duke@0 2135 //
duke@0 2136 void GenerateOopMap::error_work(const char *format, va_list ap) {
duke@0 2137 _got_error = true;
duke@0 2138 char msg_buffer[512];
duke@0 2139 vsnprintf(msg_buffer, sizeof(msg_buffer), format, ap);
duke@0 2140 // Append method name
duke@0 2141 char msg_buffer2[512];
duke@0 2142 jio_snprintf(msg_buffer2, sizeof(msg_buffer2), "%s in method %s", msg_buffer, method()->name()->as_C_string());
duke@0 2143 _exception = Exceptions::new_exception(Thread::current(),
duke@0 2144 vmSymbols::java_lang_LinkageError(), msg_buffer2);
duke@0 2145 }
duke@0 2146
duke@0 2147 void GenerateOopMap::report_error(const char *format, ...) {
duke@0 2148 va_list ap;
duke@0 2149 va_start(ap, format);
duke@0 2150 error_work(format, ap);
duke@0 2151 }
duke@0 2152
duke@0 2153 void GenerateOopMap::verify_error(const char *format, ...) {
duke@0 2154 // We do not distinguish between different types of errors for verification
duke@0 2155 // errors. Let the verifier give a better message.
duke@0 2156 const char *msg = "Illegal class file encountered. Try running with -Xverify:all";
bobv@1601 2157 _got_error = true;
bobv@1601 2158 // Append method name
bobv@1601 2159 char msg_buffer2[512];
bobv@1601 2160 jio_snprintf(msg_buffer2, sizeof(msg_buffer2), "%s in method %s", msg,
bobv@1601 2161 method()->name()->as_C_string());
bobv@1601 2162 _exception = Exceptions::new_exception(Thread::current(),
bobv@1601 2163 vmSymbols::java_lang_LinkageError(), msg_buffer2);
duke@0 2164 }
duke@0 2165
duke@0 2166 //
duke@0 2167 // Report result opcodes
duke@0 2168 //
duke@0 2169 void GenerateOopMap::report_result() {
duke@0 2170
duke@0 2171 if (TraceNewOopMapGeneration) tty->print_cr("Report result pass");
duke@0 2172
duke@0 2173 // We now want to report the result of the parse
duke@0 2174 _report_result = true;
duke@0 2175
duke@0 2176 // Prolog code
duke@0 2177 fill_stackmap_prolog(_gc_points);
duke@0 2178
duke@0 2179 // Mark everything changed, then do one interpretation pass.
duke@0 2180 for (int i = 0; i<_bb_count; i++) {
duke@0 2181 if (_basic_blocks[i].is_reachable()) {
duke@0 2182 _basic_blocks[i].set_changed(true);
duke@0 2183 interp_bb(&_basic_blocks[i]);
duke@0 2184 }
duke@0 2185 }
duke@0 2186
duke@0 2187 // Note: Since we are skipping dead-code when we are reporting results, then
duke@0 2188 // the no. of encountered gc-points might be fewer than the previously number
duke@0 2189 // we have counted. (dead-code is a pain - it should be removed before we get here)
duke@0 2190 fill_stackmap_epilog();
duke@0 2191
duke@0 2192 // Report initvars
duke@0 2193 fill_init_vars(_init_vars);
duke@0 2194
duke@0 2195 _report_result = false;
duke@0 2196 }
duke@0 2197
duke@0 2198 void GenerateOopMap::result_for_basicblock(int bci) {
duke@0 2199 if (TraceNewOopMapGeneration) tty->print_cr("Report result pass for basicblock");
duke@0 2200
duke@0 2201 // We now want to report the result of the parse
duke@0 2202 _report_result = true;
duke@0 2203
duke@0 2204 // Find basicblock and report results
duke@0 2205 BasicBlock* bb = get_basic_block_containing(bci);
morris@4338 2206 guarantee(bb != NULL, "no basic block for bci");
duke@0 2207 assert(bb->is_reachable(), "getting result from unreachable basicblock");
duke@0 2208 bb->set_changed(true);
duke@0 2209 interp_bb(bb);
duke@0 2210 }
duke@0 2211
duke@0 2212 //
duke@0 2213 // Conflict handling code
duke@0 2214 //
duke@0 2215
duke@0 2216 void GenerateOopMap::record_refval_conflict(int varNo) {
duke@0 2217 assert(varNo>=0 && varNo< _max_locals, "index out of range");
duke@0 2218
duke@0 2219 if (TraceOopMapRewrites) {
duke@0 2220 tty->print("### Conflict detected (local no: %d)\n", varNo);
duke@0 2221 }
duke@0 2222
duke@0 2223 if (!_new_var_map) {
duke@0 2224 _new_var_map = NEW_RESOURCE_ARRAY(int, _max_locals);
duke@0 2225 for (int k = 0; k < _max_locals; k++) _new_var_map[k] = k;
duke@0 2226 }
duke@0 2227
duke@0 2228 if ( _new_var_map[varNo] == varNo) {
duke@0 2229 // Check if max. number of locals has been reached
duke@0 2230 if (_max_locals + _nof_refval_conflicts >= MAX_LOCAL_VARS) {
duke@0 2231 report_error("Rewriting exceeded local variable limit");
duke@0 2232 return;
duke@0 2233 }
duke@0 2234 _new_var_map[varNo] = _max_locals + _nof_refval_conflicts;
duke@0 2235 _nof_refval_conflicts++;
duke@0 2236 }
duke@0 2237 }
duke@0 2238
duke@0 2239 void GenerateOopMap::rewrite_refval_conflicts()
duke@0 2240 {
duke@0 2241 // We can get here two ways: Either a rewrite conflict was detected, or
duke@0 2242 // an uninitialize reference was detected. In the second case, we do not
duke@0 2243 // do any rewriting, we just want to recompute the reference set with the
duke@0 2244 // new information
duke@0 2245
duke@0 2246 int nof_conflicts = 0; // Used for debugging only
duke@0 2247
duke@0 2248 if ( _nof_refval_conflicts == 0 )
duke@0 2249 return;
duke@0 2250
duke@0 2251 // Check if rewrites are allowed in this parse.
duke@0 2252 if (!allow_rewrites() && !IgnoreRewrites) {
duke@0 2253 fatal("Rewriting method not allowed at this stage");
duke@0 2254 }
duke@0 2255
duke@0 2256
duke@0 2257 // This following flag is to tempoary supress rewrites. The locals that might conflict will
duke@0 2258 // all be set to contain values. This is UNSAFE - however, until the rewriting has been completely
duke@0 2259 // tested it is nice to have.
duke@0 2260 if (IgnoreRewrites) {
duke@0 2261 if (Verbose) {
duke@0 2262 tty->print("rewrites suppressed for local no. ");
duke@0 2263 for (int l = 0; l < _max_locals; l++) {
duke@0 2264 if (_new_var_map[l] != l) {
duke@0 2265 tty->print("%d ", l);
duke@0 2266 vars()[l] = CellTypeState::value;
duke@0 2267 }
duke@0 2268 }
duke@0 2269 tty->cr();
duke@0 2270 }
duke@0 2271
duke@0 2272 // That was that...
duke@0 2273 _new_var_map = NULL;
duke@0 2274 _nof_refval_conflicts = 0;
duke@0 2275 _conflict = false;
duke@0 2276
duke@0 2277 return;
duke@0 2278 }
duke@0 2279
duke@0 2280 // Tracing flag
duke@0 2281 _did_rewriting = true;
duke@0 2282
duke@0 2283 if (TraceOopMapRewrites) {
duke@0 2284 tty->print_cr("ref/value conflict for method %s - bytecodes are getting rewritten", method()->name()->as_C_string());
duke@0 2285 method()->print();
duke@0 2286 method()->print_codes();
duke@0 2287 }
duke@0 2288
duke@0 2289 assert(_new_var_map!=NULL, "nothing to rewrite");
duke@0 2290 assert(_conflict==true, "We should not be here");
duke@0 2291
duke@0 2292 compute_ret_adr_at_TOS();
duke@0 2293 if (!_got_error) {
duke@0 2294 for (int k = 0; k < _max_locals && !_got_error; k++) {
duke@0 2295 if (_new_var_map[k] != k) {
duke@0 2296 if (TraceOopMapRewrites) {
duke@0 2297 tty->print_cr("Rewriting: %d -> %d", k, _new_var_map[k]);
duke@0 2298 }
duke@0 2299 rewrite_refval_conflict(k, _new_var_map[k]);
duke@0 2300 if (_got_error) return;
duke@0 2301 nof_conflicts++;
duke@0 2302 }
duke@0 2303 }
duke@0 2304 }
duke@0 2305
duke@0 2306 assert(nof_conflicts == _nof_refval_conflicts, "sanity check");
duke@0 2307
duke@0 2308 // Adjust the number of locals
duke@0 2309 method()->set_max_locals(_max_locals+_nof_refval_conflicts);
duke@0 2310 _max_locals += _nof_refval_conflicts;
duke@0 2311
duke@0 2312 // That was that...
duke@0 2313 _new_var_map = NULL;
duke@0 2314 _nof_refval_conflicts = 0;
duke@0 2315 }
duke@0 2316
duke@0 2317 void GenerateOopMap::rewrite_refval_conflict(int from, int to) {
duke@0 2318 bool startOver;
duke@0 2319 do {
duke@0 2320 // Make sure that the BytecodeStream is constructed in the loop, since
duke@0 2321 // during rewriting a new method oop is going to be used, and the next time
duke@0 2322 // around we want to use that.
duke@0 2323 BytecodeStream bcs(_method);
duke@0 2324 startOver = false;
duke@0 2325
coleenp@3602 2326 while( !startOver && !_got_error &&
coleenp@3602 2327 // test bcs in case method changed and it became invalid
coleenp@3602 2328 bcs.next() >=0) {
duke@0 2329 startOver = rewrite_refval_conflict_inst(&bcs, from, to);
duke@0 2330 }
duke@0 2331 } while (startOver && !_got_error);
duke@0 2332 }
duke@0 2333
duke@0 2334 /* If the current instruction is one that uses local variable "from"
duke@0 2335 in a ref way, change it to use "to". There's a subtle reason why we
duke@0 2336 renumber the ref uses and not the non-ref uses: non-ref uses may be
duke@0 2337 2 slots wide (double, long) which would necessitate keeping track of
duke@0 2338 whether we should add one or two variables to the method. If the change
duke@0 2339 affected the width of some instruction, returns "TRUE"; otherwise, returns "FALSE".
duke@0 2340 Another reason for moving ref's value is for solving (addr, ref) conflicts, which
duke@0 2341 both uses aload/astore methods.
duke@0 2342 */
duke@0 2343 bool GenerateOopMap::rewrite_refval_conflict_inst(BytecodeStream *itr, int from, int to) {
duke@0 2344 Bytecodes::Code bc = itr->code();
duke@0 2345 int index;
duke@0 2346 int bci = itr->bci();
duke@0 2347
duke@0 2348 if (is_aload(itr, &index) && index == from) {
duke@0 2349 if (TraceOopMapRewrites) {
duke@0 2350 tty->print_cr("Rewriting aload at bci: %d", bci);
duke@0 2351 }
duke@0 2352 return rewrite_load_or_store(itr, Bytecodes::_aload, Bytecodes::_aload_0, to);
duke@0 2353 }
duke@0 2354
duke@0 2355 if (is_astore(itr, &index) && index == from) {
duke@0 2356 if (!stack_top_holds_ret_addr(bci)) {
duke@0 2357 if (TraceOopMapRewrites) {
duke@0 2358 tty->print_cr("Rewriting astore at bci: %d", bci);
duke@0 2359 }
duke@0 2360 return rewrite_load_or_store(itr, Bytecodes::_astore, Bytecodes::_astore_0, to);
duke@0 2361 } else {
duke@0 2362 if (TraceOopMapRewrites) {
duke@0 2363 tty->print_cr("Supress rewriting of astore at bci: %d", bci);
duke@0 2364 }
duke@0 2365 }
duke@0 2366 }
duke@0 2367
duke@0 2368 return false;
duke@0 2369 }
duke@0 2370
duke@0 2371 // The argument to this method is:
duke@0 2372 // bc : Current bytecode
duke@0 2373 // bcN : either _aload or _astore
duke@0 2374 // bc0 : either _aload_0 or _astore_0
duke@0 2375 bool GenerateOopMap::rewrite_load_or_store(BytecodeStream *bcs, Bytecodes::Code bcN, Bytecodes::Code bc0, unsigned int varNo) {
duke@0 2376 assert(bcN == Bytecodes::_astore || bcN == Bytecodes::_aload, "wrong argument (bcN)");
duke@0 2377 assert(bc0 == Bytecodes::_astore_0 || bc0 == Bytecodes::_aload_0, "wrong argument (bc0)");
never@2027 2378 int ilen = Bytecodes::length_at(_method(), bcs->bcp());
duke@0 2379 int newIlen;
duke@0 2380
duke@0 2381 if (ilen == 4) {
duke@0 2382 // Original instruction was wide; keep it wide for simplicity
duke@0 2383 newIlen = 4;
duke@0 2384 } else if (varNo < 4)
duke@0 2385 newIlen = 1;
duke@0 2386 else if (varNo >= 256)
duke@0 2387 newIlen = 4;
duke@0 2388 else
duke@0 2389 newIlen = 2;
duke@0 2390
duke@0 2391 // If we need to relocate in order to patch the byte, we
duke@0 2392 // do the patching in a temp. buffer, that is passed to the reloc.
duke@0 2393 // The patching of the bytecode stream is then done by the Relocator.
duke@0 2394 // This is neccesary, since relocating the instruction at a certain bci, might
duke@0 2395 // also relocate that instruction, e.g., if a _goto before it gets widen to a _goto_w.
duke@0 2396 // Hence, we do not know which bci to patch after relocation.
duke@0 2397
duke@0 2398 assert(newIlen <= 4, "sanity check");
duke@0 2399 u_char inst_buffer[4]; // Max. instruction size is 4.
duke@0 2400 address bcp;
duke@0 2401
duke@0 2402 if (newIlen != ilen) {
duke@0 2403 // Relocation needed do patching in temp. buffer
duke@0 2404 bcp = (address)inst_buffer;
duke@0 2405 } else {
duke@0 2406 bcp = _method->bcp_from(bcs->bci());
duke@0 2407 }
duke@0 2408
coleenp@3602 2409 // Patch either directly in Method* or in temp. buffer
duke@0 2410 if (newIlen == 1) {
duke@0 2411 assert(varNo < 4, "varNo too large");
duke@0 2412 *bcp = bc0 + varNo;
duke@0 2413 } else if (newIlen == 2) {
duke@0 2414 assert(varNo < 256, "2-byte index needed!");
duke@0 2415 *(bcp + 0) = bcN;
duke@0 2416 *(bcp + 1) = varNo;
duke@0 2417 } else {
duke@0 2418 assert(newIlen == 4, "Wrong instruction length");
duke@0 2419 *(bcp + 0) = Bytecodes::_wide;
duke@0 2420 *(bcp + 1) = bcN;
duke@0 2421 Bytes::put_Java_u2(bcp+2, varNo);
duke@0 2422 }
duke@0 2423
duke@0 2424 if (newIlen != ilen) {
duke@0 2425 expand_current_instr(bcs->bci(), ilen, newIlen, inst_buffer);
duke@0 2426 }
duke@0 2427
duke@0 2428
duke@0 2429 return (newIlen != ilen);
duke@0 2430 }
duke@0 2431
duke@0 2432 class RelocCallback : public RelocatorListener {
duke@0 2433 private:
duke@0 2434 GenerateOopMap* _gom;
duke@0 2435 public:
duke@0 2436 RelocCallback(GenerateOopMap* gom) { _gom = gom; };
duke@0 2437
duke@0 2438 // Callback method
duke@0 2439 virtual void relocated(int bci, int delta, int new_code_length) {
duke@0 2440 _gom->update_basic_blocks (bci, delta, new_code_length);
duke@0 2441 _gom->update_ret_adr_at_TOS(bci, delta);
duke@0 2442 _gom->_rt.update_ret_table (bci, delta);
duke@0 2443 }
duke@0 2444 };
duke@0 2445
duke@0 2446 // Returns true if expanding was succesful. Otherwise, reports an error and
duke@0 2447 // returns false.
duke@0 2448 void GenerateOopMap::expand_current_instr(int bci, int ilen, int newIlen, u_char inst_buffer[]) {
duke@0 2449 Thread *THREAD = Thread::current(); // Could really have TRAPS argument.
duke@0 2450 RelocCallback rcb(this);
duke@0 2451 Relocator rc(_method, &rcb);
duke@0 2452 methodHandle m= rc.insert_space_at(bci, newIlen, inst_buffer, THREAD);
duke@0 2453 if (m.is_null() || HAS_PENDING_EXCEPTION) {
duke@0 2454 report_error("could not rewrite method - exception occurred or bytecode buffer overflow");
duke@0 2455 return;
duke@0 2456 }
duke@0 2457
duke@0 2458 // Relocator returns a new method oop.
duke@0 2459 _did_relocation = true;
duke@0 2460 _method = m;
duke@0 2461 }
duke@0 2462
duke@0 2463
duke@0 2464 bool GenerateOopMap::is_astore(BytecodeStream *itr, int *index) {
duke@0 2465 Bytecodes::Code bc = itr->code();
duke@0 2466 switch(bc) {
duke@0 2467 case Bytecodes::_astore_0:
duke@0 2468 case Bytecodes::_astore_1:
duke@0 2469 case Bytecodes::_astore_2:
duke@0 2470 case Bytecodes::_astore_3:
duke@0 2471 *index = bc - Bytecodes::_astore_0;
duke@0 2472 return true;
duke@0 2473 case Bytecodes::_astore:
duke@0 2474 *index = itr->get_index();
duke@0 2475 return true;
duke@0 2476 }
duke@0 2477 return false;
duke@0 2478 }
duke@0 2479
duke@0 2480 bool GenerateOopMap::is_aload(BytecodeStream *itr, int *index) {
duke@0 2481 Bytecodes::Code bc = itr->code();
duke@0 2482 switch(bc) {
duke@0 2483 case Bytecodes::_aload_0:
duke@0 2484 case Bytecodes::_aload_1:
duke@0 2485 case Bytecodes::_aload_2:
duke@0 2486 case Bytecodes::_aload_3:
duke@0 2487 *index = bc - Bytecodes::_aload_0;
duke@0 2488 return true;
duke@0 2489
duke@0 2490 case Bytecodes::_aload:
duke@0 2491 *index = itr->get_index();
duke@0 2492 return true;
duke@0 2493 }
duke@0 2494 return false;
duke@0 2495 }
duke@0 2496
duke@0 2497
duke@0 2498 // Return true iff the top of the operand stack holds a return address at
duke@0 2499 // the current instruction
duke@0 2500 bool GenerateOopMap::stack_top_holds_ret_addr(int bci) {
duke@0 2501 for(int i = 0; i < _ret_adr_tos->length(); i++) {
duke@0 2502 if (_ret_adr_tos->at(i) == bci)
duke@0 2503 return true;
duke@0 2504 }
duke@0 2505
duke@0 2506 return false;
duke@0 2507 }
duke@0 2508
duke@0 2509 void GenerateOopMap::compute_ret_adr_at_TOS() {
duke@0 2510 assert(_ret_adr_tos != NULL, "must be initialized");
duke@0 2511 _ret_adr_tos->clear();
duke@0 2512
duke@0 2513 for (int i = 0; i < bb_count(); i++) {
duke@0 2514 BasicBlock* bb = &_basic_blocks[i];
duke@0 2515
duke@0 2516 // Make sure to only check basicblocks that are reachable
duke@0 2517 if (bb->is_reachable()) {
duke@0 2518
duke@0 2519 // For each Basic block we check all instructions
duke@0 2520 BytecodeStream bcs(_method);
duke@0 2521 bcs.set_interval(bb->_bci, next_bb_start_pc(bb));
duke@0 2522
duke@0 2523 restore_state(bb);
duke@0 2524
duke@0 2525 while (bcs.next()>=0 && !_got_error) {
duke@0 2526 // TDT: should this be is_good_address() ?
duke@0 2527 if (_stack_top > 0 && stack()[_stack_top-1].is_address()) {
duke@0 2528 _ret_adr_tos->append(bcs.bci());
duke@0 2529 if (TraceNewOopMapGeneration) {
duke@0 2530 tty->print_cr("Ret_adr TOS at bci: %d", bcs.bci());
duke@0 2531 }
duke@0 2532 }
duke@0 2533 interp1(&bcs);
duke@0 2534 }
duke@0 2535 }
duke@0 2536 }
duke@0 2537 }
duke@0 2538
duke@0 2539 void GenerateOopMap::update_ret_adr_at_TOS(int bci, int delta) {
duke@0 2540 for(int i = 0; i < _ret_adr_tos->length(); i++) {
duke@0 2541 int v = _ret_adr_tos->at(i);
duke@0 2542 if (v > bci) _ret_adr_tos->at_put(i, v + delta);
duke@0 2543 }
duke@0 2544 }
duke@0 2545
duke@0 2546 // ===================================================================
duke@0 2547
duke@0 2548 #ifndef PRODUCT
duke@0 2549 int ResolveOopMapConflicts::_nof_invocations = 0;
duke@0 2550 int ResolveOopMapConflicts::_nof_rewrites = 0;
duke@0 2551 int ResolveOopMapConflicts::_nof_relocations = 0;
duke@0 2552 #endif
duke@0 2553
duke@0 2554 methodHandle ResolveOopMapConflicts::do_potential_rewrite(TRAPS) {
duke@0 2555 compute_map(CHECK_(methodHandle()));
duke@0 2556
duke@0 2557 #ifndef PRODUCT
duke@0 2558 // Tracking and statistics
duke@0 2559 if (PrintRewrites) {
duke@0 2560 _nof_invocations++;
duke@0 2561 if (did_rewriting()) {
duke@0 2562 _nof_rewrites++;
duke@0 2563 if (did_relocation()) _nof_relocations++;
duke@0 2564 tty->print("Method was rewritten %s: ", (did_relocation()) ? "and relocated" : "");
duke@0 2565 method()->print_value(); tty->cr();
duke@0 2566 tty->print_cr("Cand.: %d rewrts: %d (%d%%) reloc.: %d (%d%%)",
duke@0 2567 _nof_invocations,
duke@0 2568 _nof_rewrites, (_nof_rewrites * 100) / _nof_invocations,
duke@0 2569 _nof_relocations, (_nof_relocations * 100) / _nof_invocations);
duke@0 2570 }
duke@0 2571 }
duke@0 2572 #endif
duke@0 2573 return methodHandle(THREAD, method());
duke@0 2574 }