Go to the documentation of this file.
11 #ifndef OPENVDB_AX_CODEGEN_TYPES_HAS_BEEN_INCLUDED
12 #define OPENVDB_AX_CODEGEN_TYPES_HAS_BEEN_INCLUDED
14 #include "../ast/Tokens.h"
15 #include "../Exceptions.h"
16 #include "../compiler/CustomData.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 32:
return llvm::Type::getFloatTy(C);
84 case 64:
return llvm::Type::getDoubleTy(C);
88 std::string(typeNameAsString<T>()) +
"\".");
97 static inline llvm::Constant*
98 get(llvm::LLVMContext& C,
const T V)
101 llvm::Constant* constant =
nullptr;
103 if (std::is_floating_point<T>::value) {
104 assert(llvm::ConstantFP::isValueValidForType(type,
105 llvm::APFloat(
static_cast<typename std::conditional
106 <std::is_floating_point<T>::value, T,
double>::type>(V))));
107 constant = llvm::ConstantFP::get(type,
static_cast<double>(V));
109 else if (std::is_integral<T>::value) {
110 const constexpr
bool isSigned = std::is_signed<T>::value;
111 assert((isSigned && llvm::ConstantInt::isValueValidForType(type,
static_cast<int64_t
>(V))) ||
112 (!isSigned && llvm::ConstantInt::isValueValidForType(type,
static_cast<uint64_t
>(V))));
113 constant = llvm::ConstantInt::get(type,
static_cast<uint64_t
>(V), isSigned);
124 static inline llvm::Constant*
125 get(llvm::LLVMContext& C,
const T*
const V)
128 reinterpret_cast<uintptr_t
>(V));
132 template <
typename T,
size_t S>
135 static_assert(S != 0,
136 "Zero size array types are not supported for automatic LLVM "
139 static inline llvm::Type*
140 get(llvm::LLVMContext& C) {
143 static inline llvm::Constant*
144 get(llvm::LLVMContext& C,
const T(&array)[S]) {
145 return llvm::ConstantDataArray::get(C, array);
147 static inline llvm::Constant*
148 get(llvm::LLVMContext& C,
const T(*array)[S])
151 reinterpret_cast<uintptr_t
>(array));
155 template <
typename T>
158 static inline llvm::PointerType*
159 get(llvm::LLVMContext& C) {
167 static_assert(std::is_same<uint8_t, unsigned char>::value,
168 "This library requires std::uint8_t to be implemented as unsigned char.");
174 static inline llvm::StructType*
175 get(llvm::LLVMContext& C) {
176 const std::vector<llvm::Type*> types {
180 return llvm::StructType::get(C, types);
182 static inline llvm::Value*
183 get(llvm::LLVMContext& C, llvm::Constant*
string, llvm::Constant* size) {
191 static inline llvm::Value*
192 get(llvm::LLVMContext& C,
const std::string&
string, llvm::IRBuilder<>& builder) {
193 llvm::Constant* constant =
194 llvm::ConstantDataArray::getString(C,
string,
true);
195 llvm::Constant* size = llvm::cast<llvm::Constant>
198 llvm::Value* args[] = { zero, zero };
199 constant = llvm::cast<llvm::Constant>
200 (builder.CreateInBoundsGEP(constant->getType(), constant, args));
203 static inline llvm::Constant*
207 reinterpret_cast<uintptr_t
>(
string));
214 static inline llvm::Type*
215 get(llvm::LLVMContext& C) {
216 return llvm::Type::getVoidTy(C);
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>
287 template<
typename ReturnT,
typename ...Args>
292 static const size_t N_ARGS =
sizeof...(Args);
298 static_assert(I < N_ARGS,
299 "Invalid index specified for function argument access");
300 using Type =
typename std::tuple_element<I, std::tuple<Args...>>::type;
301 static_assert(!std::is_reference<Type>::value,
302 "Reference types/arguments are not supported for automatic "
303 "LLVM Type conversion. Use pointers instead.");
315 template <
typename T>
316 inline llvm::Constant*
319 static_assert(std::is_floating_point<T>::value || std::is_integral<T>::value,
320 "T type for llvmConstant must be a floating point or integral type.");
322 if (type->isIntegerTy()) {
323 return llvm::ConstantInt::get(type,
static_cast<uint64_t
>(t),
true);
326 assert(type->isFloatingPointTy());
327 return llvm::ConstantFP::get(type,
static_cast<double>(t));
335 llvm::IntegerType*
llvmIntType(
const uint32_t size, llvm::LLVMContext& C);
364 #endif // OPENVDB_AX_CODEGEN_TYPES_HAS_BEEN_INCLUDED
static llvm::Constant * get(llvm::LLVMContext &C, const AXString *const string)
Definition: ax/openvdb_ax/codegen/Types.h:204
typename std::tuple_element< I, std::tuple< Args... > >::type Type
Definition: ax/openvdb_ax/codegen/Types.h:300
Alias mapping between two types, a frontend type T1 and a backend type T2. This class is the intended...
Definition: ax/openvdb_ax/codegen/Types.h:239
static llvm::Constant * get(llvm::LLVMContext &C, const T1 &value)
Definition: ax/openvdb_ax/codegen/Types.h:255
Definition: ax/openvdb_ax/codegen/Types.h:37
ReturnT ReturnType
Definition: ax/openvdb_ax/codegen/Types.h:290
static llvm::PointerType * get(llvm::LLVMContext &C)
Definition: ax/openvdb_ax/codegen/Types.h:159
3x3 matrix class.
Definition: Mat3.h:29
int8_t type
Definition: ax/openvdb_ax/codegen/Types.h:38
CoreType
Definition: Tokens.h:32
std::allocator< char >::size_type SizeType
Definition: CustomData.h:33
static llvm::Type * get(llvm::LLVMContext &C)
Definition: ax/openvdb_ax/codegen/Types.h:215
Library and file format version numbers.
llvm::Constant * llvmConstant(const T t, llvm::Type *type)
Returns an llvm Constant holding a scalar value.
Definition: ax/openvdb_ax/codegen/Types.h:317
Definition: ax/openvdb_ax/codegen/Types.h:157
#define OPENVDB_THROW(exception, message)
Definition: openvdb/Exceptions.h:74
ast::tokens::CoreType tokenFromLLVMType(const llvm::Type *type)
Return a corresponding AX token which represents the given LLVM Type.
int16_t type
Definition: ax/openvdb_ax/codegen/Types.h:39
static llvm::Type * get(llvm::LLVMContext &C)
Return an LLVM type which represents T.
Definition: ax/openvdb_ax/codegen/Types.h:66
static llvm::Constant * get(llvm::LLVMContext &C, const T V)
Return an LLVM constant Value which represents T value.
Definition: ax/openvdb_ax/codegen/Types.h:98
LLVM type mapping from pod types.
Definition: ax/openvdb_ax/codegen/Types.h:55
static llvm::Type * get(llvm::LLVMContext &C)
Definition: ax/openvdb_ax/codegen/Types.h:140
Templated function traits which provides compile-time index access to the types of the function signa...
Definition: ax/openvdb_ax/codegen/Types.h:279
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:153
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: ax/openvdb_ax/codegen/Types.h:125
int32_t type
Definition: ax/openvdb_ax/codegen/Types.h:40
static llvm::Value * get(llvm::LLVMContext &C, llvm::Constant *string, llvm::Constant *size)
Definition: ax/openvdb_ax/codegen/Types.h:183
static llvm::Type * get(llvm::LLVMContext &C)
Definition: ax/openvdb_ax/codegen/Types.h:251
static llvm::Constant * get(llvm::LLVMContext &C, const T(&array)[S])
Definition: ax/openvdb_ax/codegen/Types.h:144
int64_t type
Definition: ax/openvdb_ax/codegen/Types.h:41
llvm::IntegerType * llvmIntType(const uint32_t size, llvm::LLVMContext &C)
Returns an llvm IntegerType given a requested size and context.
llvm::Type * llvmFloatType(const uint32_t size, llvm::LLVMContext &C)
Returns an llvm floating point Type given a requested size and context.
static llvm::StructType * get(llvm::LLVMContext &C)
Definition: ax/openvdb_ax/codegen/Types.h:175
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h:101
The backend representation of strings in AX. This is also how strings are passed from the AX code gen...
Definition: CustomData.h:31
static llvm::Constant * get(llvm::LLVMContext &C, const T1 *const value)
Definition: ax/openvdb_ax/codegen/Types.h:259
Definition: ax/openvdb_ax/Exceptions.h:36
static llvm::Value * get(llvm::LLVMContext &C, const std::string &string, llvm::IRBuilder<> &builder)
Definition: ax/openvdb_ax/codegen/Types.h:192
ReturnType(Args...) SignatureType
Definition: ax/openvdb_ax/codegen/Types.h:291
Definition: openvdb/Exceptions.h:13
llvm::Type * llvmTypeFromToken(const ast::tokens::CoreType &type, llvm::LLVMContext &C)
Returns an llvm type representing a type defined by a string.
static llvm::Constant * get(llvm::LLVMContext &C, const T(*array)[S])
Definition: ax/openvdb_ax/codegen/Types.h:148
4x4 -matrix class.
Definition: Mat4.h:24