1 /*
   2  * Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved.
   3  * Copyright (c) 2016 SAP SE. All rights reserved.
   4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   5  *
   6  * This code is free software; you can redistribute it and/or modify it
   7  * under the terms of the GNU General Public License version 2 only, as
   8  * published by the Free Software Foundation.
   9  *
  10  * This code is distributed in the hope that it will be useful, but WITHOUT
  11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  13  * version 2 for more details (a copy is included in the LICENSE file that
  14  * accompanied this code).
  15  *
  16  * You should have received a copy of the GNU General Public License version
  17  * 2 along with this work; if not, write to the Free Software Foundation,
  18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  19  *
  20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  21  * or visit www.oracle.com if you need additional information or have any
  22  * questions.
  23  *
  24  */
  25 
  26 #include "precompiled.hpp"
  27 #include "asm/macroAssembler.inline.hpp"
  28 #include "interpreter/interp_masm.hpp"
  29 #include "interpreter/interpreter.hpp"
  30 #include "interpreter/interpreterRuntime.hpp"
  31 #include "memory/allocation.inline.hpp"
  32 #include "memory/universe.hpp"
  33 #include "oops/oop.inline.hpp"
  34 #include "runtime/handles.inline.hpp"
  35 #include "runtime/icache.hpp"
  36 #include "runtime/interfaceSupport.inline.hpp"
  37 #include "runtime/signature.hpp"
  38 
  39 // Access macros for Java and C arguments.
  40 // First Java argument is at index-1.
  41 #define locals_j_arg_at(index) Address(Z_R1/*locals*/, in_ByteSize((-(index)*wordSize)))
  42 
  43 #define __ _masm->
  44 
  45 static int sp_c_int_arg_offset(int arg_nr, int fp_arg_nr) {
  46   int int_arg_nr = arg_nr-fp_arg_nr;
  47 
  48   // arg_nr, fp_arg_nr start with 1 => int_arg_nr starts with 0
  49   if (int_arg_nr < 5) {
  50     return int_arg_nr * wordSize + _z_abi(carg_1);
  51   }
  52   int offset = int_arg_nr - 5 + (fp_arg_nr > 4 ? fp_arg_nr - 4 : 0);
  53   return offset * wordSize + _z_abi(remaining_cargs);
  54 }
  55 
  56 static int sp_c_fp_arg_offset(int arg_nr, int fp_arg_nr) {
  57   int int_arg_nr = arg_nr-fp_arg_nr;
  58 
  59   // Arg_nr, fp_arg_nr start with 1 => int_arg_nr starts with 0.
  60   if (fp_arg_nr < 5) {
  61     return (fp_arg_nr - 1 ) * wordSize + _z_abi(cfarg_1);
  62   }
  63   int offset = fp_arg_nr - 5 + (int_arg_nr > 4 ? int_arg_nr - 4 : 0);
  64   return offset * wordSize + _z_abi(remaining_cargs);
  65 }
  66 
  67 // Implementation of SignatureHandlerGenerator
  68 InteprerterRuntime::SignatureHandlerGenerator::SignatureHandlerGenerator(
  69     const methodHandle& method, CodeBuffer* buffer) : NativeSignatureIterator(method) {
  70   _masm = new MacroAssembler(buffer);
  71   _fp_arg_nr = 0;
  72 }
  73 
  74 void InterpreterRuntime::SignatureHandlerGenerator::pass_int() {
  75   int int_arg_nr = jni_offset() - _fp_arg_nr;
  76   Register r = (int_arg_nr < 5 /*max_int_register_arguments*/) ?
  77                  as_Register(int_arg_nr) + Z_ARG1->encoding() : Z_R0;
  78 
  79   __ z_lgf(r, locals_j_arg_at(offset()));
  80   if (DEBUG_ONLY(true ||) int_arg_nr >= 5) {
  81     __ z_stg(r, sp_c_int_arg_offset(jni_offset(), _fp_arg_nr), Z_SP);
  82   }
  83 }
  84 
  85 void InterpreterRuntime::SignatureHandlerGenerator::pass_long() {
  86   int int_arg_nr = jni_offset() - _fp_arg_nr;
  87   Register r = (int_arg_nr < 5 /*max_int_register_arguments*/) ?
  88                  as_Register(int_arg_nr) + Z_ARG1->encoding() : Z_R0;
  89 
  90   __ z_lg(r, locals_j_arg_at(offset() + 1)); // Long resides in upper slot.
  91   if (DEBUG_ONLY(true ||) int_arg_nr >= 5) {
  92     __ z_stg(r, sp_c_int_arg_offset(jni_offset(), _fp_arg_nr), Z_SP);
  93   }
  94 }
  95 
  96 void InterpreterRuntime::SignatureHandlerGenerator::pass_float() {
  97   FloatRegister fp_reg = (_fp_arg_nr < 4/*max_fp_register_arguments*/) ?
  98                            as_FloatRegister((_fp_arg_nr * 2) + Z_FARG1->encoding()) : Z_F1;
  99   _fp_arg_nr++;
 100   __ z_ley(fp_reg, locals_j_arg_at(offset()));
 101   if (DEBUG_ONLY(true ||) _fp_arg_nr > 4) {
 102     __ z_ste(fp_reg, sp_c_fp_arg_offset(jni_offset(), _fp_arg_nr) + 4, Z_SP);
 103   }
 104 }
 105 
 106 void InterpreterRuntime::SignatureHandlerGenerator::pass_double() {
 107   FloatRegister fp_reg = (_fp_arg_nr < 4/*max_fp_register_arguments*/) ?
 108                            as_FloatRegister((_fp_arg_nr*2) + Z_FARG1->encoding()) : Z_F1;
 109   _fp_arg_nr++;
 110   __ z_ldy(fp_reg, locals_j_arg_at(offset()+1));
 111   if (DEBUG_ONLY(true ||) _fp_arg_nr > 4) {
 112     __ z_std(fp_reg, sp_c_fp_arg_offset(jni_offset(), _fp_arg_nr), Z_SP);
 113   }
 114 }
 115 
 116 void InterpreterRuntime::SignatureHandlerGenerator::pass_object() {
 117   int int_arg_nr = jni_offset() - _fp_arg_nr;
 118   Register  r = (int_arg_nr < 5 /*max_int_register_arguments*/) ?
 119                   as_Register(int_arg_nr) + Z_ARG1->encoding() : Z_R0;
 120 
 121   // The handle for a receiver will never be null.
 122   bool do_NULL_check = offset() != 0 || is_static();
 123 
 124   Label do_null;
 125   if (do_NULL_check) {
 126     __ clear_reg(r, true, false);
 127     __ load_and_test_long(Z_R0, locals_j_arg_at(offset()));
 128     __ z_bre(do_null);
 129   }
 130   __ add2reg(r, -offset() * wordSize, Z_R1 /* locals */);
 131   __ bind(do_null);
 132   if (DEBUG_ONLY(true ||) int_arg_nr >= 5) {
 133     __ z_stg(r, sp_c_int_arg_offset(jni_offset(), _fp_arg_nr), Z_SP);
 134   }
 135 }
 136 
 137 
 138 void InterpreterRuntime::SignatureHandlerGenerator::generate(uint64_t fingerprint) {
 139   __ z_lgr(Z_R1, Z_ARG1); // Z_R1 is used in locals_j_arg_at(index) macro.
 140 
 141   // Generate code to handle arguments.
 142   iterate(fingerprint);
 143   __ load_const_optimized(Z_RET, AbstractInterpreter::result_handler(method()->result_type()));
 144   __ z_br(Z_R14);
 145   __ flush();
 146 }
 147 
 148 #undef  __
 149 
 150 // Implementation of SignatureHandlerLibrary
 151 
 152 void SignatureHandlerLibrary::pd_set_handler(address handler) {}
 153 
 154 IRT_ENTRY(address, InterpreterRuntime::get_signature(JavaThread* thread, Method* method))
 155   methodHandle m(thread, method);
 156   assert(m->is_native(), "sanity check");
 157   Symbol *s = m->signature();
 158   return (address) s->base();
 159 IRT_END
 160 
 161 IRT_ENTRY(address, InterpreterRuntime::get_result_handler(JavaThread* thread, Method* method))
 162   methodHandle m(thread, method);
 163   assert(m->is_native(), "sanity check");
 164   return AbstractInterpreter::result_handler(m->result_type());
 165 IRT_END