Line | Branch | Exec | Source |
---|---|---|---|
1 | // Copyright Contributors to the OpenVDB Project | ||
2 | // SPDX-License-Identifier: MPL-2.0 | ||
3 | |||
4 | #ifndef OPENVDB_AX_CODEGEN_CODECS_HAS_BEEN_INCLUDED | ||
5 | #define OPENVDB_AX_CODEGEN_CODECS_HAS_BEEN_INCLUDED | ||
6 | |||
7 | #include <openvdb/openvdb.h> | ||
8 | #include <openvdb/version.h> | ||
9 | |||
10 | #include "openvdb_ax/ast/Tokens.h" | ||
11 | #include "openvdb_ax/codegen/FunctionTypes.h" | ||
12 | |||
13 | namespace openvdb { | ||
14 | OPENVDB_USE_VERSION_NAMESPACE | ||
15 | namespace OPENVDB_VERSION_NAME { | ||
16 | namespace ax { | ||
17 | namespace codegen { | ||
18 | |||
19 | class Codec; | ||
20 | |||
21 | using CodecNameMap = std::map<const std::string, const Codec*>; | ||
22 | using CodecTypeMap = std::map<const ast::tokens::CoreType, CodecNameMap>; | ||
23 | using Codecs = std::vector<const Codec*>; | ||
24 | |||
25 | /// @brief Get the global codec map | ||
26 | OPENVDB_AX_API const CodecTypeMap& getCodecTypeMap(); | ||
27 | |||
28 | /// @brief Get a specific codec. Returns a nullptr if no codec exists. | ||
29 | /// @param type The type the codec encodes | ||
30 | /// @param name The name of the codec | ||
31 | OPENVDB_AX_API const Codec* getCodec(const ast::tokens::CoreType type, const std::string& name); | ||
32 | |||
33 | /// @brief Get a specific set of codecs which encode a given type. Returns a | ||
34 | /// nullptr if no codec exists. | ||
35 | /// @param type The type the codecs encode | ||
36 | OPENVDB_AX_API const CodecNameMap* getTypeSupportedCodecs(const ast::tokens::CoreType type); | ||
37 | |||
38 | 5 | class Codec | |
39 | { | ||
40 | public: | ||
41 | using UniquePtr = std::unique_ptr<Codec>; | ||
42 | |||
43 | 5 | Codec(codegen::FunctionGroup::UniquePtr encoder, | |
44 | codegen::FunctionGroup::UniquePtr decoder, | ||
45 | uint32_t flag) | ||
46 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
|
5 | : mEncoder(std::move(encoder)) |
47 | , mDecoder(std::move(decoder)) | ||
48 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
|
5 | , mFlag(flag) { |
49 | #ifndef NDEBUG | ||
50 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
|
5 | assert(!mEncoder->list().empty()); |
51 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
|
5 | assert(!mDecoder->list().empty()); |
52 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
|
5 | assert(mEncoder->list().size() == mDecoder->list().size()); |
53 |
2/2✓ Branch 0 taken 16 times.
✓ Branch 1 taken 5 times.
|
21 | for (const auto& F : mEncoder->list()) { |
54 |
2/4✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 16 times.
|
16 | assert(F->size() == 1 || F->size() == 2); |
55 | } | ||
56 | #endif | ||
57 | 5 | } | |
58 | |||
59 | /// @brief Given a core type supported by the AX frontend, return a llvm | ||
60 | /// compatible type which represents how the core type is encoded in | ||
61 | /// memory. | ||
62 | /// @return A llvm type representing the encoded C type. Can be a nullptr | ||
63 | /// if this codec does not support the provided core type. | ||
64 | 8781 | llvm::Type* decodedToEncoded(const ast::tokens::CoreType& in, llvm::LLVMContext& C) const | |
65 | { | ||
66 | // the input "decoded" type - unlike the encoded type, the decoded type | ||
67 | // has to be available as an AX "CoreType" which is why this function | ||
68 | // takes a CoreType in | ||
69 | 8781 | llvm::Type* type = codegen::llvmTypeFromToken(in, C); | |
70 | // For each encoder function in this codec, find the one which | ||
71 | // one takes the provided "in" decoded type and return the type | ||
72 | // of that function return signature | ||
73 | 8781 | llvm::Type* ret = findReturnTypeFromArg(this->encoder(), type->getPointerTo()); | |
74 |
2/2✓ Branch 0 taken 8769 times.
✓ Branch 1 taken 12 times.
|
8781 | return ret ? ret->getPointerElementType() : nullptr; |
75 | } | ||
76 | |||
77 | /// @brief Given a llvm type, return a compatible llvm type which | ||
78 | /// represents how the provided type should be exposed to the AX frontend. | ||
79 | /// @note The return type is guaranteed to either be a supported CoreType | ||
80 | /// (such that ax::codegen::tokenFromLLVMType(in) returns a valid value) | ||
81 | /// or a nullptr. | ||
82 | /// @return A llvm type representing the decoded C type. Can be a nullptr | ||
83 | /// if this codec does not support the provided core type. | ||
84 |
1/2✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
|
10 | llvm::Type* encodedToDecoded(llvm::Type* in) const |
85 | { | ||
86 | // For each decoder function in this codec, find the one which | ||
87 | // one takes the provided "in" encoded type and return the type | ||
88 | // of that function return signature | ||
89 |
1/2✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
|
10 | if (!in->isPointerTy()) in = in->getPointerTo(); |
90 | 10 | llvm::Type* ret = findReturnTypeFromArg(this->decoder(), in); | |
91 |
1/2✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
|
10 | return ret ? ret->getPointerElementType() : nullptr; |
92 | } | ||
93 | |||
94 | const codegen::FunctionGroup* encoder() const { return mEncoder.get(); } | ||
95 | const codegen::FunctionGroup* decoder() const { return mDecoder.get(); } | ||
96 |
1/2✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
|
8778 | inline uint32_t flag() const { return mFlag; } |
97 | |||
98 | private: | ||
99 | llvm::Type* findReturnTypeFromArg(const codegen::FunctionGroup* const, llvm::Type*) const; | ||
100 | |||
101 | const codegen::FunctionGroup::UniquePtr mEncoder; | ||
102 | const codegen::FunctionGroup::UniquePtr mDecoder; | ||
103 | const uint32_t mFlag; | ||
104 | }; | ||
105 | |||
106 | |||
107 | } // namespace codegen | ||
108 | } // namespace ax | ||
109 | } // namespace OPENVDB_VERSION_NAME | ||
110 | } // namespace openvdb | ||
111 | |||
112 | #endif // OPENVDB_AX_CODEGEN_CODECS_HAS_BEEN_INCLUDED | ||
113 | |||
114 |