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