11 #ifndef OPENVDB_AX_CODEGEN_TYPES_HAS_BEEN_INCLUDED 12 #define OPENVDB_AX_CODEGEN_TYPES_HAS_BEEN_INCLUDED 18 #include <openvdb/version.h> 24 #include <llvm/IR/Constants.h> 25 #include <llvm/IR/IRBuilder.h> 26 #include <llvm/IR/LLVMContext.h> 28 #include <type_traits> 37 template <
size_t Bits>
struct int_t;
38 template <>
struct int_t<8> {
using type = int8_t; };
39 template <>
struct int_t<16> {
using type = int16_t; };
40 template <>
struct int_t<32> {
using type = int32_t; };
41 template <>
struct int_t<64> {
using type = int64_t; };
56 static_assert(!std::is_reference<T>::value,
57 "Reference types/arguments are not supported for automatic " 58 "LLVM Type conversion. Use pointers instead.");
59 static_assert(!std::is_class<T>::value,
60 "Object types/arguments are not supported for automatic " 61 "LLVM Type conversion.");
65 static inline llvm::Type*
66 get(llvm::LLVMContext& C)
70 if (std::is_same<T, bool>::value) {
71 return llvm::Type::getInt1Ty(C);
74 #if LLVM_VERSION_MAJOR > 6 75 return llvm::Type::getScalarTy<T>(C);
77 int bits =
sizeof(T) * CHAR_BIT;
78 if (std::is_integral<T>::value) {
79 return llvm::Type::getIntNTy(C, bits);
81 else if (std::is_floating_point<T>::value) {
83 case 16:
return llvm::Type::getHalfTy(C);
84 case 32:
return llvm::Type::getFloatTy(C);
85 case 64:
return llvm::Type::getDoubleTy(C);
89 std::string(typeNameAsString<T>()) +
"\".");
98 static inline llvm::Constant*
99 get(llvm::LLVMContext& C,
const T V)
102 llvm::Constant* constant =
nullptr;
104 if (std::is_floating_point<T>::value) {
105 assert(llvm::ConstantFP::isValueValidForType(type,
106 llvm::APFloat(
static_cast<typename std::conditional
107 <std::is_floating_point<T>::value, T,
double>::type>(V))));
108 constant = llvm::ConstantFP::get(type, static_cast<double>(V));
110 else if (std::is_integral<T>::value) {
111 const constexpr
bool isSigned = std::is_signed<T>::value;
112 assert((isSigned && llvm::ConstantInt::isValueValidForType(type, static_cast<int64_t>(V))) ||
113 (!isSigned && llvm::ConstantInt::isValueValidForType(type, static_cast<uint64_t>(V))));
114 constant = llvm::ConstantInt::get(type, static_cast<uint64_t>(V), isSigned);
125 static inline llvm::Constant*
126 get(llvm::LLVMContext& C,
const T*
const V)
129 reinterpret_cast<uintptr_t>(V));
133 template <
typename T,
size_t S>
136 static_assert(S != 0,
137 "Zero size array types are not supported for automatic LLVM " 140 static inline llvm::Type*
141 get(llvm::LLVMContext& C) {
144 static inline llvm::Constant*
145 get(llvm::LLVMContext& C,
const T(&array)[S]) {
146 return llvm::ConstantDataArray::get(C, array);
148 static inline llvm::Constant*
149 get(llvm::LLVMContext& C,
const T(*array)[S])
152 reinterpret_cast<uintptr_t>(array));
156 template <
typename T>
159 static inline llvm::PointerType*
160 get(llvm::LLVMContext& C) {
168 static_assert(std::is_same<uint8_t, unsigned char>::value,
169 "This library requires std::uint8_t to be implemented as unsigned char.");
175 static inline llvm::StructType*
176 get(llvm::LLVMContext& C) {
177 const std::vector<llvm::Type*> types {
182 return llvm::StructType::get(C, types);
184 static inline llvm::Constant*
188 reinterpret_cast<uintptr_t>(
string));
195 static inline llvm::Type*
196 get(llvm::LLVMContext& C) {
197 return llvm::Type::getVoidTy(C);
208 static inline llvm::Type*
get(llvm::LLVMContext& C) {
return llvm::Type::getHalfTy(C); }
212 assert(llvm::ConstantFP::isValueValidForType(type, llvm::APFloat(V)));
213 llvm::Constant* constant = llvm::ConstantFP::get(type, static_cast<double>(V));
237 template <
typename T1,
typename T2>
242 static_assert(
sizeof(T1) ==
sizeof(T2),
243 "T1 differs in size to T2 during alias mapping. Types should have " 244 "the same memory layout.");
245 static_assert(std::is_standard_layout<T1>::value,
246 "T1 in instantiation of an AliasTypeMap does not have a standard layout. " 247 "This will most likely cause undefined behaviour when attempting to map " 250 static inline llvm::Type*
251 get(llvm::LLVMContext& C) {
252 return LLVMTypeT::get(C);
254 static inline llvm::Constant*
255 get(llvm::LLVMContext& C,
const T1& value) {
256 return LLVMTypeT::get(C, reinterpret_cast<const T2&>(value));
258 static inline llvm::Constant*
259 get(llvm::LLVMContext& C,
const T1*
const value) {
260 return LLVMTypeT::get(C, reinterpret_cast<const T2* const>(value));
278 template<
typename SignatureT>
281 template<
typename R,
typename... Args>
284 template<
typename R,
typename... Args>
290 #if __cplusplus >= 201703L 291 template<
typename R,
typename... Args>
294 template<
typename R,
typename... Args>
298 template<
typename ReturnT,
typename ...Args>
303 static const size_t N_ARGS =
sizeof...(Args);
309 static_assert(I < N_ARGS,
310 "Invalid index specified for function argument access");
311 using Type =
typename std::tuple_element<I, std::tuple<Args...>>::type;
312 static_assert(!std::is_reference<Type>::value,
313 "Reference types/arguments are not supported for automatic " 314 "LLVM Type conversion. Use pointers instead.");
326 template <
typename T>
327 inline llvm::Constant*
330 static_assert(std::is_floating_point<T>::value || std::is_integral<T>::value,
331 "T type for llvmConstant must be a floating point or integral type.");
333 if (type->isIntegerTy()) {
334 return llvm::ConstantInt::get(type, static_cast<uint64_t>(t),
true);
337 assert(type->isFloatingPointTy());
338 return llvm::ConstantFP::get(type, static_cast<double>(t));
375 #endif // OPENVDB_AX_CODEGEN_TYPES_HAS_BEEN_INCLUDED Definition: Exceptions.h:38
LLVM type mapping from pod types.
Definition: Types.h:54
#define OPENVDB_THROW(exception, message)
Definition: Exceptions.h:74
int8_t type
Definition: Types.h:38
typename std::tuple_element< I, std::tuple< Args... >>::type Type
Definition: Types.h:311
CoreType
Definition: Tokens.h:31
internal::half half
Definition: Types.h:29
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.
int16_t type
Definition: Types.h:39
ReturnType(Args...) SignatureType
Definition: Types.h:302
Definition: Exceptions.h:13
int32_t type
Definition: Types.h:40
An extremely basic but native representation of a string class with SSO support. This exists to provi...
Definition: String.h:33
Alias mapping between two types, a frontend type T1 and a backend type T2. This class is the intended...
Definition: Types.h:238
llvm::Constant * llvmConstant(const T t, llvm::Type *type)
Returns an llvm Constant holding a scalar value.
Definition: Types.h:328
3x3 matrix class.
Definition: Mat3.h:28
Various function and operator tokens used throughout the AST and code generation. ...
Templated function traits which provides compile-time index access to the types of the function signa...
Definition: Types.h:279
ReturnT ReturnType
Definition: Types.h:301
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.
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h.in:121
int64_t type
Definition: Types.h:41
OPENVDB_AX_API ast::tokens::CoreType tokenFromLLVMType(const llvm::Type *type)
Return a corresponding AX token which represents the given LLVM Type.
Provides the class definition for the equivalent IR representation and logic for strings in AX...
4x4 -matrix class.
Definition: Mat3.h:22
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h.in:212
OPENVDB_AX_API llvm::IntegerType * llvmIntType(const uint32_t size, llvm::LLVMContext &C)
Returns an llvm IntegerType given a requested size and context.