1 /*
   2  * Copyright (c) 2003, 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 #ifndef CC_INTERP
  53 
  54 /**
  55  * Method entry for static native methods:
  56  *   int java.util.zip.CRC32.update(int crc, int b)
  57  */
  58 address InterpreterGenerator::generate_CRC32_update_entry() {
  59   if (UseCRC32Intrinsics) {
  60     address entry = __ pc();
  61 
  62     // rbx,: Method*
  63     // r13: senderSP must preserved for slow path, set SP to it on fast path
  64     // c_rarg0: scratch (rdi on non-Win64, rcx on Win64)
  65     // c_rarg1: scratch (rsi on non-Win64, rdx on Win64)
  66 
  67     Label slow_path;
  68     // If we need a safepoint check, generate full interpreter entry.
  69     ExternalAddress state(SafepointSynchronize::address_of_state());
  70     __ cmp32(ExternalAddress(SafepointSynchronize::address_of_state()),
  71              SafepointSynchronize::_not_synchronized);
  72     __ jcc(Assembler::notEqual, slow_path);
  73 
  74     // We don't generate local frame and don't align stack because
  75     // we call stub code and there is no safepoint on this path.
  76 
  77     // Load parameters
  78     const Register crc = rax;  // crc
  79     const Register val = c_rarg0;  // source java byte value
  80     const Register tbl = c_rarg1;  // scratch
  81 
  82     // Arguments are reversed on java expression stack
  83     __ movl(val, Address(rsp,   wordSize)); // byte value
  84     __ movl(crc, Address(rsp, 2*wordSize)); // Initial CRC
  85 
  86     __ lea(tbl, ExternalAddress(StubRoutines::crc_table_addr()));
  87     __ notl(crc); // ~crc
  88     __ update_byte_crc32(crc, val, tbl);
  89     __ notl(crc); // ~crc
  90     // result in rax
  91 
  92     // _areturn
  93     __ pop(rdi);                // get return address
  94     __ mov(rsp, r13);           // set sp to sender sp
  95     __ jmp(rdi);
  96 
  97     // generate a vanilla native entry as the slow path
  98     __ bind(slow_path);
  99     __ jump_to_entry(Interpreter::entry_for_kind(Interpreter::native));
 100     return entry;
 101   }
 102   return NULL;
 103 }
 104 
 105 /**
 106  * Method entry for static native methods:
 107  *   int java.util.zip.CRC32.updateBytes(int crc, byte[] b, int off, int len)
 108  *   int java.util.zip.CRC32.updateByteBuffer(int crc, long buf, int off, int len)
 109  */
 110 address InterpreterGenerator::generate_CRC32_updateBytes_entry(AbstractInterpreter::MethodKind kind) {
 111   if (UseCRC32Intrinsics) {
 112     address entry = __ pc();
 113 
 114     // rbx,: Method*
 115     // r13: senderSP must preserved for slow path, set SP to it on fast path
 116 
 117     Label slow_path;
 118     // If we need a safepoint check, generate full interpreter entry.
 119     ExternalAddress state(SafepointSynchronize::address_of_state());
 120     __ cmp32(ExternalAddress(SafepointSynchronize::address_of_state()),
 121              SafepointSynchronize::_not_synchronized);
 122     __ jcc(Assembler::notEqual, slow_path);
 123 
 124     // We don't generate local frame and don't align stack because
 125     // we call stub code and there is no safepoint on this path.
 126 
 127     // Load parameters
 128     const Register crc = c_rarg0;  // crc
 129     const Register buf = c_rarg1;  // source java byte array address
 130     const Register len = c_rarg2;  // length
 131     const Register off = len;      // offset (never overlaps with 'len')
 132 
 133     // Arguments are reversed on java expression stack
 134     // Calculate address of start element
 135     if (kind == Interpreter::java_util_zip_CRC32_updateByteBuffer) {
 136       __ movptr(buf, Address(rsp, 3*wordSize)); // long buf
 137       __ movl2ptr(off, Address(rsp, 2*wordSize)); // offset
 138       __ addq(buf, off); // + offset
 139       __ movl(crc,   Address(rsp, 5*wordSize)); // Initial CRC
 140     } else {
 141       __ movptr(buf, Address(rsp, 3*wordSize)); // byte[] array
 142       __ addptr(buf, arrayOopDesc::base_offset_in_bytes(T_BYTE)); // + header size
 143       __ movl2ptr(off, Address(rsp, 2*wordSize)); // offset
 144       __ addq(buf, off); // + offset
 145       __ movl(crc,   Address(rsp, 4*wordSize)); // Initial CRC
 146     }
 147     // Can now load 'len' since we're finished with 'off'
 148     __ movl(len, Address(rsp, wordSize)); // Length
 149 
 150     __ super_call_VM_leaf(CAST_FROM_FN_PTR(address, StubRoutines::updateBytesCRC32()), crc, buf, len);
 151     // result in rax
 152 
 153     // _areturn
 154     __ pop(rdi);                // get return address
 155     __ mov(rsp, r13);           // set sp to sender sp
 156     __ jmp(rdi);
 157 
 158     // generate a vanilla native entry as the slow path
 159     __ bind(slow_path);
 160     __ jump_to_entry(Interpreter::entry_for_kind(Interpreter::native));
 161     return entry;
 162   }
 163   return NULL;
 164 }
 165 
 166 /**
 167 * Method entry for static native methods:
 168 *   int java.util.zip.CRC32C.updateBytes(int crc, byte[] b, int off, int end)
 169 *   int java.util.zip.CRC32C.updateByteBuffer(int crc, long address, int off, int end)
 170 */
 171 address InterpreterGenerator::generate_CRC32C_updateBytes_entry(AbstractInterpreter::MethodKind kind) {
 172   if (UseCRC32CIntrinsics) {
 173     address entry = __ pc();
 174     // Load parameters
 175     const Register crc = c_rarg0;  // crc
 176     const Register buf = c_rarg1;  // source java byte array address
 177     const Register len = c_rarg2;
 178     const Register off = c_rarg3;  // offset
 179     const Register end = len;
 180 
 181     // Arguments are reversed on java expression stack
 182     // Calculate address of start element
 183     if (kind == Interpreter::java_util_zip_CRC32C_updateDirectByteBuffer) {
 184       __ movptr(buf, Address(rsp, 3 * wordSize)); // long buf
 185       __ movl2ptr(off, Address(rsp, 2 * wordSize)); // offset
 186       __ addq(buf, off); // + offset
 187       __ movl(crc, Address(rsp, 5 * wordSize)); // Initial CRC
 188       // Note on 5 * wordSize vs. 4 * wordSize:
 189       // *   int java.util.zip.CRC32C.updateByteBuffer(int crc, long address, int off, int end)
 190       //                                                   4         2,3          1        0
 191       // end starts at SP + 8
 192       // The Java(R) Virtual Machine Specification Java SE 7 Edition
 193       // 4.10.2.3. Values of Types long and double
 194       //    "When calculating operand stack length, values of type long and double have length two."
 195     } else {
 196       __ movptr(buf, Address(rsp, 3 * wordSize)); // byte[] array
 197       __ addptr(buf, arrayOopDesc::base_offset_in_bytes(T_BYTE)); // + header size
 198       __ movl2ptr(off, Address(rsp, 2 * wordSize)); // offset
 199       __ addq(buf, off); // + offset
 200       __ movl(crc, Address(rsp, 4 * wordSize)); // Initial CRC
 201     }
 202     __ movl(end, Address(rsp, wordSize)); // end
 203     __ subl(end, off); // end - off
 204     __ super_call_VM_leaf(CAST_FROM_FN_PTR(address, StubRoutines::updateBytesCRC32C()), crc, buf, len);
 205     // result in rax
 206     // _areturn
 207     __ pop(rdi);                // get return address
 208     __ mov(rsp, r13);           // set sp to sender sp
 209     __ jmp(rdi);
 210 
 211     return entry;
 212   }
 213 
 214   return NULL;
 215 }
 216 #endif // ! CC_INTERP