Sun, 23 May 2010 01:38:26 -0700
6939207: refactor constant pool index processing
Summary: Factored cleanup of instruction decode which prepares for enhanced ldc semantics.
Reviewed-by: twisti
duke@435 | 1 | /* |
jrose@1920 | 2 | * Copyright 1997-2010 Sun Microsystems, Inc. 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 | * |
duke@435 | 19 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
duke@435 | 20 | * CA 95054 USA or visit www.sun.com if you need additional information or |
duke@435 | 21 | * have any 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 | Bytecodes::Code bc = adjusted_invoke_code(); |
duke@435 | 140 | assert(is_valid(), "check invoke"); |
jrose@1920 | 141 | assert(method()->constants()->cache() != NULL, "do not call this from verifier or rewriter"); |
duke@435 | 142 | } |
duke@435 | 143 | |
duke@435 | 144 | |
duke@435 | 145 | symbolOop Bytecode_invoke::signature() const { |
duke@435 | 146 | constantPoolOop constants = method()->constants(); |
duke@435 | 147 | return constants->signature_ref_at(index()); |
duke@435 | 148 | } |
duke@435 | 149 | |
duke@435 | 150 | |
duke@435 | 151 | symbolOop Bytecode_invoke::name() const { |
duke@435 | 152 | constantPoolOop constants = method()->constants(); |
duke@435 | 153 | return constants->name_ref_at(index()); |
duke@435 | 154 | } |
duke@435 | 155 | |
duke@435 | 156 | |
duke@435 | 157 | BasicType Bytecode_invoke::result_type(Thread *thread) const { |
duke@435 | 158 | symbolHandle sh(thread, signature()); |
duke@435 | 159 | ResultTypeFinder rts(sh); |
duke@435 | 160 | rts.iterate(); |
duke@435 | 161 | return rts.type(); |
duke@435 | 162 | } |
duke@435 | 163 | |
duke@435 | 164 | |
duke@435 | 165 | methodHandle Bytecode_invoke::static_target(TRAPS) { |
duke@435 | 166 | methodHandle m; |
duke@435 | 167 | KlassHandle resolved_klass; |
duke@435 | 168 | constantPoolHandle constants(THREAD, _method->constants()); |
duke@435 | 169 | |
twisti@1570 | 170 | if (adjusted_invoke_code() == Bytecodes::_invokedynamic) { |
twisti@1570 | 171 | LinkResolver::resolve_dynamic_method(m, resolved_klass, constants, index(), CHECK_(methodHandle())); |
twisti@1570 | 172 | } else if (adjusted_invoke_code() != Bytecodes::_invokeinterface) { |
duke@435 | 173 | LinkResolver::resolve_method(m, resolved_klass, constants, index(), CHECK_(methodHandle())); |
duke@435 | 174 | } else { |
duke@435 | 175 | LinkResolver::resolve_interface_method(m, resolved_klass, constants, index(), CHECK_(methodHandle())); |
duke@435 | 176 | } |
duke@435 | 177 | return m; |
duke@435 | 178 | } |
duke@435 | 179 | |
duke@435 | 180 | |
duke@435 | 181 | int Bytecode_invoke::index() const { |
jrose@1161 | 182 | // Note: Rewriter::rewrite changes the Java_u2 of an invokedynamic to a native_u4, |
jrose@1161 | 183 | // at the same time it allocates per-call-site CP cache entries. |
jrose@1920 | 184 | Bytecodes::Code stdc = Bytecodes::java_code(code()); |
jrose@1920 | 185 | Bytecode* invoke = Bytecode_at(bcp()); |
jrose@1920 | 186 | if (invoke->has_index_u4(stdc)) |
jrose@1920 | 187 | return invoke->get_index_u4(stdc); |
jrose@1161 | 188 | else |
jrose@1920 | 189 | return invoke->get_index_u2_cpcache(stdc); |
duke@435 | 190 | } |
duke@435 | 191 | |
duke@435 | 192 | |
duke@435 | 193 | // Implementation of Bytecode_field |
duke@435 | 194 | |
duke@435 | 195 | void Bytecode_field::verify() const { |
duke@435 | 196 | Bytecodes::Code stdc = Bytecodes::java_code(code()); |
duke@435 | 197 | assert(stdc == Bytecodes::_putstatic || stdc == Bytecodes::_getstatic || |
duke@435 | 198 | stdc == Bytecodes::_putfield || stdc == Bytecodes::_getfield, "check field"); |
duke@435 | 199 | } |
duke@435 | 200 | |
duke@435 | 201 | |
duke@435 | 202 | bool Bytecode_field::is_static() const { |
duke@435 | 203 | Bytecodes::Code stdc = Bytecodes::java_code(code()); |
duke@435 | 204 | return stdc == Bytecodes::_putstatic || stdc == Bytecodes::_getstatic; |
duke@435 | 205 | } |
duke@435 | 206 | |
duke@435 | 207 | |
duke@435 | 208 | int Bytecode_field::index() const { |
jrose@1920 | 209 | Bytecode* invoke = Bytecode_at(bcp()); |
jrose@1920 | 210 | return invoke->get_index_u2_cpcache(Bytecodes::_getfield); |
duke@435 | 211 | } |
duke@435 | 212 | |
duke@435 | 213 | |
duke@435 | 214 | // Implementation of Bytecodes loac constant |
duke@435 | 215 | |
duke@435 | 216 | int Bytecode_loadconstant::index() const { |
duke@435 | 217 | Bytecodes::Code stdc = Bytecodes::java_code(code()); |
jrose@1920 | 218 | return stdc == Bytecodes::_ldc ? get_index_u1(stdc) : get_index_u2(stdc); |
duke@435 | 219 | } |
duke@435 | 220 | |
duke@435 | 221 | //------------------------------------------------------------------------------ |
duke@435 | 222 | // Non-product code |
duke@435 | 223 | |
duke@435 | 224 | #ifndef PRODUCT |
duke@435 | 225 | |
duke@435 | 226 | void Bytecode_lookupswitch::verify() const { |
duke@435 | 227 | switch (Bytecodes::java_code(code())) { |
duke@435 | 228 | case Bytecodes::_lookupswitch: |
duke@435 | 229 | { int i = number_of_pairs() - 1; |
duke@435 | 230 | while (i-- > 0) { |
duke@435 | 231 | assert(pair_at(i)->match() < pair_at(i+1)->match(), "unsorted table entries"); |
duke@435 | 232 | } |
duke@435 | 233 | } |
duke@435 | 234 | break; |
duke@435 | 235 | default: |
duke@435 | 236 | fatal("not a lookupswitch bytecode"); |
duke@435 | 237 | } |
duke@435 | 238 | } |
duke@435 | 239 | |
duke@435 | 240 | void Bytecode_tableswitch::verify() const { |
duke@435 | 241 | switch (Bytecodes::java_code(code())) { |
duke@435 | 242 | case Bytecodes::_tableswitch: |
duke@435 | 243 | { int lo = low_key(); |
duke@435 | 244 | int hi = high_key(); |
duke@435 | 245 | assert (hi >= lo, "incorrect hi/lo values in tableswitch"); |
duke@435 | 246 | int i = hi - lo - 1 ; |
duke@435 | 247 | while (i-- > 0) { |
duke@435 | 248 | // no special check needed |
duke@435 | 249 | } |
duke@435 | 250 | } |
duke@435 | 251 | break; |
duke@435 | 252 | default: |
duke@435 | 253 | fatal("not a tableswitch bytecode"); |
duke@435 | 254 | } |
duke@435 | 255 | } |
duke@435 | 256 | |
duke@435 | 257 | #endif |