src/share/vm/interpreter/bytecode.cpp

Wed, 03 Jul 2019 20:42:37 +0800

author
aoqi
date
Wed, 03 Jul 2019 20:42:37 +0800
changeset 9637
eef07cd490d4
parent 6876
710a3c8b516e
permissions
-rw-r--r--

Merge

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation.
aoqi@0 8 *
aoqi@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 12 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 13 * accompanied this code).
aoqi@0 14 *
aoqi@0 15 * You should have received a copy of the GNU General Public License version
aoqi@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 18 *
aoqi@0 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 20 * or visit www.oracle.com if you need additional information or have any
aoqi@0 21 * questions.
aoqi@0 22 *
aoqi@0 23 */
aoqi@0 24
aoqi@0 25 #include "precompiled.hpp"
aoqi@0 26 #include "interpreter/bytecode.hpp"
aoqi@0 27 #include "interpreter/linkResolver.hpp"
aoqi@0 28 #include "oops/constantPool.hpp"
aoqi@0 29 #include "oops/oop.inline.hpp"
aoqi@0 30 #include "runtime/fieldType.hpp"
aoqi@0 31 #include "runtime/handles.inline.hpp"
aoqi@0 32 #include "runtime/safepoint.hpp"
aoqi@0 33 #include "runtime/signature.hpp"
aoqi@0 34
aoqi@0 35 // Implementation of Bytecode
aoqi@0 36
aoqi@0 37 #ifdef ASSERT
aoqi@0 38
aoqi@0 39 void Bytecode::assert_same_format_as(Bytecodes::Code testbc, bool is_wide) const {
aoqi@0 40 Bytecodes::Code thisbc = Bytecodes::cast(byte_at(0));
aoqi@0 41 if (thisbc == Bytecodes::_breakpoint) return; // let the assertion fail silently
aoqi@0 42 if (is_wide) {
aoqi@0 43 assert(thisbc == Bytecodes::_wide, "expected a wide instruction");
aoqi@0 44 thisbc = Bytecodes::cast(byte_at(1));
aoqi@0 45 if (thisbc == Bytecodes::_breakpoint) return;
aoqi@0 46 }
aoqi@0 47 int thisflags = Bytecodes::flags(testbc, is_wide) & Bytecodes::_all_fmt_bits;
aoqi@0 48 int testflags = Bytecodes::flags(thisbc, is_wide) & Bytecodes::_all_fmt_bits;
aoqi@0 49 if (thisflags != testflags)
aoqi@0 50 tty->print_cr("assert_same_format_as(%d) failed on bc=%d%s; %d != %d",
aoqi@0 51 (int)testbc, (int)thisbc, (is_wide?"/wide":""), testflags, thisflags);
aoqi@0 52 assert(thisflags == testflags, "expected format");
aoqi@0 53 }
aoqi@0 54
aoqi@0 55 void Bytecode::assert_index_size(int size, Bytecodes::Code bc, bool is_wide) {
aoqi@0 56 int have_fmt = (Bytecodes::flags(bc, is_wide)
aoqi@0 57 & (Bytecodes::_fmt_has_u2 | Bytecodes::_fmt_has_u4 |
aoqi@0 58 Bytecodes::_fmt_not_simple |
aoqi@0 59 // Not an offset field:
aoqi@0 60 Bytecodes::_fmt_has_o));
aoqi@0 61 int need_fmt = -1;
aoqi@0 62 switch (size) {
aoqi@0 63 case 1: need_fmt = 0; break;
aoqi@0 64 case 2: need_fmt = Bytecodes::_fmt_has_u2; break;
aoqi@0 65 case 4: need_fmt = Bytecodes::_fmt_has_u4; break;
aoqi@0 66 }
aoqi@0 67 if (is_wide) need_fmt |= Bytecodes::_fmt_not_simple;
aoqi@0 68 if (have_fmt != need_fmt) {
aoqi@0 69 tty->print_cr("assert_index_size %d: bc=%d%s %d != %d", size, bc, (is_wide?"/wide":""), have_fmt, need_fmt);
aoqi@0 70 assert(have_fmt == need_fmt, "assert_index_size");
aoqi@0 71 }
aoqi@0 72 }
aoqi@0 73
aoqi@0 74 void Bytecode::assert_offset_size(int size, Bytecodes::Code bc, bool is_wide) {
aoqi@0 75 int have_fmt = Bytecodes::flags(bc, is_wide) & Bytecodes::_all_fmt_bits;
aoqi@0 76 int need_fmt = -1;
aoqi@0 77 switch (size) {
aoqi@0 78 case 2: need_fmt = Bytecodes::_fmt_bo2; break;
aoqi@0 79 case 4: need_fmt = Bytecodes::_fmt_bo4; break;
aoqi@0 80 }
aoqi@0 81 if (is_wide) need_fmt |= Bytecodes::_fmt_not_simple;
aoqi@0 82 if (have_fmt != need_fmt) {
aoqi@0 83 tty->print_cr("assert_offset_size %d: bc=%d%s %d != %d", size, bc, (is_wide?"/wide":""), have_fmt, need_fmt);
aoqi@0 84 assert(have_fmt == need_fmt, "assert_offset_size");
aoqi@0 85 }
aoqi@0 86 }
aoqi@0 87
aoqi@0 88 void Bytecode::assert_constant_size(int size, int where, Bytecodes::Code bc, bool is_wide) {
aoqi@0 89 int have_fmt = Bytecodes::flags(bc, is_wide) & (Bytecodes::_all_fmt_bits
aoqi@0 90 // Ignore any 'i' field (for iinc):
aoqi@0 91 & ~Bytecodes::_fmt_has_i);
aoqi@0 92 int need_fmt = -1;
aoqi@0 93 switch (size) {
aoqi@0 94 case 1: need_fmt = Bytecodes::_fmt_bc; break;
aoqi@0 95 case 2: need_fmt = Bytecodes::_fmt_bc | Bytecodes::_fmt_has_u2; break;
aoqi@0 96 }
aoqi@0 97 if (is_wide) need_fmt |= Bytecodes::_fmt_not_simple;
aoqi@0 98 int length = is_wide ? Bytecodes::wide_length_for(bc) : Bytecodes::length_for(bc);
aoqi@0 99 if (have_fmt != need_fmt || where + size != length) {
aoqi@0 100 tty->print_cr("assert_constant_size %d @%d: bc=%d%s %d != %d", size, where, bc, (is_wide?"/wide":""), have_fmt, need_fmt);
aoqi@0 101 }
aoqi@0 102 assert(have_fmt == need_fmt, "assert_constant_size");
aoqi@0 103 assert(where + size == length, "assert_constant_size oob");
aoqi@0 104 }
aoqi@0 105
aoqi@0 106 void Bytecode::assert_native_index(Bytecodes::Code bc, bool is_wide) {
aoqi@0 107 assert((Bytecodes::flags(bc, is_wide) & Bytecodes::_fmt_has_nbo) != 0, "native index");
aoqi@0 108 }
aoqi@0 109
aoqi@0 110 #endif //ASSERT
aoqi@0 111
aoqi@0 112 // Implementation of Bytecode_tableupswitch
aoqi@0 113
aoqi@0 114 int Bytecode_tableswitch::dest_offset_at(int i) const {
aoqi@0 115 return get_Java_u4_at(aligned_offset(1 + (3 + i)*jintSize));
aoqi@0 116 }
aoqi@0 117
aoqi@0 118
aoqi@0 119 // Implementation of Bytecode_invoke
aoqi@0 120
aoqi@0 121 void Bytecode_invoke::verify() const {
aoqi@0 122 assert(is_valid(), "check invoke");
aoqi@0 123 assert(cpcache() != NULL, "do not call this from verifier or rewriter");
aoqi@0 124 }
aoqi@0 125
aoqi@0 126
aoqi@0 127 Symbol* Bytecode_member_ref::klass() const {
aoqi@0 128 return constants()->klass_ref_at_noresolve(index());
aoqi@0 129 }
aoqi@0 130
aoqi@0 131
aoqi@0 132 Symbol* Bytecode_member_ref::name() const {
aoqi@0 133 return constants()->name_ref_at(index());
aoqi@0 134 }
aoqi@0 135
aoqi@0 136
aoqi@0 137 Symbol* Bytecode_member_ref::signature() const {
aoqi@0 138 return constants()->signature_ref_at(index());
aoqi@0 139 }
aoqi@0 140
aoqi@0 141
aoqi@0 142 BasicType Bytecode_member_ref::result_type() const {
aoqi@0 143 ResultTypeFinder rts(signature());
aoqi@0 144 rts.iterate();
aoqi@0 145 return rts.type();
aoqi@0 146 }
aoqi@0 147
aoqi@0 148
aoqi@0 149 methodHandle Bytecode_invoke::static_target(TRAPS) {
aoqi@0 150 methodHandle m;
aoqi@0 151 KlassHandle resolved_klass;
aoqi@0 152 constantPoolHandle constants(THREAD, this->constants());
aoqi@0 153
aoqi@0 154 Bytecodes::Code bc = invoke_code();
aoqi@0 155 LinkResolver::resolve_method_statically(m, resolved_klass, bc, constants, index(), CHECK_(methodHandle()));
aoqi@0 156 return m;
aoqi@0 157 }
aoqi@0 158
aoqi@0 159 Handle Bytecode_invoke::appendix(TRAPS) {
aoqi@0 160 ConstantPoolCacheEntry* cpce = cpcache_entry();
aoqi@0 161 if (cpce->has_appendix())
aoqi@0 162 return Handle(THREAD, cpce->appendix_if_resolved(constants()));
aoqi@0 163 return Handle(); // usual case
aoqi@0 164 }
aoqi@0 165
aoqi@0 166 int Bytecode_member_ref::index() const {
aoqi@0 167 // Note: Rewriter::rewrite changes the Java_u2 of an invokedynamic to a native_u4,
aoqi@0 168 // at the same time it allocates per-call-site CP cache entries.
aoqi@0 169 Bytecodes::Code rawc = code();
aoqi@0 170 if (has_index_u4(rawc))
aoqi@0 171 return get_index_u4(rawc);
aoqi@0 172 else
aoqi@0 173 return get_index_u2_cpcache(rawc);
aoqi@0 174 }
aoqi@0 175
aoqi@0 176 int Bytecode_member_ref::pool_index() const {
aoqi@0 177 return cpcache_entry()->constant_pool_index();
aoqi@0 178 }
aoqi@0 179
aoqi@0 180 ConstantPoolCacheEntry* Bytecode_member_ref::cpcache_entry() const {
aoqi@0 181 int index = this->index();
aoqi@0 182 return cpcache()->entry_at(ConstantPool::decode_cpcache_index(index, true));
aoqi@0 183 }
aoqi@0 184
aoqi@0 185 // Implementation of Bytecode_field
aoqi@0 186
aoqi@0 187 void Bytecode_field::verify() const {
aoqi@0 188 assert(is_valid(), "check field");
aoqi@0 189 }
aoqi@0 190
aoqi@0 191
aoqi@0 192 // Implementation of Bytecode_loadconstant
aoqi@0 193
aoqi@0 194 int Bytecode_loadconstant::raw_index() const {
aoqi@0 195 Bytecodes::Code rawc = code();
aoqi@0 196 assert(rawc != Bytecodes::_wide, "verifier prevents this");
aoqi@0 197 if (Bytecodes::java_code(rawc) == Bytecodes::_ldc)
aoqi@0 198 return get_index_u1(rawc);
aoqi@0 199 else
aoqi@0 200 return get_index_u2(rawc, false);
aoqi@0 201 }
aoqi@0 202
aoqi@0 203 int Bytecode_loadconstant::pool_index() const {
aoqi@0 204 int index = raw_index();
aoqi@0 205 if (has_cache_index()) {
aoqi@0 206 return _method->constants()->object_to_cp_index(index);
aoqi@0 207 }
aoqi@0 208 return index;
aoqi@0 209 }
aoqi@0 210
aoqi@0 211 BasicType Bytecode_loadconstant::result_type() const {
aoqi@0 212 int index = pool_index();
aoqi@0 213 constantTag tag = _method->constants()->tag_at(index);
aoqi@0 214 return tag.basic_type();
aoqi@0 215 }
aoqi@0 216
aoqi@0 217 oop Bytecode_loadconstant::resolve_constant(TRAPS) const {
aoqi@0 218 assert(_method.not_null(), "must supply method to resolve constant");
aoqi@0 219 int index = raw_index();
aoqi@0 220 ConstantPool* constants = _method->constants();
aoqi@0 221 if (has_cache_index()) {
aoqi@0 222 return constants->resolve_cached_constant_at(index, THREAD);
aoqi@0 223 } else {
aoqi@0 224 return constants->resolve_constant_at(index, THREAD);
aoqi@0 225 }
aoqi@0 226 }
aoqi@0 227
aoqi@0 228 //------------------------------------------------------------------------------
aoqi@0 229 // Non-product code
aoqi@0 230
aoqi@0 231 #ifndef PRODUCT
aoqi@0 232
aoqi@0 233 void Bytecode_lookupswitch::verify() const {
aoqi@0 234 switch (Bytecodes::java_code(code())) {
aoqi@0 235 case Bytecodes::_lookupswitch:
aoqi@0 236 { int i = number_of_pairs() - 1;
aoqi@0 237 while (i-- > 0) {
aoqi@0 238 assert(pair_at(i).match() < pair_at(i+1).match(), "unsorted table entries");
aoqi@0 239 }
aoqi@0 240 }
aoqi@0 241 break;
aoqi@0 242 default:
aoqi@0 243 fatal("not a lookupswitch bytecode");
aoqi@0 244 }
aoqi@0 245 }
aoqi@0 246
aoqi@0 247 void Bytecode_tableswitch::verify() const {
aoqi@0 248 switch (Bytecodes::java_code(code())) {
aoqi@0 249 case Bytecodes::_tableswitch:
aoqi@0 250 { int lo = low_key();
aoqi@0 251 int hi = high_key();
aoqi@0 252 assert (hi >= lo, "incorrect hi/lo values in tableswitch");
aoqi@0 253 int i = hi - lo - 1 ;
aoqi@0 254 while (i-- > 0) {
aoqi@0 255 // no special check needed
aoqi@0 256 }
aoqi@0 257 }
aoqi@0 258 break;
aoqi@0 259 default:
aoqi@0 260 fatal("not a tableswitch bytecode");
aoqi@0 261 }
aoqi@0 262 }
aoqi@0 263
aoqi@0 264 #endif

mercurial