assembly_grammar.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // Copyright (c) 2015-2016 The Khronos Group Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #ifndef SOURCE_ASSEMBLY_GRAMMAR_H_
  15. #define SOURCE_ASSEMBLY_GRAMMAR_H_
  16. #include "source/enum_set.h"
  17. #include "source/latest_version_spirv_header.h"
  18. #include "source/operand.h"
  19. #include "source/table.h"
  20. #include "source/util/span.h"
  21. #include "spirv-tools/libspirv.h"
  22. namespace spvtools {
  23. // Encapsulates the grammar to use for SPIR-V assembly.
  24. // Contains methods to query for valid instructions and operands.
  25. class AssemblyGrammar {
  26. public:
  27. explicit AssemblyGrammar(const spv_const_context context)
  28. : target_env_(context->target_env) {}
  29. // Returns the SPIR-V target environment.
  30. spv_target_env target_env() const { return target_env_; }
  31. // Removes capabilities not available in the current target environment and
  32. // returns the rest.
  33. // TODO(crbug.com/266223071) Remove this.
  34. CapabilitySet filterCapsAgainstTargetEnv(const spv::Capability* cap_array,
  35. uint32_t count) const;
  36. // Removes capabilities not available in the current target environment and
  37. // returns the rest.
  38. CapabilitySet filterCapsAgainstTargetEnv(
  39. const spvtools::utils::Span<const spv::Capability>& caps) const {
  40. return filterCapsAgainstTargetEnv(caps.begin(),
  41. static_cast<uint32_t>(caps.size()));
  42. }
  43. // Finds operand entry in the grammar table and returns its name.
  44. // Returns "Unknown" if not found.
  45. const char* lookupOperandName(spv_operand_type_t type,
  46. uint32_t operand) const;
  47. // Finds the opcode for the given OpSpecConstantOp opcode name. The name
  48. // should not have the "Op" prefix. For example, "IAdd" corresponds to
  49. // the integer add opcode for OpSpecConstantOp. On success, returns
  50. // SPV_SUCCESS and sends the discovered operation code through the opcode
  51. // parameter. On failure, returns SPV_ERROR_INVALID_LOOKUP.
  52. spv_result_t lookupSpecConstantOpcode(const char* name,
  53. spv::Op* opcode) const;
  54. // Returns SPV_SUCCESS if the given opcode is valid as the opcode operand
  55. // to OpSpecConstantOp.
  56. spv_result_t lookupSpecConstantOpcode(spv::Op opcode) const;
  57. // Parses a mask expression string for the given operand type.
  58. //
  59. // A mask expression is a sequence of one or more terms separated by '|',
  60. // where each term is a named enum value for a given type. No whitespace
  61. // is permitted.
  62. //
  63. // On success, the value is written to pValue, and SPV_SUCCESS is returned.
  64. // The operand type is defined by the type parameter, and the text to be
  65. // parsed is defined by the textValue parameter.
  66. spv_result_t parseMaskOperand(const spv_operand_type_t type,
  67. const char* textValue, uint32_t* pValue) const;
  68. // Inserts the operands expected after the given typed mask onto the end
  69. // of the given pattern.
  70. //
  71. // Each set bit in the mask represents zero or more operand types that
  72. // should be appended onto the pattern. Operands for a less significant
  73. // bit must always match before operands for a more significant bit, so
  74. // the operands for a less significant bit must appear closer to the end
  75. // of the pattern stack.
  76. //
  77. // If a set bit is unknown, then we assume it has no operands.
  78. void pushOperandTypesForMask(const spv_operand_type_t type,
  79. const uint32_t mask,
  80. spv_operand_pattern_t* pattern) const;
  81. private:
  82. const spv_target_env target_env_;
  83. };
  84. } // namespace spvtools
  85. #endif // SOURCE_ASSEMBLY_GRAMMAR_H_