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