duke@435: /* coleenp@4037: * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved. duke@435: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@435: * duke@435: * This code is free software; you can redistribute it and/or modify it duke@435: * under the terms of the GNU General Public License version 2 only, as duke@435: * published by the Free Software Foundation. duke@435: * duke@435: * This code is distributed in the hope that it will be useful, but WITHOUT duke@435: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@435: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@435: * version 2 for more details (a copy is included in the LICENSE file that duke@435: * accompanied this code). duke@435: * duke@435: * You should have received a copy of the GNU General Public License version duke@435: * 2 along with this work; if not, write to the Free Software Foundation, duke@435: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@435: * trims@1907: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA trims@1907: * or visit www.oracle.com if you need additional information or have any trims@1907: * questions. duke@435: * duke@435: */ duke@435: stefank@2314: #include "precompiled.hpp" stefank@2314: #include "interpreter/bytecodes.hpp" stefank@2314: #include "memory/resourceArea.hpp" coleenp@4037: #include "oops/method.hpp" stefank@2314: #ifdef TARGET_ARCH_x86 stefank@2314: # include "bytes_x86.hpp" stefank@2314: #endif stefank@2314: #ifdef TARGET_ARCH_sparc stefank@2314: # include "bytes_sparc.hpp" stefank@2314: #endif stefank@2314: #ifdef TARGET_ARCH_zero stefank@2314: # include "bytes_zero.hpp" stefank@2314: #endif bobv@2508: #ifdef TARGET_ARCH_arm bobv@2508: # include "bytes_arm.hpp" bobv@2508: #endif bobv@2508: #ifdef TARGET_ARCH_ppc bobv@2508: # include "bytes_ppc.hpp" bobv@2508: #endif duke@435: duke@435: prr@1881: #if defined(WIN32) && (defined(_MSC_VER) && (_MSC_VER < 1600)) duke@435: // Windows AMD64 Compiler Hangs compiling this file duke@435: // unless optimization is off duke@435: #ifdef _M_AMD64 duke@435: #pragma optimize ("", off) duke@435: #endif prr@1881: #endif duke@435: duke@435: duke@435: bool Bytecodes::_is_initialized = false; duke@435: const char* Bytecodes::_name [Bytecodes::number_of_codes]; duke@435: BasicType Bytecodes::_result_type [Bytecodes::number_of_codes]; duke@435: s_char Bytecodes::_depth [Bytecodes::number_of_codes]; jrose@1920: u_char Bytecodes::_lengths [Bytecodes::number_of_codes]; duke@435: Bytecodes::Code Bytecodes::_java_code [Bytecodes::number_of_codes]; jrose@1920: u_short Bytecodes::_flags [(1<contains(bcp); never@2462: } never@2462: #endif never@2462: never@2462: bool Bytecodes::check_must_rewrite(Bytecodes::Code code) { never@2462: assert(can_rewrite(code), "post-check only"); never@2462: never@2462: // Some codes are conditionally rewriting. Look closely at them. never@2462: switch (code) { never@2462: case Bytecodes::_aload_0: never@2462: // Even if RewriteFrequentPairs is turned on, never@2462: // the _aload_0 code might delay its rewrite until never@2462: // a following _getfield rewrites itself. never@2462: return false; never@2462: never@2462: case Bytecodes::_lookupswitch: never@2462: return false; // the rewrite is not done by the interpreter never@2462: never@2462: case Bytecodes::_new: never@2462: // (Could actually look at the class here, but the profit would be small.) never@2462: return false; // the rewrite is not always done never@2462: } never@2462: never@2462: // No other special cases. never@2462: return true; never@2462: } duke@435: coleenp@4037: Bytecodes::Code Bytecodes::code_at(Method* method, int bci) { never@2462: return code_at(method, method->bcp_from(bci)); duke@435: } duke@435: coleenp@4037: Bytecodes::Code Bytecodes::non_breakpoint_code_at(const Method* method, address bcp) { never@2462: assert(method != NULL, "must have the method for breakpoint conversion"); never@2462: assert(method->contains(bcp), "must be valid bcp in method"); duke@435: return method->orig_bytecode_at(method->bci_from(bcp)); duke@435: } duke@435: never@2462: int Bytecodes::special_length_at(Bytecodes::Code code, address bcp, address end) { duke@435: switch (code) { duke@435: case _wide: kamg@848: if (end != NULL && bcp + 1 >= end) { kamg@848: return -1; // don't read past end of code buffer kamg@848: } duke@435: return wide_length_for(cast(*(bcp + 1))); duke@435: case _tableswitch: duke@435: { address aligned_bcp = (address)round_to((intptr_t)bcp + 1, jintSize); kamg@848: if (end != NULL && aligned_bcp + 3*jintSize >= end) { kamg@848: return -1; // don't read past end of code buffer kamg@848: } duke@435: jlong lo = (jint)Bytes::get_Java_u4(aligned_bcp + 1*jintSize); duke@435: jlong hi = (jint)Bytes::get_Java_u4(aligned_bcp + 2*jintSize); duke@435: jlong len = (aligned_bcp - bcp) + (3 + hi - lo + 1)*jintSize; duke@435: // only return len if it can be represented as a positive int; duke@435: // return -1 otherwise duke@435: return (len > 0 && len == (int)len) ? len : -1; duke@435: } duke@435: duke@435: case _lookupswitch: // fall through duke@435: case _fast_binaryswitch: // fall through duke@435: case _fast_linearswitch: duke@435: { address aligned_bcp = (address)round_to((intptr_t)bcp + 1, jintSize); kamg@848: if (end != NULL && aligned_bcp + 2*jintSize >= end) { kamg@848: return -1; // don't read past end of code buffer kamg@848: } duke@435: jlong npairs = (jint)Bytes::get_Java_u4(aligned_bcp + jintSize); duke@435: jlong len = (aligned_bcp - bcp) + (2 + 2*npairs)*jintSize; duke@435: // only return len if it can be represented as a positive int; duke@435: // return -1 otherwise duke@435: return (len > 0 && len == (int)len) ? len : -1; duke@435: } duke@435: } jrose@1925: // Note: Length functions must return <=0 for invalid bytecodes. duke@435: return 0; duke@435: } duke@435: duke@435: // At a breakpoint instruction, this returns the breakpoint's length, duke@435: // otherwise, it's the same as special_length_at(). This is used by duke@435: // the RawByteCodeStream, which wants to see the actual bytecode duke@435: // values (including breakpoint). RawByteCodeStream is used by the duke@435: // verifier when reading in bytecode to verify. Other mechanisms that duke@435: // run at runtime (such as generateOopMaps) need to iterate over the code duke@435: // and don't expect to see breakpoints: they want to see the instruction kamg@848: // which was replaced so that they can get the correct length and find duke@435: // the next bytecode. kamg@848: // kamg@848: // 'end' indicates the end of the code buffer, which we should not try to read kamg@848: // past. kamg@848: int Bytecodes::raw_special_length_at(address bcp, address end) { duke@435: Code code = code_or_bp_at(bcp); duke@435: if (code == _breakpoint) { duke@435: return 1; duke@435: } else { never@2462: return special_length_at(code, bcp, end); duke@435: } duke@435: } duke@435: duke@435: duke@435: duke@435: void Bytecodes::def(Code code, const char* name, const char* format, const char* wide_format, BasicType result_type, int depth, bool can_trap) { duke@435: def(code, name, format, wide_format, result_type, depth, can_trap, code); duke@435: } duke@435: duke@435: duke@435: void Bytecodes::def(Code code, const char* name, const char* format, const char* wide_format, BasicType result_type, int depth, bool can_trap, Code java_code) { duke@435: assert(wide_format == NULL || format != NULL, "short form must exist if there's a wide form"); jrose@1920: int len = (format != NULL ? (int) strlen(format) : 0); jrose@1920: int wlen = (wide_format != NULL ? (int) strlen(wide_format) : 0); duke@435: _name [code] = name; duke@435: _result_type [code] = result_type; duke@435: _depth [code] = depth; jrose@1920: _lengths [code] = (wlen << 4) | (len & 0xF); duke@435: _java_code [code] = java_code; jrose@1920: int bc_flags = 0; jrose@1920: if (can_trap) bc_flags |= _bc_can_trap; jrose@1920: if (java_code != code) bc_flags |= _bc_can_rewrite; jrose@1920: _flags[(u1)code+0*(1< %s can trap, too", name(code), jcoomes@1845: name(java))); duke@435: } duke@435: } duke@435: } duke@435: #endif duke@435: duke@435: // initialization successful duke@435: _is_initialized = true; duke@435: } duke@435: duke@435: duke@435: void bytecodes_init() { duke@435: Bytecodes::initialize(); duke@435: } duke@435: duke@435: // Restore optimization duke@435: #ifdef _M_AMD64 duke@435: #pragma optimize ("", on) duke@435: #endif