1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/vm/interpreter/bytecode.cpp Wed Apr 27 01:25:04 2016 +0800 1.3 @@ -0,0 +1,264 @@ 1.4 +/* 1.5 + * Copyright (c) 1997, 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 "interpreter/bytecode.hpp" 1.30 +#include "interpreter/linkResolver.hpp" 1.31 +#include "oops/constantPool.hpp" 1.32 +#include "oops/oop.inline.hpp" 1.33 +#include "runtime/fieldType.hpp" 1.34 +#include "runtime/handles.inline.hpp" 1.35 +#include "runtime/safepoint.hpp" 1.36 +#include "runtime/signature.hpp" 1.37 + 1.38 +// Implementation of Bytecode 1.39 + 1.40 +#ifdef ASSERT 1.41 + 1.42 +void Bytecode::assert_same_format_as(Bytecodes::Code testbc, bool is_wide) const { 1.43 + Bytecodes::Code thisbc = Bytecodes::cast(byte_at(0)); 1.44 + if (thisbc == Bytecodes::_breakpoint) return; // let the assertion fail silently 1.45 + if (is_wide) { 1.46 + assert(thisbc == Bytecodes::_wide, "expected a wide instruction"); 1.47 + thisbc = Bytecodes::cast(byte_at(1)); 1.48 + if (thisbc == Bytecodes::_breakpoint) return; 1.49 + } 1.50 + int thisflags = Bytecodes::flags(testbc, is_wide) & Bytecodes::_all_fmt_bits; 1.51 + int testflags = Bytecodes::flags(thisbc, is_wide) & Bytecodes::_all_fmt_bits; 1.52 + if (thisflags != testflags) 1.53 + tty->print_cr("assert_same_format_as(%d) failed on bc=%d%s; %d != %d", 1.54 + (int)testbc, (int)thisbc, (is_wide?"/wide":""), testflags, thisflags); 1.55 + assert(thisflags == testflags, "expected format"); 1.56 +} 1.57 + 1.58 +void Bytecode::assert_index_size(int size, Bytecodes::Code bc, bool is_wide) { 1.59 + int have_fmt = (Bytecodes::flags(bc, is_wide) 1.60 + & (Bytecodes::_fmt_has_u2 | Bytecodes::_fmt_has_u4 | 1.61 + Bytecodes::_fmt_not_simple | 1.62 + // Not an offset field: 1.63 + Bytecodes::_fmt_has_o)); 1.64 + int need_fmt = -1; 1.65 + switch (size) { 1.66 + case 1: need_fmt = 0; break; 1.67 + case 2: need_fmt = Bytecodes::_fmt_has_u2; break; 1.68 + case 4: need_fmt = Bytecodes::_fmt_has_u4; break; 1.69 + } 1.70 + if (is_wide) need_fmt |= Bytecodes::_fmt_not_simple; 1.71 + if (have_fmt != need_fmt) { 1.72 + tty->print_cr("assert_index_size %d: bc=%d%s %d != %d", size, bc, (is_wide?"/wide":""), have_fmt, need_fmt); 1.73 + assert(have_fmt == need_fmt, "assert_index_size"); 1.74 + } 1.75 +} 1.76 + 1.77 +void Bytecode::assert_offset_size(int size, Bytecodes::Code bc, bool is_wide) { 1.78 + int have_fmt = Bytecodes::flags(bc, is_wide) & Bytecodes::_all_fmt_bits; 1.79 + int need_fmt = -1; 1.80 + switch (size) { 1.81 + case 2: need_fmt = Bytecodes::_fmt_bo2; break; 1.82 + case 4: need_fmt = Bytecodes::_fmt_bo4; break; 1.83 + } 1.84 + if (is_wide) need_fmt |= Bytecodes::_fmt_not_simple; 1.85 + if (have_fmt != need_fmt) { 1.86 + tty->print_cr("assert_offset_size %d: bc=%d%s %d != %d", size, bc, (is_wide?"/wide":""), have_fmt, need_fmt); 1.87 + assert(have_fmt == need_fmt, "assert_offset_size"); 1.88 + } 1.89 +} 1.90 + 1.91 +void Bytecode::assert_constant_size(int size, int where, Bytecodes::Code bc, bool is_wide) { 1.92 + int have_fmt = Bytecodes::flags(bc, is_wide) & (Bytecodes::_all_fmt_bits 1.93 + // Ignore any 'i' field (for iinc): 1.94 + & ~Bytecodes::_fmt_has_i); 1.95 + int need_fmt = -1; 1.96 + switch (size) { 1.97 + case 1: need_fmt = Bytecodes::_fmt_bc; break; 1.98 + case 2: need_fmt = Bytecodes::_fmt_bc | Bytecodes::_fmt_has_u2; break; 1.99 + } 1.100 + if (is_wide) need_fmt |= Bytecodes::_fmt_not_simple; 1.101 + int length = is_wide ? Bytecodes::wide_length_for(bc) : Bytecodes::length_for(bc); 1.102 + if (have_fmt != need_fmt || where + size != length) { 1.103 + tty->print_cr("assert_constant_size %d @%d: bc=%d%s %d != %d", size, where, bc, (is_wide?"/wide":""), have_fmt, need_fmt); 1.104 + } 1.105 + assert(have_fmt == need_fmt, "assert_constant_size"); 1.106 + assert(where + size == length, "assert_constant_size oob"); 1.107 +} 1.108 + 1.109 +void Bytecode::assert_native_index(Bytecodes::Code bc, bool is_wide) { 1.110 + assert((Bytecodes::flags(bc, is_wide) & Bytecodes::_fmt_has_nbo) != 0, "native index"); 1.111 +} 1.112 + 1.113 +#endif //ASSERT 1.114 + 1.115 +// Implementation of Bytecode_tableupswitch 1.116 + 1.117 +int Bytecode_tableswitch::dest_offset_at(int i) const { 1.118 + return get_Java_u4_at(aligned_offset(1 + (3 + i)*jintSize)); 1.119 +} 1.120 + 1.121 + 1.122 +// Implementation of Bytecode_invoke 1.123 + 1.124 +void Bytecode_invoke::verify() const { 1.125 + assert(is_valid(), "check invoke"); 1.126 + assert(cpcache() != NULL, "do not call this from verifier or rewriter"); 1.127 +} 1.128 + 1.129 + 1.130 +Symbol* Bytecode_member_ref::klass() const { 1.131 + return constants()->klass_ref_at_noresolve(index()); 1.132 +} 1.133 + 1.134 + 1.135 +Symbol* Bytecode_member_ref::name() const { 1.136 + return constants()->name_ref_at(index()); 1.137 +} 1.138 + 1.139 + 1.140 +Symbol* Bytecode_member_ref::signature() const { 1.141 + return constants()->signature_ref_at(index()); 1.142 +} 1.143 + 1.144 + 1.145 +BasicType Bytecode_member_ref::result_type() const { 1.146 + ResultTypeFinder rts(signature()); 1.147 + rts.iterate(); 1.148 + return rts.type(); 1.149 +} 1.150 + 1.151 + 1.152 +methodHandle Bytecode_invoke::static_target(TRAPS) { 1.153 + methodHandle m; 1.154 + KlassHandle resolved_klass; 1.155 + constantPoolHandle constants(THREAD, this->constants()); 1.156 + 1.157 + Bytecodes::Code bc = invoke_code(); 1.158 + LinkResolver::resolve_method_statically(m, resolved_klass, bc, constants, index(), CHECK_(methodHandle())); 1.159 + return m; 1.160 +} 1.161 + 1.162 +Handle Bytecode_invoke::appendix(TRAPS) { 1.163 + ConstantPoolCacheEntry* cpce = cpcache_entry(); 1.164 + if (cpce->has_appendix()) 1.165 + return Handle(THREAD, cpce->appendix_if_resolved(constants())); 1.166 + return Handle(); // usual case 1.167 +} 1.168 + 1.169 +int Bytecode_member_ref::index() const { 1.170 + // Note: Rewriter::rewrite changes the Java_u2 of an invokedynamic to a native_u4, 1.171 + // at the same time it allocates per-call-site CP cache entries. 1.172 + Bytecodes::Code rawc = code(); 1.173 + if (has_index_u4(rawc)) 1.174 + return get_index_u4(rawc); 1.175 + else 1.176 + return get_index_u2_cpcache(rawc); 1.177 +} 1.178 + 1.179 +int Bytecode_member_ref::pool_index() const { 1.180 + return cpcache_entry()->constant_pool_index(); 1.181 +} 1.182 + 1.183 +ConstantPoolCacheEntry* Bytecode_member_ref::cpcache_entry() const { 1.184 + int index = this->index(); 1.185 + return cpcache()->entry_at(ConstantPool::decode_cpcache_index(index, true)); 1.186 +} 1.187 + 1.188 +// Implementation of Bytecode_field 1.189 + 1.190 +void Bytecode_field::verify() const { 1.191 + assert(is_valid(), "check field"); 1.192 +} 1.193 + 1.194 + 1.195 +// Implementation of Bytecode_loadconstant 1.196 + 1.197 +int Bytecode_loadconstant::raw_index() const { 1.198 + Bytecodes::Code rawc = code(); 1.199 + assert(rawc != Bytecodes::_wide, "verifier prevents this"); 1.200 + if (Bytecodes::java_code(rawc) == Bytecodes::_ldc) 1.201 + return get_index_u1(rawc); 1.202 + else 1.203 + return get_index_u2(rawc, false); 1.204 +} 1.205 + 1.206 +int Bytecode_loadconstant::pool_index() const { 1.207 + int index = raw_index(); 1.208 + if (has_cache_index()) { 1.209 + return _method->constants()->object_to_cp_index(index); 1.210 + } 1.211 + return index; 1.212 +} 1.213 + 1.214 +BasicType Bytecode_loadconstant::result_type() const { 1.215 + int index = pool_index(); 1.216 + constantTag tag = _method->constants()->tag_at(index); 1.217 + return tag.basic_type(); 1.218 +} 1.219 + 1.220 +oop Bytecode_loadconstant::resolve_constant(TRAPS) const { 1.221 + assert(_method.not_null(), "must supply method to resolve constant"); 1.222 + int index = raw_index(); 1.223 + ConstantPool* constants = _method->constants(); 1.224 + if (has_cache_index()) { 1.225 + return constants->resolve_cached_constant_at(index, THREAD); 1.226 + } else { 1.227 + return constants->resolve_constant_at(index, THREAD); 1.228 + } 1.229 +} 1.230 + 1.231 +//------------------------------------------------------------------------------ 1.232 +// Non-product code 1.233 + 1.234 +#ifndef PRODUCT 1.235 + 1.236 +void Bytecode_lookupswitch::verify() const { 1.237 + switch (Bytecodes::java_code(code())) { 1.238 + case Bytecodes::_lookupswitch: 1.239 + { int i = number_of_pairs() - 1; 1.240 + while (i-- > 0) { 1.241 + assert(pair_at(i).match() < pair_at(i+1).match(), "unsorted table entries"); 1.242 + } 1.243 + } 1.244 + break; 1.245 + default: 1.246 + fatal("not a lookupswitch bytecode"); 1.247 + } 1.248 +} 1.249 + 1.250 +void Bytecode_tableswitch::verify() const { 1.251 + switch (Bytecodes::java_code(code())) { 1.252 + case Bytecodes::_tableswitch: 1.253 + { int lo = low_key(); 1.254 + int hi = high_key(); 1.255 + assert (hi >= lo, "incorrect hi/lo values in tableswitch"); 1.256 + int i = hi - lo - 1 ; 1.257 + while (i-- > 0) { 1.258 + // no special check needed 1.259 + } 1.260 + } 1.261 + break; 1.262 + default: 1.263 + fatal("not a tableswitch bytecode"); 1.264 + } 1.265 +} 1.266 + 1.267 +#endif