11#ifndef OPENVDB_AX_CODEGEN_TYPES_HAS_BEEN_INCLUDED
12#define OPENVDB_AX_CODEGEN_TYPES_HAS_BEEN_INCLUDED
18#include <openvdb/version.h>
25#include <llvm/IR/Constants.h>
26#include <llvm/IR/IRBuilder.h>
27#include <llvm/IR/LLVMContext.h>
38template <
size_t Bits>
struct int_t;
39template <>
struct int_t<8> {
using type = int8_t; };
40template <>
struct int_t<16> {
using type = int16_t; };
41template <>
struct int_t<32> {
using type = int32_t; };
42template <>
struct int_t<64> {
using type = int64_t; };
69 static_assert(!std::is_reference<T>::value,
70 "Reference types/arguments are not supported for automatic "
71 "LLVM Type conversion. Use pointers instead.");
72 static_assert(!std::is_class<T>::value,
73 "Object types/arguments are not supported for automatic "
74 "LLVM Type conversion.");
80 static inline llvm::Type*
81 get(llvm::LLVMContext& C)
85 if (std::is_same<T, bool>::value) {
86 return llvm::Type::getInt1Ty(C);
89#if LLVM_VERSION_MAJOR > 6
90 return llvm::Type::getScalarTy<T>(C);
92 int bits =
sizeof(T) * CHAR_BIT;
93 if (std::is_integral<T>::value) {
94 return llvm::Type::getIntNTy(C, bits);
96 else if (std::is_floating_point<T>::value) {
98 case 16:
return llvm::Type::getHalfTy(C);
99 case 32:
return llvm::Type::getFloatTy(C);
100 case 64:
return llvm::Type::getDoubleTy(C);
113 static inline llvm::Constant*
114 get(llvm::LLVMContext& C,
const T V)
117 llvm::Constant* constant =
nullptr;
119 if constexpr (std::is_floating_point<T>::value)
122 llvm::APFloat(
static_cast<typename std::conditional
123 <std::is_floating_point<T>::value, T,
double>::type>(V))));
124 constant = llvm::ConstantFP::get(type,
static_cast<double>(V));
126 else if constexpr(std::is_integral<T>::value)
128 const constexpr bool isSigned = std::is_signed<T>::value;
129 OPENVDB_ASSERT((isSigned && llvm::ConstantInt::isValueValidForType(type,
static_cast<int64_t
>(V))) ||
130 (!isSigned && llvm::ConstantInt::isValueValidForType(type,
static_cast<uint64_t
>(V))));
131 constant = llvm::ConstantInt::get(type,
static_cast<uint64_t
>(V), isSigned);
142 static inline llvm::Constant*
143 get(llvm::LLVMContext& C,
const T*
const V)
146 reinterpret_cast<uintptr_t
>(V));
150template <
typename T,
size_t S>
155 static_assert(S != 0,
156 "Zero size array types are not supported for automatic LLVM "
159 static inline llvm::Type*
160 get(llvm::LLVMContext& C) {
163 static inline llvm::Constant*
164 get(llvm::LLVMContext& C,
const T(&array)[S]) {
165 return llvm::ConstantDataArray::get(C, array);
167 static inline llvm::Constant*
168 get(llvm::LLVMContext& C,
const T(*array)[S])
171 reinterpret_cast<uintptr_t
>(array));
180 static inline llvm::PointerType*
181 get(llvm::LLVMContext& C) {
191 static_assert(std::is_same<uint8_t, unsigned char>::value,
192 "This library requires std::uint8_t to be implemented as unsigned char.");
200 static inline llvm::StructType*
201 get(llvm::LLVMContext& C) {
202 const std::vector<llvm::Type*> types {
207 return llvm::StructType::get(C, types);
209 static inline llvm::Constant*
213 reinterpret_cast<uintptr_t
>(
string));
222 static inline llvm::Type*
223 get(llvm::LLVMContext& C) {
224 return llvm::Type::getVoidTy(C);
238 static inline llvm::Type*
get(llvm::LLVMContext& C) {
return llvm::Type::getHalfTy(C); }
239 static inline llvm::Constant*
get(llvm::LLVMContext& C,
const openvdb::math::half V)
242 OPENVDB_ASSERT(llvm::ConstantFP::isValueValidForType(type, llvm::APFloat(V)));
243 llvm::Constant* constant = llvm::ConstantFP::get(type,
static_cast<double>(V));
247 static inline llvm::Constant*
get(llvm::LLVMContext& C,
const openvdb::math::half*
const V)
267template <
typename T1,
typename T2>
272 static_assert(
sizeof(T1) ==
sizeof(T2),
273 "T1 differs in size to T2 during alias mapping. Types should have "
274 "the same memory layout.");
275 static_assert(std::is_standard_layout<T1>::value,
276 "T1 in instantiation of an AliasTypeMap does not have a standard layout. "
277 "This will most likely cause undefined behaviour when attempting to map "
282 static inline llvm::Type*
283 get(llvm::LLVMContext& C) {
286 static inline llvm::Constant*
287 get(llvm::LLVMContext& C,
const T1& value) {
290 static inline llvm::Constant*
291 get(llvm::LLVMContext& C,
const T1*
const value) {
292 return LLVMTypeT::get(C,
reinterpret_cast<const T2* const
>(value));
310template<
typename SignatureT>
313template<
typename R,
typename... Args>
316template<
typename R,
typename... Args>
322#if __cplusplus >= 201703L
323template<
typename R,
typename... Args>
326template<
typename R,
typename... Args>
327struct FunctionTraits<R(*)(Args...)
noexcept> :
public FunctionTraits<R(Args...)> {};
330template<
typename ReturnT,
typename ...Args>
335 static const size_t N_ARGS =
sizeof...(Args);
342 "Invalid index specified for function argument access");
343 using Type =
typename std::tuple_element<I, std::tuple<Args...>>::type;
344 static_assert(!std::is_reference<Type>::value,
345 "Reference types/arguments are not supported for automatic "
346 "LLVM Type conversion. Use pointers instead.");
359inline llvm::Constant*
362 static_assert(std::is_floating_point<T>::value || std::is_integral<T>::value,
363 "T type for llvmConstant must be a floating point or integral type.");
365 if (type->isIntegerTy()) {
366 return llvm::ConstantInt::get(type,
static_cast<uint64_t
>(t),
true);
370 return llvm::ConstantFP::get(type,
static_cast<double>(t));
#define OPENVDB_ASSERT(X)
Definition Assert.h:41
Provides the class definition for the equivalent IR representation and logic for strings in AX.
Various function and operator tokens used throughout the AST and code generation.
Definition Exceptions.h:38
3x3 matrix class.
Definition Mat3.h:29
4x4 -matrix class.
Definition Mat4.h:31
CoreType
Definition Tokens.h:32
OPENVDB_AX_API llvm::Type * llvmFloatType(const uint32_t size, llvm::LLVMContext &C)
Returns an llvm floating point Type given a requested size and context.
OPENVDB_AX_API llvm::IntegerType * llvmIntType(const uint32_t size, llvm::LLVMContext &C)
Returns an llvm IntegerType given a requested size and context.
OPENVDB_AX_API llvm::Type * llvmTypeFromToken(const ast::tokens::CoreType &type, llvm::LLVMContext &C)
Returns an llvm type representing a type defined by a string.
OPENVDB_AX_API ast::tokens::CoreType tokenFromLLVMType(const llvm::Type *type)
Return a corresponding AX token which represents the given LLVM Type.
llvm::Constant * llvmConstant(const T t, llvm::Type *type)
Returns an llvm Constant holding a scalar value.
Definition Types.h:360
internal::half half
Definition Types.h:29
const char * typeNameAsString()
Definition Types.h:587
Definition Exceptions.h:13
#define OPENVDB_THROW(exception, message)
Definition Exceptions.h:74
Alias mapping between two types, a frontend type T1 and a backend type T2. This class is the intended...
Definition Types.h:269
LLVMType< T2 > LLVMTypeT
Definition Types.h:270
static llvm::Constant * get(llvm::LLVMContext &C, const T1 *const value)
Definition Types.h:291
static llvm::Constant * get(llvm::LLVMContext &C, const T1 &value)
Definition Types.h:287
static const bool CXXUTypeIsNativeType
Definition Types.h:280
static llvm::Type * get(llvm::LLVMContext &C)
Definition Types.h:283
static constexpr uint8_t value
Definition Types.h:51
static constexpr uint8_t value
Definition Types.h:52
static constexpr uint8_t value
Definition Types.h:54
static constexpr uint8_t value
Definition Types.h:53
static constexpr uint8_t value
Definition Types.h:50
typename std::tuple_element< I, std::tuple< Args... > >::type Type
Definition Types.h:343
static const size_t N_ARGS
Definition Types.h:335
ReturnT ReturnType
Definition Types.h:333
ReturnType(Args...) SignatureType
Definition Types.h:334
Templated function traits which provides compile-time index access to the types of the function signa...
Definition Types.h:311
static llvm::PointerType * get(llvm::LLVMContext &C)
Definition Types.h:181
static const bool CXXUTypeIsNativeType
Definition Types.h:178
static llvm::Constant * get(llvm::LLVMContext &C, const T(&array)[S])
Definition Types.h:164
static llvm::Constant * get(llvm::LLVMContext &C, const T(*array)[S])
Definition Types.h:168
static const bool CXXUTypeIsNativeType
Definition Types.h:153
static llvm::Type * get(llvm::LLVMContext &C)
Definition Types.h:160
static const bool CXXUTypeIsNativeType
Definition Types.h:189
static llvm::StructType * get(llvm::LLVMContext &C)
Definition Types.h:201
static llvm::Constant * get(llvm::LLVMContext &C, const codegen::String *const string)
Definition Types.h:210
static const bool CXXUTypeIsNativeType
Definition Types.h:198
static llvm::Constant * get(llvm::LLVMContext &C, const openvdb::math::half V)
Definition Types.h:239
static llvm::Constant * get(llvm::LLVMContext &C, const openvdb::math::half *const V)
Definition Types.h:247
static const bool CXXUTypeIsNativeType
Definition Types.h:236
static llvm::Type * get(llvm::LLVMContext &C)
Definition Types.h:238
static const bool CXXUTypeIsNativeType
Definition Types.h:220
static llvm::Type * get(llvm::LLVMContext &C)
Definition Types.h:223
LLVM type mapping from pod types.
Definition Types.h:68
static llvm::Constant * get(llvm::LLVMContext &C, const T V)
Return an LLVM constant Value which represents T value.
Definition Types.h:114
static llvm::Constant * get(llvm::LLVMContext &C, const T *const V)
Return an LLVM constant which holds an uintptr_t, representing the current address of the given value...
Definition Types.h:143
static const bool CXXUTypeIsNativeType
Definition Types.h:76
static llvm::Type * get(llvm::LLVMContext &C)
Return an LLVM type which represents T.
Definition Types.h:81
typename RemoveAllPtrTypes< T >::Type Type
Definition Types.h:45
typename RemoveAllPtrTypes< T >::Type Type
Definition Types.h:46
typename RemoveAllPtrTypes< T >::Type Type
Definition Types.h:48
typename RemoveAllPtrTypes< T >::Type Type
Definition Types.h:47
T Type
Definition Types.h:44
An extremely basic but native representation of a string class with SSO support. This exists to provi...
Definition String.h:34
int16_t type
Definition Types.h:40
int32_t type
Definition Types.h:41
int64_t type
Definition Types.h:42
int8_t type
Definition Types.h:39
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition version.h.in:121
#define OPENVDB_USE_VERSION_NAMESPACE
Definition version.h.in:218