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