spirv_parser.hpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. * Copyright 2018-2021 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. * At your option, you may choose to accept this material under either:
  18. * 1. The Apache License, Version 2.0, found at <http://www.apache.org/licenses/LICENSE-2.0>, or
  19. * 2. The MIT License, found at <http://opensource.org/licenses/MIT>.
  20. * SPDX-License-Identifier: Apache-2.0 OR MIT.
  21. */
  22. #ifndef SPIRV_CROSS_PARSER_HPP
  23. #define SPIRV_CROSS_PARSER_HPP
  24. #include "spirv_cross_parsed_ir.hpp"
  25. #include <stdint.h>
  26. namespace SPIRV_CROSS_NAMESPACE
  27. {
  28. class Parser
  29. {
  30. public:
  31. Parser(const uint32_t *spirv_data, size_t word_count);
  32. Parser(std::vector<uint32_t> spirv);
  33. void parse();
  34. ParsedIR &get_parsed_ir()
  35. {
  36. return ir;
  37. }
  38. private:
  39. ParsedIR ir;
  40. SPIRFunction *current_function = nullptr;
  41. SPIRBlock *current_block = nullptr;
  42. void parse(const Instruction &instr);
  43. const uint32_t *stream(const Instruction &instr) const;
  44. template <typename T, typename... P>
  45. T &set(uint32_t id, P &&... args)
  46. {
  47. ir.add_typed_id(static_cast<Types>(T::type), id);
  48. auto &var = variant_set<T>(ir.ids[id], std::forward<P>(args)...);
  49. var.self = id;
  50. return var;
  51. }
  52. template <typename T>
  53. T &get(uint32_t id)
  54. {
  55. return variant_get<T>(ir.ids[id]);
  56. }
  57. template <typename T>
  58. T *maybe_get(uint32_t id)
  59. {
  60. if (ir.ids[id].get_type() == static_cast<Types>(T::type))
  61. return &get<T>(id);
  62. else
  63. return nullptr;
  64. }
  65. template <typename T>
  66. const T &get(uint32_t id) const
  67. {
  68. return variant_get<T>(ir.ids[id]);
  69. }
  70. template <typename T>
  71. const T *maybe_get(uint32_t id) const
  72. {
  73. if (ir.ids[id].get_type() == T::type)
  74. return &get<T>(id);
  75. else
  76. return nullptr;
  77. }
  78. // This must be an ordered data structure so we always pick the same type aliases.
  79. SmallVector<uint32_t> global_struct_cache;
  80. SmallVector<std::pair<uint32_t, uint32_t>> forward_pointer_fixups;
  81. bool types_are_logically_equivalent(const SPIRType &a, const SPIRType &b) const;
  82. bool variable_storage_is_aliased(const SPIRVariable &v) const;
  83. };
  84. } // namespace SPIRV_CROSS_NAMESPACE
  85. #endif