1405N/A/*
5253N/A * Copyright (c) 1998, 2010, Oracle and/or its affiliates. All rights reserved.
1405N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1405N/A *
1405N/A * This code is free software; you can redistribute it and/or modify it
1405N/A * under the terms of the GNU General Public License version 2 only, as
1405N/A * published by the Free Software Foundation.
1405N/A *
1405N/A * This code is distributed in the hope that it will be useful, but WITHOUT
1405N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1405N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
1405N/A * version 2 for more details (a copy is included in the LICENSE file that
1405N/A * accompanied this code).
1405N/A *
1405N/A * You should have received a copy of the GNU General Public License version
1405N/A * 2 along with this work; if not, write to the Free Software Foundation,
1405N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1405N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
1405N/A *
1405N/A */
1405N/A
1405N/A#include "precompiled.hpp"
1405N/A#include "code/exceptionHandlerTable.hpp"
1405N/A#include "code/nmethod.hpp"
1405N/A#include "memory/allocation.inline.hpp"
5254N/A
5254N/Avoid ExceptionHandlerTable::add_entry(HandlerTableEntry entry) {
1405N/A _nesting.check();
1405N/A if (_length >= _size) {
1405N/A // not enough space => grow the table (amortized growth, double its size)
1405N/A guarantee(_size > 0, "no space allocated => cannot grow the table since it is part of nmethod");
1405N/A int new_size = _size * 2;
1405N/A _table = REALLOC_RESOURCE_ARRAY(HandlerTableEntry, _table, _size, new_size);
1405N/A _size = new_size;
1405N/A }
1405N/A assert(_length < _size, "sanity check");
1405N/A _table[_length++] = entry;
5253N/A}
5253N/A
5253N/A
5253N/AHandlerTableEntry* ExceptionHandlerTable::subtable_for(int catch_pco) const {
5253N/A int i = 0;
5253N/A while (i < _length) {
5253N/A HandlerTableEntry* t = _table + i;
5253N/A if (t->pco() == catch_pco) {
5253N/A // found subtable matching the catch_pco
5253N/A return t;
5253N/A } else {
1405N/A // advance to next subtable
1405N/A i += t->len() + 1; // +1 for header
1405N/A }
1405N/A }
1405N/A return NULL;
1405N/A}
1405N/A
1405N/A
1405N/AExceptionHandlerTable::ExceptionHandlerTable(int initial_size) {
1405N/A guarantee(initial_size > 0, "initial size must be > 0");
1405N/A _table = NEW_RESOURCE_ARRAY(HandlerTableEntry, initial_size);
1405N/A _length = 0;
1405N/A _size = initial_size;
1405N/A}
1405N/A
1405N/A
1405N/AExceptionHandlerTable::ExceptionHandlerTable(const nmethod* nm) {
1405N/A _table = (HandlerTableEntry*)nm->handler_table_begin();
1405N/A _length = nm->handler_table_size() / sizeof(HandlerTableEntry);
1405N/A _size = 0; // no space allocated by ExeptionHandlerTable!
1405N/A}
1405N/A
1405N/A
1405N/Avoid ExceptionHandlerTable::add_subtable(
1405N/A int catch_pco,
1405N/A GrowableArray<intptr_t>* handler_bcis,
1405N/A GrowableArray<intptr_t>* scope_depths_from_top_scope,
1405N/A GrowableArray<intptr_t>* handler_pcos
1405N/A) {
1405N/A assert(subtable_for(catch_pco) == NULL, "catch handlers for this catch_pco added twice");
1405N/A assert(handler_bcis->length() == handler_pcos->length(), "bci & pc table have different length");
1405N/A assert(scope_depths_from_top_scope == NULL || handler_bcis->length() == scope_depths_from_top_scope->length(), "bci & scope_depths table have different length");
1405N/A if (handler_bcis->length() > 0) {
1405N/A // add subtable header
1405N/A add_entry(HandlerTableEntry(handler_bcis->length(), catch_pco, 0));
1405N/A // add individual entries
1405N/A for (int i = 0; i < handler_bcis->length(); i++) {
1405N/A intptr_t scope_depth = 0;
1405N/A if (scope_depths_from_top_scope != NULL) {
1405N/A scope_depth = scope_depths_from_top_scope->at(i);
1405N/A }
1405N/A add_entry(HandlerTableEntry(handler_bcis->at(i), handler_pcos->at(i), scope_depth));
1405N/A assert(entry_for(catch_pco, handler_bcis->at(i), scope_depth)->pco() == handler_pcos->at(i), "entry not added correctly (1)");
1405N/A assert(entry_for(catch_pco, handler_bcis->at(i), scope_depth)->scope_depth() == scope_depth, "entry not added correctly (2)");
1405N/A }
1405N/A }
1405N/A}
1405N/A
1405N/A
1405N/Avoid ExceptionHandlerTable::copy_to(nmethod* nm) {
1405N/A assert(size_in_bytes() == nm->handler_table_size(), "size of space allocated in nmethod incorrect");
1405N/A memmove(nm->handler_table_begin(), _table, size_in_bytes());
1405N/A}
1405N/A
1405N/A
1405N/AHandlerTableEntry* ExceptionHandlerTable::entry_for(int catch_pco, int handler_bci, int scope_depth) const {
1405N/A HandlerTableEntry* t = subtable_for(catch_pco);
1405N/A if (t != NULL) {
1405N/A int l = t->len();
1405N/A while (l-- > 0) {
1405N/A t++;
1405N/A if (t->bci() == handler_bci && t->scope_depth() == scope_depth) return t;
1405N/A }
1405N/A }
1405N/A return NULL;
1405N/A}
1405N/A
1405N/A
1405N/Avoid ExceptionHandlerTable::print_subtable(HandlerTableEntry* t) const {
1405N/A int l = t->len();
1405N/A tty->print_cr("catch_pco = %d (%d entries)", t->pco(), l);
1405N/A while (l-- > 0) {
1405N/A t++;
1405N/A tty->print_cr(" bci %d at scope depth %d -> pco %d", t->bci(), t->scope_depth(), t->pco());
1405N/A }
1405N/A}
1405N/A
1405N/A
1405N/Avoid ExceptionHandlerTable::print() const {
1405N/A tty->print_cr("ExceptionHandlerTable (size = %d bytes)", size_in_bytes());
1405N/A int i = 0;
1405N/A while (i < _length) {
1405N/A HandlerTableEntry* t = _table + i;
1405N/A print_subtable(t);
// advance to next subtable
i += t->len() + 1; // +1 for header
}
}
void ExceptionHandlerTable::print_subtable_for(int catch_pco) const {
HandlerTableEntry* subtable = subtable_for(catch_pco);
if( subtable != NULL ) { print_subtable( subtable ); }
}
// ----------------------------------------------------------------------------
// Implicit null exception tables. Maps an exception PC offset to a
// continuation PC offset. During construction it's a variable sized
// array with a max size and current length. When stored inside an
// nmethod a zero length table takes no space. This is detected by
// nul_chk_table_size() == 0. Otherwise the table has a length word
// followed by pairs of <excp-offset, const-offset>.
void ImplicitExceptionTable::set_size( uint size ) {
_size = size;
_data = NEW_RESOURCE_ARRAY(implicit_null_entry, (size*2));
_len = 0;
}
void ImplicitExceptionTable::append( uint exec_off, uint cont_off ) {
assert( (sizeof(implicit_null_entry) >= 4) || (exec_off < 65535), "" );
assert( (sizeof(implicit_null_entry) >= 4) || (cont_off < 65535), "" );
uint l = len();
if (l == _size) {
uint old_size_in_elements = _size*2;
if (_size == 0) _size = 4;
_size *= 2;
uint new_size_in_elements = _size*2;
_data = REALLOC_RESOURCE_ARRAY(uint, _data, old_size_in_elements, new_size_in_elements);
}
*(adr(l) ) = exec_off;
*(adr(l)+1) = cont_off;
_len = l+1;
};
uint ImplicitExceptionTable::at( uint exec_off ) const {
uint l = len();
for( uint i=0; i<l; i++ )
if( *adr(i) == exec_off )
return *(adr(i)+1);
return 0; // Failed to find any execption offset
}
void ImplicitExceptionTable::print(address base) const {
tty->print("{");
for( uint i=0; i<len(); i++ )
tty->print("< "INTPTR_FORMAT", "INTPTR_FORMAT" > ",base + *adr(i), base + *(adr(i)+1));
tty->print_cr("}");
}
ImplicitExceptionTable::ImplicitExceptionTable(const nmethod* nm) {
if (nm->nul_chk_table_size() == 0) {
_len = 0;
_data = NULL;
} else {
// the first word is the length if non-zero, so read it out and
// skip to the next word to get the table.
_data = (implicit_null_entry*)nm->nul_chk_table_begin();
_len = _data[0];
_data++;
}
_size = len();
assert(size_in_bytes() <= nm->nul_chk_table_size(), "size of space allocated in nmethod incorrect");
}
void ImplicitExceptionTable::copy_to( nmethod* nm ) {
assert(size_in_bytes() <= nm->nul_chk_table_size(), "size of space allocated in nmethod incorrect");
if (len() != 0) {
implicit_null_entry* nmdata = (implicit_null_entry*)nm->nul_chk_table_begin();
// store the length in the first uint
nmdata[0] = _len;
nmdata++;
// copy the table after the length
memmove( nmdata, _data, 2 * len() * sizeof(implicit_null_entry));
} else {
// zero length table takes zero bytes
assert(size_in_bytes() == 0, "bad size");
assert(nm->nul_chk_table_size() == 0, "bad size");
}
}
void ImplicitExceptionTable::verify(nmethod *nm) const {
for (uint i = 0; i < len(); i++) {
if ((*adr(i) > (unsigned int)nm->insts_size()) ||
(*(adr(i)+1) > (unsigned int)nm->insts_size()))
fatal(err_msg("Invalid offset in ImplicitExceptionTable at " PTR_FORMAT, _data));
}
}