blob: dabc0e22446bce815ee98b0eb28eff503d50415d [file] [log] [blame]
Hans-Kristian Arntzen5bcf02f2018-10-05 11:30:57 +02001/*
Hans-Kristian Arntzen47044822021-01-14 16:07:49 +01002 * Copyright 2018-2021 Arm Limited
Jon Leechf2a65542021-05-08 01:47:48 -07003 * SPDX-License-Identifier: Apache-2.0 OR MIT
Hans-Kristian Arntzen5bcf02f2018-10-05 11:30:57 +02004 *
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
Hans-Kristian Arntzencf1e9e02020-11-25 15:22:08 +010018/*
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>.
Hans-Kristian Arntzencf1e9e02020-11-25 15:22:08 +010022 */
23
Hans-Kristian Arntzen5bcf02f2018-10-05 11:30:57 +020024#ifndef SPIRV_CROSS_PARSER_HPP
25#define SPIRV_CROSS_PARSER_HPP
26
27#include "spirv_cross_parsed_ir.hpp"
28#include <stdint.h>
Hans-Kristian Arntzen5bcf02f2018-10-05 11:30:57 +020029
Hans-Kristian Arntzen9b92e682019-03-29 10:29:44 +010030namespace SPIRV_CROSS_NAMESPACE
Hans-Kristian Arntzen5bcf02f2018-10-05 11:30:57 +020031{
32class Parser
33{
34public:
35 Parser(const uint32_t *spirv_data, size_t word_count);
Hans-Kristian Arntzen3fe57d32019-04-09 12:46:23 +020036 Parser(std::vector<uint32_t> spirv);
Hans-Kristian Arntzen5bcf02f2018-10-05 11:30:57 +020037
38 void parse();
39
40 ParsedIR &get_parsed_ir()
41 {
42 return ir;
43 }
44
45private:
46 ParsedIR ir;
47 SPIRFunction *current_function = nullptr;
48 SPIRBlock *current_block = nullptr;
Hans-Kristian Arntzen4c345162022-09-05 12:31:22 +020049 // For workarounds.
50 bool ignore_trailing_block_opcodes = false;
Hans-Kristian Arntzen5bcf02f2018-10-05 11:30:57 +020051
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 {
Hans-Kristian Arntzend92de002019-01-10 09:49:33 +010058 ir.add_typed_id(static_cast<Types>(T::type), id);
Hans-Kristian Arntzenb6298782019-01-10 14:04:01 +010059 auto &var = variant_set<T>(ir.ids[id], std::forward<P>(args)...);
Hans-Kristian Arntzen5bcf02f2018-10-05 11:30:57 +020060 var.self = id;
61 return var;
62 }
63
64 template <typename T>
65 T &get(uint32_t id)
66 {
Hans-Kristian Arntzenb6298782019-01-10 14:04:01 +010067 return variant_get<T>(ir.ids[id]);
Hans-Kristian Arntzen5bcf02f2018-10-05 11:30:57 +020068 }
69
70 template <typename T>
71 T *maybe_get(uint32_t id)
72 {
Hans-Kristian Arntzenb6298782019-01-10 14:04:01 +010073 if (ir.ids[id].get_type() == static_cast<Types>(T::type))
Hans-Kristian Arntzen5bcf02f2018-10-05 11:30:57 +020074 return &get<T>(id);
75 else
76 return nullptr;
77 }
78
79 template <typename T>
80 const T &get(uint32_t id) const
81 {
Hans-Kristian Arntzenb6298782019-01-10 14:04:01 +010082 return variant_get<T>(ir.ids[id]);
Hans-Kristian Arntzen5bcf02f2018-10-05 11:30:57 +020083 }
84
85 template <typename T>
86 const T *maybe_get(uint32_t id) const
87 {
Hans-Kristian Arntzenb6298782019-01-10 14:04:01 +010088 if (ir.ids[id].get_type() == T::type)
Hans-Kristian Arntzen5bcf02f2018-10-05 11:30:57 +020089 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.
Hans-Kristian Arntzena489ba72019-04-02 11:19:03 +020095 SmallVector<uint32_t> global_struct_cache;
Hans-Kristian Arntzen58dad822020-05-25 11:05:42 +020096 SmallVector<std::pair<uint32_t, uint32_t>> forward_pointer_fixups;
Hans-Kristian Arntzen5bcf02f2018-10-05 11:30:57 +020097
98 bool types_are_logically_equivalent(const SPIRType &a, const SPIRType &b) const;
99 bool variable_storage_is_aliased(const SPIRVariable &v) const;
Hans-Kristian Arntzen5bcf02f2018-10-05 11:30:57 +0200100};
Hans-Kristian Arntzena489ba72019-04-02 11:19:03 +0200101} // namespace SPIRV_CROSS_NAMESPACE
Hans-Kristian Arntzen5bcf02f2018-10-05 11:30:57 +0200102
103#endif