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