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> 29 #include <type_traits> 38 template <
size_t Bits>
struct int_t;
39 template <>
struct int_t<8> {
using type = int8_t; };
40 template <>
struct int_t<16> {
using type = int16_t; };
41 template <>
struct int_t<32> {
using type = int32_t; };
42 template <>
struct int_t<64> {
using type = int64_t; };
57 static_assert(!std::is_reference<T>::value,
58 "Reference types/arguments are not supported for automatic " 59 "LLVM Type conversion. Use pointers instead.");
60 static_assert(!std::is_class<T>::value,
61 "Object types/arguments are not supported for automatic " 62 "LLVM Type conversion.");
66 static inline llvm::Type*
67 get(llvm::LLVMContext& C)
71 if (std::is_same<T, bool>::value) {
72 return llvm::Type::getInt1Ty(C);
75 #if LLVM_VERSION_MAJOR > 6 76 return llvm::Type::getScalarTy<T>(C);
78 int bits =
sizeof(T) * CHAR_BIT;
79 if (std::is_integral<T>::value) {
80 return llvm::Type::getIntNTy(C, bits);
82 else if (std::is_floating_point<T>::value) {
84 case 16:
return llvm::Type::getHalfTy(C);
85 case 32:
return llvm::Type::getFloatTy(C);
86 case 64:
return llvm::Type::getDoubleTy(C);
90 std::string(typeNameAsString<T>()) +
"\".");
99 static inline llvm::Constant*
100 get(llvm::LLVMContext& C,
const T V)
103 llvm::Constant* constant =
nullptr;
105 if (std::is_floating_point<T>::value) {
107 llvm::APFloat(
static_cast<typename std::conditional
108 <std::is_floating_point<T>::value, T,
double>::type>(V))));
109 constant = llvm::ConstantFP::get(type, static_cast<double>(V));
111 else if (std::is_integral<T>::value) {
112 const constexpr
bool isSigned = std::is_signed<T>::value;
113 OPENVDB_ASSERT((isSigned && llvm::ConstantInt::isValueValidForType(type, static_cast<int64_t>(V))) ||
114 (!isSigned && llvm::ConstantInt::isValueValidForType(type, static_cast<uint64_t>(V))));
115 constant = llvm::ConstantInt::get(type, static_cast<uint64_t>(V), isSigned);
126 static inline llvm::Constant*
127 get(llvm::LLVMContext& C,
const T*
const V)
130 reinterpret_cast<uintptr_t>(V));
134 template <
typename T,
size_t S>
137 static_assert(S != 0,
138 "Zero size array types are not supported for automatic LLVM " 141 static inline llvm::Type*
142 get(llvm::LLVMContext& C) {
145 static inline llvm::Constant*
146 get(llvm::LLVMContext& C,
const T(&array)[S]) {
147 return llvm::ConstantDataArray::get(C, array);
149 static inline llvm::Constant*
150 get(llvm::LLVMContext& C,
const T(*array)[S])
153 reinterpret_cast<uintptr_t>(array));
157 template <
typename T>
160 static inline llvm::PointerType*
161 get(llvm::LLVMContext& C) {
169 static_assert(std::is_same<uint8_t, unsigned char>::value,
170 "This library requires std::uint8_t to be implemented as unsigned char.");
176 static inline llvm::StructType*
177 get(llvm::LLVMContext& C) {
178 const std::vector<llvm::Type*> types {
183 return llvm::StructType::get(C, types);
185 static inline llvm::Constant*
189 reinterpret_cast<uintptr_t>(
string));
196 static inline llvm::Type*
197 get(llvm::LLVMContext& C) {
198 return llvm::Type::getVoidTy(C);
209 static inline llvm::Type*
get(llvm::LLVMContext& C) {
return llvm::Type::getHalfTy(C); }
213 OPENVDB_ASSERT(llvm::ConstantFP::isValueValidForType(type, llvm::APFloat(V)));
214 llvm::Constant* constant = llvm::ConstantFP::get(type, static_cast<double>(V));
238 template <
typename T1,
typename T2>
243 static_assert(
sizeof(T1) ==
sizeof(T2),
244 "T1 differs in size to T2 during alias mapping. Types should have " 245 "the same memory layout.");
246 static_assert(std::is_standard_layout<T1>::value,
247 "T1 in instantiation of an AliasTypeMap does not have a standard layout. " 248 "This will most likely cause undefined behaviour when attempting to map " 251 static inline llvm::Type*
252 get(llvm::LLVMContext& C) {
253 return LLVMTypeT::get(C);
255 static inline llvm::Constant*
256 get(llvm::LLVMContext& C,
const T1& value) {
257 return LLVMTypeT::get(C, reinterpret_cast<const T2&>(value));
259 static inline llvm::Constant*
260 get(llvm::LLVMContext& C,
const T1*
const value) {
261 return LLVMTypeT::get(C, reinterpret_cast<const T2* const>(value));
279 template<
typename SignatureT>
282 template<
typename R,
typename... Args>
285 template<
typename R,
typename... Args>
291 #if __cplusplus >= 201703L 292 template<
typename R,
typename... Args>
295 template<
typename R,
typename... Args>
299 template<
typename ReturnT,
typename ...Args>
304 static const size_t N_ARGS =
sizeof...(Args);
310 static_assert(I < N_ARGS,
311 "Invalid index specified for function argument access");
312 using Type =
typename std::tuple_element<I, std::tuple<Args...>>::type;
313 static_assert(!std::is_reference<Type>::value,
314 "Reference types/arguments are not supported for automatic " 315 "LLVM Type conversion. Use pointers instead.");
327 template <
typename T>
328 inline llvm::Constant*
331 static_assert(std::is_floating_point<T>::value || std::is_integral<T>::value,
332 "T type for llvmConstant must be a floating point or integral type.");
334 if (type->isIntegerTy()) {
335 return llvm::ConstantInt::get(type, static_cast<uint64_t>(t),
true);
339 return llvm::ConstantFP::get(type, static_cast<double>(t));
376 #endif // OPENVDB_AX_CODEGEN_TYPES_HAS_BEEN_INCLUDED LLVM type mapping from pod types.
Definition: Types.h:55
#define OPENVDB_THROW(exception, message)
Definition: Exceptions.h:74
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 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 * llvmFloatType(const uint32_t size, llvm::LLVMContext &C)
Returns an llvm floating point Type given a requested size and context.
Alias mapping between two types, a frontend type T1 and a backend type T2. This class is the intended...
Definition: Types.h:239
internal::half half
Definition: Types.h:29
3x3 matrix class.
Definition: Mat3.h:28
llvm::Constant * llvmConstant(const T t, llvm::Type *type)
Returns an llvm Constant holding a scalar value.
Definition: Types.h:329
ReturnT ReturnType
Definition: Types.h:302
typename std::tuple_element< I, std::tuple< Args... >>::type Type
Definition: Types.h:312
4x4 -matrix class.
Definition: Mat3.h:22
int16_t type
Definition: Types.h:40
int32_t type
Definition: Types.h:41
#define OPENVDB_ASSERT(X)
Definition: Assert.h:41
CoreType
Definition: Tokens.h:31
OPENVDB_AX_API ast::tokens::CoreType tokenFromLLVMType(const llvm::Type *type)
Return a corresponding AX token which represents the given LLVM Type.
Definition: Exceptions.h:13
Templated function traits which provides compile-time index access to the types of the function signa...
Definition: Types.h:280
Various function and operator tokens used throughout the AST and code generation. ...
int64_t type
Definition: Types.h:42
ReturnType(Args...) SignatureType
Definition: Types.h:303
An extremely basic but native representation of a string class with SSO support. This exists to provi...
Definition: String.h:33
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h.in:121
int8_t type
Definition: Types.h:39
Provides the class definition for the equivalent IR representation and logic for strings in AX...
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h.in:218
Definition: Exceptions.h:38