1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/vm/ci/ciStreams.cpp Wed Apr 27 01:25:04 2016 +0800 1.3 @@ -0,0 +1,486 @@ 1.4 +/* 1.5 + * Copyright (c) 1999, 2012, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.23 + * or visit www.oracle.com if you need additional information or have any 1.24 + * questions. 1.25 + * 1.26 + */ 1.27 + 1.28 +#include "precompiled.hpp" 1.29 +#include "ci/ciCallSite.hpp" 1.30 +#include "ci/ciConstant.hpp" 1.31 +#include "ci/ciField.hpp" 1.32 +#include "ci/ciStreams.hpp" 1.33 +#include "ci/ciUtilities.hpp" 1.34 + 1.35 +// ciExceptionHandlerStream 1.36 +// 1.37 +// Walk over some selected set of a methods exception handlers. 1.38 + 1.39 +// ------------------------------------------------------------------ 1.40 +// ciExceptionHandlerStream::count 1.41 +// 1.42 +// How many exception handlers are there in this stream? 1.43 +// 1.44 +// Implementation note: Compiler2 needs this functionality, so I had 1.45 +int ciExceptionHandlerStream::count() { 1.46 + int save_pos = _pos; 1.47 + int save_end = _end; 1.48 + 1.49 + int count = 0; 1.50 + 1.51 + _pos = -1; 1.52 + _end = _method->_handler_count; 1.53 + 1.54 + 1.55 + next(); 1.56 + while (!is_done()) { 1.57 + count++; 1.58 + next(); 1.59 + } 1.60 + 1.61 + _pos = save_pos; 1.62 + _end = save_end; 1.63 + 1.64 + return count; 1.65 +} 1.66 + 1.67 +int ciExceptionHandlerStream::count_remaining() { 1.68 + int save_pos = _pos; 1.69 + int save_end = _end; 1.70 + 1.71 + int count = 0; 1.72 + 1.73 + while (!is_done()) { 1.74 + count++; 1.75 + next(); 1.76 + } 1.77 + 1.78 + _pos = save_pos; 1.79 + _end = save_end; 1.80 + 1.81 + return count; 1.82 +} 1.83 + 1.84 +// ciBytecodeStream 1.85 +// 1.86 +// The class is used to iterate over the bytecodes of a method. 1.87 +// It hides the details of constant pool structure/access by 1.88 +// providing accessors for constant pool items. 1.89 + 1.90 +// ------------------------------------------------------------------ 1.91 +// ciBytecodeStream::next_wide_or_table 1.92 +// 1.93 +// Special handling for switch ops 1.94 +Bytecodes::Code ciBytecodeStream::next_wide_or_table(Bytecodes::Code bc) { 1.95 + switch (bc) { // Check for special bytecode handling 1.96 + case Bytecodes::_wide: 1.97 + // Special handling for the wide bytcode 1.98 + // Get following bytecode; do not return wide 1.99 + assert(Bytecodes::Code(_pc[0]) == Bytecodes::_wide, ""); 1.100 + bc = Bytecodes::java_code(_raw_bc = (Bytecodes::Code)_pc[1]); 1.101 + assert(Bytecodes::wide_length_for(bc) > 2, "must make progress"); 1.102 + _pc += Bytecodes::wide_length_for(bc); 1.103 + _was_wide = _pc; // Flag last wide bytecode found 1.104 + assert(is_wide(), "accessor works right"); 1.105 + break; 1.106 + 1.107 + case Bytecodes::_lookupswitch: 1.108 + _pc++; // Skip wide bytecode 1.109 + _pc += (_start-_pc)&3; // Word align 1.110 + _table_base = (jint*)_pc; // Capture for later usage 1.111 + // table_base[0] is default far_dest 1.112 + // Table has 2 lead elements (default, length), then pairs of u4 values. 1.113 + // So load table length, and compute address at end of table 1.114 + _pc = (address)&_table_base[2+ 2*Bytes::get_Java_u4((address)&_table_base[1])]; 1.115 + break; 1.116 + 1.117 + case Bytecodes::_tableswitch: { 1.118 + _pc++; // Skip wide bytecode 1.119 + _pc += (_start-_pc)&3; // Word align 1.120 + _table_base = (jint*)_pc; // Capture for later usage 1.121 + // table_base[0] is default far_dest 1.122 + int lo = Bytes::get_Java_u4((address)&_table_base[1]);// Low bound 1.123 + int hi = Bytes::get_Java_u4((address)&_table_base[2]);// High bound 1.124 + int len = hi - lo + 1; // Dense table size 1.125 + _pc = (address)&_table_base[3+len]; // Skip past table 1.126 + break; 1.127 + } 1.128 + 1.129 + default: 1.130 + fatal("unhandled bytecode"); 1.131 + } 1.132 + return bc; 1.133 +} 1.134 + 1.135 +// ------------------------------------------------------------------ 1.136 +// ciBytecodeStream::reset_to_bci 1.137 +void ciBytecodeStream::reset_to_bci( int bci ) { 1.138 + _bc_start=_was_wide=0; 1.139 + _pc = _start+bci; 1.140 +} 1.141 + 1.142 +// ------------------------------------------------------------------ 1.143 +// ciBytecodeStream::force_bci 1.144 +void ciBytecodeStream::force_bci(int bci) { 1.145 + if (bci < 0) { 1.146 + reset_to_bci(0); 1.147 + _bc_start = _start + bci; 1.148 + _bc = EOBC(); 1.149 + } else { 1.150 + reset_to_bci(bci); 1.151 + next(); 1.152 + } 1.153 +} 1.154 + 1.155 + 1.156 +// ------------------------------------------------------------------ 1.157 +// Constant pool access 1.158 +// ------------------------------------------------------------------ 1.159 + 1.160 +// ------------------------------------------------------------------ 1.161 +// ciBytecodeStream::get_klass_index 1.162 +// 1.163 +// If this bytecodes references a klass, return the index of the 1.164 +// referenced klass. 1.165 +int ciBytecodeStream::get_klass_index() const { 1.166 + switch(cur_bc()) { 1.167 + case Bytecodes::_ldc: 1.168 + return get_index_u1(); 1.169 + case Bytecodes::_ldc_w: 1.170 + case Bytecodes::_ldc2_w: 1.171 + case Bytecodes::_checkcast: 1.172 + case Bytecodes::_instanceof: 1.173 + case Bytecodes::_anewarray: 1.174 + case Bytecodes::_multianewarray: 1.175 + case Bytecodes::_new: 1.176 + case Bytecodes::_newarray: 1.177 + return get_index_u2(); 1.178 + default: 1.179 + ShouldNotReachHere(); 1.180 + return 0; 1.181 + } 1.182 +} 1.183 + 1.184 +// ------------------------------------------------------------------ 1.185 +// ciBytecodeStream::get_klass 1.186 +// 1.187 +// If this bytecode is a new, newarray, multianewarray, instanceof, 1.188 +// or checkcast, get the referenced klass. 1.189 +ciKlass* ciBytecodeStream::get_klass(bool& will_link) { 1.190 + VM_ENTRY_MARK; 1.191 + constantPoolHandle cpool(_method->get_Method()->constants()); 1.192 + return CURRENT_ENV->get_klass_by_index(cpool, get_klass_index(), will_link, _holder); 1.193 +} 1.194 + 1.195 +// ------------------------------------------------------------------ 1.196 +// ciBytecodeStream::get_constant_raw_index 1.197 +// 1.198 +// If this bytecode is one of the ldc variants, get the index of the 1.199 +// referenced constant. 1.200 +int ciBytecodeStream::get_constant_raw_index() const { 1.201 + // work-alike for Bytecode_loadconstant::raw_index() 1.202 + switch (cur_bc()) { 1.203 + case Bytecodes::_ldc: 1.204 + return get_index_u1(); 1.205 + case Bytecodes::_ldc_w: 1.206 + case Bytecodes::_ldc2_w: 1.207 + return get_index_u2(); 1.208 + default: 1.209 + ShouldNotReachHere(); 1.210 + return 0; 1.211 + } 1.212 +} 1.213 + 1.214 +// ------------------------------------------------------------------ 1.215 +// ciBytecodeStream::get_constant_pool_index 1.216 +// Decode any reference index into a regular pool index. 1.217 +int ciBytecodeStream::get_constant_pool_index() const { 1.218 + // work-alike for Bytecode_loadconstant::pool_index() 1.219 + int index = get_constant_raw_index(); 1.220 + if (has_cache_index()) { 1.221 + VM_ENTRY_MARK; 1.222 + constantPoolHandle cpool(_method->get_Method()->constants()); 1.223 + return cpool->object_to_cp_index(index); 1.224 + } 1.225 + return index; 1.226 +} 1.227 + 1.228 +// ------------------------------------------------------------------ 1.229 +// ciBytecodeStream::get_constant_cache_index 1.230 +// Return the CP cache index, or -1 if there isn't any. 1.231 +int ciBytecodeStream::get_constant_cache_index() const { 1.232 + // work-alike for Bytecode_loadconstant::cache_index() 1.233 + return has_cache_index() ? get_constant_raw_index() : -1; 1.234 +} 1.235 + 1.236 +// ------------------------------------------------------------------ 1.237 +// ciBytecodeStream::get_constant 1.238 +// 1.239 +// If this bytecode is one of the ldc variants, get the referenced 1.240 +// constant. 1.241 +ciConstant ciBytecodeStream::get_constant() { 1.242 + int pool_index = get_constant_raw_index(); 1.243 + int cache_index = -1; 1.244 + if (has_cache_index()) { 1.245 + cache_index = pool_index; 1.246 + pool_index = -1; 1.247 + } 1.248 + VM_ENTRY_MARK; 1.249 + constantPoolHandle cpool(_method->get_Method()->constants()); 1.250 + return CURRENT_ENV->get_constant_by_index(cpool, pool_index, cache_index, _holder); 1.251 +} 1.252 + 1.253 +// ------------------------------------------------------------------ 1.254 +// ciBytecodeStream::get_constant_pool_tag 1.255 +// 1.256 +// If this bytecode is one of the ldc variants, get the referenced 1.257 +// constant. 1.258 +constantTag ciBytecodeStream::get_constant_pool_tag(int index) const { 1.259 + VM_ENTRY_MARK; 1.260 + return _method->get_Method()->constants()->tag_at(index); 1.261 +} 1.262 + 1.263 +// ------------------------------------------------------------------ 1.264 +// ciBytecodeStream::get_field_index 1.265 +// 1.266 +// If this is a field access bytecode, get the constant pool 1.267 +// index of the referenced field. 1.268 +int ciBytecodeStream::get_field_index() { 1.269 + assert(cur_bc() == Bytecodes::_getfield || 1.270 + cur_bc() == Bytecodes::_putfield || 1.271 + cur_bc() == Bytecodes::_getstatic || 1.272 + cur_bc() == Bytecodes::_putstatic, "wrong bc"); 1.273 + return get_index_u2_cpcache(); 1.274 +} 1.275 + 1.276 + 1.277 +// ------------------------------------------------------------------ 1.278 +// ciBytecodeStream::get_field 1.279 +// 1.280 +// If this bytecode is one of get_field, get_static, put_field, 1.281 +// or put_static, get the referenced field. 1.282 +ciField* ciBytecodeStream::get_field(bool& will_link) { 1.283 + ciField* f = CURRENT_ENV->get_field_by_index(_holder, get_field_index()); 1.284 + will_link = f->will_link(_holder, _bc); 1.285 + return f; 1.286 +} 1.287 + 1.288 + 1.289 +// ------------------------------------------------------------------ 1.290 +// ciBytecodeStream::get_declared_field_holder 1.291 +// 1.292 +// Get the declared holder of the currently referenced field. 1.293 +// 1.294 +// Usage note: the holder() of a ciField class returns the canonical 1.295 +// holder of the field, rather than the holder declared in the 1.296 +// bytecodes. 1.297 +// 1.298 +// There is no "will_link" result passed back. The user is responsible 1.299 +// for checking linkability when retrieving the associated field. 1.300 +ciInstanceKlass* ciBytecodeStream::get_declared_field_holder() { 1.301 + VM_ENTRY_MARK; 1.302 + constantPoolHandle cpool(_method->get_Method()->constants()); 1.303 + int holder_index = get_field_holder_index(); 1.304 + bool ignore; 1.305 + return CURRENT_ENV->get_klass_by_index(cpool, holder_index, ignore, _holder) 1.306 + ->as_instance_klass(); 1.307 +} 1.308 + 1.309 +// ------------------------------------------------------------------ 1.310 +// ciBytecodeStream::get_field_holder_index 1.311 +// 1.312 +// Get the constant pool index of the declared holder of the field 1.313 +// referenced by the current bytecode. Used for generating 1.314 +// deoptimization information. 1.315 +int ciBytecodeStream::get_field_holder_index() { 1.316 + GUARDED_VM_ENTRY( 1.317 + ConstantPool* cpool = _holder->get_instanceKlass()->constants(); 1.318 + return cpool->klass_ref_index_at(get_field_index()); 1.319 + ) 1.320 +} 1.321 + 1.322 +// ------------------------------------------------------------------ 1.323 +// ciBytecodeStream::get_field_signature_index 1.324 +// 1.325 +// Get the constant pool index of the signature of the field 1.326 +// referenced by the current bytecode. Used for generating 1.327 +// deoptimization information. 1.328 +int ciBytecodeStream::get_field_signature_index() { 1.329 + VM_ENTRY_MARK; 1.330 + ConstantPool* cpool = _holder->get_instanceKlass()->constants(); 1.331 + int nt_index = cpool->name_and_type_ref_index_at(get_field_index()); 1.332 + return cpool->signature_ref_index_at(nt_index); 1.333 +} 1.334 + 1.335 +// ------------------------------------------------------------------ 1.336 +// ciBytecodeStream::get_method_index 1.337 +// 1.338 +// If this is a method invocation bytecode, get the constant pool 1.339 +// index of the invoked method. 1.340 +int ciBytecodeStream::get_method_index() { 1.341 +#ifdef ASSERT 1.342 + switch (cur_bc()) { 1.343 + case Bytecodes::_invokeinterface: 1.344 + case Bytecodes::_invokevirtual: 1.345 + case Bytecodes::_invokespecial: 1.346 + case Bytecodes::_invokestatic: 1.347 + case Bytecodes::_invokedynamic: 1.348 + break; 1.349 + default: 1.350 + ShouldNotReachHere(); 1.351 + } 1.352 +#endif 1.353 + if (has_index_u4()) 1.354 + return get_index_u4(); // invokedynamic 1.355 + return get_index_u2_cpcache(); 1.356 +} 1.357 + 1.358 +// ------------------------------------------------------------------ 1.359 +// ciBytecodeStream::get_method 1.360 +// 1.361 +// If this is a method invocation bytecode, get the invoked method. 1.362 +// Additionally return the declared signature to get more concrete 1.363 +// type information if required (Cf. invokedynamic and invokehandle). 1.364 +ciMethod* ciBytecodeStream::get_method(bool& will_link, ciSignature* *declared_signature_result) { 1.365 + VM_ENTRY_MARK; 1.366 + ciEnv* env = CURRENT_ENV; 1.367 + constantPoolHandle cpool(_method->get_Method()->constants()); 1.368 + ciMethod* m = env->get_method_by_index(cpool, get_method_index(), cur_bc(), _holder); 1.369 + will_link = m->is_loaded(); 1.370 + 1.371 + // Use the MethodType stored in the CP cache to create a signature 1.372 + // with correct types (in respect to class loaders). 1.373 + if (has_method_type()) { 1.374 + ciSymbol* sig_sym = env->get_symbol(cpool->symbol_at(get_method_signature_index())); 1.375 + ciKlass* pool_holder = env->get_klass(cpool->pool_holder()); 1.376 + ciMethodType* method_type = get_method_type(); 1.377 + ciSignature* declared_signature = new (env->arena()) ciSignature(pool_holder, sig_sym, method_type); 1.378 + (*declared_signature_result) = declared_signature; 1.379 + } else { 1.380 + (*declared_signature_result) = m->signature(); 1.381 + } 1.382 + return m; 1.383 +} 1.384 + 1.385 +// ------------------------------------------------------------------ 1.386 +// ciBytecodeStream::has_appendix 1.387 +// 1.388 +// Returns true if there is an appendix argument stored in the 1.389 +// constant pool cache at the current bci. 1.390 +bool ciBytecodeStream::has_appendix() { 1.391 + VM_ENTRY_MARK; 1.392 + constantPoolHandle cpool(_method->get_Method()->constants()); 1.393 + return ConstantPool::has_appendix_at_if_loaded(cpool, get_method_index()); 1.394 +} 1.395 + 1.396 +// ------------------------------------------------------------------ 1.397 +// ciBytecodeStream::get_appendix 1.398 +// 1.399 +// Return the appendix argument stored in the constant pool cache at 1.400 +// the current bci. 1.401 +ciObject* ciBytecodeStream::get_appendix() { 1.402 + VM_ENTRY_MARK; 1.403 + constantPoolHandle cpool(_method->get_Method()->constants()); 1.404 + oop appendix_oop = ConstantPool::appendix_at_if_loaded(cpool, get_method_index()); 1.405 + return CURRENT_ENV->get_object(appendix_oop); 1.406 +} 1.407 + 1.408 +// ------------------------------------------------------------------ 1.409 +// ciBytecodeStream::has_method_type 1.410 +// 1.411 +// Returns true if there is a MethodType argument stored in the 1.412 +// constant pool cache at the current bci. 1.413 +bool ciBytecodeStream::has_method_type() { 1.414 + GUARDED_VM_ENTRY( 1.415 + constantPoolHandle cpool(_method->get_Method()->constants()); 1.416 + return ConstantPool::has_method_type_at_if_loaded(cpool, get_method_index()); 1.417 + ) 1.418 +} 1.419 + 1.420 +// ------------------------------------------------------------------ 1.421 +// ciBytecodeStream::get_method_type 1.422 +// 1.423 +// Return the MethodType stored in the constant pool cache at 1.424 +// the current bci. 1.425 +ciMethodType* ciBytecodeStream::get_method_type() { 1.426 + GUARDED_VM_ENTRY( 1.427 + constantPoolHandle cpool(_method->get_Method()->constants()); 1.428 + oop method_type_oop = ConstantPool::method_type_at_if_loaded(cpool, get_method_index()); 1.429 + return CURRENT_ENV->get_object(method_type_oop)->as_method_type(); 1.430 + ) 1.431 +} 1.432 + 1.433 +// ------------------------------------------------------------------ 1.434 +// ciBytecodeStream::get_declared_method_holder 1.435 +// 1.436 +// Get the declared holder of the currently referenced method. 1.437 +// 1.438 +// Usage note: the holder() of a ciMethod class returns the canonical 1.439 +// holder of the method, rather than the holder declared in the 1.440 +// bytecodes. 1.441 +// 1.442 +// There is no "will_link" result passed back. The user is responsible 1.443 +// for checking linkability when retrieving the associated method. 1.444 +ciKlass* ciBytecodeStream::get_declared_method_holder() { 1.445 + VM_ENTRY_MARK; 1.446 + constantPoolHandle cpool(_method->get_Method()->constants()); 1.447 + bool ignore; 1.448 + // report as MethodHandle for invokedynamic, which is syntactically classless 1.449 + if (cur_bc() == Bytecodes::_invokedynamic) 1.450 + return CURRENT_ENV->get_klass_by_name(_holder, ciSymbol::java_lang_invoke_MethodHandle(), false); 1.451 + return CURRENT_ENV->get_klass_by_index(cpool, get_method_holder_index(), ignore, _holder); 1.452 +} 1.453 + 1.454 +// ------------------------------------------------------------------ 1.455 +// ciBytecodeStream::get_method_holder_index 1.456 +// 1.457 +// Get the constant pool index of the declared holder of the method 1.458 +// referenced by the current bytecode. Used for generating 1.459 +// deoptimization information. 1.460 +int ciBytecodeStream::get_method_holder_index() { 1.461 + ConstantPool* cpool = _method->get_Method()->constants(); 1.462 + return cpool->klass_ref_index_at(get_method_index()); 1.463 +} 1.464 + 1.465 +// ------------------------------------------------------------------ 1.466 +// ciBytecodeStream::get_method_signature_index 1.467 +// 1.468 +// Get the constant pool index of the signature of the method 1.469 +// referenced by the current bytecode. Used for generating 1.470 +// deoptimization information. 1.471 +int ciBytecodeStream::get_method_signature_index() { 1.472 + GUARDED_VM_ENTRY( 1.473 + ConstantPool* cpool = _holder->get_instanceKlass()->constants(); 1.474 + const int method_index = get_method_index(); 1.475 + const int name_and_type_index = cpool->name_and_type_ref_index_at(method_index); 1.476 + return cpool->signature_ref_index_at(name_and_type_index); 1.477 + ) 1.478 +} 1.479 + 1.480 +// ------------------------------------------------------------------ 1.481 +// ciBytecodeStream::get_resolved_references 1.482 +ciObjArray* ciBytecodeStream::get_resolved_references() { 1.483 + VM_ENTRY_MARK; 1.484 + // Get the constant pool. 1.485 + ConstantPool* cpool = _holder->get_instanceKlass()->constants(); 1.486 + 1.487 + // Create a resolved references array and return it. 1.488 + return CURRENT_ENV->get_object(cpool->resolved_references())->as_obj_array(); 1.489 + }