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