1 /*
   2  * Copyright 2003-2005 Sun Microsystems, Inc.  All Rights Reserved.
   3  * Copyright 2007, 2008 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  21  * CA 95054 USA or visit www.sun.com if you need additional information or
  22  * have any questions.
  23  *
  24  */
  25 
  26 class SignatureHandler {
  27  public:
  28   static SignatureHandler *from_handlerAddr(address handlerAddr) {
  29     return (SignatureHandler *) handlerAddr;
  30   }
  31 
  32  public:
  33   ffi_cif* cif() const {
  34     return (ffi_cif *) this;
  35   }
  36 
  37   int argument_count() const {
  38     return cif()->nargs;
  39   }
  40 
  41   ffi_type** argument_types() const {
  42     return (ffi_type**) (cif() + 1);
  43   }
  44 
  45   ffi_type* argument_type(int i) const {
  46     return argument_types()[i];
  47   }
  48 
  49   ffi_type* result_type() const {
  50     return *(argument_types() + argument_count());
  51   }
  52 
  53  protected:
  54   friend class InterpreterRuntime;
  55   friend class SignatureHandlerLibrary;
  56 
  57   void finalize();
  58 };
  59 
  60 class SignatureHandlerGeneratorBase : public NativeSignatureIterator {
  61  private:
  62   ffi_cif* _cif;
  63 
  64  protected:
  65   SignatureHandlerGeneratorBase(methodHandle method, ffi_cif *cif)
  66     : NativeSignatureIterator(method), _cif(cif) {
  67     _cif->nargs = 0;
  68   }
  69 
  70   ffi_cif *cif() const {
  71     return _cif;
  72   }
  73 
  74  public:
  75   void generate(uint64_t fingerprint);
  76 
  77  private:
  78   void pass_int();
  79   void pass_long();
  80   void pass_float();
  81   void pass_double();
  82   void pass_object();
  83 
  84  private:
  85   void push(BasicType type);
  86   virtual void push(intptr_t value) = 0;
  87 };
  88 
  89 class SignatureHandlerGenerator : public SignatureHandlerGeneratorBase {
  90  private:
  91   CodeBuffer* _cb;
  92 
  93  public:
  94   SignatureHandlerGenerator(methodHandle method, CodeBuffer* buffer)
  95     : SignatureHandlerGeneratorBase(method, (ffi_cif *) buffer->code_end()),
  96       _cb(buffer) {
  97     _cb->set_code_end((address) (cif() + 1));
  98   }
  99 
 100  private:
 101   void push(intptr_t value) {
 102     intptr_t *dst = (intptr_t *) _cb->code_end();
 103     _cb->set_code_end((address) (dst + 1));
 104     *dst = value;
 105   }
 106 };
 107 
 108 class SlowSignatureHandlerGenerator : public SignatureHandlerGeneratorBase {
 109  private:
 110   intptr_t *_dst;
 111 
 112  public:
 113   SlowSignatureHandlerGenerator(methodHandle method, intptr_t* buf)
 114     : SignatureHandlerGeneratorBase(method, (ffi_cif *) buf) {
 115     _dst = (intptr_t *) (cif() + 1);
 116   }
 117 
 118  private:
 119   void push(intptr_t value) {
 120     *(_dst++) = value;
 121   }
 122 
 123  public:
 124   SignatureHandler *handler() const {
 125     return (SignatureHandler *) cif();
 126   }
 127 };