src/share/vm/oops/generateOopMap.cpp

Wed, 27 Apr 2016 01:25:04 +0800

author
aoqi
date
Wed, 27 Apr 2016 01:25:04 +0800
changeset 0
f90c822e73f8
child 6876
710a3c8b516e
permissions
-rw-r--r--

Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/hotspot/
changeset: 6782:28b50d07f6f8
tag: jdk8u25-b17

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

mercurial