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