1 /*
  2  * Copyright (c) 1998, 2019, Oracle and/or its affiliates. All rights reserved.
  3  * Copyright (c) 2014, Red Hat Inc. All rights reserved.
  4  * Copyright (c) 2020, 2021, Huawei Technologies Co., Ltd. All rights reserved.
  5  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  6  *
  7  * This code is free software; you can redistribute it and/or modify it
  8  * under the terms of the GNU General Public License version 2 only, as
  9  * published by the Free Software Foundation.
 10  *
 11  * This code is distributed in the hope that it will be useful, but WITHOUT
 12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 14  * version 2 for more details (a copy is included in the LICENSE file that
 15  * accompanied this code).
 16  *
 17  * You should have received a copy of the GNU General Public License version
 18  * 2 along with this work; if not, write to the Free Software Foundation,
 19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 20  *
 21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 22  * or visit www.oracle.com if you need additional information or have any
 23  * questions.
 24  *
 25  */
 26 
 27 #ifndef CPU_RISCV_JNITYPES_RISCV_HPP
 28 #define CPU_RISCV_JNITYPES_RISCV_HPP
 29 
 30 #include "jni.h"
 31 #include "memory/allocation.hpp"
 32 #include "oops/oop.hpp"
 33 
 34 // This file holds platform-dependent routines used to write primitive jni
 35 // types to the array of arguments passed into JavaCalls::call
 36 
 37 class JNITypes : private AllStatic {
 38   // These functions write a java primitive type (in native format)
 39   // to a java stack slot array to be passed as an argument to JavaCalls:calls.
 40   // I.e., they are functionally 'push' operations if they have a 'pos'
 41   // formal parameter.  Note that jlong's and jdouble's are written
 42   // _in reverse_ of the order in which they appear in the interpreter
 43   // stack.  This is because call stubs (see stubGenerator_sparc.cpp)
 44   // reverse the argument list constructed by JavaCallArguments (see
 45   // javaCalls.hpp).
 46 
 47 public:
 48   // Ints are stored in native format in one JavaCallArgument slot at *to.
 49   static inline void    put_int(jint  from, intptr_t *to)           { *(jint *)(to +   0  ) =  from; }
 50   static inline void    put_int(jint  from, intptr_t *to, int& pos) { *(jint *)(to + pos++) =  from; }
 51   static inline void    put_int(jint *from, intptr_t *to, int& pos) { *(jint *)(to + pos++) = *from; }
 52 
 53   // Longs are stored in native format in one JavaCallArgument slot at
 54   // *(to+1).
 55   static inline void put_long(jlong  from, intptr_t *to) {
 56     *(jlong*) (to + 1) = from;
 57   }
 58 
 59   static inline void put_long(jlong  from, intptr_t *to, int& pos) {
 60     *(jlong*) (to + 1 + pos) = from;
 61     pos += 2;
 62   }
 63 
 64   static inline void put_long(jlong *from, intptr_t *to, int& pos) {
 65     *(jlong*) (to + 1 + pos) = *from;
 66     pos += 2;
 67   }
 68 
 69   // Oops are stored in native format in one JavaCallArgument slot at *to.
 70   static inline void    put_obj(const Handle& from_handle, intptr_t *to, int& pos) { *(to + pos++) = (intptr_t)from_handle.raw_value(); }
 71   static inline void    put_obj(jobject       from_handle, intptr_t *to, int& pos) { *(to + pos++) = (intptr_t)from_handle; }
 72 
 73   // Floats are stored in native format in one JavaCallArgument slot at *to.
 74   static inline void    put_float(jfloat  from, intptr_t *to)           { *(jfloat *)(to +   0  ) =  from;  }
 75   static inline void    put_float(jfloat  from, intptr_t *to, int& pos) { *(jfloat *)(to + pos++) =  from; }
 76   static inline void    put_float(jfloat *from, intptr_t *to, int& pos) { *(jfloat *)(to + pos++) = *from; }
 77 
 78 #undef _JNI_SLOT_OFFSET
 79 #define _JNI_SLOT_OFFSET 1
 80   // Doubles are stored in native word format in one JavaCallArgument
 81   // slot at *(to+1).
 82   static inline void put_double(jdouble  from, intptr_t *to) {
 83     *(jdouble*) (to + 1) = from;
 84   }
 85 
 86   static inline void put_double(jdouble  from, intptr_t *to, int& pos) {
 87     *(jdouble*) (to + 1 + pos) = from;
 88     pos += 2;
 89   }
 90 
 91   static inline void put_double(jdouble *from, intptr_t *to, int& pos) {
 92     *(jdouble*) (to + 1 + pos) = *from;
 93     pos += 2;
 94   }
 95 
 96   // The get_xxx routines, on the other hand, actually _do_ fetch
 97   // java primitive types from the interpreter stack.
 98   // No need to worry about alignment on Intel.
 99   static inline jint    get_int   (intptr_t *from) { return *(jint *)   from; }
100   static inline jlong   get_long  (intptr_t *from) { return *(jlong *)  (from + _JNI_SLOT_OFFSET); }
101   static inline oop     get_obj   (intptr_t *from) { return *(oop *)    from; }
102   static inline jfloat  get_float (intptr_t *from) { return *(jfloat *) from; }
103   static inline jdouble get_double(intptr_t *from) { return *(jdouble *)(from + _JNI_SLOT_OFFSET); }
104 #undef _JNI_SLOT_OFFSET
105 };
106 
107 #endif // CPU_RISCV_JNITYPES_RISCV_HPP