1 /*
   2  * Copyright (c) 1997, 2015, 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 "asm/macroAssembler.hpp"
  27 #include "interpreter/bytecodeHistogram.hpp"
  28 #include "interpreter/interpreter.hpp"
  29 #include "interpreter/interpreterGenerator.hpp"
  30 #include "interpreter/interpreterRuntime.hpp"
  31 #include "interpreter/interp_masm.hpp"
  32 #include "interpreter/templateTable.hpp"
  33 #include "oops/arrayOop.hpp"
  34 #include "oops/methodData.hpp"
  35 #include "oops/method.hpp"
  36 #include "oops/oop.inline.hpp"
  37 #include "prims/jvmtiExport.hpp"
  38 #include "prims/jvmtiThreadState.hpp"
  39 #include "runtime/arguments.hpp"
  40 #include "runtime/deoptimization.hpp"
  41 #include "runtime/frame.inline.hpp"
  42 #include "runtime/sharedRuntime.hpp"
  43 #include "runtime/stubRoutines.hpp"
  44 #include "runtime/synchronizer.hpp"
  45 #include "runtime/timer.hpp"
  46 #include "runtime/vframeArray.hpp"
  47 #include "utilities/debug.hpp"
  48 #include "utilities/macros.hpp"
  49 
  50 #define __ _masm->
  51 
  52 
  53 #ifndef CC_INTERP
  54 
  55 /**
  56  * Method entry for static native methods:
  57  *   int java.util.zip.CRC32.update(int crc, int b)
  58  */
  59 address InterpreterGenerator::generate_CRC32_update_entry() {
  60   if (UseCRC32Intrinsics) {
  61     address entry = __ pc();
  62 
  63     // rbx: Method*
  64     // rsi: senderSP must preserved for slow path, set SP to it on fast path
  65     // rdx: scratch
  66     // rdi: scratch
  67 
  68     Label slow_path;
  69     // If we need a safepoint check, generate full interpreter entry.
  70     ExternalAddress state(SafepointSynchronize::address_of_state());
  71     __ cmp32(ExternalAddress(SafepointSynchronize::address_of_state()),
  72              SafepointSynchronize::_not_synchronized);
  73     __ jcc(Assembler::notEqual, slow_path);
  74 
  75     // We don't generate local frame and don't align stack because
  76     // we call stub code and there is no safepoint on this path.
  77 
  78     // Load parameters
  79     const Register crc = rax;  // crc
  80     const Register val = rdx;  // source java byte value
  81     const Register tbl = rdi;  // scratch
  82 
  83     // Arguments are reversed on java expression stack
  84     __ movl(val, Address(rsp,   wordSize)); // byte value
  85     __ movl(crc, Address(rsp, 2*wordSize)); // Initial CRC
  86 
  87     __ lea(tbl, ExternalAddress(StubRoutines::crc_table_addr()));
  88     __ notl(crc); // ~crc
  89     __ update_byte_crc32(crc, val, tbl);
  90     __ notl(crc); // ~crc
  91     // result in rax
  92 
  93     // _areturn
  94     __ pop(rdi);                // get return address
  95     __ mov(rsp, rsi);           // set sp to sender sp
  96     __ jmp(rdi);
  97 
  98     // generate a vanilla native entry as the slow path
  99     __ bind(slow_path);
 100     __ jump_to_entry(Interpreter::entry_for_kind(Interpreter::native));
 101     return entry;
 102   }
 103   return NULL;
 104 }
 105 
 106 /**
 107  * Method entry for static native methods:
 108  *   int java.util.zip.CRC32.updateBytes(int crc, byte[] b, int off, int len)
 109  *   int java.util.zip.CRC32.updateByteBuffer(int crc, long buf, int off, int len)
 110  */
 111 address InterpreterGenerator::generate_CRC32_updateBytes_entry(AbstractInterpreter::MethodKind kind) {
 112   if (UseCRC32Intrinsics) {
 113     address entry = __ pc();
 114 
 115     // rbx,: Method*
 116     // rsi: senderSP must preserved for slow path, set SP to it on fast path
 117     // rdx: scratch
 118     // rdi: scratch
 119 
 120     Label slow_path;
 121     // If we need a safepoint check, generate full interpreter entry.
 122     ExternalAddress state(SafepointSynchronize::address_of_state());
 123     __ cmp32(ExternalAddress(SafepointSynchronize::address_of_state()),
 124              SafepointSynchronize::_not_synchronized);
 125     __ jcc(Assembler::notEqual, slow_path);
 126 
 127     // We don't generate local frame and don't align stack because
 128     // we call stub code and there is no safepoint on this path.
 129 
 130     // Load parameters
 131     const Register crc = rax;  // crc
 132     const Register buf = rdx;  // source java byte array address
 133     const Register len = rdi;  // length
 134 
 135     // value              x86_32
 136     // interp. arg ptr    ESP + 4
 137     // int java.util.zip.CRC32.updateBytes(int crc, byte[] b, int off, int len)
 138     //                                         3           2      1        0
 139     // int java.util.zip.CRC32.updateByteBuffer(int crc, long buf, int off, int len)
 140     //                                              4         2,3      1        0
 141 
 142     // Arguments are reversed on java expression stack
 143     __ movl(len,   Address(rsp,   4 + 0)); // Length
 144     // Calculate address of start element
 145     if (kind == Interpreter::java_util_zip_CRC32_updateByteBuffer) {
 146       __ movptr(buf, Address(rsp, 4 + 2 * wordSize)); // long buf
 147       __ addptr(buf, Address(rsp, 4 + 1 * wordSize)); // + offset
 148       __ movl(crc,   Address(rsp, 4 + 4 * wordSize)); // Initial CRC
 149     } else {
 150       __ movptr(buf, Address(rsp, 4 + 2 * wordSize)); // byte[] array
 151       __ addptr(buf, arrayOopDesc::base_offset_in_bytes(T_BYTE)); // + header size
 152       __ addptr(buf, Address(rsp, 4 + 1 * wordSize)); // + offset
 153       __ movl(crc,   Address(rsp, 4 + 3 * wordSize)); // Initial CRC
 154     }
 155 
 156     __ super_call_VM_leaf(CAST_FROM_FN_PTR(address, StubRoutines::updateBytesCRC32()), crc, buf, len);
 157     // result in rax
 158 
 159     // _areturn
 160     __ pop(rdi);                // get return address
 161     __ mov(rsp, rsi);           // set sp to sender sp
 162     __ jmp(rdi);
 163 
 164     // generate a vanilla native entry as the slow path
 165     __ bind(slow_path);
 166     __ jump_to_entry(Interpreter::entry_for_kind(Interpreter::native));
 167     return entry;
 168   }
 169   return NULL;
 170 }
 171 
 172 /**
 173 * Method entry for static native methods:
 174 *   int java.util.zip.CRC32C.updateBytes(int crc, byte[] b, int off, int end)
 175 *   int java.util.zip.CRC32C.updateByteBuffer(int crc, long address, int off, int end)
 176 */
 177 address InterpreterGenerator::generate_CRC32C_updateBytes_entry(AbstractInterpreter::MethodKind kind) {
 178   if (UseCRC32CIntrinsics) {
 179     address entry = __ pc();
 180     // Load parameters
 181     const Register crc = rax;  // crc
 182     const Register buf = rcx;  // source java byte array address
 183     const Register len = rdx;  // length
 184     const Register end = len;
 185 
 186     // value              x86_32
 187     // interp. arg ptr    ESP + 4
 188     // int java.util.zip.CRC32.updateBytes(int crc, byte[] b, int off, int end)
 189     //                                         3           2      1        0
 190     // int java.util.zip.CRC32.updateByteBuffer(int crc, long address, int off, int end)
 191     //                                              4         2,3          1        0
 192 
 193     // Arguments are reversed on java expression stack
 194     __ movl(end, Address(rsp, 4 + 0)); // end
 195     __ subl(len, Address(rsp, 4 + 1 * wordSize));  // end - offset == length
 196     // Calculate address of start element
 197     if (kind == Interpreter::java_util_zip_CRC32C_updateDirectByteBuffer) {
 198       __ movptr(buf, Address(rsp, 4 + 2 * wordSize)); // long address
 199       __ addptr(buf, Address(rsp, 4 + 1 * wordSize)); // + offset
 200       __ movl(crc, Address(rsp, 4 + 4 * wordSize)); // Initial CRC
 201     } else {
 202       __ movptr(buf, Address(rsp, 4 + 2 * wordSize)); // byte[] array
 203       __ addptr(buf, arrayOopDesc::base_offset_in_bytes(T_BYTE)); // + header size
 204       __ addptr(buf, Address(rsp, 4 + 1 * wordSize)); // + offset
 205       __ movl(crc, Address(rsp, 4 + 3 * wordSize)); // Initial CRC
 206     }
 207     __ super_call_VM_leaf(CAST_FROM_FN_PTR(address, StubRoutines::updateBytesCRC32C()), crc, buf, len);
 208     // result in rax
 209     // _areturn
 210     __ pop(rdi);                // get return address
 211     __ mov(rsp, rsi);           // set sp to sender sp
 212     __ jmp(rdi);
 213 
 214     return entry;
 215   }
 216   return NULL;
 217 }
 218 
 219 /**
 220  * Method entry for static native method:
 221  *    java.lang.Float.intBitsToFloat(int bits)
 222  */
 223 address InterpreterGenerator::generate_Float_intBitsToFloat_entry() {
 224   if (UseSSE >= 1) {
 225     address entry = __ pc();
 226 
 227     // rsi: the sender's SP
 228 
 229     // Skip safepoint check (compiler intrinsic versions of this method
 230     // do not perform safepoint checks either).
 231 
 232     // Load 'bits' into xmm0 (interpreter returns results in xmm0)
 233     __ movflt(xmm0, Address(rsp, wordSize));
 234 
 235     // Return
 236     __ pop(rdi); // get return address
 237     __ mov(rsp, rsi); // set rsp to the sender's SP
 238     __ jmp(rdi);
 239     return entry;
 240   }
 241 
 242   return NULL;
 243 }
 244 
 245 /**
 246  * Method entry for static native method:
 247  *    java.lang.Float.floatToRawIntBits(float value)
 248  */
 249 address InterpreterGenerator::generate_Float_floatToRawIntBits_entry() {
 250   if (UseSSE >= 1) {
 251     address entry = __ pc();
 252 
 253     // rsi: the sender's SP
 254 
 255     // Skip safepoint check (compiler intrinsic versions of this method
 256     // do not perform safepoint checks either).
 257 
 258     // Load the parameter (a floating-point value) into rax.
 259     __ movl(rax, Address(rsp, wordSize));
 260 
 261     // Return
 262     __ pop(rdi); // get return address
 263     __ mov(rsp, rsi); // set rsp to the sender's SP
 264     __ jmp(rdi);
 265     return entry;
 266   }
 267 
 268   return NULL;
 269 }
 270 
 271 
 272 /**
 273  * Method entry for static native method:
 274  *    java.lang.Double.longBitsToDouble(long bits)
 275  */
 276 address InterpreterGenerator::generate_Double_longBitsToDouble_entry() {
 277    if (UseSSE >= 2) {
 278      address entry = __ pc();
 279 
 280      // rsi: the sender's SP
 281 
 282      // Skip safepoint check (compiler intrinsic versions of this method
 283      // do not perform safepoint checks either).
 284 
 285      // Load 'bits' into xmm0 (interpreter returns results in xmm0)
 286      __ movdbl(xmm0, Address(rsp, wordSize));
 287 
 288      // Return
 289      __ pop(rdi); // get return address
 290      __ mov(rsp, rsi); // set rsp to the sender's SP
 291      __ jmp(rdi);
 292      return entry;
 293    }
 294 
 295    return NULL;
 296 }
 297 
 298 /**
 299  * Method entry for static native method:
 300  *    java.lang.Double.doubleToRawLongBits(double value)
 301  */
 302 address InterpreterGenerator::generate_Double_doubleToRawLongBits_entry() {
 303   if (UseSSE >= 2) {
 304     address entry = __ pc();
 305 
 306     // rsi: the sender's SP
 307 
 308     // Skip safepoint check (compiler intrinsic versions of this method
 309     // do not perform safepoint checks either).
 310 
 311     // Load the parameter (a floating-point value) into rax.
 312     __ movl(rdx, Address(rsp, 2*wordSize));
 313     __ movl(rax, Address(rsp, wordSize));
 314 
 315     // Return
 316     __ pop(rdi); // get return address
 317     __ mov(rsp, rsi); // set rsp to the sender's SP
 318     __ jmp(rdi);
 319     return entry;
 320   }
 321 
 322   return NULL;
 323 }
 324 #endif // CC_INTERP