1 /*
   2  * Copyright (c) 2003, 2018, Oracle and/or its affiliates. All rights reserved.
   3  * Copyright 2007, 2008, 2010 Red Hat, Inc.
   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.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.inline.hpp"
  36 #include "runtime/signature.hpp"
  37 #include "stack_zero.inline.hpp"
  38 #include "utilities/align.hpp"
  39 
  40 void InterpreterRuntime::SignatureHandlerGeneratorBase::pass_int() {
  41   push(T_INT);
  42   _cif->nargs++;
  43 }
  44 
  45 void InterpreterRuntime::SignatureHandlerGeneratorBase::pass_long() {
  46   push(T_LONG);
  47   _cif->nargs++;
  48 }
  49 
  50 void InterpreterRuntime::SignatureHandlerGeneratorBase::pass_float() {
  51   push(T_FLOAT);
  52   _cif->nargs++;
  53 }
  54 
  55 void InterpreterRuntime::SignatureHandlerGeneratorBase::pass_double() {
  56   push(T_DOUBLE);
  57   _cif->nargs++;
  58 }
  59 
  60 void InterpreterRuntime::SignatureHandlerGeneratorBase::pass_object() {
  61   push(T_OBJECT);
  62   _cif->nargs++;
  63 }
  64 
  65 void InterpreterRuntime::SignatureHandlerGeneratorBase::push(BasicType type) {
  66   ffi_type *ftype = NULL;
  67   switch (type) {
  68   case T_VOID:
  69     ftype = &ffi_type_void;
  70     break;
  71 
  72   case T_BOOLEAN:
  73     ftype = &ffi_type_uint8;
  74     break;
  75 
  76   case T_CHAR:
  77     ftype = &ffi_type_uint16;
  78     break;
  79 
  80   case T_BYTE:
  81     ftype = &ffi_type_sint8;
  82     break;
  83 
  84   case T_SHORT:
  85     ftype = &ffi_type_sint16;
  86     break;
  87 
  88   case T_INT:
  89     ftype = &ffi_type_sint32;
  90     break;
  91 
  92   case T_LONG:
  93     ftype = &ffi_type_sint64;
  94     break;
  95 
  96   case T_FLOAT:
  97     ftype = &ffi_type_float;
  98     break;
  99 
 100   case T_DOUBLE:
 101     ftype = &ffi_type_double;
 102     break;
 103 
 104   case T_OBJECT:
 105   case T_ARRAY:
 106     ftype = &ffi_type_pointer;
 107     break;
 108 
 109   default:
 110     ShouldNotReachHere();
 111   }
 112   push((intptr_t) ftype);
 113 }
 114 
 115 // For fast signature handlers the "signature handler" is generated
 116 // into a temporary buffer.  It is then copied to its final location,
 117 // and pd_set_handler is called on it.  We have this two stage thing
 118 // to accomodate this.
 119 
 120 void InterpreterRuntime::SignatureHandlerGeneratorBase::generate(
 121   uint64_t fingerprint) {
 122 
 123   // Build the argument types list
 124   pass_object();
 125   if (method()->is_static())
 126     pass_object();
 127   iterate(fingerprint);
 128 
 129   // Tack on the result type
 130   push(method()->result_type());
 131 }
 132 
 133 void InterpreterRuntime::SignatureHandler::finalize() {
 134   ffi_status status =
 135     ffi_prep_cif(cif(),
 136                  FFI_DEFAULT_ABI,
 137                  argument_count(),
 138                  result_type(),
 139                  argument_types());
 140 
 141   assert(status == FFI_OK, "should be");
 142 }
 143 
 144 IRT_ENTRY(address,
 145           InterpreterRuntime::slow_signature_handler(JavaThread* thread,
 146                                                      Method*     method,
 147                                                      intptr_t*   unused1,
 148                                                      intptr_t*   unused2))
 149   ZeroStack *stack = thread->zero_stack();
 150 
 151   int required_words =
 152     (align_up(sizeof(ffi_cif), wordSize) >> LogBytesPerWord) +
 153     (method->is_static() ? 2 : 1) + method->size_of_parameters() + 1;
 154 
 155   stack->overflow_check(required_words, CHECK_NULL);
 156 
 157   intptr_t *buf = (intptr_t *) stack->alloc(required_words * wordSize);
 158   SlowSignatureHandlerGenerator sshg(methodHandle(thread, method), buf);
 159   sshg.generate((uint64_t)CONST64(-1));
 160 
 161   SignatureHandler *handler = sshg.handler();
 162   handler->finalize();
 163 
 164   return (address) handler;
 165 IRT_END
 166 
 167 void SignatureHandlerLibrary::pd_set_handler(address handlerAddr) {
 168   InterpreterRuntime::SignatureHandler *handler =
 169     InterpreterRuntime::SignatureHandler::from_handlerAddr(handlerAddr);
 170 
 171   handler->finalize();
 172 }