spirv_parser.hpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. * At your option, you may choose to accept this material under either:
  19. * 1. The Apache License, Version 2.0, found at <http://www.apache.org/licenses/LICENSE-2.0>, or
  20. * 2. The MIT License, found at <http://opensource.org/licenses/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. // For workarounds.
  43. bool ignore_trailing_block_opcodes = false;
  44. void parse(const Instruction &instr);
  45. const uint32_t *stream(const Instruction &instr) const;
  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. template <typename T>
  55. T &get(uint32_t id)
  56. {
  57. return variant_get<T>(ir.ids[id]);
  58. }
  59. template <typename T>
  60. T *maybe_get(uint32_t id)
  61. {
  62. if (ir.ids[id].get_type() == static_cast<Types>(T::type))
  63. return &get<T>(id);
  64. else
  65. return nullptr;
  66. }
  67. template <typename T>
  68. const T &get(uint32_t id) const
  69. {
  70. return variant_get<T>(ir.ids[id]);
  71. }
  72. template <typename T>
  73. const T *maybe_get(uint32_t id) const
  74. {
  75. if (ir.ids[id].get_type() == T::type)
  76. return &get<T>(id);
  77. else
  78. return nullptr;
  79. }
  80. // This must be an ordered data structure so we always pick the same type aliases.
  81. SmallVector<uint32_t> global_struct_cache;
  82. SmallVector<std::pair<uint32_t, uint32_t>> forward_pointer_fixups;
  83. bool types_are_logically_equivalent(const SPIRType &a, const SPIRType &b) const;
  84. bool variable_storage_is_aliased(const SPIRVariable &v) const;
  85. };
  86. } // namespace SPIRV_CROSS_NAMESPACE
  87. #endif