| 1 |
/* |
| 2 |
* Copyright 2018-2020 Arm Limited |
| 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 |
|
| 23 |
namespace SPIRV_CROSS_NAMESPACE |
| 24 |
{ |
| 25 |
class Parser |
| 26 |
{ |
| 27 |
public: |
| 28 |
Parser(const uint32_t *spirv_data, size_t word_count); |
| 29 |
Parser(std::vector<uint32_t> spirv); |
| 30 |
|
| 31 |
void parse(); |
| 32 |
|
| 33 |
ParsedIR &get_parsed_ir() |
| 34 |
{ |
| 35 |
return ir; |
| 36 |
} |
| 37 |
|
| 38 |
private: |
| 39 |
ParsedIR ir; |
| 40 |
SPIRFunction *current_function = nullptr; |
| 41 |
SPIRBlock *current_block = nullptr; |
| 42 |
|
| 43 |
void parse(const Instruction &instr); |
| 44 |
const uint32_t *stream(const Instruction &instr) const; |
| 45 |
|
| 46 |
template <typename T, typename... P> |
| 47 |
T &set(uint32_t id, P &&... args) |
| 48 |
{ |
| 49 |
ir.add_typed_id(static_cast<Types>(T::type), id); |
| 50 |
auto &var = variant_set<T>(ir.ids[id], std::forward<P>(args)...); |
| 51 |
var.self = id; |
| 52 |
return var; |
| 53 |
} |
| 54 |
|
| 55 |
template <typename T> |
| 56 |
T &get(uint32_t id) |
| 57 |
{ |
| 58 |
return variant_get<T>(ir.ids[id]); |
| 59 |
} |
| 60 |
|
| 61 |
template <typename T> |
| 62 |
T *maybe_get(uint32_t id) |
| 63 |
{ |
| 64 |
if (ir.ids[id].get_type() == static_cast<Types>(T::type)) |
| 65 |
return &get<T>(id); |
| 66 |
else |
| 67 |
return nullptr; |
| 68 |
} |
| 69 |
|
| 70 |
template <typename T> |
| 71 |
const T &get(uint32_t id) const |
| 72 |
{ |
| 73 |
return variant_get<T>(ir.ids[id]); |
| 74 |
} |
| 75 |
|
| 76 |
template <typename T> |
| 77 |
const T *maybe_get(uint32_t id) const |
| 78 |
{ |
| 79 |
if (ir.ids[id].get_type() == T::type) |
| 80 |
return &get<T>(id); |
| 81 |
else |
| 82 |
return nullptr; |
| 83 |
} |
| 84 |
|
| 85 |
// This must be an ordered data structure so we always pick the same type aliases. |
| 86 |
SmallVector<uint32_t> global_struct_cache; |
| 87 |
SmallVector<std::pair<uint32_t, uint32_t>> forward_pointer_fixups; |
| 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 |
}; |
| 92 |
} // namespace SPIRV_CROSS_NAMESPACE |
| 93 |
|
| 94 |
#endif |