1 /*
   2  * Copyright (c) 1998, 2012, 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 "interpreter/interpreter.hpp"
  27 #include "interpreter/interpreterRuntime.hpp"
  28 #include "memory/allocation.inline.hpp"
  29 #include "memory/universe.inline.hpp"
  30 #include "oops/method.hpp"
  31 #include "oops/oop.inline.hpp"
  32 #include "runtime/handles.inline.hpp"
  33 #include "runtime/icache.hpp"
  34 #include "runtime/interfaceSupport.hpp"
  35 #include "runtime/signature.hpp"
  36 
  37 
  38 #define __ _masm->
  39 
  40 
  41 // Implementation of SignatureHandlerGenerator
  42 void InterpreterRuntime::SignatureHandlerGenerator::pass_int() {
  43   move(offset(), jni_offset() + 1);
  44 }
  45 
  46 void InterpreterRuntime::SignatureHandlerGenerator::pass_float() {
  47   move(offset(), jni_offset() + 1);
  48 }
  49 
  50 void InterpreterRuntime::SignatureHandlerGenerator::pass_long() {
  51    move(offset(), jni_offset() + 2);
  52    move(offset() + 1, jni_offset() + 1);
  53 }
  54 
  55 void InterpreterRuntime::SignatureHandlerGenerator::pass_object() {
  56   box (offset(), jni_offset() + 1);
  57 }
  58 
  59 void InterpreterRuntime::SignatureHandlerGenerator::pass_valuetype() {
  60   box (offset(), jni_offset() + 1);
  61 }
  62 
  63 void InterpreterRuntime::SignatureHandlerGenerator::move(int from_offset, int to_offset) {
  64   __ movl(temp(), Address(from(), Interpreter::local_offset_in_bytes(from_offset)));
  65   __ movl(Address(to(), to_offset * wordSize), temp());
  66 }
  67 
  68 
  69 void InterpreterRuntime::SignatureHandlerGenerator::box(int from_offset, int to_offset) {
  70   __ lea(temp(), Address(from(), Interpreter::local_offset_in_bytes(from_offset)));
  71   __ cmpptr(Address(from(), Interpreter::local_offset_in_bytes(from_offset)), (int32_t)NULL_WORD); // do not use temp() to avoid AGI
  72   Label L;
  73   __ jcc(Assembler::notZero, L);
  74   __ movptr(temp(), NULL_WORD);
  75   __ bind(L);
  76   __ movptr(Address(to(), to_offset * wordSize), temp());
  77 }
  78 
  79 
  80 void InterpreterRuntime::SignatureHandlerGenerator::generate( uint64_t fingerprint) {
  81   // generate code to handle arguments
  82   iterate(fingerprint);
  83   // return result handler
  84   __ lea(rax,
  85          ExternalAddress((address)Interpreter::result_handler(method()->result_type())));
  86   // return
  87   __ ret(0);
  88   __ flush();
  89 }
  90 
  91 
  92 Register InterpreterRuntime::SignatureHandlerGenerator::from()       { return rdi; }
  93 Register InterpreterRuntime::SignatureHandlerGenerator::to()         { return rsp; }
  94 Register InterpreterRuntime::SignatureHandlerGenerator::temp()       { return rcx; }
  95 
  96 
  97 // Implementation of SignatureHandlerLibrary
  98 
  99 void SignatureHandlerLibrary::pd_set_handler(address handler) {}
 100 
 101 class SlowSignatureHandler: public NativeSignatureIterator {
 102  private:
 103   address   _from;
 104   intptr_t* _to;
 105 
 106   virtual void pass_int() {
 107     *_to++ = *(jint *)(_from+Interpreter::local_offset_in_bytes(0));
 108     _from -= Interpreter::stackElementSize;
 109   }
 110 
 111   virtual void pass_float() {
 112     *_to++ = *(jint *)(_from+Interpreter::local_offset_in_bytes(0));
 113     _from -= Interpreter::stackElementSize;
 114   }
 115 
 116   virtual void pass_long() {
 117     _to[0] = *(intptr_t*)(_from+Interpreter::local_offset_in_bytes(1));
 118     _to[1] = *(intptr_t*)(_from+Interpreter::local_offset_in_bytes(0));
 119     _to += 2;
 120     _from -= 2*Interpreter::stackElementSize;
 121   }
 122 
 123   virtual void pass_object() {
 124     // pass address of from
 125     intptr_t from_addr = (intptr_t)(_from + Interpreter::local_offset_in_bytes(0));
 126     *_to++ = (*(intptr_t*)from_addr == 0) ? NULL_WORD : from_addr;
 127     _from -= Interpreter::stackElementSize;
 128    }
 129 
 130   virtual void pass_valuetype() {
 131     // pass address of from
 132     intptr_t from_addr = (intptr_t)(_from + Interpreter::local_offset_in_bytes(0));
 133     *_to++ = (*(intptr_t*)from_addr == 0) ? NULL_WORD : from_addr;
 134     _from -= Interpreter::stackElementSize;
 135    }
 136 
 137  public:
 138   SlowSignatureHandler(methodHandle method, address from, intptr_t* to) :
 139     NativeSignatureIterator(method) {
 140     _from = from;
 141     _to   = to + (is_static() ? 2 : 1);
 142   }
 143 };
 144 
 145 IRT_ENTRY(address, InterpreterRuntime::slow_signature_handler(JavaThread* thread, Method* method, intptr_t* from, intptr_t* to))
 146   methodHandle m(thread, (Method*)method);
 147   assert(m->is_native(), "sanity check");
 148   // handle arguments
 149   SlowSignatureHandler(m, (address)from, to + 1).iterate((uint64_t)CONST64(-1));
 150   // return result handler
 151   return Interpreter::result_handler(m->result_type());
 152 IRT_END