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/interpreter.hpp"
  29 #include "interpreter/interpreterRuntime.hpp"
  30 #include "memory/allocation.inline.hpp"
  31 #include "memory/universe.hpp"
  32 #include "oops/oop.inline.hpp"
  33 #include "runtime/handles.inline.hpp"
  34 #include "runtime/icache.hpp"
  35 #include "runtime/interfaceSupport.inline.hpp"
  36 #include "runtime/signature.hpp"
  37 
  38 // Access macros for Java and C arguments.
  39 // First Java argument is at index-1.
  40 #define locals_j_arg_at(index) Address(Z_R1/*locals*/, in_ByteSize((-(index)*wordSize)))
  41 
  42 #define __ _masm->
  43 
  44 static int sp_c_int_arg_offset(int arg_nr, int fp_arg_nr) {
  45   int int_arg_nr = arg_nr-fp_arg_nr;
  46 
  47   // arg_nr, fp_arg_nr start with 1 => int_arg_nr starts with 0
  48   if (int_arg_nr < 5) {
  49     return int_arg_nr * wordSize + _z_abi(carg_1);
  50   }
  51   int offset = int_arg_nr - 5 + (fp_arg_nr > 4 ? fp_arg_nr - 4 : 0);
  52   return offset * wordSize + _z_abi(remaining_cargs);
  53 }
  54 
  55 static int sp_c_fp_arg_offset(int arg_nr, int fp_arg_nr) {
  56   int int_arg_nr = arg_nr-fp_arg_nr;
  57 
  58   // Arg_nr, fp_arg_nr start with 1 => int_arg_nr starts with 0.
  59   if (fp_arg_nr < 5) {
  60     return (fp_arg_nr - 1 ) * wordSize + _z_abi(cfarg_1);
  61   }
  62   int offset = fp_arg_nr - 5 + (int_arg_nr > 4 ? int_arg_nr - 4 : 0);
  63   return offset * wordSize + _z_abi(remaining_cargs);
  64 }
  65 
  66 // Implementation of SignatureHandlerGenerator
  67 
  68 void InterpreterRuntime::SignatureHandlerGenerator::pass_int() {
  69   int int_arg_nr = jni_offset() - _fp_arg_nr;
  70   Register r = (int_arg_nr < 5 /*max_int_register_arguments*/) ?
  71                  as_Register(int_arg_nr) + Z_ARG1->encoding() : Z_R0;
  72 
  73   __ z_lgf(r, locals_j_arg_at(offset()));
  74   if (DEBUG_ONLY(true ||) int_arg_nr >= 5) {
  75     __ z_stg(r, sp_c_int_arg_offset(jni_offset(), _fp_arg_nr), Z_SP);
  76   }
  77 }
  78 
  79 void InterpreterRuntime::SignatureHandlerGenerator::pass_long() {
  80   int int_arg_nr = jni_offset() - _fp_arg_nr;
  81   Register r = (int_arg_nr < 5 /*max_int_register_arguments*/) ?
  82                  as_Register(int_arg_nr) + Z_ARG1->encoding() : Z_R0;
  83 
  84   __ z_lg(r, locals_j_arg_at(offset() + 1)); // Long resides in upper slot.
  85   if (DEBUG_ONLY(true ||) int_arg_nr >= 5) {
  86     __ z_stg(r, sp_c_int_arg_offset(jni_offset(), _fp_arg_nr), Z_SP);
  87   }
  88 }
  89 
  90 void InterpreterRuntime::SignatureHandlerGenerator::pass_float() {
  91   FloatRegister fp_reg = (_fp_arg_nr < 4/*max_fp_register_arguments*/) ?
  92                            as_FloatRegister((_fp_arg_nr * 2) + Z_FARG1->encoding()) : Z_F1;
  93   _fp_arg_nr++;
  94   __ z_ley(fp_reg, locals_j_arg_at(offset()));
  95   if (DEBUG_ONLY(true ||) _fp_arg_nr > 4) {
  96     __ z_ste(fp_reg, sp_c_fp_arg_offset(jni_offset(), _fp_arg_nr) + 4, Z_SP);
  97   }
  98 }
  99 
 100 void InterpreterRuntime::SignatureHandlerGenerator::pass_double() {
 101   FloatRegister fp_reg = (_fp_arg_nr < 4/*max_fp_register_arguments*/) ?
 102                            as_FloatRegister((_fp_arg_nr*2) + Z_FARG1->encoding()) : Z_F1;
 103   _fp_arg_nr++;
 104   __ z_ldy(fp_reg, locals_j_arg_at(offset()+1));
 105   if (DEBUG_ONLY(true ||) _fp_arg_nr > 4) {
 106     __ z_std(fp_reg, sp_c_fp_arg_offset(jni_offset(), _fp_arg_nr), Z_SP);
 107   }
 108 }
 109 
 110 void InterpreterRuntime::SignatureHandlerGenerator::pass_object() {
 111   int int_arg_nr = jni_offset() - _fp_arg_nr;
 112   Register  r = (int_arg_nr < 5 /*max_int_register_arguments*/) ?
 113                   as_Register(int_arg_nr) + Z_ARG1->encoding() : Z_R0;
 114 
 115   // The handle for a receiver will never be null.
 116   bool do_NULL_check = offset() != 0 || is_static();
 117 
 118   Label do_null;
 119   if (do_NULL_check) {
 120     __ clear_reg(r, true, false);
 121     __ load_and_test_long(Z_R0, locals_j_arg_at(offset()));
 122     __ z_bre(do_null);
 123   }
 124   __ add2reg(r, -offset() * wordSize, Z_R1 /* locals */);
 125   __ bind(do_null);
 126   if (DEBUG_ONLY(true ||) int_arg_nr >= 5) {
 127     __ z_stg(r, sp_c_int_arg_offset(jni_offset(), _fp_arg_nr), Z_SP);
 128   }
 129 }
 130 
 131 
 132 void InterpreterRuntime::SignatureHandlerGenerator::generate(uint64_t fingerprint) {
 133   __ z_lgr(Z_R1, Z_ARG1); // Z_R1 is used in locals_j_arg_at(index) macro.
 134 
 135   // Generate code to handle arguments.
 136   iterate(fingerprint);
 137   __ load_const_optimized(Z_RET, AbstractInterpreter::result_handler(method()->result_type()));
 138   __ z_br(Z_R14);
 139   __ flush();
 140 }
 141 
 142 #undef  __
 143 
 144 // Implementation of SignatureHandlerLibrary
 145 
 146 void SignatureHandlerLibrary::pd_set_handler(address handler) {}
 147 
 148 IRT_ENTRY(address, InterpreterRuntime::get_signature(JavaThread* thread, Method* method))
 149   methodHandle m(thread, method);
 150   assert(m->is_native(), "sanity check");
 151   Symbol *s = m->signature();
 152   return (address) s->base();
 153 IRT_END
 154 
 155 IRT_ENTRY(address, InterpreterRuntime::get_result_handler(JavaThread* thread, Method* method))
 156   methodHandle m(thread, method);
 157   assert(m->is_native(), "sanity check");
 158   return AbstractInterpreter::result_handler(m->result_type());
 159 IRT_END