1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/vm/code/exceptionHandlerTable.cpp Sat Dec 01 00:00:00 2007 +0000 1.3 @@ -0,0 +1,226 @@ 1.4 +/* 1.5 + * Copyright 1998-2005 Sun Microsystems, Inc. All Rights Reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. 1.11 + * 1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 + * version 2 for more details (a copy is included in the LICENSE file that 1.16 + * accompanied this code). 1.17 + * 1.18 + * You should have received a copy of the GNU General Public License version 1.19 + * 2 along with this work; if not, write to the Free Software Foundation, 1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 + * 1.22 + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 1.23 + * CA 95054 USA or visit www.sun.com if you need additional information or 1.24 + * have any questions. 1.25 + * 1.26 + */ 1.27 + 1.28 +#include "incls/_precompiled.incl" 1.29 +#include "incls/_exceptionHandlerTable.cpp.incl" 1.30 + 1.31 +void ExceptionHandlerTable::add_entry(HandlerTableEntry entry) { 1.32 + _nesting.check(); 1.33 + if (_length >= _size) { 1.34 + // not enough space => grow the table (amortized growth, double its size) 1.35 + guarantee(_size > 0, "no space allocated => cannot grow the table since it is part of nmethod"); 1.36 + int new_size = _size * 2; 1.37 + _table = REALLOC_RESOURCE_ARRAY(HandlerTableEntry, _table, _size, new_size); 1.38 + _size = new_size; 1.39 + } 1.40 + assert(_length < _size, "sanity check"); 1.41 + _table[_length++] = entry; 1.42 +} 1.43 + 1.44 + 1.45 +HandlerTableEntry* ExceptionHandlerTable::subtable_for(int catch_pco) const { 1.46 + int i = 0; 1.47 + while (i < _length) { 1.48 + HandlerTableEntry* t = _table + i; 1.49 + if (t->pco() == catch_pco) { 1.50 + // found subtable matching the catch_pco 1.51 + return t; 1.52 + } else { 1.53 + // advance to next subtable 1.54 + i += t->len() + 1; // +1 for header 1.55 + } 1.56 + } 1.57 + return NULL; 1.58 +} 1.59 + 1.60 + 1.61 +ExceptionHandlerTable::ExceptionHandlerTable(int initial_size) { 1.62 + guarantee(initial_size > 0, "initial size must be > 0"); 1.63 + _table = NEW_RESOURCE_ARRAY(HandlerTableEntry, initial_size); 1.64 + _length = 0; 1.65 + _size = initial_size; 1.66 +} 1.67 + 1.68 + 1.69 +ExceptionHandlerTable::ExceptionHandlerTable(const nmethod* nm) { 1.70 + _table = (HandlerTableEntry*)nm->handler_table_begin(); 1.71 + _length = nm->handler_table_size() / sizeof(HandlerTableEntry); 1.72 + _size = 0; // no space allocated by ExeptionHandlerTable! 1.73 +} 1.74 + 1.75 + 1.76 +void ExceptionHandlerTable::add_subtable( 1.77 + int catch_pco, 1.78 + GrowableArray<intptr_t>* handler_bcis, 1.79 + GrowableArray<intptr_t>* scope_depths_from_top_scope, 1.80 + GrowableArray<intptr_t>* handler_pcos 1.81 +) { 1.82 + assert(subtable_for(catch_pco) == NULL, "catch handlers for this catch_pco added twice"); 1.83 + assert(handler_bcis->length() == handler_pcos->length(), "bci & pc table have different length"); 1.84 + assert(scope_depths_from_top_scope == NULL || handler_bcis->length() == scope_depths_from_top_scope->length(), "bci & scope_depths table have different length"); 1.85 + if (handler_bcis->length() > 0) { 1.86 + // add subtable header 1.87 + add_entry(HandlerTableEntry(handler_bcis->length(), catch_pco, 0)); 1.88 + // add individual entries 1.89 + for (int i = 0; i < handler_bcis->length(); i++) { 1.90 + intptr_t scope_depth = 0; 1.91 + if (scope_depths_from_top_scope != NULL) { 1.92 + scope_depth = scope_depths_from_top_scope->at(i); 1.93 + } 1.94 + add_entry(HandlerTableEntry(handler_bcis->at(i), handler_pcos->at(i), scope_depth)); 1.95 + assert(entry_for(catch_pco, handler_bcis->at(i), scope_depth)->pco() == handler_pcos->at(i), "entry not added correctly (1)"); 1.96 + assert(entry_for(catch_pco, handler_bcis->at(i), scope_depth)->scope_depth() == scope_depth, "entry not added correctly (2)"); 1.97 + } 1.98 + } 1.99 +} 1.100 + 1.101 + 1.102 +void ExceptionHandlerTable::copy_to(nmethod* nm) { 1.103 + assert(size_in_bytes() == nm->handler_table_size(), "size of space allocated in nmethod incorrect"); 1.104 + memmove(nm->handler_table_begin(), _table, size_in_bytes()); 1.105 +} 1.106 + 1.107 + 1.108 +HandlerTableEntry* ExceptionHandlerTable::entry_for(int catch_pco, int handler_bci, int scope_depth) const { 1.109 + HandlerTableEntry* t = subtable_for(catch_pco); 1.110 + if (t != NULL) { 1.111 + int l = t->len(); 1.112 + while (l-- > 0) { 1.113 + t++; 1.114 + if (t->bci() == handler_bci && t->scope_depth() == scope_depth) return t; 1.115 + } 1.116 + } 1.117 + return NULL; 1.118 +} 1.119 + 1.120 + 1.121 +void ExceptionHandlerTable::print_subtable(HandlerTableEntry* t) const { 1.122 + int l = t->len(); 1.123 + tty->print_cr("catch_pco = %d (%d entries)", t->pco(), l); 1.124 + while (l-- > 0) { 1.125 + t++; 1.126 + tty->print_cr(" bci %d at scope depth %d -> pco %d", t->bci(), t->scope_depth(), t->pco()); 1.127 + } 1.128 +} 1.129 + 1.130 + 1.131 +void ExceptionHandlerTable::print() const { 1.132 + tty->print_cr("ExceptionHandlerTable (size = %d bytes)", size_in_bytes()); 1.133 + int i = 0; 1.134 + while (i < _length) { 1.135 + HandlerTableEntry* t = _table + i; 1.136 + print_subtable(t); 1.137 + // advance to next subtable 1.138 + i += t->len() + 1; // +1 for header 1.139 + } 1.140 +} 1.141 + 1.142 +void ExceptionHandlerTable::print_subtable_for(int catch_pco) const { 1.143 + HandlerTableEntry* subtable = subtable_for(catch_pco); 1.144 + 1.145 + if( subtable != NULL ) { print_subtable( subtable ); } 1.146 +} 1.147 + 1.148 +// ---------------------------------------------------------------------------- 1.149 +// Implicit null exception tables. Maps an exception PC offset to a 1.150 +// continuation PC offset. During construction it's a variable sized 1.151 +// array with a max size and current length. When stored inside an 1.152 +// nmethod a zero length table takes no space. This is detected by 1.153 +// nul_chk_table_size() == 0. Otherwise the table has a length word 1.154 +// followed by pairs of <excp-offset, const-offset>. 1.155 +void ImplicitExceptionTable::set_size( uint size ) { 1.156 + _size = size; 1.157 + _data = NEW_RESOURCE_ARRAY(implicit_null_entry, (size*2)); 1.158 + _len = 0; 1.159 +} 1.160 + 1.161 +void ImplicitExceptionTable::append( uint exec_off, uint cont_off ) { 1.162 + assert( (sizeof(implicit_null_entry) >= 4) || (exec_off < 65535), "" ); 1.163 + assert( (sizeof(implicit_null_entry) >= 4) || (cont_off < 65535), "" ); 1.164 + uint l = len(); 1.165 + if (l == _size) { 1.166 + uint old_size_in_elements = _size*2; 1.167 + if (_size == 0) _size = 4; 1.168 + _size *= 2; 1.169 + uint new_size_in_elements = _size*2; 1.170 + _data = REALLOC_RESOURCE_ARRAY(uint, _data, old_size_in_elements, new_size_in_elements); 1.171 + } 1.172 + *(adr(l) ) = exec_off; 1.173 + *(adr(l)+1) = cont_off; 1.174 + _len = l+1; 1.175 +}; 1.176 + 1.177 +uint ImplicitExceptionTable::at( uint exec_off ) const { 1.178 + uint l = len(); 1.179 + for( uint i=0; i<l; i++ ) 1.180 + if( *adr(i) == exec_off ) 1.181 + return *(adr(i)+1); 1.182 + return 0; // Failed to find any execption offset 1.183 +} 1.184 + 1.185 +void ImplicitExceptionTable::print(address base) const { 1.186 + tty->print("{"); 1.187 + for( uint i=0; i<len(); i++ ) 1.188 + tty->print("< "INTPTR_FORMAT", "INTPTR_FORMAT" > ",base + *adr(i), base + *(adr(i)+1)); 1.189 + tty->print_cr("}"); 1.190 +} 1.191 + 1.192 +ImplicitExceptionTable::ImplicitExceptionTable(const nmethod* nm) { 1.193 + if (nm->nul_chk_table_size() == 0) { 1.194 + _len = 0; 1.195 + _data = NULL; 1.196 + } else { 1.197 + // the first word is the length if non-zero, so read it out and 1.198 + // skip to the next word to get the table. 1.199 + _data = (implicit_null_entry*)nm->nul_chk_table_begin(); 1.200 + _len = _data[0]; 1.201 + _data++; 1.202 + } 1.203 + _size = len(); 1.204 + assert(size_in_bytes() <= nm->nul_chk_table_size(), "size of space allocated in nmethod incorrect"); 1.205 +} 1.206 + 1.207 +void ImplicitExceptionTable::copy_to( nmethod* nm ) { 1.208 + assert(size_in_bytes() <= nm->nul_chk_table_size(), "size of space allocated in nmethod incorrect"); 1.209 + if (len() != 0) { 1.210 + implicit_null_entry* nmdata = (implicit_null_entry*)nm->nul_chk_table_begin(); 1.211 + // store the length in the first uint 1.212 + nmdata[0] = _len; 1.213 + nmdata++; 1.214 + // copy the table after the length 1.215 + memmove( nmdata, _data, 2 * len() * sizeof(implicit_null_entry)); 1.216 + } else { 1.217 + // zero length table takes zero bytes 1.218 + assert(size_in_bytes() == 0, "bad size"); 1.219 + assert(nm->nul_chk_table_size() == 0, "bad size"); 1.220 + } 1.221 +} 1.222 + 1.223 +void ImplicitExceptionTable::verify(nmethod *nm) const { 1.224 + for (uint i = 0; i < len(); i++) { 1.225 + if ((*adr(i) > (unsigned int)nm->code_size()) || 1.226 + (*(adr(i)+1) > (unsigned int)nm->code_size())) 1.227 + fatal1("Invalid offset in ImplicitExceptionTable at %lx", _data); 1.228 + } 1.229 +}