aoqi@0: /* aoqi@0: * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: * aoqi@0: */ aoqi@0: aoqi@0: #include "precompiled.hpp" aoqi@0: #include "interpreter/bytecodeStream.hpp" aoqi@0: #include "oops/generateOopMap.hpp" aoqi@0: #include "oops/oop.inline.hpp" aoqi@0: #include "oops/symbol.hpp" aoqi@0: #include "runtime/handles.inline.hpp" aoqi@0: #include "runtime/java.hpp" aoqi@0: #include "runtime/relocator.hpp" aoqi@0: #include "utilities/bitMap.inline.hpp" aoqi@0: #include "prims/methodHandles.hpp" aoqi@0: aoqi@0: // aoqi@0: // aoqi@0: // Compute stack layouts for each instruction in method. aoqi@0: // aoqi@0: // Problems: aoqi@0: // - What to do about jsr with different types of local vars? aoqi@0: // Need maps that are conditional on jsr path? aoqi@0: // - Jsr and exceptions should be done more efficiently (the retAddr stuff) aoqi@0: // aoqi@0: // Alternative: aoqi@0: // - Could extend verifier to provide this information. aoqi@0: // For: one fewer abstract interpreter to maintain. Against: the verifier aoqi@0: // solves a bigger problem so slower (undesirable to force verification of aoqi@0: // everything?). aoqi@0: // aoqi@0: // Algorithm: aoqi@0: // Partition bytecodes into basic blocks aoqi@0: // For each basic block: store entry state (vars, stack). For instructions aoqi@0: // inside basic blocks we do not store any state (instead we recompute it aoqi@0: // from state produced by previous instruction). aoqi@0: // aoqi@0: // Perform abstract interpretation of bytecodes over this lattice: aoqi@0: // aoqi@0: // _--'#'--_ aoqi@0: // / / \ \ aoqi@0: // / / \ \ aoqi@0: // / | | \ aoqi@0: // 'r' 'v' 'p' ' ' aoqi@0: // \ | | / aoqi@0: // \ \ / / aoqi@0: // \ \ / / aoqi@0: // -- '@' -- aoqi@0: // aoqi@0: // '#' top, result of conflict merge aoqi@0: // 'r' reference type aoqi@0: // 'v' value type aoqi@0: // 'p' pc type for jsr/ret aoqi@0: // ' ' uninitialized; never occurs on operand stack in Java aoqi@0: // '@' bottom/unexecuted; initial state each bytecode. aoqi@0: // aoqi@0: // Basic block headers are the only merge points. We use this iteration to aoqi@0: // compute the information: aoqi@0: // aoqi@0: // find basic blocks; aoqi@0: // initialize them with uninitialized state; aoqi@0: // initialize first BB according to method signature; aoqi@0: // mark first BB changed aoqi@0: // while (some BB is changed) do { aoqi@0: // perform abstract interpration of all bytecodes in BB; aoqi@0: // merge exit state of BB into entry state of all successor BBs, aoqi@0: // noting if any of these change; aoqi@0: // } aoqi@0: // aoqi@0: // One additional complication is necessary. The jsr instruction pushes aoqi@0: // a return PC on the stack (a 'p' type in the abstract interpretation). aoqi@0: // To be able to process "ret" bytecodes, we keep track of these return aoqi@0: // PC's in a 'retAddrs' structure in abstract interpreter context (when aoqi@0: // processing a "ret" bytecodes, it is not sufficient to know that it gets aoqi@0: // an argument of the right type 'p'; we need to know which address it aoqi@0: // returns to). aoqi@0: // aoqi@0: // (Note this comment is borrowed form the original author of the algorithm) aoqi@0: aoqi@0: // ComputeCallStack aoqi@0: // aoqi@0: // Specialization of SignatureIterator - compute the effects of a call aoqi@0: // aoqi@0: class ComputeCallStack : public SignatureIterator { aoqi@0: CellTypeState *_effect; aoqi@0: int _idx; aoqi@0: aoqi@0: void setup(); aoqi@0: void set(CellTypeState state) { _effect[_idx++] = state; } aoqi@0: int length() { return _idx; }; aoqi@0: aoqi@0: virtual void do_bool () { set(CellTypeState::value); }; aoqi@0: virtual void do_char () { set(CellTypeState::value); }; aoqi@0: virtual void do_float () { set(CellTypeState::value); }; aoqi@0: virtual void do_byte () { set(CellTypeState::value); }; aoqi@0: virtual void do_short () { set(CellTypeState::value); }; aoqi@0: virtual void do_int () { set(CellTypeState::value); }; aoqi@0: virtual void do_void () { set(CellTypeState::bottom);}; aoqi@0: virtual void do_object(int begin, int end) { set(CellTypeState::ref); }; aoqi@0: virtual void do_array (int begin, int end) { set(CellTypeState::ref); }; aoqi@0: aoqi@0: void do_double() { set(CellTypeState::value); aoqi@0: set(CellTypeState::value); } aoqi@0: void do_long () { set(CellTypeState::value); aoqi@0: set(CellTypeState::value); } aoqi@0: aoqi@0: public: aoqi@0: ComputeCallStack(Symbol* signature) : SignatureIterator(signature) {}; aoqi@0: aoqi@0: // Compute methods aoqi@0: int compute_for_parameters(bool is_static, CellTypeState *effect) { aoqi@0: _idx = 0; aoqi@0: _effect = effect; aoqi@0: aoqi@0: if (!is_static) aoqi@0: effect[_idx++] = CellTypeState::ref; aoqi@0: aoqi@0: iterate_parameters(); aoqi@0: aoqi@0: return length(); aoqi@0: }; aoqi@0: aoqi@0: int compute_for_returntype(CellTypeState *effect) { aoqi@0: _idx = 0; aoqi@0: _effect = effect; aoqi@0: iterate_returntype(); aoqi@0: set(CellTypeState::bottom); // Always terminate with a bottom state, so ppush works aoqi@0: aoqi@0: return length(); aoqi@0: } aoqi@0: }; aoqi@0: aoqi@0: //========================================================================================= aoqi@0: // ComputeEntryStack aoqi@0: // aoqi@0: // Specialization of SignatureIterator - in order to set up first stack frame aoqi@0: // aoqi@0: class ComputeEntryStack : public SignatureIterator { aoqi@0: CellTypeState *_effect; aoqi@0: int _idx; aoqi@0: aoqi@0: void setup(); aoqi@0: void set(CellTypeState state) { _effect[_idx++] = state; } aoqi@0: int length() { return _idx; }; aoqi@0: aoqi@0: virtual void do_bool () { set(CellTypeState::value); }; aoqi@0: virtual void do_char () { set(CellTypeState::value); }; aoqi@0: virtual void do_float () { set(CellTypeState::value); }; aoqi@0: virtual void do_byte () { set(CellTypeState::value); }; aoqi@0: virtual void do_short () { set(CellTypeState::value); }; aoqi@0: virtual void do_int () { set(CellTypeState::value); }; aoqi@0: virtual void do_void () { set(CellTypeState::bottom);}; aoqi@0: virtual void do_object(int begin, int end) { set(CellTypeState::make_slot_ref(_idx)); } aoqi@0: virtual void do_array (int begin, int end) { set(CellTypeState::make_slot_ref(_idx)); } aoqi@0: aoqi@0: void do_double() { set(CellTypeState::value); aoqi@0: set(CellTypeState::value); } aoqi@0: void do_long () { set(CellTypeState::value); aoqi@0: set(CellTypeState::value); } aoqi@0: aoqi@0: public: aoqi@0: ComputeEntryStack(Symbol* signature) : SignatureIterator(signature) {}; aoqi@0: aoqi@0: // Compute methods aoqi@0: int compute_for_parameters(bool is_static, CellTypeState *effect) { aoqi@0: _idx = 0; aoqi@0: _effect = effect; aoqi@0: aoqi@0: if (!is_static) aoqi@0: effect[_idx++] = CellTypeState::make_slot_ref(0); aoqi@0: aoqi@0: iterate_parameters(); aoqi@0: aoqi@0: return length(); aoqi@0: }; aoqi@0: aoqi@0: int compute_for_returntype(CellTypeState *effect) { aoqi@0: _idx = 0; aoqi@0: _effect = effect; aoqi@0: iterate_returntype(); aoqi@0: set(CellTypeState::bottom); // Always terminate with a bottom state, so ppush works aoqi@0: aoqi@0: return length(); aoqi@0: } aoqi@0: }; aoqi@0: aoqi@0: //===================================================================================== aoqi@0: // aoqi@0: // Implementation of RetTable/RetTableEntry aoqi@0: // aoqi@0: // Contains function to itereate through all bytecodes aoqi@0: // and find all return entry points aoqi@0: // aoqi@0: int RetTable::_init_nof_entries = 10; aoqi@0: int RetTableEntry::_init_nof_jsrs = 5; aoqi@0: aoqi@0: void RetTableEntry::add_delta(int bci, int delta) { aoqi@0: if (_target_bci > bci) _target_bci += delta; aoqi@0: aoqi@0: for (int k = 0; k < _jsrs->length(); k++) { aoqi@0: int jsr = _jsrs->at(k); aoqi@0: if (jsr > bci) _jsrs->at_put(k, jsr+delta); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: void RetTable::compute_ret_table(methodHandle method) { aoqi@0: BytecodeStream i(method); aoqi@0: Bytecodes::Code bytecode; aoqi@0: aoqi@0: while( (bytecode = i.next()) >= 0) { aoqi@0: switch (bytecode) { aoqi@0: case Bytecodes::_jsr: aoqi@0: add_jsr(i.next_bci(), i.dest()); aoqi@0: break; aoqi@0: case Bytecodes::_jsr_w: aoqi@0: add_jsr(i.next_bci(), i.dest_w()); aoqi@0: break; aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: void RetTable::add_jsr(int return_bci, int target_bci) { aoqi@0: RetTableEntry* entry = _first; aoqi@0: aoqi@0: // Scan table for entry aoqi@0: for (;entry && entry->target_bci() != target_bci; entry = entry->next()); aoqi@0: aoqi@0: if (!entry) { aoqi@0: // Allocate new entry and put in list aoqi@0: entry = new RetTableEntry(target_bci, _first); aoqi@0: _first = entry; aoqi@0: } aoqi@0: aoqi@0: // Now "entry" is set. Make sure that the entry is initialized aoqi@0: // and has room for the new jsr. aoqi@0: entry->add_jsr(return_bci); aoqi@0: } aoqi@0: aoqi@0: RetTableEntry* RetTable::find_jsrs_for_target(int targBci) { aoqi@0: RetTableEntry *cur = _first; aoqi@0: aoqi@0: while(cur) { aoqi@0: assert(cur->target_bci() != -1, "sanity check"); aoqi@0: if (cur->target_bci() == targBci) return cur; aoqi@0: cur = cur->next(); aoqi@0: } aoqi@0: ShouldNotReachHere(); aoqi@0: return NULL; aoqi@0: } aoqi@0: aoqi@0: // The instruction at bci is changing size by "delta". Update the return map. aoqi@0: void RetTable::update_ret_table(int bci, int delta) { aoqi@0: RetTableEntry *cur = _first; aoqi@0: while(cur) { aoqi@0: cur->add_delta(bci, delta); aoqi@0: cur = cur->next(); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // aoqi@0: // Celltype state aoqi@0: // aoqi@0: aoqi@0: CellTypeState CellTypeState::bottom = CellTypeState::make_bottom(); aoqi@0: CellTypeState CellTypeState::uninit = CellTypeState::make_any(uninit_value); aoqi@0: CellTypeState CellTypeState::ref = CellTypeState::make_any(ref_conflict); aoqi@0: CellTypeState CellTypeState::value = CellTypeState::make_any(val_value); aoqi@0: CellTypeState CellTypeState::refUninit = CellTypeState::make_any(ref_conflict | uninit_value); aoqi@0: CellTypeState CellTypeState::top = CellTypeState::make_top(); aoqi@0: CellTypeState CellTypeState::addr = CellTypeState::make_any(addr_conflict); aoqi@0: aoqi@0: // Commonly used constants aoqi@0: static CellTypeState epsilonCTS[1] = { CellTypeState::bottom }; aoqi@0: static CellTypeState refCTS = CellTypeState::ref; aoqi@0: static CellTypeState valCTS = CellTypeState::value; aoqi@0: static CellTypeState vCTS[2] = { CellTypeState::value, CellTypeState::bottom }; aoqi@0: static CellTypeState rCTS[2] = { CellTypeState::ref, CellTypeState::bottom }; aoqi@0: static CellTypeState rrCTS[3] = { CellTypeState::ref, CellTypeState::ref, CellTypeState::bottom }; aoqi@0: static CellTypeState vrCTS[3] = { CellTypeState::value, CellTypeState::ref, CellTypeState::bottom }; aoqi@0: static CellTypeState vvCTS[3] = { CellTypeState::value, CellTypeState::value, CellTypeState::bottom }; aoqi@0: static CellTypeState rvrCTS[4] = { CellTypeState::ref, CellTypeState::value, CellTypeState::ref, CellTypeState::bottom }; aoqi@0: static CellTypeState vvrCTS[4] = { CellTypeState::value, CellTypeState::value, CellTypeState::ref, CellTypeState::bottom }; aoqi@0: static CellTypeState vvvCTS[4] = { CellTypeState::value, CellTypeState::value, CellTypeState::value, CellTypeState::bottom }; aoqi@0: static CellTypeState vvvrCTS[5] = { CellTypeState::value, CellTypeState::value, CellTypeState::value, CellTypeState::ref, CellTypeState::bottom }; aoqi@0: static CellTypeState vvvvCTS[5] = { CellTypeState::value, CellTypeState::value, CellTypeState::value, CellTypeState::value, CellTypeState::bottom }; aoqi@0: aoqi@0: char CellTypeState::to_char() const { aoqi@0: if (can_be_reference()) { aoqi@0: if (can_be_value() || can_be_address()) aoqi@0: return '#'; // Conflict that needs to be rewritten aoqi@0: else aoqi@0: return 'r'; aoqi@0: } else if (can_be_value()) aoqi@0: return 'v'; aoqi@0: else if (can_be_address()) aoqi@0: return 'p'; aoqi@0: else if (can_be_uninit()) aoqi@0: return ' '; aoqi@0: else aoqi@0: return '@'; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: // Print a detailed CellTypeState. Indicate all bits that are set. If aoqi@0: // the CellTypeState represents an address or a reference, print the aoqi@0: // value of the additional information. aoqi@0: void CellTypeState::print(outputStream *os) { aoqi@0: if (can_be_address()) { aoqi@0: os->print("(p"); aoqi@0: } else { aoqi@0: os->print("( "); aoqi@0: } aoqi@0: if (can_be_reference()) { aoqi@0: os->print("r"); aoqi@0: } else { aoqi@0: os->print(" "); aoqi@0: } aoqi@0: if (can_be_value()) { aoqi@0: os->print("v"); aoqi@0: } else { aoqi@0: os->print(" "); aoqi@0: } aoqi@0: if (can_be_uninit()) { aoqi@0: os->print("u|"); aoqi@0: } else { aoqi@0: os->print(" |"); aoqi@0: } aoqi@0: if (is_info_top()) { aoqi@0: os->print("Top)"); aoqi@0: } else if (is_info_bottom()) { aoqi@0: os->print("Bot)"); aoqi@0: } else { aoqi@0: if (is_reference()) { aoqi@0: int info = get_info(); aoqi@0: int data = info & ~(ref_not_lock_bit | ref_slot_bit); aoqi@0: if (info & ref_not_lock_bit) { aoqi@0: // Not a monitor lock reference. aoqi@0: if (info & ref_slot_bit) { aoqi@0: // slot aoqi@0: os->print("slot%d)", data); aoqi@0: } else { aoqi@0: // line aoqi@0: os->print("line%d)", data); aoqi@0: } aoqi@0: } else { aoqi@0: // lock aoqi@0: os->print("lock%d)", data); aoqi@0: } aoqi@0: } else { aoqi@0: os->print("%d)", get_info()); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // aoqi@0: // Basicblock handling methods aoqi@0: // aoqi@0: aoqi@0: void GenerateOopMap ::initialize_bb() { aoqi@0: _gc_points = 0; aoqi@0: _bb_count = 0; aoqi@0: _bb_hdr_bits.clear(); aoqi@0: _bb_hdr_bits.resize(method()->code_size()); aoqi@0: } aoqi@0: aoqi@0: void GenerateOopMap::bb_mark_fct(GenerateOopMap *c, int bci, int *data) { aoqi@0: assert(bci>= 0 && bci < c->method()->code_size(), "index out of bounds"); aoqi@0: if (c->is_bb_header(bci)) aoqi@0: return; aoqi@0: aoqi@0: if (TraceNewOopMapGeneration) { aoqi@0: tty->print_cr("Basicblock#%d begins at: %d", c->_bb_count, bci); aoqi@0: } aoqi@0: c->set_bbmark_bit(bci); aoqi@0: c->_bb_count++; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void GenerateOopMap::mark_bbheaders_and_count_gc_points() { aoqi@0: initialize_bb(); aoqi@0: aoqi@0: bool fellThrough = false; // False to get first BB marked. aoqi@0: aoqi@0: // First mark all exception handlers as start of a basic-block aoqi@0: ExceptionTable excps(method()); aoqi@0: for(int i = 0; i < excps.length(); i ++) { aoqi@0: bb_mark_fct(this, excps.handler_pc(i), NULL); aoqi@0: } aoqi@0: aoqi@0: // Then iterate through the code aoqi@0: BytecodeStream bcs(_method); aoqi@0: Bytecodes::Code bytecode; aoqi@0: aoqi@0: while( (bytecode = bcs.next()) >= 0) { aoqi@0: int bci = bcs.bci(); aoqi@0: aoqi@0: if (!fellThrough) aoqi@0: bb_mark_fct(this, bci, NULL); aoqi@0: aoqi@0: fellThrough = jump_targets_do(&bcs, &GenerateOopMap::bb_mark_fct, NULL); aoqi@0: aoqi@0: /* We will also mark successors of jsr's as basic block headers. */ aoqi@0: switch (bytecode) { aoqi@0: case Bytecodes::_jsr: aoqi@0: assert(!fellThrough, "should not happen"); aoqi@0: bb_mark_fct(this, bci + Bytecodes::length_for(bytecode), NULL); aoqi@0: break; aoqi@0: case Bytecodes::_jsr_w: aoqi@0: assert(!fellThrough, "should not happen"); aoqi@0: bb_mark_fct(this, bci + Bytecodes::length_for(bytecode), NULL); aoqi@0: break; aoqi@0: } aoqi@0: aoqi@0: if (possible_gc_point(&bcs)) aoqi@0: _gc_points++; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: void GenerateOopMap::reachable_basicblock(GenerateOopMap *c, int bci, int *data) { aoqi@0: assert(bci>= 0 && bci < c->method()->code_size(), "index out of bounds"); aoqi@0: BasicBlock* bb = c->get_basic_block_at(bci); aoqi@0: if (bb->is_dead()) { aoqi@0: bb->mark_as_alive(); aoqi@0: *data = 1; // Mark basicblock as changed aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void GenerateOopMap::mark_reachable_code() { aoqi@0: int change = 1; // int to get function pointers to work aoqi@0: aoqi@0: // Mark entry basic block as alive and all exception handlers aoqi@0: _basic_blocks[0].mark_as_alive(); aoqi@0: ExceptionTable excps(method()); aoqi@0: for(int i = 0; i < excps.length(); i++) { aoqi@0: BasicBlock *bb = get_basic_block_at(excps.handler_pc(i)); aoqi@0: // If block is not already alive (due to multiple exception handlers to same bb), then aoqi@0: // make it alive aoqi@0: if (bb->is_dead()) bb->mark_as_alive(); aoqi@0: } aoqi@0: aoqi@0: BytecodeStream bcs(_method); aoqi@0: aoqi@0: // Iterate through all basic blocks until we reach a fixpoint aoqi@0: while (change) { aoqi@0: change = 0; aoqi@0: aoqi@0: for (int i = 0; i < _bb_count; i++) { aoqi@0: BasicBlock *bb = &_basic_blocks[i]; aoqi@0: if (bb->is_alive()) { aoqi@0: // Position bytecodestream at last bytecode in basicblock aoqi@0: bcs.set_start(bb->_end_bci); aoqi@0: bcs.next(); aoqi@0: Bytecodes::Code bytecode = bcs.code(); aoqi@0: int bci = bcs.bci(); aoqi@0: assert(bci == bb->_end_bci, "wrong bci"); aoqi@0: aoqi@0: bool fell_through = jump_targets_do(&bcs, &GenerateOopMap::reachable_basicblock, &change); aoqi@0: aoqi@0: // We will also mark successors of jsr's as alive. aoqi@0: switch (bytecode) { aoqi@0: case Bytecodes::_jsr: aoqi@0: case Bytecodes::_jsr_w: aoqi@0: assert(!fell_through, "should not happen"); aoqi@0: reachable_basicblock(this, bci + Bytecodes::length_for(bytecode), &change); aoqi@0: break; aoqi@0: } aoqi@0: if (fell_through) { aoqi@0: // Mark successor as alive aoqi@0: if (bb[1].is_dead()) { aoqi@0: bb[1].mark_as_alive(); aoqi@0: change = 1; aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: /* If the current instruction in "c" has no effect on control flow, aoqi@0: returns "true". Otherwise, calls "jmpFct" one or more times, with aoqi@0: "c", an appropriate "pcDelta", and "data" as arguments, then aoqi@0: returns "false". There is one exception: if the current aoqi@0: instruction is a "ret", returns "false" without calling "jmpFct". aoqi@0: Arrangements for tracking the control flow of a "ret" must be made aoqi@0: externally. */ aoqi@0: bool GenerateOopMap::jump_targets_do(BytecodeStream *bcs, jmpFct_t jmpFct, int *data) { aoqi@0: int bci = bcs->bci(); aoqi@0: aoqi@0: switch (bcs->code()) { aoqi@0: case Bytecodes::_ifeq: aoqi@0: case Bytecodes::_ifne: aoqi@0: case Bytecodes::_iflt: aoqi@0: case Bytecodes::_ifge: aoqi@0: case Bytecodes::_ifgt: aoqi@0: case Bytecodes::_ifle: aoqi@0: case Bytecodes::_if_icmpeq: aoqi@0: case Bytecodes::_if_icmpne: aoqi@0: case Bytecodes::_if_icmplt: aoqi@0: case Bytecodes::_if_icmpge: aoqi@0: case Bytecodes::_if_icmpgt: aoqi@0: case Bytecodes::_if_icmple: aoqi@0: case Bytecodes::_if_acmpeq: aoqi@0: case Bytecodes::_if_acmpne: aoqi@0: case Bytecodes::_ifnull: aoqi@0: case Bytecodes::_ifnonnull: aoqi@0: (*jmpFct)(this, bcs->dest(), data); aoqi@0: (*jmpFct)(this, bci + 3, data); aoqi@0: break; aoqi@0: aoqi@0: case Bytecodes::_goto: aoqi@0: (*jmpFct)(this, bcs->dest(), data); aoqi@0: break; aoqi@0: case Bytecodes::_goto_w: aoqi@0: (*jmpFct)(this, bcs->dest_w(), data); aoqi@0: break; aoqi@0: case Bytecodes::_tableswitch: aoqi@0: { Bytecode_tableswitch tableswitch(method(), bcs->bcp()); aoqi@0: int len = tableswitch.length(); aoqi@0: aoqi@0: (*jmpFct)(this, bci + tableswitch.default_offset(), data); /* Default. jump address */ aoqi@0: while (--len >= 0) { aoqi@0: (*jmpFct)(this, bci + tableswitch.dest_offset_at(len), data); aoqi@0: } aoqi@0: break; aoqi@0: } aoqi@0: aoqi@0: case Bytecodes::_lookupswitch: aoqi@0: { Bytecode_lookupswitch lookupswitch(method(), bcs->bcp()); aoqi@0: int npairs = lookupswitch.number_of_pairs(); aoqi@0: (*jmpFct)(this, bci + lookupswitch.default_offset(), data); /* Default. */ aoqi@0: while(--npairs >= 0) { aoqi@0: LookupswitchPair pair = lookupswitch.pair_at(npairs); aoqi@0: (*jmpFct)(this, bci + pair.offset(), data); aoqi@0: } aoqi@0: break; aoqi@0: } aoqi@0: case Bytecodes::_jsr: aoqi@0: assert(bcs->is_wide()==false, "sanity check"); aoqi@0: (*jmpFct)(this, bcs->dest(), data); aoqi@0: aoqi@0: aoqi@0: aoqi@0: break; aoqi@0: case Bytecodes::_jsr_w: aoqi@0: (*jmpFct)(this, bcs->dest_w(), data); aoqi@0: break; aoqi@0: case Bytecodes::_wide: aoqi@0: ShouldNotReachHere(); aoqi@0: return true; aoqi@0: break; aoqi@0: case Bytecodes::_athrow: aoqi@0: case Bytecodes::_ireturn: aoqi@0: case Bytecodes::_lreturn: aoqi@0: case Bytecodes::_freturn: aoqi@0: case Bytecodes::_dreturn: aoqi@0: case Bytecodes::_areturn: aoqi@0: case Bytecodes::_return: aoqi@0: case Bytecodes::_ret: aoqi@0: break; aoqi@0: default: aoqi@0: return true; aoqi@0: } aoqi@0: return false; aoqi@0: } aoqi@0: aoqi@0: /* Requires "pc" to be the head of a basic block; returns that basic aoqi@0: block. */ aoqi@0: BasicBlock *GenerateOopMap::get_basic_block_at(int bci) const { aoqi@0: BasicBlock* bb = get_basic_block_containing(bci); aoqi@0: assert(bb->_bci == bci, "should have found BB"); aoqi@0: return bb; aoqi@0: } aoqi@0: aoqi@0: // Requires "pc" to be the start of an instruction; returns the basic aoqi@0: // block containing that instruction. */ aoqi@0: BasicBlock *GenerateOopMap::get_basic_block_containing(int bci) const { aoqi@0: BasicBlock *bbs = _basic_blocks; aoqi@0: int lo = 0, hi = _bb_count - 1; aoqi@0: aoqi@0: while (lo <= hi) { aoqi@0: int m = (lo + hi) / 2; aoqi@0: int mbci = bbs[m]._bci; aoqi@0: int nbci; aoqi@0: aoqi@0: if ( m == _bb_count-1) { aoqi@0: assert( bci >= mbci && bci < method()->code_size(), "sanity check failed"); aoqi@0: return bbs+m; aoqi@0: } else { aoqi@0: nbci = bbs[m+1]._bci; aoqi@0: } aoqi@0: aoqi@0: if ( mbci <= bci && bci < nbci) { aoqi@0: return bbs+m; aoqi@0: } else if (mbci < bci) { aoqi@0: lo = m + 1; aoqi@0: } else { aoqi@0: assert(mbci > bci, "sanity check"); aoqi@0: hi = m - 1; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: fatal("should have found BB"); aoqi@0: return NULL; aoqi@0: } aoqi@0: aoqi@0: void GenerateOopMap::restore_state(BasicBlock *bb) aoqi@0: { aoqi@0: memcpy(_state, bb->_state, _state_len*sizeof(CellTypeState)); aoqi@0: _stack_top = bb->_stack_top; aoqi@0: _monitor_top = bb->_monitor_top; aoqi@0: } aoqi@0: aoqi@0: int GenerateOopMap::next_bb_start_pc(BasicBlock *bb) { aoqi@0: int bbNum = bb - _basic_blocks + 1; aoqi@0: if (bbNum == _bb_count) aoqi@0: return method()->code_size(); aoqi@0: aoqi@0: return _basic_blocks[bbNum]._bci; aoqi@0: } aoqi@0: aoqi@0: // aoqi@0: // CellType handling methods aoqi@0: // aoqi@0: aoqi@0: // Allocate memory and throw LinkageError if failure. aoqi@0: #define ALLOC_RESOURCE_ARRAY(var, type, count) \ aoqi@0: var = NEW_RESOURCE_ARRAY_RETURN_NULL(type, count); \ aoqi@0: if (var == NULL) { \ aoqi@0: report_error("Cannot reserve enough memory to analyze this method"); \ aoqi@0: return; \ aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void GenerateOopMap::init_state() { aoqi@0: _state_len = _max_locals + _max_stack + _max_monitors; aoqi@0: ALLOC_RESOURCE_ARRAY(_state, CellTypeState, _state_len); aoqi@0: memset(_state, 0, _state_len * sizeof(CellTypeState)); aoqi@0: int count = MAX3(_max_locals, _max_stack, _max_monitors) + 1/*for null terminator char */; aoqi@0: ALLOC_RESOURCE_ARRAY(_state_vec_buf, char, count); aoqi@0: } aoqi@0: aoqi@0: void GenerateOopMap::make_context_uninitialized() { aoqi@0: CellTypeState* vs = vars(); aoqi@0: aoqi@0: for (int i = 0; i < _max_locals; i++) aoqi@0: vs[i] = CellTypeState::uninit; aoqi@0: aoqi@0: _stack_top = 0; aoqi@0: _monitor_top = 0; aoqi@0: } aoqi@0: aoqi@0: int GenerateOopMap::methodsig_to_effect(Symbol* signature, bool is_static, CellTypeState* effect) { aoqi@0: ComputeEntryStack ces(signature); aoqi@0: return ces.compute_for_parameters(is_static, effect); aoqi@0: } aoqi@0: aoqi@0: // Return result of merging cts1 and cts2. aoqi@0: CellTypeState CellTypeState::merge(CellTypeState cts, int slot) const { aoqi@0: CellTypeState result; aoqi@0: aoqi@0: assert(!is_bottom() && !cts.is_bottom(), aoqi@0: "merge of bottom values is handled elsewhere"); aoqi@0: aoqi@0: result._state = _state | cts._state; aoqi@0: aoqi@0: // If the top bit is set, we don't need to do any more work. aoqi@0: if (!result.is_info_top()) { aoqi@0: assert((result.can_be_address() || result.can_be_reference()), aoqi@0: "only addresses and references have non-top info"); aoqi@0: aoqi@0: if (!equal(cts)) { aoqi@0: // The two values being merged are different. Raise to top. aoqi@0: if (result.is_reference()) { aoqi@0: result = CellTypeState::make_slot_ref(slot); aoqi@0: } else { aoqi@0: result._state |= info_conflict; aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: assert(result.is_valid_state(), "checking that CTS merge maintains legal state"); aoqi@0: aoqi@0: return result; aoqi@0: } aoqi@0: aoqi@0: // Merge the variable state for locals and stack from cts into bbts. aoqi@0: bool GenerateOopMap::merge_local_state_vectors(CellTypeState* cts, aoqi@0: CellTypeState* bbts) { aoqi@0: int i; aoqi@0: int len = _max_locals + _stack_top; aoqi@0: bool change = false; aoqi@0: aoqi@0: for (i = len - 1; i >= 0; i--) { aoqi@0: CellTypeState v = cts[i].merge(bbts[i], i); aoqi@0: change = change || !v.equal(bbts[i]); aoqi@0: bbts[i] = v; aoqi@0: } aoqi@0: aoqi@0: return change; aoqi@0: } aoqi@0: aoqi@0: // Merge the monitor stack state from cts into bbts. aoqi@0: bool GenerateOopMap::merge_monitor_state_vectors(CellTypeState* cts, aoqi@0: CellTypeState* bbts) { aoqi@0: bool change = false; aoqi@0: if (_max_monitors > 0 && _monitor_top != bad_monitors) { aoqi@0: // If there are no monitors in the program, or there has been aoqi@0: // a monitor matching error before this point in the program, aoqi@0: // then we do not merge in the monitor state. aoqi@0: aoqi@0: int base = _max_locals + _max_stack; aoqi@0: int len = base + _monitor_top; aoqi@0: for (int i = len - 1; i >= base; i--) { aoqi@0: CellTypeState v = cts[i].merge(bbts[i], i); aoqi@0: aoqi@0: // Can we prove that, when there has been a change, it will already aoqi@0: // have been detected at this point? That would make this equal aoqi@0: // check here unnecessary. aoqi@0: change = change || !v.equal(bbts[i]); aoqi@0: bbts[i] = v; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: return change; aoqi@0: } aoqi@0: aoqi@0: void GenerateOopMap::copy_state(CellTypeState *dst, CellTypeState *src) { aoqi@0: int len = _max_locals + _stack_top; aoqi@0: for (int i = 0; i < len; i++) { aoqi@0: if (src[i].is_nonlock_reference()) { aoqi@0: dst[i] = CellTypeState::make_slot_ref(i); aoqi@0: } else { aoqi@0: dst[i] = src[i]; aoqi@0: } aoqi@0: } aoqi@0: if (_max_monitors > 0 && _monitor_top != bad_monitors) { aoqi@0: int base = _max_locals + _max_stack; aoqi@0: len = base + _monitor_top; aoqi@0: for (int i = base; i < len; i++) { aoqi@0: dst[i] = src[i]; aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: aoqi@0: // Merge the states for the current block and the next. As long as a aoqi@0: // block is reachable the locals and stack must be merged. If the aoqi@0: // stack heights don't match then this is a verification error and aoqi@0: // it's impossible to interpret the code. Simultaneously monitor aoqi@0: // states are being check to see if they nest statically. If monitor aoqi@0: // depths match up then their states are merged. Otherwise the aoqi@0: // mismatch is simply recorded and interpretation continues since aoqi@0: // monitor matching is purely informational and doesn't say anything aoqi@0: // about the correctness of the code. aoqi@0: void GenerateOopMap::merge_state_into_bb(BasicBlock *bb) { aoqi@0: guarantee(bb != NULL, "null basicblock"); aoqi@0: assert(bb->is_alive(), "merging state into a dead basicblock"); aoqi@0: aoqi@0: if (_stack_top == bb->_stack_top) { aoqi@0: // always merge local state even if monitors don't match. aoqi@0: if (merge_local_state_vectors(_state, bb->_state)) { aoqi@0: bb->set_changed(true); aoqi@0: } aoqi@0: if (_monitor_top == bb->_monitor_top) { aoqi@0: // monitors still match so continue merging monitor states. aoqi@0: if (merge_monitor_state_vectors(_state, bb->_state)) { aoqi@0: bb->set_changed(true); aoqi@0: } aoqi@0: } else { aoqi@0: if (TraceMonitorMismatch) { aoqi@0: report_monitor_mismatch("monitor stack height merge conflict"); aoqi@0: } aoqi@0: // When the monitor stacks are not matched, we set _monitor_top to aoqi@0: // bad_monitors. This signals that, from here on, the monitor stack cannot aoqi@0: // be trusted. In particular, monitorexit bytecodes may throw aoqi@0: // exceptions. We mark this block as changed so that the change aoqi@0: // propagates properly. aoqi@0: bb->_monitor_top = bad_monitors; aoqi@0: bb->set_changed(true); aoqi@0: _monitor_safe = false; aoqi@0: } aoqi@0: } else if (!bb->is_reachable()) { aoqi@0: // First time we look at this BB aoqi@0: copy_state(bb->_state, _state); aoqi@0: bb->_stack_top = _stack_top; aoqi@0: bb->_monitor_top = _monitor_top; aoqi@0: bb->set_changed(true); aoqi@0: } else { aoqi@0: verify_error("stack height conflict: %d vs. %d", _stack_top, bb->_stack_top); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: void GenerateOopMap::merge_state(GenerateOopMap *gom, int bci, int* data) { aoqi@0: gom->merge_state_into_bb(gom->get_basic_block_at(bci)); aoqi@0: } aoqi@0: aoqi@0: void GenerateOopMap::set_var(int localNo, CellTypeState cts) { aoqi@0: assert(cts.is_reference() || cts.is_value() || cts.is_address(), aoqi@0: "wrong celltypestate"); aoqi@0: if (localNo < 0 || localNo > _max_locals) { aoqi@0: verify_error("variable write error: r%d", localNo); aoqi@0: return; aoqi@0: } aoqi@0: vars()[localNo] = cts; aoqi@0: } aoqi@0: aoqi@0: CellTypeState GenerateOopMap::get_var(int localNo) { aoqi@0: assert(localNo < _max_locals + _nof_refval_conflicts, "variable read error"); aoqi@0: if (localNo < 0 || localNo > _max_locals) { aoqi@0: verify_error("variable read error: r%d", localNo); aoqi@0: return valCTS; // just to pick something; aoqi@0: } aoqi@0: return vars()[localNo]; aoqi@0: } aoqi@0: aoqi@0: CellTypeState GenerateOopMap::pop() { aoqi@0: if ( _stack_top <= 0) { aoqi@0: verify_error("stack underflow"); aoqi@0: return valCTS; // just to pick something aoqi@0: } aoqi@0: return stack()[--_stack_top]; aoqi@0: } aoqi@0: aoqi@0: void GenerateOopMap::push(CellTypeState cts) { aoqi@0: if ( _stack_top >= _max_stack) { aoqi@0: verify_error("stack overflow"); aoqi@0: return; aoqi@0: } aoqi@0: stack()[_stack_top++] = cts; aoqi@0: } aoqi@0: aoqi@0: CellTypeState GenerateOopMap::monitor_pop() { aoqi@0: assert(_monitor_top != bad_monitors, "monitor_pop called on error monitor stack"); aoqi@0: if (_monitor_top == 0) { aoqi@0: // We have detected a pop of an empty monitor stack. aoqi@0: _monitor_safe = false; aoqi@0: _monitor_top = bad_monitors; aoqi@0: aoqi@0: if (TraceMonitorMismatch) { aoqi@0: report_monitor_mismatch("monitor stack underflow"); aoqi@0: } aoqi@0: return CellTypeState::ref; // just to keep the analysis going. aoqi@0: } aoqi@0: return monitors()[--_monitor_top]; aoqi@0: } aoqi@0: aoqi@0: void GenerateOopMap::monitor_push(CellTypeState cts) { aoqi@0: assert(_monitor_top != bad_monitors, "monitor_push called on error monitor stack"); aoqi@0: if (_monitor_top >= _max_monitors) { aoqi@0: // Some monitorenter is being executed more than once. aoqi@0: // This means that the monitor stack cannot be simulated. aoqi@0: _monitor_safe = false; aoqi@0: _monitor_top = bad_monitors; aoqi@0: aoqi@0: if (TraceMonitorMismatch) { aoqi@0: report_monitor_mismatch("monitor stack overflow"); aoqi@0: } aoqi@0: return; aoqi@0: } aoqi@0: monitors()[_monitor_top++] = cts; aoqi@0: } aoqi@0: aoqi@0: // aoqi@0: // Interpretation handling methods aoqi@0: // aoqi@0: aoqi@0: void GenerateOopMap::do_interpretation() aoqi@0: { aoqi@0: // "i" is just for debugging, so we can detect cases where this loop is aoqi@0: // iterated more than once. aoqi@0: int i = 0; aoqi@0: do { aoqi@0: #ifndef PRODUCT aoqi@0: if (TraceNewOopMapGeneration) { aoqi@0: tty->print("\n\nIteration #%d of do_interpretation loop, method:\n", i); aoqi@0: method()->print_name(tty); aoqi@0: tty->print("\n\n"); aoqi@0: } aoqi@0: #endif aoqi@0: _conflict = false; aoqi@0: _monitor_safe = true; aoqi@0: // init_state is now called from init_basic_blocks. The length of a aoqi@0: // state vector cannot be determined until we have made a pass through aoqi@0: // the bytecodes counting the possible monitor entries. aoqi@0: if (!_got_error) init_basic_blocks(); aoqi@0: if (!_got_error) setup_method_entry_state(); aoqi@0: if (!_got_error) interp_all(); aoqi@0: if (!_got_error) rewrite_refval_conflicts(); aoqi@0: i++; aoqi@0: } while (_conflict && !_got_error); aoqi@0: } aoqi@0: aoqi@0: void GenerateOopMap::init_basic_blocks() { aoqi@0: // Note: Could consider reserving only the needed space for each BB's state aoqi@0: // (entry stack may not be of maximal height for every basic block). aoqi@0: // But cumbersome since we don't know the stack heights yet. (Nor the aoqi@0: // monitor stack heights...) aoqi@0: aoqi@0: ALLOC_RESOURCE_ARRAY(_basic_blocks, BasicBlock, _bb_count); aoqi@0: aoqi@0: // Make a pass through the bytecodes. Count the number of monitorenters. aoqi@0: // This can be used an upper bound on the monitor stack depth in programs aoqi@0: // which obey stack discipline with their monitor usage. Initialize the aoqi@0: // known information about basic blocks. aoqi@0: BytecodeStream j(_method); aoqi@0: Bytecodes::Code bytecode; aoqi@0: aoqi@0: int bbNo = 0; aoqi@0: int monitor_count = 0; aoqi@0: int prev_bci = -1; aoqi@0: while( (bytecode = j.next()) >= 0) { aoqi@0: if (j.code() == Bytecodes::_monitorenter) { aoqi@0: monitor_count++; aoqi@0: } aoqi@0: aoqi@0: int bci = j.bci(); aoqi@0: if (is_bb_header(bci)) { aoqi@0: // Initialize the basicblock structure aoqi@0: BasicBlock *bb = _basic_blocks + bbNo; aoqi@0: bb->_bci = bci; aoqi@0: bb->_max_locals = _max_locals; aoqi@0: bb->_max_stack = _max_stack; aoqi@0: bb->set_changed(false); aoqi@0: bb->_stack_top = BasicBlock::_dead_basic_block; // Initialize all basicblocks are dead. aoqi@0: bb->_monitor_top = bad_monitors; aoqi@0: aoqi@0: if (bbNo > 0) { aoqi@0: _basic_blocks[bbNo - 1]._end_bci = prev_bci; aoqi@0: } aoqi@0: aoqi@0: bbNo++; aoqi@0: } aoqi@0: // Remember prevous bci. aoqi@0: prev_bci = bci; aoqi@0: } aoqi@0: // Set aoqi@0: _basic_blocks[bbNo-1]._end_bci = prev_bci; aoqi@0: aoqi@0: aoqi@0: // Check that the correct number of basicblocks was found aoqi@0: if (bbNo !=_bb_count) { aoqi@0: if (bbNo < _bb_count) { aoqi@0: verify_error("jump into the middle of instruction?"); aoqi@0: return; aoqi@0: } else { aoqi@0: verify_error("extra basic blocks - should not happen?"); aoqi@0: return; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: _max_monitors = monitor_count; aoqi@0: aoqi@0: // Now that we have a bound on the depth of the monitor stack, we can aoqi@0: // initialize the CellTypeState-related information. aoqi@0: init_state(); aoqi@0: aoqi@0: // We allocate space for all state-vectors for all basicblocks in one huge aoqi@0: // chunk. Then in the next part of the code, we set a pointer in each aoqi@0: // _basic_block that points to each piece. aoqi@0: aoqi@0: // The product of bbNo and _state_len can get large if there are lots of aoqi@0: // basic blocks and stack/locals/monitors. Need to check to make sure aoqi@0: // we don't overflow the capacity of a pointer. aoqi@0: if ((unsigned)bbNo > UINTPTR_MAX / sizeof(CellTypeState) / _state_len) { aoqi@0: report_error("The amount of memory required to analyze this method " aoqi@0: "exceeds addressable range"); aoqi@0: return; aoqi@0: } aoqi@0: aoqi@0: CellTypeState *basicBlockState; aoqi@0: ALLOC_RESOURCE_ARRAY(basicBlockState, CellTypeState, bbNo * _state_len); aoqi@0: memset(basicBlockState, 0, bbNo * _state_len * sizeof(CellTypeState)); aoqi@0: aoqi@0: // Make a pass over the basicblocks and assign their state vectors. aoqi@0: for (int blockNum=0; blockNum < bbNo; blockNum++) { aoqi@0: BasicBlock *bb = _basic_blocks + blockNum; aoqi@0: bb->_state = basicBlockState + blockNum * _state_len; aoqi@0: aoqi@0: #ifdef ASSERT aoqi@0: if (blockNum + 1 < bbNo) { aoqi@0: address bcp = _method->bcp_from(bb->_end_bci); aoqi@0: int bc_len = Bytecodes::java_length_at(_method(), bcp); aoqi@0: assert(bb->_end_bci + bc_len == bb[1]._bci, "unmatched bci info in basicblock"); aoqi@0: } aoqi@0: #endif aoqi@0: } aoqi@0: #ifdef ASSERT aoqi@0: { BasicBlock *bb = &_basic_blocks[bbNo-1]; aoqi@0: address bcp = _method->bcp_from(bb->_end_bci); aoqi@0: int bc_len = Bytecodes::java_length_at(_method(), bcp); aoqi@0: assert(bb->_end_bci + bc_len == _method->code_size(), "wrong end bci"); aoqi@0: } aoqi@0: #endif aoqi@0: aoqi@0: // Mark all alive blocks aoqi@0: mark_reachable_code(); aoqi@0: } aoqi@0: aoqi@0: void GenerateOopMap::setup_method_entry_state() { aoqi@0: aoqi@0: // Initialize all locals to 'uninit' and set stack-height to 0 aoqi@0: make_context_uninitialized(); aoqi@0: aoqi@0: // Initialize CellState type of arguments aoqi@0: methodsig_to_effect(method()->signature(), method()->is_static(), vars()); aoqi@0: aoqi@0: // If some references must be pre-assigned to null, then set that up aoqi@0: initialize_vars(); aoqi@0: aoqi@0: // This is the start state aoqi@0: merge_state_into_bb(&_basic_blocks[0]); aoqi@0: aoqi@0: assert(_basic_blocks[0].changed(), "we are not getting off the ground"); aoqi@0: } aoqi@0: aoqi@0: // The instruction at bci is changing size by "delta". Update the basic blocks. aoqi@0: void GenerateOopMap::update_basic_blocks(int bci, int delta, aoqi@0: int new_method_size) { aoqi@0: assert(new_method_size >= method()->code_size() + delta, aoqi@0: "new method size is too small"); aoqi@0: aoqi@0: BitMap::bm_word_t* new_bb_hdr_bits = aoqi@0: NEW_RESOURCE_ARRAY(BitMap::bm_word_t, aoqi@0: BitMap::word_align_up(new_method_size)); aoqi@0: _bb_hdr_bits.set_map(new_bb_hdr_bits); aoqi@0: _bb_hdr_bits.set_size(new_method_size); aoqi@0: _bb_hdr_bits.clear(); aoqi@0: aoqi@0: aoqi@0: for(int k = 0; k < _bb_count; k++) { aoqi@0: if (_basic_blocks[k]._bci > bci) { aoqi@0: _basic_blocks[k]._bci += delta; aoqi@0: _basic_blocks[k]._end_bci += delta; aoqi@0: } aoqi@0: _bb_hdr_bits.at_put(_basic_blocks[k]._bci, true); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // aoqi@0: // Initvars handling aoqi@0: // aoqi@0: aoqi@0: void GenerateOopMap::initialize_vars() { aoqi@0: for (int k = 0; k < _init_vars->length(); k++) aoqi@0: _state[_init_vars->at(k)] = CellTypeState::make_slot_ref(k); aoqi@0: } aoqi@0: aoqi@0: void GenerateOopMap::add_to_ref_init_set(int localNo) { aoqi@0: aoqi@0: if (TraceNewOopMapGeneration) aoqi@0: tty->print_cr("Added init vars: %d", localNo); aoqi@0: aoqi@0: // Is it already in the set? aoqi@0: if (_init_vars->contains(localNo) ) aoqi@0: return; aoqi@0: aoqi@0: _init_vars->append(localNo); aoqi@0: } aoqi@0: aoqi@0: // aoqi@0: // Interpreration code aoqi@0: // aoqi@0: aoqi@0: void GenerateOopMap::interp_all() { aoqi@0: bool change = true; aoqi@0: aoqi@0: while (change && !_got_error) { aoqi@0: change = false; aoqi@0: for (int i = 0; i < _bb_count && !_got_error; i++) { aoqi@0: BasicBlock *bb = &_basic_blocks[i]; aoqi@0: if (bb->changed()) { aoqi@0: if (_got_error) return; aoqi@0: change = true; aoqi@0: bb->set_changed(false); aoqi@0: interp_bb(bb); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: void GenerateOopMap::interp_bb(BasicBlock *bb) { aoqi@0: aoqi@0: // We do not want to do anything in case the basic-block has not been initialized. This aoqi@0: // will happen in the case where there is dead-code hang around in a method. aoqi@0: assert(bb->is_reachable(), "should be reachable or deadcode exist"); aoqi@0: restore_state(bb); aoqi@0: aoqi@0: BytecodeStream itr(_method); aoqi@0: aoqi@0: // Set iterator interval to be the current basicblock aoqi@0: int lim_bci = next_bb_start_pc(bb); aoqi@0: itr.set_interval(bb->_bci, lim_bci); aoqi@0: assert(lim_bci != bb->_bci, "must be at least one instruction in a basicblock"); aoqi@0: itr.next(); // read first instruction aoqi@0: aoqi@0: // Iterates through all bytecodes except the last in a basic block. aoqi@0: // We handle the last one special, since there is controlflow change. aoqi@0: while(itr.next_bci() < lim_bci && !_got_error) { aoqi@0: if (_has_exceptions || _monitor_top != 0) { aoqi@0: // We do not need to interpret the results of exceptional aoqi@0: // continuation from this instruction when the method has no aoqi@0: // exception handlers and the monitor stack is currently aoqi@0: // empty. aoqi@0: do_exception_edge(&itr); aoqi@0: } aoqi@0: interp1(&itr); aoqi@0: itr.next(); aoqi@0: } aoqi@0: aoqi@0: // Handle last instruction. aoqi@0: if (!_got_error) { aoqi@0: assert(itr.next_bci() == lim_bci, "must point to end"); aoqi@0: if (_has_exceptions || _monitor_top != 0) { aoqi@0: do_exception_edge(&itr); aoqi@0: } aoqi@0: interp1(&itr); aoqi@0: aoqi@0: bool fall_through = jump_targets_do(&itr, GenerateOopMap::merge_state, NULL); aoqi@0: if (_got_error) return; aoqi@0: aoqi@0: if (itr.code() == Bytecodes::_ret) { aoqi@0: assert(!fall_through, "cannot be set if ret instruction"); aoqi@0: // Automatically handles 'wide' ret indicies aoqi@0: ret_jump_targets_do(&itr, GenerateOopMap::merge_state, itr.get_index(), NULL); aoqi@0: } else if (fall_through) { aoqi@0: // Hit end of BB, but the instr. was a fall-through instruction, aoqi@0: // so perform transition as if the BB ended in a "jump". aoqi@0: if (lim_bci != bb[1]._bci) { aoqi@0: verify_error("bytecodes fell through last instruction"); aoqi@0: return; aoqi@0: } aoqi@0: merge_state_into_bb(bb + 1); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: void GenerateOopMap::do_exception_edge(BytecodeStream* itr) { aoqi@0: // Only check exception edge, if bytecode can trap aoqi@0: if (!Bytecodes::can_trap(itr->code())) return; aoqi@0: switch (itr->code()) { aoqi@0: case Bytecodes::_aload_0: aoqi@0: // These bytecodes can trap for rewriting. We need to assume that aoqi@0: // they do not throw exceptions to make the monitor analysis work. aoqi@0: return; aoqi@0: aoqi@0: case Bytecodes::_ireturn: aoqi@0: case Bytecodes::_lreturn: aoqi@0: case Bytecodes::_freturn: aoqi@0: case Bytecodes::_dreturn: aoqi@0: case Bytecodes::_areturn: aoqi@0: case Bytecodes::_return: aoqi@0: // If the monitor stack height is not zero when we leave the method, aoqi@0: // then we are either exiting with a non-empty stack or we have aoqi@0: // found monitor trouble earlier in our analysis. In either case, aoqi@0: // assume an exception could be taken here. aoqi@0: if (_monitor_top == 0) { aoqi@0: return; aoqi@0: } aoqi@0: break; aoqi@0: aoqi@0: case Bytecodes::_monitorexit: aoqi@0: // If the monitor stack height is bad_monitors, then we have detected a aoqi@0: // monitor matching problem earlier in the analysis. If the aoqi@0: // monitor stack height is 0, we are about to pop a monitor aoqi@0: // off of an empty stack. In either case, the bytecode aoqi@0: // could throw an exception. aoqi@0: if (_monitor_top != bad_monitors && _monitor_top != 0) { aoqi@0: return; aoqi@0: } aoqi@0: break; aoqi@0: } aoqi@0: aoqi@0: if (_has_exceptions) { aoqi@0: int bci = itr->bci(); aoqi@0: ExceptionTable exct(method()); aoqi@0: for(int i = 0; i< exct.length(); i++) { aoqi@0: int start_pc = exct.start_pc(i); aoqi@0: int end_pc = exct.end_pc(i); aoqi@0: int handler_pc = exct.handler_pc(i); aoqi@0: int catch_type = exct.catch_type_index(i); aoqi@0: aoqi@0: if (start_pc <= bci && bci < end_pc) { aoqi@0: BasicBlock *excBB = get_basic_block_at(handler_pc); aoqi@0: guarantee(excBB != NULL, "no basic block for exception"); aoqi@0: CellTypeState *excStk = excBB->stack(); aoqi@0: CellTypeState *cOpStck = stack(); aoqi@0: CellTypeState cOpStck_0 = cOpStck[0]; aoqi@0: int cOpStackTop = _stack_top; aoqi@0: aoqi@0: // Exception stacks are always the same. aoqi@0: assert(method()->max_stack() > 0, "sanity check"); aoqi@0: aoqi@0: // We remembered the size and first element of "cOpStck" aoqi@0: // above; now we temporarily set them to the appropriate aoqi@0: // values for an exception handler. */ aoqi@0: cOpStck[0] = CellTypeState::make_slot_ref(_max_locals); aoqi@0: _stack_top = 1; aoqi@0: aoqi@0: merge_state_into_bb(excBB); aoqi@0: aoqi@0: // Now undo the temporary change. aoqi@0: cOpStck[0] = cOpStck_0; aoqi@0: _stack_top = cOpStackTop; aoqi@0: aoqi@0: // If this is a "catch all" handler, then we do not need to aoqi@0: // consider any additional handlers. aoqi@0: if (catch_type == 0) { aoqi@0: return; aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // It is possible that none of the exception handlers would have caught aoqi@0: // the exception. In this case, we will exit the method. We must aoqi@0: // ensure that the monitor stack is empty in this case. aoqi@0: if (_monitor_top == 0) { aoqi@0: return; aoqi@0: } aoqi@0: aoqi@0: // We pessimistically assume that this exception can escape the aoqi@0: // method. (It is possible that it will always be caught, but aoqi@0: // we don't care to analyse the types of the catch clauses.) aoqi@0: aoqi@0: // We don't set _monitor_top to bad_monitors because there are no successors aoqi@0: // to this exceptional exit. aoqi@0: aoqi@0: if (TraceMonitorMismatch && _monitor_safe) { aoqi@0: // We check _monitor_safe so that we only report the first mismatched aoqi@0: // exceptional exit. aoqi@0: report_monitor_mismatch("non-empty monitor stack at exceptional exit"); aoqi@0: } aoqi@0: _monitor_safe = false; aoqi@0: aoqi@0: } aoqi@0: aoqi@0: void GenerateOopMap::report_monitor_mismatch(const char *msg) { aoqi@0: #ifndef PRODUCT aoqi@0: tty->print(" Monitor mismatch in method "); aoqi@0: method()->print_short_name(tty); aoqi@0: tty->print_cr(": %s", msg); aoqi@0: #endif aoqi@0: } aoqi@0: aoqi@0: void GenerateOopMap::print_states(outputStream *os, aoqi@0: CellTypeState* vec, int num) { aoqi@0: for (int i = 0; i < num; i++) { aoqi@0: vec[i].print(tty); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // Print the state values at the current bytecode. aoqi@0: void GenerateOopMap::print_current_state(outputStream *os, aoqi@0: BytecodeStream *currentBC, aoqi@0: bool detailed) { aoqi@0: aoqi@0: if (detailed) { aoqi@0: os->print(" %4d vars = ", currentBC->bci()); aoqi@0: print_states(os, vars(), _max_locals); aoqi@0: os->print(" %s", Bytecodes::name(currentBC->code())); aoqi@0: switch(currentBC->code()) { aoqi@0: case Bytecodes::_invokevirtual: aoqi@0: case Bytecodes::_invokespecial: aoqi@0: case Bytecodes::_invokestatic: aoqi@0: case Bytecodes::_invokedynamic: aoqi@0: case Bytecodes::_invokeinterface: aoqi@0: int idx = currentBC->has_index_u4() ? currentBC->get_index_u4() : currentBC->get_index_u2_cpcache(); aoqi@0: ConstantPool* cp = method()->constants(); aoqi@0: int nameAndTypeIdx = cp->name_and_type_ref_index_at(idx); aoqi@0: int signatureIdx = cp->signature_ref_index_at(nameAndTypeIdx); aoqi@0: Symbol* signature = cp->symbol_at(signatureIdx); aoqi@0: os->print("%s", signature->as_C_string()); aoqi@0: } aoqi@0: os->cr(); aoqi@0: os->print(" stack = "); aoqi@0: print_states(os, stack(), _stack_top); aoqi@0: os->cr(); aoqi@0: if (_monitor_top != bad_monitors) { aoqi@0: os->print(" monitors = "); aoqi@0: print_states(os, monitors(), _monitor_top); aoqi@0: } else { aoqi@0: os->print(" [bad monitor stack]"); aoqi@0: } aoqi@0: os->cr(); aoqi@0: } else { aoqi@0: os->print(" %4d vars = '%s' ", currentBC->bci(), state_vec_to_string(vars(), _max_locals)); aoqi@0: os->print(" stack = '%s' ", state_vec_to_string(stack(), _stack_top)); aoqi@0: if (_monitor_top != bad_monitors) { aoqi@0: os->print(" monitors = '%s' \t%s", state_vec_to_string(monitors(), _monitor_top), Bytecodes::name(currentBC->code())); aoqi@0: } else { aoqi@0: os->print(" [bad monitor stack]"); aoqi@0: } aoqi@0: switch(currentBC->code()) { aoqi@0: case Bytecodes::_invokevirtual: aoqi@0: case Bytecodes::_invokespecial: aoqi@0: case Bytecodes::_invokestatic: aoqi@0: case Bytecodes::_invokedynamic: aoqi@0: case Bytecodes::_invokeinterface: aoqi@0: int idx = currentBC->has_index_u4() ? currentBC->get_index_u4() : currentBC->get_index_u2_cpcache(); aoqi@0: ConstantPool* cp = method()->constants(); aoqi@0: int nameAndTypeIdx = cp->name_and_type_ref_index_at(idx); aoqi@0: int signatureIdx = cp->signature_ref_index_at(nameAndTypeIdx); aoqi@0: Symbol* signature = cp->symbol_at(signatureIdx); aoqi@0: os->print("%s", signature->as_C_string()); aoqi@0: } aoqi@0: os->cr(); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // Sets the current state to be the state after executing the aoqi@0: // current instruction, starting in the current state. aoqi@0: void GenerateOopMap::interp1(BytecodeStream *itr) { aoqi@0: if (TraceNewOopMapGeneration) { aoqi@0: print_current_state(tty, itr, TraceNewOopMapGenerationDetailed); aoqi@0: } aoqi@0: aoqi@0: // Should we report the results? Result is reported *before* the instruction at the current bci is executed. aoqi@0: // However, not for calls. For calls we do not want to include the arguments, so we postpone the reporting until aoqi@0: // they have been popped (in method ppl). aoqi@0: if (_report_result == true) { aoqi@0: switch(itr->code()) { aoqi@0: case Bytecodes::_invokevirtual: aoqi@0: case Bytecodes::_invokespecial: aoqi@0: case Bytecodes::_invokestatic: aoqi@0: case Bytecodes::_invokedynamic: aoqi@0: case Bytecodes::_invokeinterface: aoqi@0: _itr_send = itr; aoqi@0: _report_result_for_send = true; aoqi@0: break; aoqi@0: default: aoqi@0: fill_stackmap_for_opcodes(itr, vars(), stack(), _stack_top); aoqi@0: break; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // abstract interpretation of current opcode aoqi@0: switch(itr->code()) { aoqi@0: case Bytecodes::_nop: break; aoqi@0: case Bytecodes::_goto: break; aoqi@0: case Bytecodes::_goto_w: break; aoqi@0: case Bytecodes::_iinc: break; aoqi@0: case Bytecodes::_return: do_return_monitor_check(); aoqi@0: break; aoqi@0: aoqi@0: case Bytecodes::_aconst_null: aoqi@0: case Bytecodes::_new: ppush1(CellTypeState::make_line_ref(itr->bci())); aoqi@0: break; aoqi@0: aoqi@0: case Bytecodes::_iconst_m1: aoqi@0: case Bytecodes::_iconst_0: aoqi@0: case Bytecodes::_iconst_1: aoqi@0: case Bytecodes::_iconst_2: aoqi@0: case Bytecodes::_iconst_3: aoqi@0: case Bytecodes::_iconst_4: aoqi@0: case Bytecodes::_iconst_5: aoqi@0: case Bytecodes::_fconst_0: aoqi@0: case Bytecodes::_fconst_1: aoqi@0: case Bytecodes::_fconst_2: aoqi@0: case Bytecodes::_bipush: aoqi@0: case Bytecodes::_sipush: ppush1(valCTS); break; aoqi@0: aoqi@0: case Bytecodes::_lconst_0: aoqi@0: case Bytecodes::_lconst_1: aoqi@0: case Bytecodes::_dconst_0: aoqi@0: case Bytecodes::_dconst_1: ppush(vvCTS); break; aoqi@0: aoqi@0: case Bytecodes::_ldc2_w: ppush(vvCTS); break; aoqi@0: aoqi@0: case Bytecodes::_ldc: // fall through: aoqi@0: case Bytecodes::_ldc_w: do_ldc(itr->bci()); break; aoqi@0: aoqi@0: case Bytecodes::_iload: aoqi@0: case Bytecodes::_fload: ppload(vCTS, itr->get_index()); break; aoqi@0: aoqi@0: case Bytecodes::_lload: aoqi@0: case Bytecodes::_dload: ppload(vvCTS,itr->get_index()); break; aoqi@0: aoqi@0: case Bytecodes::_aload: ppload(rCTS, itr->get_index()); break; aoqi@0: aoqi@0: case Bytecodes::_iload_0: aoqi@0: case Bytecodes::_fload_0: ppload(vCTS, 0); break; aoqi@0: case Bytecodes::_iload_1: aoqi@0: case Bytecodes::_fload_1: ppload(vCTS, 1); break; aoqi@0: case Bytecodes::_iload_2: aoqi@0: case Bytecodes::_fload_2: ppload(vCTS, 2); break; aoqi@0: case Bytecodes::_iload_3: aoqi@0: case Bytecodes::_fload_3: ppload(vCTS, 3); break; aoqi@0: aoqi@0: case Bytecodes::_lload_0: aoqi@0: case Bytecodes::_dload_0: ppload(vvCTS, 0); break; aoqi@0: case Bytecodes::_lload_1: aoqi@0: case Bytecodes::_dload_1: ppload(vvCTS, 1); break; aoqi@0: case Bytecodes::_lload_2: aoqi@0: case Bytecodes::_dload_2: ppload(vvCTS, 2); break; aoqi@0: case Bytecodes::_lload_3: aoqi@0: case Bytecodes::_dload_3: ppload(vvCTS, 3); break; aoqi@0: aoqi@0: case Bytecodes::_aload_0: ppload(rCTS, 0); break; aoqi@0: case Bytecodes::_aload_1: ppload(rCTS, 1); break; aoqi@0: case Bytecodes::_aload_2: ppload(rCTS, 2); break; aoqi@0: case Bytecodes::_aload_3: ppload(rCTS, 3); break; aoqi@0: aoqi@0: case Bytecodes::_iaload: aoqi@0: case Bytecodes::_faload: aoqi@0: case Bytecodes::_baload: aoqi@0: case Bytecodes::_caload: aoqi@0: case Bytecodes::_saload: pp(vrCTS, vCTS); break; aoqi@0: aoqi@0: case Bytecodes::_laload: pp(vrCTS, vvCTS); break; aoqi@0: case Bytecodes::_daload: pp(vrCTS, vvCTS); break; aoqi@0: aoqi@0: case Bytecodes::_aaload: pp_new_ref(vrCTS, itr->bci()); break; aoqi@0: aoqi@0: case Bytecodes::_istore: aoqi@0: case Bytecodes::_fstore: ppstore(vCTS, itr->get_index()); break; aoqi@0: aoqi@0: case Bytecodes::_lstore: aoqi@0: case Bytecodes::_dstore: ppstore(vvCTS, itr->get_index()); break; aoqi@0: aoqi@0: case Bytecodes::_astore: do_astore(itr->get_index()); break; aoqi@0: aoqi@0: case Bytecodes::_istore_0: aoqi@0: case Bytecodes::_fstore_0: ppstore(vCTS, 0); break; aoqi@0: case Bytecodes::_istore_1: aoqi@0: case Bytecodes::_fstore_1: ppstore(vCTS, 1); break; aoqi@0: case Bytecodes::_istore_2: aoqi@0: case Bytecodes::_fstore_2: ppstore(vCTS, 2); break; aoqi@0: case Bytecodes::_istore_3: aoqi@0: case Bytecodes::_fstore_3: ppstore(vCTS, 3); break; aoqi@0: aoqi@0: case Bytecodes::_lstore_0: aoqi@0: case Bytecodes::_dstore_0: ppstore(vvCTS, 0); break; aoqi@0: case Bytecodes::_lstore_1: aoqi@0: case Bytecodes::_dstore_1: ppstore(vvCTS, 1); break; aoqi@0: case Bytecodes::_lstore_2: aoqi@0: case Bytecodes::_dstore_2: ppstore(vvCTS, 2); break; aoqi@0: case Bytecodes::_lstore_3: aoqi@0: case Bytecodes::_dstore_3: ppstore(vvCTS, 3); break; aoqi@0: aoqi@0: case Bytecodes::_astore_0: do_astore(0); break; aoqi@0: case Bytecodes::_astore_1: do_astore(1); break; aoqi@0: case Bytecodes::_astore_2: do_astore(2); break; aoqi@0: case Bytecodes::_astore_3: do_astore(3); break; aoqi@0: aoqi@0: case Bytecodes::_iastore: aoqi@0: case Bytecodes::_fastore: aoqi@0: case Bytecodes::_bastore: aoqi@0: case Bytecodes::_castore: aoqi@0: case Bytecodes::_sastore: ppop(vvrCTS); break; aoqi@0: case Bytecodes::_lastore: aoqi@0: case Bytecodes::_dastore: ppop(vvvrCTS); break; aoqi@0: case Bytecodes::_aastore: ppop(rvrCTS); break; aoqi@0: aoqi@0: case Bytecodes::_pop: ppop_any(1); break; aoqi@0: case Bytecodes::_pop2: ppop_any(2); break; aoqi@0: aoqi@0: case Bytecodes::_dup: ppdupswap(1, "11"); break; aoqi@0: case Bytecodes::_dup_x1: ppdupswap(2, "121"); break; aoqi@0: case Bytecodes::_dup_x2: ppdupswap(3, "1321"); break; aoqi@0: case Bytecodes::_dup2: ppdupswap(2, "2121"); break; aoqi@0: case Bytecodes::_dup2_x1: ppdupswap(3, "21321"); break; aoqi@0: case Bytecodes::_dup2_x2: ppdupswap(4, "214321"); break; aoqi@0: case Bytecodes::_swap: ppdupswap(2, "12"); break; aoqi@0: aoqi@0: case Bytecodes::_iadd: aoqi@0: case Bytecodes::_fadd: aoqi@0: case Bytecodes::_isub: aoqi@0: case Bytecodes::_fsub: aoqi@0: case Bytecodes::_imul: aoqi@0: case Bytecodes::_fmul: aoqi@0: case Bytecodes::_idiv: aoqi@0: case Bytecodes::_fdiv: aoqi@0: case Bytecodes::_irem: aoqi@0: case Bytecodes::_frem: aoqi@0: case Bytecodes::_ishl: aoqi@0: case Bytecodes::_ishr: aoqi@0: case Bytecodes::_iushr: aoqi@0: case Bytecodes::_iand: aoqi@0: case Bytecodes::_ior: aoqi@0: case Bytecodes::_ixor: aoqi@0: case Bytecodes::_l2f: aoqi@0: case Bytecodes::_l2i: aoqi@0: case Bytecodes::_d2f: aoqi@0: case Bytecodes::_d2i: aoqi@0: case Bytecodes::_fcmpl: aoqi@0: case Bytecodes::_fcmpg: pp(vvCTS, vCTS); break; aoqi@0: aoqi@0: case Bytecodes::_ladd: aoqi@0: case Bytecodes::_dadd: aoqi@0: case Bytecodes::_lsub: aoqi@0: case Bytecodes::_dsub: aoqi@0: case Bytecodes::_lmul: aoqi@0: case Bytecodes::_dmul: aoqi@0: case Bytecodes::_ldiv: aoqi@0: case Bytecodes::_ddiv: aoqi@0: case Bytecodes::_lrem: aoqi@0: case Bytecodes::_drem: aoqi@0: case Bytecodes::_land: aoqi@0: case Bytecodes::_lor: aoqi@0: case Bytecodes::_lxor: pp(vvvvCTS, vvCTS); break; aoqi@0: aoqi@0: case Bytecodes::_ineg: aoqi@0: case Bytecodes::_fneg: aoqi@0: case Bytecodes::_i2f: aoqi@0: case Bytecodes::_f2i: aoqi@0: case Bytecodes::_i2c: aoqi@0: case Bytecodes::_i2s: aoqi@0: case Bytecodes::_i2b: pp(vCTS, vCTS); break; aoqi@0: aoqi@0: case Bytecodes::_lneg: aoqi@0: case Bytecodes::_dneg: aoqi@0: case Bytecodes::_l2d: aoqi@0: case Bytecodes::_d2l: pp(vvCTS, vvCTS); break; aoqi@0: aoqi@0: case Bytecodes::_lshl: aoqi@0: case Bytecodes::_lshr: aoqi@0: case Bytecodes::_lushr: pp(vvvCTS, vvCTS); break; aoqi@0: aoqi@0: case Bytecodes::_i2l: aoqi@0: case Bytecodes::_i2d: aoqi@0: case Bytecodes::_f2l: aoqi@0: case Bytecodes::_f2d: pp(vCTS, vvCTS); break; aoqi@0: aoqi@0: case Bytecodes::_lcmp: pp(vvvvCTS, vCTS); break; aoqi@0: case Bytecodes::_dcmpl: aoqi@0: case Bytecodes::_dcmpg: pp(vvvvCTS, vCTS); break; aoqi@0: aoqi@0: case Bytecodes::_ifeq: aoqi@0: case Bytecodes::_ifne: aoqi@0: case Bytecodes::_iflt: aoqi@0: case Bytecodes::_ifge: aoqi@0: case Bytecodes::_ifgt: aoqi@0: case Bytecodes::_ifle: aoqi@0: case Bytecodes::_tableswitch: ppop1(valCTS); aoqi@0: break; aoqi@0: case Bytecodes::_ireturn: aoqi@0: case Bytecodes::_freturn: do_return_monitor_check(); aoqi@0: ppop1(valCTS); aoqi@0: break; aoqi@0: case Bytecodes::_if_icmpeq: aoqi@0: case Bytecodes::_if_icmpne: aoqi@0: case Bytecodes::_if_icmplt: aoqi@0: case Bytecodes::_if_icmpge: aoqi@0: case Bytecodes::_if_icmpgt: aoqi@0: case Bytecodes::_if_icmple: ppop(vvCTS); aoqi@0: break; aoqi@0: aoqi@0: case Bytecodes::_lreturn: do_return_monitor_check(); aoqi@0: ppop(vvCTS); aoqi@0: break; aoqi@0: aoqi@0: case Bytecodes::_dreturn: do_return_monitor_check(); aoqi@0: ppop(vvCTS); aoqi@0: break; aoqi@0: aoqi@0: case Bytecodes::_if_acmpeq: aoqi@0: case Bytecodes::_if_acmpne: ppop(rrCTS); break; aoqi@0: aoqi@0: case Bytecodes::_jsr: do_jsr(itr->dest()); break; aoqi@0: case Bytecodes::_jsr_w: do_jsr(itr->dest_w()); break; aoqi@0: aoqi@0: case Bytecodes::_getstatic: do_field(true, true, itr->get_index_u2_cpcache(), itr->bci()); break; aoqi@0: case Bytecodes::_putstatic: do_field(false, true, itr->get_index_u2_cpcache(), itr->bci()); break; aoqi@0: case Bytecodes::_getfield: do_field(true, false, itr->get_index_u2_cpcache(), itr->bci()); break; aoqi@0: case Bytecodes::_putfield: do_field(false, false, itr->get_index_u2_cpcache(), itr->bci()); break; aoqi@0: aoqi@0: case Bytecodes::_invokevirtual: aoqi@0: case Bytecodes::_invokespecial: do_method(false, false, itr->get_index_u2_cpcache(), itr->bci()); break; aoqi@0: case Bytecodes::_invokestatic: do_method(true, false, itr->get_index_u2_cpcache(), itr->bci()); break; aoqi@0: case Bytecodes::_invokedynamic: do_method(true, false, itr->get_index_u4(), itr->bci()); break; aoqi@0: case Bytecodes::_invokeinterface: do_method(false, true, itr->get_index_u2_cpcache(), itr->bci()); break; aoqi@0: case Bytecodes::_newarray: aoqi@0: case Bytecodes::_anewarray: pp_new_ref(vCTS, itr->bci()); break; aoqi@0: case Bytecodes::_checkcast: do_checkcast(); break; aoqi@0: case Bytecodes::_arraylength: aoqi@0: case Bytecodes::_instanceof: pp(rCTS, vCTS); break; aoqi@0: case Bytecodes::_monitorenter: do_monitorenter(itr->bci()); break; aoqi@0: case Bytecodes::_monitorexit: do_monitorexit(itr->bci()); break; aoqi@0: aoqi@0: case Bytecodes::_athrow: // handled by do_exception_edge() BUT ... aoqi@0: // vlh(apple): do_exception_edge() does not get aoqi@0: // called if method has no exception handlers aoqi@0: if ((!_has_exceptions) && (_monitor_top > 0)) { aoqi@0: _monitor_safe = false; aoqi@0: } aoqi@0: break; aoqi@0: aoqi@0: case Bytecodes::_areturn: do_return_monitor_check(); aoqi@0: ppop1(refCTS); aoqi@0: break; aoqi@0: case Bytecodes::_ifnull: aoqi@0: case Bytecodes::_ifnonnull: ppop1(refCTS); break; aoqi@0: case Bytecodes::_multianewarray: do_multianewarray(*(itr->bcp()+3), itr->bci()); break; aoqi@0: aoqi@0: case Bytecodes::_wide: fatal("Iterator should skip this bytecode"); break; aoqi@0: case Bytecodes::_ret: break; aoqi@0: aoqi@0: // Java opcodes aoqi@0: case Bytecodes::_lookupswitch: ppop1(valCTS); break; aoqi@0: aoqi@0: default: aoqi@0: tty->print("unexpected opcode: %d\n", itr->code()); aoqi@0: ShouldNotReachHere(); aoqi@0: break; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: void GenerateOopMap::check_type(CellTypeState expected, CellTypeState actual) { aoqi@0: if (!expected.equal_kind(actual)) { aoqi@0: verify_error("wrong type on stack (found: %c expected: %c)", actual.to_char(), expected.to_char()); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: void GenerateOopMap::ppstore(CellTypeState *in, int loc_no) { aoqi@0: while(!(*in).is_bottom()) { aoqi@0: CellTypeState expected =*in++; aoqi@0: CellTypeState actual = pop(); aoqi@0: check_type(expected, actual); aoqi@0: assert(loc_no >= 0, "sanity check"); aoqi@0: set_var(loc_no++, actual); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: void GenerateOopMap::ppload(CellTypeState *out, int loc_no) { aoqi@0: while(!(*out).is_bottom()) { aoqi@0: CellTypeState out1 = *out++; aoqi@0: CellTypeState vcts = get_var(loc_no); aoqi@0: assert(out1.can_be_reference() || out1.can_be_value(), aoqi@0: "can only load refs. and values."); aoqi@0: if (out1.is_reference()) { aoqi@0: assert(loc_no>=0, "sanity check"); aoqi@0: if (!vcts.is_reference()) { aoqi@0: // We were asked to push a reference, but the type of the aoqi@0: // variable can be something else aoqi@0: _conflict = true; aoqi@0: if (vcts.can_be_uninit()) { aoqi@0: // It is a ref-uninit conflict (at least). If there are other aoqi@0: // problems, we'll get them in the next round aoqi@0: add_to_ref_init_set(loc_no); aoqi@0: vcts = out1; aoqi@0: } else { aoqi@0: // It wasn't a ref-uninit conflict. So must be a aoqi@0: // ref-val or ref-pc conflict. Split the variable. aoqi@0: record_refval_conflict(loc_no); aoqi@0: vcts = out1; aoqi@0: } aoqi@0: push(out1); // recover... aoqi@0: } else { aoqi@0: push(vcts); // preserve reference. aoqi@0: } aoqi@0: // Otherwise it is a conflict, but one that verification would aoqi@0: // have caught if illegal. In particular, it can't be a topCTS aoqi@0: // resulting from mergeing two difference pcCTS's since the verifier aoqi@0: // would have rejected any use of such a merge. aoqi@0: } else { aoqi@0: push(out1); // handle val/init conflict aoqi@0: } aoqi@0: loc_no++; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: void GenerateOopMap::ppdupswap(int poplen, const char *out) { aoqi@0: CellTypeState actual[5]; aoqi@0: assert(poplen < 5, "this must be less than length of actual vector"); aoqi@0: aoqi@0: // pop all arguments aoqi@0: for(int i = 0; i < poplen; i++) actual[i] = pop(); aoqi@0: aoqi@0: // put them back aoqi@0: char push_ch = *out++; aoqi@0: while (push_ch != '\0') { aoqi@0: int idx = push_ch - '1'; aoqi@0: assert(idx >= 0 && idx < poplen, "wrong arguments"); aoqi@0: push(actual[idx]); aoqi@0: push_ch = *out++; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: void GenerateOopMap::ppop1(CellTypeState out) { aoqi@0: CellTypeState actual = pop(); aoqi@0: check_type(out, actual); aoqi@0: } aoqi@0: aoqi@0: void GenerateOopMap::ppop(CellTypeState *out) { aoqi@0: while (!(*out).is_bottom()) { aoqi@0: ppop1(*out++); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: void GenerateOopMap::ppush1(CellTypeState in) { aoqi@0: assert(in.is_reference() | in.is_value(), "sanity check"); aoqi@0: push(in); aoqi@0: } aoqi@0: aoqi@0: void GenerateOopMap::ppush(CellTypeState *in) { aoqi@0: while (!(*in).is_bottom()) { aoqi@0: ppush1(*in++); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: void GenerateOopMap::pp(CellTypeState *in, CellTypeState *out) { aoqi@0: ppop(in); aoqi@0: ppush(out); aoqi@0: } aoqi@0: aoqi@0: void GenerateOopMap::pp_new_ref(CellTypeState *in, int bci) { aoqi@0: ppop(in); aoqi@0: ppush1(CellTypeState::make_line_ref(bci)); aoqi@0: } aoqi@0: aoqi@0: void GenerateOopMap::ppop_any(int poplen) { aoqi@0: if (_stack_top >= poplen) { aoqi@0: _stack_top -= poplen; aoqi@0: } else { aoqi@0: verify_error("stack underflow"); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // Replace all occurences of the state 'match' with the state 'replace' aoqi@0: // in our current state vector. aoqi@0: void GenerateOopMap::replace_all_CTS_matches(CellTypeState match, aoqi@0: CellTypeState replace) { aoqi@0: int i; aoqi@0: int len = _max_locals + _stack_top; aoqi@0: bool change = false; aoqi@0: aoqi@0: for (i = len - 1; i >= 0; i--) { aoqi@0: if (match.equal(_state[i])) { aoqi@0: _state[i] = replace; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: if (_monitor_top > 0) { aoqi@0: int base = _max_locals + _max_stack; aoqi@0: len = base + _monitor_top; aoqi@0: for (i = len - 1; i >= base; i--) { aoqi@0: if (match.equal(_state[i])) { aoqi@0: _state[i] = replace; aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: void GenerateOopMap::do_checkcast() { aoqi@0: CellTypeState actual = pop(); aoqi@0: check_type(refCTS, actual); aoqi@0: push(actual); aoqi@0: } aoqi@0: aoqi@0: void GenerateOopMap::do_monitorenter(int bci) { aoqi@0: CellTypeState actual = pop(); aoqi@0: if (_monitor_top == bad_monitors) { aoqi@0: return; aoqi@0: } aoqi@0: aoqi@0: // Bail out when we get repeated locks on an identical monitor. This case aoqi@0: // isn't too hard to handle and can be made to work if supporting nested aoqi@0: // redundant synchronized statements becomes a priority. aoqi@0: // aoqi@0: // See also "Note" in do_monitorexit(), below. aoqi@0: if (actual.is_lock_reference()) { aoqi@0: _monitor_top = bad_monitors; aoqi@0: _monitor_safe = false; aoqi@0: aoqi@0: if (TraceMonitorMismatch) { aoqi@0: report_monitor_mismatch("nested redundant lock -- bailout..."); aoqi@0: } aoqi@0: return; aoqi@0: } aoqi@0: aoqi@0: CellTypeState lock = CellTypeState::make_lock_ref(bci); aoqi@0: check_type(refCTS, actual); aoqi@0: if (!actual.is_info_top()) { aoqi@0: replace_all_CTS_matches(actual, lock); aoqi@0: monitor_push(lock); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: void GenerateOopMap::do_monitorexit(int bci) { aoqi@0: CellTypeState actual = pop(); aoqi@0: if (_monitor_top == bad_monitors) { aoqi@0: return; aoqi@0: } aoqi@0: check_type(refCTS, actual); aoqi@0: CellTypeState expected = monitor_pop(); aoqi@0: if (!actual.is_lock_reference() || !expected.equal(actual)) { aoqi@0: // The monitor we are exiting is not verifiably the one aoqi@0: // on the top of our monitor stack. This causes a monitor aoqi@0: // mismatch. aoqi@0: _monitor_top = bad_monitors; aoqi@0: _monitor_safe = false; aoqi@0: aoqi@0: // We need to mark this basic block as changed so that aoqi@0: // this monitorexit will be visited again. We need to aoqi@0: // do this to ensure that we have accounted for the aoqi@0: // possibility that this bytecode will throw an aoqi@0: // exception. aoqi@0: BasicBlock* bb = get_basic_block_containing(bci); aoqi@0: guarantee(bb != NULL, "no basic block for bci"); aoqi@0: bb->set_changed(true); aoqi@0: bb->_monitor_top = bad_monitors; aoqi@0: aoqi@0: if (TraceMonitorMismatch) { aoqi@0: report_monitor_mismatch("improper monitor pair"); aoqi@0: } aoqi@0: } else { aoqi@0: // This code is a fix for the case where we have repeated aoqi@0: // locking of the same object in straightline code. We clear aoqi@0: // out the lock when it is popped from the monitor stack aoqi@0: // and replace it with an unobtrusive reference value that can aoqi@0: // be locked again. aoqi@0: // aoqi@0: // Note: when generateOopMap is fixed to properly handle repeated, aoqi@0: // nested, redundant locks on the same object, then this aoqi@0: // fix will need to be removed at that time. aoqi@0: replace_all_CTS_matches(actual, CellTypeState::make_line_ref(bci)); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: void GenerateOopMap::do_return_monitor_check() { aoqi@0: if (_monitor_top > 0) { aoqi@0: // The monitor stack must be empty when we leave the method aoqi@0: // for the monitors to be properly matched. aoqi@0: _monitor_safe = false; aoqi@0: aoqi@0: // Since there are no successors to the *return bytecode, it aoqi@0: // isn't necessary to set _monitor_top to bad_monitors. aoqi@0: aoqi@0: if (TraceMonitorMismatch) { aoqi@0: report_monitor_mismatch("non-empty monitor stack at return"); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: void GenerateOopMap::do_jsr(int targ_bci) { aoqi@0: push(CellTypeState::make_addr(targ_bci)); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: aoqi@0: void GenerateOopMap::do_ldc(int bci) { aoqi@0: Bytecode_loadconstant ldc(method(), bci); aoqi@0: ConstantPool* cp = method()->constants(); aoqi@0: constantTag tag = cp->tag_at(ldc.pool_index()); // idx is index in resolved_references aoqi@0: BasicType bt = ldc.result_type(); aoqi@0: CellTypeState cts; aoqi@0: if (tag.basic_type() == T_OBJECT) { aoqi@0: assert(!tag.is_string_index() && !tag.is_klass_index(), "Unexpected index tag"); aoqi@0: assert(bt == T_OBJECT, "Guard is incorrect"); aoqi@0: cts = CellTypeState::make_line_ref(bci); aoqi@0: } else { aoqi@0: assert(bt != T_OBJECT, "Guard is incorrect"); aoqi@0: cts = valCTS; aoqi@0: } aoqi@0: ppush1(cts); aoqi@0: } aoqi@0: aoqi@0: void GenerateOopMap::do_multianewarray(int dims, int bci) { aoqi@0: assert(dims >= 1, "sanity check"); aoqi@0: for(int i = dims -1; i >=0; i--) { aoqi@0: ppop1(valCTS); aoqi@0: } aoqi@0: ppush1(CellTypeState::make_line_ref(bci)); aoqi@0: } aoqi@0: aoqi@0: void GenerateOopMap::do_astore(int idx) { aoqi@0: CellTypeState r_or_p = pop(); aoqi@0: if (!r_or_p.is_address() && !r_or_p.is_reference()) { aoqi@0: // We actually expected ref or pc, but we only report that we expected a ref. It does not aoqi@0: // really matter (at least for now) aoqi@0: verify_error("wrong type on stack (found: %c, expected: {pr})", r_or_p.to_char()); aoqi@0: return; aoqi@0: } aoqi@0: set_var(idx, r_or_p); aoqi@0: } aoqi@0: aoqi@0: // Copies bottom/zero terminated CTS string from "src" into "dst". aoqi@0: // Does NOT terminate with a bottom. Returns the number of cells copied. aoqi@0: int GenerateOopMap::copy_cts(CellTypeState *dst, CellTypeState *src) { aoqi@0: int idx = 0; aoqi@0: while (!src[idx].is_bottom()) { aoqi@0: dst[idx] = src[idx]; aoqi@0: idx++; aoqi@0: } aoqi@0: return idx; aoqi@0: } aoqi@0: aoqi@0: void GenerateOopMap::do_field(int is_get, int is_static, int idx, int bci) { aoqi@0: // Dig up signature for field in constant pool aoqi@0: ConstantPool* cp = method()->constants(); aoqi@0: int nameAndTypeIdx = cp->name_and_type_ref_index_at(idx); aoqi@0: int signatureIdx = cp->signature_ref_index_at(nameAndTypeIdx); aoqi@0: Symbol* signature = cp->symbol_at(signatureIdx); aoqi@0: aoqi@0: // Parse signature (espcially simple for fields) aoqi@0: assert(signature->utf8_length() > 0, "field signatures cannot have zero length"); aoqi@0: // The signature is UFT8 encoded, but the first char is always ASCII for signatures. aoqi@0: char sigch = (char)*(signature->base()); aoqi@0: CellTypeState temp[4]; aoqi@0: CellTypeState *eff = sigchar_to_effect(sigch, bci, temp); aoqi@0: aoqi@0: CellTypeState in[4]; aoqi@0: CellTypeState *out; aoqi@0: int i = 0; aoqi@0: aoqi@0: if (is_get) { aoqi@0: out = eff; aoqi@0: } else { aoqi@0: out = epsilonCTS; aoqi@0: i = copy_cts(in, eff); aoqi@0: } aoqi@0: if (!is_static) in[i++] = CellTypeState::ref; aoqi@0: in[i] = CellTypeState::bottom; aoqi@0: assert(i<=3, "sanity check"); aoqi@0: pp(in, out); aoqi@0: } aoqi@0: aoqi@0: void GenerateOopMap::do_method(int is_static, int is_interface, int idx, int bci) { aoqi@0: // Dig up signature for field in constant pool aoqi@0: ConstantPool* cp = _method->constants(); aoqi@0: Symbol* signature = cp->signature_ref_at(idx); aoqi@0: aoqi@0: // Parse method signature aoqi@0: CellTypeState out[4]; aoqi@0: CellTypeState in[MAXARGSIZE+1]; // Includes result aoqi@0: ComputeCallStack cse(signature); aoqi@0: aoqi@0: // Compute return type aoqi@0: int res_length= cse.compute_for_returntype(out); aoqi@0: aoqi@0: // Temporary hack. aoqi@0: if (out[0].equal(CellTypeState::ref) && out[1].equal(CellTypeState::bottom)) { aoqi@0: out[0] = CellTypeState::make_line_ref(bci); aoqi@0: } aoqi@0: aoqi@0: assert(res_length<=4, "max value should be vv"); aoqi@0: aoqi@0: // Compute arguments aoqi@0: int arg_length = cse.compute_for_parameters(is_static != 0, in); aoqi@0: assert(arg_length<=MAXARGSIZE, "too many locals"); aoqi@0: aoqi@0: // Pop arguments aoqi@0: for (int i = arg_length - 1; i >= 0; i--) ppop1(in[i]);// Do args in reverse order. aoqi@0: aoqi@0: // Report results aoqi@0: if (_report_result_for_send == true) { aoqi@0: fill_stackmap_for_opcodes(_itr_send, vars(), stack(), _stack_top); aoqi@0: _report_result_for_send = false; aoqi@0: } aoqi@0: aoqi@0: // Push return address aoqi@0: ppush(out); aoqi@0: } aoqi@0: aoqi@0: // This is used to parse the signature for fields, since they are very simple... aoqi@0: CellTypeState *GenerateOopMap::sigchar_to_effect(char sigch, int bci, CellTypeState *out) { aoqi@0: // Object and array aoqi@0: if (sigch=='L' || sigch=='[') { aoqi@0: out[0] = CellTypeState::make_line_ref(bci); aoqi@0: out[1] = CellTypeState::bottom; aoqi@0: return out; aoqi@0: } aoqi@0: if (sigch == 'J' || sigch == 'D' ) return vvCTS; // Long and Double aoqi@0: if (sigch == 'V' ) return epsilonCTS; // Void aoqi@0: return vCTS; // Otherwise aoqi@0: } aoqi@0: aoqi@0: long GenerateOopMap::_total_byte_count = 0; aoqi@0: elapsedTimer GenerateOopMap::_total_oopmap_time; aoqi@0: aoqi@0: // This function assumes "bcs" is at a "ret" instruction and that the vars aoqi@0: // state is valid for that instruction. Furthermore, the ret instruction aoqi@0: // must be the last instruction in "bb" (we store information about the aoqi@0: // "ret" in "bb"). aoqi@0: void GenerateOopMap::ret_jump_targets_do(BytecodeStream *bcs, jmpFct_t jmpFct, int varNo, int *data) { aoqi@0: CellTypeState ra = vars()[varNo]; aoqi@0: if (!ra.is_good_address()) { aoqi@0: verify_error("ret returns from two jsr subroutines?"); aoqi@0: return; aoqi@0: } aoqi@0: int target = ra.get_info(); aoqi@0: aoqi@0: RetTableEntry* rtEnt = _rt.find_jsrs_for_target(target); aoqi@0: int bci = bcs->bci(); aoqi@0: for (int i = 0; i < rtEnt->nof_jsrs(); i++) { aoqi@0: int target_bci = rtEnt->jsrs(i); aoqi@0: // Make sure a jrtRet does not set the changed bit for dead basicblock. aoqi@0: BasicBlock* jsr_bb = get_basic_block_containing(target_bci - 1); aoqi@0: debug_only(BasicBlock* target_bb = &jsr_bb[1];) aoqi@0: assert(target_bb == get_basic_block_at(target_bci), "wrong calc. of successor basicblock"); aoqi@0: bool alive = jsr_bb->is_alive(); aoqi@0: if (TraceNewOopMapGeneration) { aoqi@0: tty->print("pc = %d, ret -> %d alive: %s\n", bci, target_bci, alive ? "true" : "false"); aoqi@0: } aoqi@0: if (alive) jmpFct(this, target_bci, data); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // aoqi@0: // Debug method aoqi@0: // aoqi@0: char* GenerateOopMap::state_vec_to_string(CellTypeState* vec, int len) { aoqi@0: #ifdef ASSERT aoqi@0: int checklen = MAX3(_max_locals, _max_stack, _max_monitors) + 1; aoqi@0: assert(len < checklen, "state_vec_buf overflow"); aoqi@0: #endif aoqi@0: for (int i = 0; i < len; i++) _state_vec_buf[i] = vec[i].to_char(); aoqi@0: _state_vec_buf[len] = 0; aoqi@0: return _state_vec_buf; aoqi@0: } aoqi@0: aoqi@0: void GenerateOopMap::print_time() { aoqi@0: tty->print_cr ("Accumulated oopmap times:"); aoqi@0: tty->print_cr ("---------------------------"); aoqi@0: tty->print_cr (" Total : %3.3f sec.", GenerateOopMap::_total_oopmap_time.seconds()); aoqi@0: tty->print_cr (" (%3.0f bytecodes per sec) ", aoqi@0: GenerateOopMap::_total_byte_count / GenerateOopMap::_total_oopmap_time.seconds()); aoqi@0: } aoqi@0: aoqi@0: // aoqi@0: // ============ Main Entry Point =========== aoqi@0: // aoqi@0: GenerateOopMap::GenerateOopMap(methodHandle method) { aoqi@0: // We have to initialize all variables here, that can be queried directly aoqi@0: _method = method; aoqi@0: _max_locals=0; aoqi@0: _init_vars = NULL; aoqi@0: aoqi@0: #ifndef PRODUCT aoqi@0: // If we are doing a detailed trace, include the regular trace information. aoqi@0: if (TraceNewOopMapGenerationDetailed) { aoqi@0: TraceNewOopMapGeneration = true; aoqi@0: } aoqi@0: #endif aoqi@0: } aoqi@0: aoqi@0: void GenerateOopMap::compute_map(TRAPS) { aoqi@0: #ifndef PRODUCT aoqi@0: if (TimeOopMap2) { aoqi@0: method()->print_short_name(tty); aoqi@0: tty->print(" "); aoqi@0: } aoqi@0: if (TimeOopMap) { aoqi@0: _total_byte_count += method()->code_size(); aoqi@0: } aoqi@0: #endif aoqi@0: TraceTime t_single("oopmap time", TimeOopMap2); aoqi@0: TraceTime t_all(NULL, &_total_oopmap_time, TimeOopMap); aoqi@0: aoqi@0: // Initialize values aoqi@0: _got_error = false; aoqi@0: _conflict = false; aoqi@0: _max_locals = method()->max_locals(); aoqi@0: _max_stack = method()->max_stack(); aoqi@0: _has_exceptions = (method()->has_exception_handler()); aoqi@0: _nof_refval_conflicts = 0; aoqi@0: _init_vars = new GrowableArray(5); // There are seldom more than 5 init_vars aoqi@0: _report_result = false; aoqi@0: _report_result_for_send = false; aoqi@0: _new_var_map = NULL; aoqi@0: _ret_adr_tos = new GrowableArray(5); // 5 seems like a good number; aoqi@0: _did_rewriting = false; aoqi@0: _did_relocation = false; aoqi@0: aoqi@0: if (TraceNewOopMapGeneration) { aoqi@0: tty->print("Method name: %s\n", method()->name()->as_C_string()); aoqi@0: if (Verbose) { aoqi@0: _method->print_codes(); aoqi@0: tty->print_cr("Exception table:"); aoqi@0: ExceptionTable excps(method()); aoqi@0: for(int i = 0; i < excps.length(); i ++) { aoqi@0: tty->print_cr("[%d - %d] -> %d", aoqi@0: excps.start_pc(i), excps.end_pc(i), excps.handler_pc(i)); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // if no code - do nothing aoqi@0: // compiler needs info aoqi@0: if (method()->code_size() == 0 || _max_locals + method()->max_stack() == 0) { aoqi@0: fill_stackmap_prolog(0); aoqi@0: fill_stackmap_epilog(); aoqi@0: return; aoqi@0: } aoqi@0: // Step 1: Compute all jump targets and their return value aoqi@0: if (!_got_error) aoqi@0: _rt.compute_ret_table(_method); aoqi@0: aoqi@0: // Step 2: Find all basic blocks and count GC points aoqi@0: if (!_got_error) aoqi@0: mark_bbheaders_and_count_gc_points(); aoqi@0: aoqi@0: // Step 3: Calculate stack maps aoqi@0: if (!_got_error) aoqi@0: do_interpretation(); aoqi@0: aoqi@0: // Step 4:Return results aoqi@0: if (!_got_error && report_results()) aoqi@0: report_result(); aoqi@0: aoqi@0: if (_got_error) { aoqi@0: THROW_HANDLE(_exception); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // Error handling methods aoqi@0: // These methods create an exception for the current thread which is thrown aoqi@0: // at the bottom of the call stack, when it returns to compute_map(). The aoqi@0: // _got_error flag controls execution. NOT TODO: The VM exception propagation aoqi@0: // mechanism using TRAPS/CHECKs could be used here instead but it would need aoqi@0: // to be added as a parameter to every function and checked for every call. aoqi@0: // The tons of extra code it would generate didn't seem worth the change. aoqi@0: // aoqi@0: void GenerateOopMap::error_work(const char *format, va_list ap) { aoqi@0: _got_error = true; aoqi@0: char msg_buffer[512]; aoqi@0: vsnprintf(msg_buffer, sizeof(msg_buffer), format, ap); aoqi@0: // Append method name aoqi@0: char msg_buffer2[512]; aoqi@0: jio_snprintf(msg_buffer2, sizeof(msg_buffer2), "%s in method %s", msg_buffer, method()->name()->as_C_string()); aoqi@0: _exception = Exceptions::new_exception(Thread::current(), aoqi@0: vmSymbols::java_lang_LinkageError(), msg_buffer2); aoqi@0: } aoqi@0: aoqi@0: void GenerateOopMap::report_error(const char *format, ...) { aoqi@0: va_list ap; aoqi@0: va_start(ap, format); aoqi@0: error_work(format, ap); aoqi@0: } aoqi@0: aoqi@0: void GenerateOopMap::verify_error(const char *format, ...) { aoqi@0: // We do not distinguish between different types of errors for verification aoqi@0: // errors. Let the verifier give a better message. aoqi@0: const char *msg = "Illegal class file encountered. Try running with -Xverify:all"; aoqi@0: _got_error = true; aoqi@0: // Append method name aoqi@0: char msg_buffer2[512]; aoqi@0: jio_snprintf(msg_buffer2, sizeof(msg_buffer2), "%s in method %s", msg, aoqi@0: method()->name()->as_C_string()); aoqi@0: _exception = Exceptions::new_exception(Thread::current(), aoqi@0: vmSymbols::java_lang_LinkageError(), msg_buffer2); aoqi@0: } aoqi@0: aoqi@0: // aoqi@0: // Report result opcodes aoqi@0: // aoqi@0: void GenerateOopMap::report_result() { aoqi@0: aoqi@0: if (TraceNewOopMapGeneration) tty->print_cr("Report result pass"); aoqi@0: aoqi@0: // We now want to report the result of the parse aoqi@0: _report_result = true; aoqi@0: aoqi@0: // Prolog code aoqi@0: fill_stackmap_prolog(_gc_points); aoqi@0: aoqi@0: // Mark everything changed, then do one interpretation pass. aoqi@0: for (int i = 0; i<_bb_count; i++) { aoqi@0: if (_basic_blocks[i].is_reachable()) { aoqi@0: _basic_blocks[i].set_changed(true); aoqi@0: interp_bb(&_basic_blocks[i]); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // Note: Since we are skipping dead-code when we are reporting results, then aoqi@0: // the no. of encountered gc-points might be fewer than the previously number aoqi@0: // we have counted. (dead-code is a pain - it should be removed before we get here) aoqi@0: fill_stackmap_epilog(); aoqi@0: aoqi@0: // Report initvars aoqi@0: fill_init_vars(_init_vars); aoqi@0: aoqi@0: _report_result = false; aoqi@0: } aoqi@0: aoqi@0: void GenerateOopMap::result_for_basicblock(int bci) { aoqi@0: if (TraceNewOopMapGeneration) tty->print_cr("Report result pass for basicblock"); aoqi@0: aoqi@0: // We now want to report the result of the parse aoqi@0: _report_result = true; aoqi@0: aoqi@0: // Find basicblock and report results aoqi@0: BasicBlock* bb = get_basic_block_containing(bci); aoqi@0: guarantee(bb != NULL, "no basic block for bci"); aoqi@0: assert(bb->is_reachable(), "getting result from unreachable basicblock"); aoqi@0: bb->set_changed(true); aoqi@0: interp_bb(bb); aoqi@0: } aoqi@0: aoqi@0: // aoqi@0: // Conflict handling code aoqi@0: // aoqi@0: aoqi@0: void GenerateOopMap::record_refval_conflict(int varNo) { aoqi@0: assert(varNo>=0 && varNo< _max_locals, "index out of range"); aoqi@0: aoqi@0: if (TraceOopMapRewrites) { aoqi@0: tty->print("### Conflict detected (local no: %d)\n", varNo); aoqi@0: } aoqi@0: aoqi@0: if (!_new_var_map) { aoqi@0: _new_var_map = NEW_RESOURCE_ARRAY(int, _max_locals); aoqi@0: for (int k = 0; k < _max_locals; k++) _new_var_map[k] = k; aoqi@0: } aoqi@0: aoqi@0: if ( _new_var_map[varNo] == varNo) { aoqi@0: // Check if max. number of locals has been reached aoqi@0: if (_max_locals + _nof_refval_conflicts >= MAX_LOCAL_VARS) { aoqi@0: report_error("Rewriting exceeded local variable limit"); aoqi@0: return; aoqi@0: } aoqi@0: _new_var_map[varNo] = _max_locals + _nof_refval_conflicts; aoqi@0: _nof_refval_conflicts++; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: void GenerateOopMap::rewrite_refval_conflicts() aoqi@0: { aoqi@0: // We can get here two ways: Either a rewrite conflict was detected, or aoqi@0: // an uninitialize reference was detected. In the second case, we do not aoqi@0: // do any rewriting, we just want to recompute the reference set with the aoqi@0: // new information aoqi@0: aoqi@0: int nof_conflicts = 0; // Used for debugging only aoqi@0: aoqi@0: if ( _nof_refval_conflicts == 0 ) aoqi@0: return; aoqi@0: aoqi@0: // Check if rewrites are allowed in this parse. aoqi@0: if (!allow_rewrites() && !IgnoreRewrites) { aoqi@0: fatal("Rewriting method not allowed at this stage"); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: // This following flag is to tempoary supress rewrites. The locals that might conflict will aoqi@0: // all be set to contain values. This is UNSAFE - however, until the rewriting has been completely aoqi@0: // tested it is nice to have. aoqi@0: if (IgnoreRewrites) { aoqi@0: if (Verbose) { aoqi@0: tty->print("rewrites suppressed for local no. "); aoqi@0: for (int l = 0; l < _max_locals; l++) { aoqi@0: if (_new_var_map[l] != l) { aoqi@0: tty->print("%d ", l); aoqi@0: vars()[l] = CellTypeState::value; aoqi@0: } aoqi@0: } aoqi@0: tty->cr(); aoqi@0: } aoqi@0: aoqi@0: // That was that... aoqi@0: _new_var_map = NULL; aoqi@0: _nof_refval_conflicts = 0; aoqi@0: _conflict = false; aoqi@0: aoqi@0: return; aoqi@0: } aoqi@0: aoqi@0: // Tracing flag aoqi@0: _did_rewriting = true; aoqi@0: aoqi@0: if (TraceOopMapRewrites) { aoqi@0: tty->print_cr("ref/value conflict for method %s - bytecodes are getting rewritten", method()->name()->as_C_string()); aoqi@0: method()->print(); aoqi@0: method()->print_codes(); aoqi@0: } aoqi@0: aoqi@0: assert(_new_var_map!=NULL, "nothing to rewrite"); aoqi@0: assert(_conflict==true, "We should not be here"); aoqi@0: aoqi@0: compute_ret_adr_at_TOS(); aoqi@0: if (!_got_error) { aoqi@0: for (int k = 0; k < _max_locals && !_got_error; k++) { aoqi@0: if (_new_var_map[k] != k) { aoqi@0: if (TraceOopMapRewrites) { aoqi@0: tty->print_cr("Rewriting: %d -> %d", k, _new_var_map[k]); aoqi@0: } aoqi@0: rewrite_refval_conflict(k, _new_var_map[k]); aoqi@0: if (_got_error) return; aoqi@0: nof_conflicts++; aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: assert(nof_conflicts == _nof_refval_conflicts, "sanity check"); aoqi@0: aoqi@0: // Adjust the number of locals aoqi@0: method()->set_max_locals(_max_locals+_nof_refval_conflicts); aoqi@0: _max_locals += _nof_refval_conflicts; aoqi@0: aoqi@0: // That was that... aoqi@0: _new_var_map = NULL; aoqi@0: _nof_refval_conflicts = 0; aoqi@0: } aoqi@0: aoqi@0: void GenerateOopMap::rewrite_refval_conflict(int from, int to) { aoqi@0: bool startOver; aoqi@0: do { aoqi@0: // Make sure that the BytecodeStream is constructed in the loop, since aoqi@0: // during rewriting a new method oop is going to be used, and the next time aoqi@0: // around we want to use that. aoqi@0: BytecodeStream bcs(_method); aoqi@0: startOver = false; aoqi@0: aoqi@0: while( !startOver && !_got_error && aoqi@0: // test bcs in case method changed and it became invalid aoqi@0: bcs.next() >=0) { aoqi@0: startOver = rewrite_refval_conflict_inst(&bcs, from, to); aoqi@0: } aoqi@0: } while (startOver && !_got_error); aoqi@0: } aoqi@0: aoqi@0: /* If the current instruction is one that uses local variable "from" aoqi@0: in a ref way, change it to use "to". There's a subtle reason why we aoqi@0: renumber the ref uses and not the non-ref uses: non-ref uses may be aoqi@0: 2 slots wide (double, long) which would necessitate keeping track of aoqi@0: whether we should add one or two variables to the method. If the change aoqi@0: affected the width of some instruction, returns "TRUE"; otherwise, returns "FALSE". aoqi@0: Another reason for moving ref's value is for solving (addr, ref) conflicts, which aoqi@0: both uses aload/astore methods. aoqi@0: */ aoqi@0: bool GenerateOopMap::rewrite_refval_conflict_inst(BytecodeStream *itr, int from, int to) { aoqi@0: Bytecodes::Code bc = itr->code(); aoqi@0: int index; aoqi@0: int bci = itr->bci(); aoqi@0: aoqi@0: if (is_aload(itr, &index) && index == from) { aoqi@0: if (TraceOopMapRewrites) { aoqi@0: tty->print_cr("Rewriting aload at bci: %d", bci); aoqi@0: } aoqi@0: return rewrite_load_or_store(itr, Bytecodes::_aload, Bytecodes::_aload_0, to); aoqi@0: } aoqi@0: aoqi@0: if (is_astore(itr, &index) && index == from) { aoqi@0: if (!stack_top_holds_ret_addr(bci)) { aoqi@0: if (TraceOopMapRewrites) { aoqi@0: tty->print_cr("Rewriting astore at bci: %d", bci); aoqi@0: } aoqi@0: return rewrite_load_or_store(itr, Bytecodes::_astore, Bytecodes::_astore_0, to); aoqi@0: } else { aoqi@0: if (TraceOopMapRewrites) { aoqi@0: tty->print_cr("Supress rewriting of astore at bci: %d", bci); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: return false; aoqi@0: } aoqi@0: aoqi@0: // The argument to this method is: aoqi@0: // bc : Current bytecode aoqi@0: // bcN : either _aload or _astore aoqi@0: // bc0 : either _aload_0 or _astore_0 aoqi@0: bool GenerateOopMap::rewrite_load_or_store(BytecodeStream *bcs, Bytecodes::Code bcN, Bytecodes::Code bc0, unsigned int varNo) { aoqi@0: assert(bcN == Bytecodes::_astore || bcN == Bytecodes::_aload, "wrong argument (bcN)"); aoqi@0: assert(bc0 == Bytecodes::_astore_0 || bc0 == Bytecodes::_aload_0, "wrong argument (bc0)"); aoqi@0: int ilen = Bytecodes::length_at(_method(), bcs->bcp()); aoqi@0: int newIlen; aoqi@0: aoqi@0: if (ilen == 4) { aoqi@0: // Original instruction was wide; keep it wide for simplicity aoqi@0: newIlen = 4; aoqi@0: } else if (varNo < 4) aoqi@0: newIlen = 1; aoqi@0: else if (varNo >= 256) aoqi@0: newIlen = 4; aoqi@0: else aoqi@0: newIlen = 2; aoqi@0: aoqi@0: // If we need to relocate in order to patch the byte, we aoqi@0: // do the patching in a temp. buffer, that is passed to the reloc. aoqi@0: // The patching of the bytecode stream is then done by the Relocator. aoqi@0: // This is neccesary, since relocating the instruction at a certain bci, might aoqi@0: // also relocate that instruction, e.g., if a _goto before it gets widen to a _goto_w. aoqi@0: // Hence, we do not know which bci to patch after relocation. aoqi@0: aoqi@0: assert(newIlen <= 4, "sanity check"); aoqi@0: u_char inst_buffer[4]; // Max. instruction size is 4. aoqi@0: address bcp; aoqi@0: aoqi@0: if (newIlen != ilen) { aoqi@0: // Relocation needed do patching in temp. buffer aoqi@0: bcp = (address)inst_buffer; aoqi@0: } else { aoqi@0: bcp = _method->bcp_from(bcs->bci()); aoqi@0: } aoqi@0: aoqi@0: // Patch either directly in Method* or in temp. buffer aoqi@0: if (newIlen == 1) { aoqi@0: assert(varNo < 4, "varNo too large"); aoqi@0: *bcp = bc0 + varNo; aoqi@0: } else if (newIlen == 2) { aoqi@0: assert(varNo < 256, "2-byte index needed!"); aoqi@0: *(bcp + 0) = bcN; aoqi@0: *(bcp + 1) = varNo; aoqi@0: } else { aoqi@0: assert(newIlen == 4, "Wrong instruction length"); aoqi@0: *(bcp + 0) = Bytecodes::_wide; aoqi@0: *(bcp + 1) = bcN; aoqi@0: Bytes::put_Java_u2(bcp+2, varNo); aoqi@0: } aoqi@0: aoqi@0: if (newIlen != ilen) { aoqi@0: expand_current_instr(bcs->bci(), ilen, newIlen, inst_buffer); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: return (newIlen != ilen); aoqi@0: } aoqi@0: aoqi@0: class RelocCallback : public RelocatorListener { aoqi@0: private: aoqi@0: GenerateOopMap* _gom; aoqi@0: public: aoqi@0: RelocCallback(GenerateOopMap* gom) { _gom = gom; }; aoqi@0: aoqi@0: // Callback method aoqi@0: virtual void relocated(int bci, int delta, int new_code_length) { aoqi@0: _gom->update_basic_blocks (bci, delta, new_code_length); aoqi@0: _gom->update_ret_adr_at_TOS(bci, delta); aoqi@0: _gom->_rt.update_ret_table (bci, delta); aoqi@0: } aoqi@0: }; aoqi@0: aoqi@0: // Returns true if expanding was succesful. Otherwise, reports an error and aoqi@0: // returns false. aoqi@0: void GenerateOopMap::expand_current_instr(int bci, int ilen, int newIlen, u_char inst_buffer[]) { aoqi@0: Thread *THREAD = Thread::current(); // Could really have TRAPS argument. aoqi@0: RelocCallback rcb(this); aoqi@0: Relocator rc(_method, &rcb); aoqi@0: methodHandle m= rc.insert_space_at(bci, newIlen, inst_buffer, THREAD); aoqi@0: if (m.is_null() || HAS_PENDING_EXCEPTION) { aoqi@0: report_error("could not rewrite method - exception occurred or bytecode buffer overflow"); aoqi@0: return; aoqi@0: } aoqi@0: aoqi@0: // Relocator returns a new method oop. aoqi@0: _did_relocation = true; aoqi@0: _method = m; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: bool GenerateOopMap::is_astore(BytecodeStream *itr, int *index) { aoqi@0: Bytecodes::Code bc = itr->code(); aoqi@0: switch(bc) { aoqi@0: case Bytecodes::_astore_0: aoqi@0: case Bytecodes::_astore_1: aoqi@0: case Bytecodes::_astore_2: aoqi@0: case Bytecodes::_astore_3: aoqi@0: *index = bc - Bytecodes::_astore_0; aoqi@0: return true; aoqi@0: case Bytecodes::_astore: aoqi@0: *index = itr->get_index(); aoqi@0: return true; aoqi@0: } aoqi@0: return false; aoqi@0: } aoqi@0: aoqi@0: bool GenerateOopMap::is_aload(BytecodeStream *itr, int *index) { aoqi@0: Bytecodes::Code bc = itr->code(); aoqi@0: switch(bc) { aoqi@0: case Bytecodes::_aload_0: aoqi@0: case Bytecodes::_aload_1: aoqi@0: case Bytecodes::_aload_2: aoqi@0: case Bytecodes::_aload_3: aoqi@0: *index = bc - Bytecodes::_aload_0; aoqi@0: return true; aoqi@0: aoqi@0: case Bytecodes::_aload: aoqi@0: *index = itr->get_index(); aoqi@0: return true; aoqi@0: } aoqi@0: return false; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: // Return true iff the top of the operand stack holds a return address at aoqi@0: // the current instruction aoqi@0: bool GenerateOopMap::stack_top_holds_ret_addr(int bci) { aoqi@0: for(int i = 0; i < _ret_adr_tos->length(); i++) { aoqi@0: if (_ret_adr_tos->at(i) == bci) aoqi@0: return true; aoqi@0: } aoqi@0: aoqi@0: return false; aoqi@0: } aoqi@0: aoqi@0: void GenerateOopMap::compute_ret_adr_at_TOS() { aoqi@0: assert(_ret_adr_tos != NULL, "must be initialized"); aoqi@0: _ret_adr_tos->clear(); aoqi@0: aoqi@0: for (int i = 0; i < bb_count(); i++) { aoqi@0: BasicBlock* bb = &_basic_blocks[i]; aoqi@0: aoqi@0: // Make sure to only check basicblocks that are reachable aoqi@0: if (bb->is_reachable()) { aoqi@0: aoqi@0: // For each Basic block we check all instructions aoqi@0: BytecodeStream bcs(_method); aoqi@0: bcs.set_interval(bb->_bci, next_bb_start_pc(bb)); aoqi@0: aoqi@0: restore_state(bb); aoqi@0: aoqi@0: while (bcs.next()>=0 && !_got_error) { aoqi@0: // TDT: should this be is_good_address() ? aoqi@0: if (_stack_top > 0 && stack()[_stack_top-1].is_address()) { aoqi@0: _ret_adr_tos->append(bcs.bci()); aoqi@0: if (TraceNewOopMapGeneration) { aoqi@0: tty->print_cr("Ret_adr TOS at bci: %d", bcs.bci()); aoqi@0: } aoqi@0: } aoqi@0: interp1(&bcs); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: void GenerateOopMap::update_ret_adr_at_TOS(int bci, int delta) { aoqi@0: for(int i = 0; i < _ret_adr_tos->length(); i++) { aoqi@0: int v = _ret_adr_tos->at(i); aoqi@0: if (v > bci) _ret_adr_tos->at_put(i, v + delta); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // =================================================================== aoqi@0: aoqi@0: #ifndef PRODUCT aoqi@0: int ResolveOopMapConflicts::_nof_invocations = 0; aoqi@0: int ResolveOopMapConflicts::_nof_rewrites = 0; aoqi@0: int ResolveOopMapConflicts::_nof_relocations = 0; aoqi@0: #endif aoqi@0: aoqi@0: methodHandle ResolveOopMapConflicts::do_potential_rewrite(TRAPS) { aoqi@0: compute_map(CHECK_(methodHandle())); aoqi@0: aoqi@0: #ifndef PRODUCT aoqi@0: // Tracking and statistics aoqi@0: if (PrintRewrites) { aoqi@0: _nof_invocations++; aoqi@0: if (did_rewriting()) { aoqi@0: _nof_rewrites++; aoqi@0: if (did_relocation()) _nof_relocations++; aoqi@0: tty->print("Method was rewritten %s: ", (did_relocation()) ? "and relocated" : ""); aoqi@0: method()->print_value(); tty->cr(); aoqi@0: tty->print_cr("Cand.: %d rewrts: %d (%d%%) reloc.: %d (%d%%)", aoqi@0: _nof_invocations, aoqi@0: _nof_rewrites, (_nof_rewrites * 100) / _nof_invocations, aoqi@0: _nof_relocations, (_nof_relocations * 100) / _nof_invocations); aoqi@0: } aoqi@0: } aoqi@0: #endif aoqi@0: return methodHandle(THREAD, method()); aoqi@0: }