1 | /* |
2 | * Copyright 2018-2021 Arm Limited |
3 | * SPDX-License-Identifier: Apache-2.0 OR MIT |
4 | * |
5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
6 | * you may not use this file except in compliance with the License. |
7 | * You may obtain a copy of the License at |
8 | * |
9 | * http://www.apache.org/licenses/LICENSE-2.0 |
10 | * |
11 | * Unless required by applicable law or agreed to in writing, software |
12 | * distributed under the License is distributed on an "AS IS" BASIS, |
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
14 | * See the License for the specific language governing permissions and |
15 | * limitations under the License. |
16 | */ |
17 | |
18 | /* |
19 | * At your option, you may choose to accept this material under either: |
20 | * 1. The Apache License, Version 2.0, found at <http://www.apache.org/licenses/LICENSE-2.0>, or |
21 | * 2. The MIT License, found at <http://opensource.org/licenses/MIT>. |
22 | */ |
23 | |
24 | #ifndef SPIRV_CROSS_PARSER_HPP |
25 | #define SPIRV_CROSS_PARSER_HPP |
26 | |
27 | #include "spirv_cross_parsed_ir.hpp" |
28 | #include <stdint.h> |
29 | |
30 | namespace SPIRV_CROSS_NAMESPACE |
31 | { |
32 | class Parser |
33 | { |
34 | public: |
35 | Parser(const uint32_t *spirv_data, size_t word_count); |
36 | Parser(std::vector<uint32_t> spirv); |
37 | |
38 | void parse(); |
39 | |
40 | ParsedIR &get_parsed_ir() |
41 | { |
42 | return ir; |
43 | } |
44 | |
45 | private: |
46 | ParsedIR ir; |
47 | SPIRFunction *current_function = nullptr; |
48 | SPIRBlock *current_block = nullptr; |
49 | // For workarounds. |
50 | bool ignore_trailing_block_opcodes = false; |
51 | |
52 | void parse(const Instruction &instr); |
53 | const uint32_t *stream(const Instruction &instr) const; |
54 | |
55 | template <typename T, typename... P> |
56 | T &set(uint32_t id, P &&... args) |
57 | { |
58 | ir.add_typed_id(type: static_cast<Types>(T::type), id); |
59 | auto &var = variant_set<T>(ir.ids[id], std::forward<P>(args)...); |
60 | var.self = id; |
61 | return var; |
62 | } |
63 | |
64 | template <typename T> |
65 | T &get(uint32_t id) |
66 | { |
67 | return variant_get<T>(ir.ids[id]); |
68 | } |
69 | |
70 | template <typename T> |
71 | T *maybe_get(uint32_t id) |
72 | { |
73 | if (ir.ids[id].get_type() == static_cast<Types>(T::type)) |
74 | return &get<T>(id); |
75 | else |
76 | return nullptr; |
77 | } |
78 | |
79 | template <typename T> |
80 | const T &get(uint32_t id) const |
81 | { |
82 | return variant_get<T>(ir.ids[id]); |
83 | } |
84 | |
85 | template <typename T> |
86 | const T *maybe_get(uint32_t id) const |
87 | { |
88 | if (ir.ids[id].get_type() == T::type) |
89 | return &get<T>(id); |
90 | else |
91 | return nullptr; |
92 | } |
93 | |
94 | // This must be an ordered data structure so we always pick the same type aliases. |
95 | SmallVector<uint32_t> global_struct_cache; |
96 | SmallVector<std::pair<uint32_t, uint32_t>> forward_pointer_fixups; |
97 | |
98 | bool types_are_logically_equivalent(const SPIRType &a, const SPIRType &b) const; |
99 | bool variable_storage_is_aliased(const SPIRVariable &v) const; |
100 | }; |
101 | } // namespace SPIRV_CROSS_NAMESPACE |
102 | |
103 | #endif |
104 | |