1 /*
   2  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. 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 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 #ifndef CPU_ZERO_VM_INTERPRETERRT_ZERO_HPP
  27 #define CPU_ZERO_VM_INTERPRETERRT_ZERO_HPP
  28 
  29 #include "memory/allocation.hpp"
  30 
  31 class SignatureHandler {
  32  public:
  33   static SignatureHandler *from_handlerAddr(address handlerAddr) {
  34     return (SignatureHandler *) handlerAddr;
  35   }
  36 
  37  public:
  38   ffi_cif* cif() const {
  39     return (ffi_cif *) this;
  40   }
  41 
  42   int argument_count() const {
  43     return cif()->nargs;
  44   }
  45 
  46   ffi_type** argument_types() const {
  47     return (ffi_type**) (cif() + 1);
  48   }
  49 
  50   ffi_type* argument_type(int i) const {
  51     return argument_types()[i];
  52   }
  53 
  54   ffi_type* result_type() const {
  55     return *(argument_types() + argument_count());
  56   }
  57 
  58  protected:
  59   friend class InterpreterRuntime;
  60   friend class SignatureHandlerLibrary;
  61 
  62   void finalize();
  63 };
  64 
  65 class SignatureHandlerGeneratorBase : public NativeSignatureIterator {
  66  private:
  67   ffi_cif* _cif;
  68 
  69  protected:
  70   SignatureHandlerGeneratorBase(methodHandle method, ffi_cif *cif)
  71     : NativeSignatureIterator(method), _cif(cif) {
  72     _cif->nargs = 0;
  73   }
  74 
  75   ffi_cif *cif() const {
  76     return _cif;
  77   }
  78 
  79  public:
  80   void generate(uint64_t fingerprint);
  81 
  82  private:
  83   void pass_int();
  84   void pass_long();
  85   void pass_float();
  86   void pass_double();
  87   void pass_object();
  88 
  89  private:
  90   void push(BasicType type);
  91   virtual void push(intptr_t value) = 0;
  92 };
  93 
  94 class SignatureHandlerGenerator : public SignatureHandlerGeneratorBase {
  95  private:
  96   CodeBuffer* _cb;
  97 
  98  public:
  99   SignatureHandlerGenerator(methodHandle method, CodeBuffer* buffer)
 100     : SignatureHandlerGeneratorBase(method, (ffi_cif *) buffer->code_end()),
 101       _cb(buffer) {
 102     _cb->set_code_end((address) (cif() + 1));
 103   }
 104 
 105  private:
 106   void push(intptr_t value) {
 107     intptr_t *dst = (intptr_t *) _cb->code_end();
 108     _cb->set_code_end((address) (dst + 1));
 109     *dst = value;
 110   }
 111 };
 112 
 113 class SlowSignatureHandlerGenerator : public SignatureHandlerGeneratorBase {
 114  private:
 115   intptr_t *_dst;
 116 
 117  public:
 118   SlowSignatureHandlerGenerator(methodHandle method, intptr_t* buf)
 119     : SignatureHandlerGeneratorBase(method, (ffi_cif *) buf) {
 120     _dst = (intptr_t *) (cif() + 1);
 121   }
 122 
 123  private:
 124   void push(intptr_t value) {
 125     *(_dst++) = value;
 126   }
 127 
 128  public:
 129   SignatureHandler *handler() const {
 130     return (SignatureHandler *) cif();
 131   }
 132 };
 133 
 134 #endif // CPU_ZERO_VM_INTERPRETERRT_ZERO_HPP