1 /*
   2  * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * Copyright 2012, 2013 SAP AG. 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 "interpreter/interpreter.hpp"
  28 #include "interpreter/interpreterRuntime.hpp"
  29 #include "memory/allocation.inline.hpp"
  30 #include "memory/universe.inline.hpp"
  31 #include "oops/method.hpp"
  32 #include "oops/oop.inline.hpp"
  33 #include "runtime/handles.inline.hpp"
  34 #include "runtime/icache.hpp"
  35 #include "runtime/interfaceSupport.hpp"
  36 #include "runtime/signature.hpp"
  37 
  38 #define __ _masm->
  39 
  40 // Access macros for Java and C arguments.
  41 // The first Java argument is at index -1.
  42 #define locals_j_arg_at(index)    (Interpreter::local_offset_in_bytes(index)), R18_locals
  43 // The first C argument is at index 0.
  44 #define sp_c_arg_at(index)        ((index)*wordSize + _abi(carg_1)), R1_SP
  45 
  46 // Implementation of SignatureHandlerGenerator
  47 
  48 void InterpreterRuntime::SignatureHandlerGenerator::pass_int() {
  49   Argument jni_arg(jni_offset());
  50   Register r = jni_arg.is_register() ? jni_arg.as_register() : R0;
  51 
  52   __ lwa(r, locals_j_arg_at(offset())); // sign extension of integer
  53   if (DEBUG_ONLY(true ||) !jni_arg.is_register()) {
  54     __ std(r, sp_c_arg_at(jni_arg.number()));
  55   }
  56 }
  57 
  58 void InterpreterRuntime::SignatureHandlerGenerator::pass_long() {
  59   Argument jni_arg(jni_offset());
  60   Register r = jni_arg.is_register() ? jni_arg.as_register() : R0;
  61 
  62   __ ld(r, locals_j_arg_at(offset()+1)); // long resides in upper slot
  63   if (DEBUG_ONLY(true ||) !jni_arg.is_register()) {
  64     __ std(r, sp_c_arg_at(jni_arg.number()));
  65   }
  66 }
  67 
  68 void InterpreterRuntime::SignatureHandlerGenerator::pass_float() {
  69   FloatRegister fp_reg = (_num_used_fp_arg_regs < 13/*max_fp_register_arguments*/)
  70                          ? as_FloatRegister((_num_used_fp_arg_regs++) + F1_ARG1->encoding())
  71                          : F0;
  72 
  73   __ lfs(fp_reg, locals_j_arg_at(offset()));
  74   if (DEBUG_ONLY(true ||) jni_offset() > 8) {
  75     __ stfs(fp_reg, sp_c_arg_at(jni_offset()));
  76   }
  77 }
  78 
  79 void InterpreterRuntime::SignatureHandlerGenerator::pass_double() {
  80   FloatRegister fp_reg = (_num_used_fp_arg_regs < 13/*max_fp_register_arguments*/)
  81                          ? as_FloatRegister((_num_used_fp_arg_regs++) + F1_ARG1->encoding())
  82                          : F0;
  83 
  84   __ lfd(fp_reg, locals_j_arg_at(offset()+1));
  85   if (DEBUG_ONLY(true ||) jni_offset() > 8) {
  86     __ stfd(fp_reg, sp_c_arg_at(jni_offset()));
  87   }
  88 }
  89 
  90 void InterpreterRuntime::SignatureHandlerGenerator::pass_object() {
  91   Argument jni_arg(jni_offset());
  92   Register r = jni_arg.is_register() ? jni_arg.as_register() : R11_scratch1;
  93 
  94   // The handle for a receiver will never be null.
  95   bool do_NULL_check = offset() != 0 || is_static();
  96 
  97   Label do_null;
  98   if (do_NULL_check) {
  99     __ ld(R0, locals_j_arg_at(offset()));
 100     __ cmpdi(CCR0, R0, 0);
 101     __ li(r, 0);
 102     __ beq(CCR0, do_null);
 103   }
 104   __ addir(r, locals_j_arg_at(offset()));
 105   __ bind(do_null);
 106   if (DEBUG_ONLY(true ||) !jni_arg.is_register()) {
 107     __ std(r, sp_c_arg_at(jni_arg.number()));
 108   }
 109 }
 110 
 111 void InterpreterRuntime::SignatureHandlerGenerator::generate(uint64_t fingerprint) {
 112   // Emit fd for current codebuffer. Needs patching!
 113   __ emit_fd();
 114 
 115   // Generate code to handle arguments.
 116   iterate(fingerprint);
 117 
 118   // Return the result handler.
 119   __ load_const(R3_RET, AbstractInterpreter::result_handler(method()->result_type()));
 120   __ blr();
 121 
 122   __ flush();
 123 }
 124 
 125 #undef __
 126 
 127 // Implementation of SignatureHandlerLibrary
 128 
 129 void SignatureHandlerLibrary::pd_set_handler(address handler) {
 130   // patch fd here.
 131   FunctionDescriptor* fd = (FunctionDescriptor*) handler;
 132 
 133   fd->set_entry(handler + (int)sizeof(FunctionDescriptor));
 134   assert(fd->toc() == (address)0xcafe, "need to adjust TOC here");
 135 }
 136 
 137 
 138 // Access function to get the signature.
 139 IRT_ENTRY(address, InterpreterRuntime::get_signature(JavaThread* thread, Method* method))
 140   methodHandle m(thread, method);
 141   assert(m->is_native(), "sanity check");
 142   Symbol *s = m->signature();
 143   return (address) s->base();
 144 IRT_END
 145 
 146 IRT_ENTRY(address, InterpreterRuntime::get_result_handler(JavaThread* thread, Method* method))
 147   methodHandle m(thread, method);
 148   assert(m->is_native(), "sanity check");
 149   return AbstractInterpreter::result_handler(m->result_type());
 150 IRT_END