assembly_grammar.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 "spirv-tools/libspirv.h"
  21. namespace spvtools {
  22. // Encapsulates the grammar to use for SPIR-V assembly.
  23. // Contains methods to query for valid instructions and operands.
  24. class AssemblyGrammar {
  25. public:
  26. explicit AssemblyGrammar(const spv_const_context context)
  27. : target_env_(context->target_env),
  28. operandTable_(context->operand_table),
  29. opcodeTable_(context->opcode_table),
  30. extInstTable_(context->ext_inst_table) {}
  31. // Returns true if the internal tables have been initialized with valid data.
  32. bool isValid() const;
  33. // Returns the SPIR-V target environment.
  34. spv_target_env target_env() const { return target_env_; }
  35. // Removes capabilities not available in the current target environment and
  36. // returns the rest.
  37. CapabilitySet filterCapsAgainstTargetEnv(const SpvCapability* cap_array,
  38. uint32_t count) const;
  39. // Fills in the desc parameter with the information about the opcode
  40. // of the given name. Returns SPV_SUCCESS if the opcode was found, and
  41. // SPV_ERROR_INVALID_LOOKUP if the opcode does not exist.
  42. spv_result_t lookupOpcode(const char* name, spv_opcode_desc* desc) const;
  43. // Fills in the desc parameter with the information about the opcode
  44. // of the valid. Returns SPV_SUCCESS if the opcode was found, and
  45. // SPV_ERROR_INVALID_LOOKUP if the opcode does not exist.
  46. spv_result_t lookupOpcode(SpvOp opcode, spv_opcode_desc* desc) const;
  47. // Fills in the desc parameter with the information about the given
  48. // operand. Returns SPV_SUCCESS if the operand was found, and
  49. // SPV_ERROR_INVALID_LOOKUP otherwise.
  50. spv_result_t lookupOperand(spv_operand_type_t type, const char* name,
  51. size_t name_len, spv_operand_desc* desc) const;
  52. // Fills in the desc parameter with the information about the given
  53. // operand. Returns SPV_SUCCESS if the operand was found, and
  54. // SPV_ERROR_INVALID_LOOKUP otherwise.
  55. spv_result_t lookupOperand(spv_operand_type_t type, uint32_t operand,
  56. spv_operand_desc* desc) const;
  57. // Finds operand entry in the grammar table and returns its name.
  58. // Returns "Unknown" if not found.
  59. const char* lookupOperandName(spv_operand_type_t type,
  60. uint32_t operand) const {
  61. spv_operand_desc desc = nullptr;
  62. if (lookupOperand(type, operand, &desc) != SPV_SUCCESS || !desc) {
  63. return "Unknown";
  64. }
  65. return desc->name;
  66. }
  67. // Finds the opcode for the given OpSpecConstantOp opcode name. The name
  68. // should not have the "Op" prefix. For example, "IAdd" corresponds to
  69. // the integer add opcode for OpSpecConstantOp. On success, returns
  70. // SPV_SUCCESS and sends the discovered operation code through the opcode
  71. // parameter. On failure, returns SPV_ERROR_INVALID_LOOKUP.
  72. spv_result_t lookupSpecConstantOpcode(const char* name, SpvOp* opcode) const;
  73. // Returns SPV_SUCCESS if the given opcode is valid as the opcode operand
  74. // to OpSpecConstantOp.
  75. spv_result_t lookupSpecConstantOpcode(SpvOp opcode) const;
  76. // Parses a mask expression string for the given operand type.
  77. //
  78. // A mask expression is a sequence of one or more terms separated by '|',
  79. // where each term is a named enum value for a given type. No whitespace
  80. // is permitted.
  81. //
  82. // On success, the value is written to pValue, and SPV_SUCCESS is returned.
  83. // The operand type is defined by the type parameter, and the text to be
  84. // parsed is defined by the textValue parameter.
  85. spv_result_t parseMaskOperand(const spv_operand_type_t type,
  86. const char* textValue, uint32_t* pValue) const;
  87. // Writes the extended operand with the given type and text to the *extInst
  88. // parameter.
  89. // Returns SPV_SUCCESS if the value could be found.
  90. spv_result_t lookupExtInst(spv_ext_inst_type_t type, const char* textValue,
  91. spv_ext_inst_desc* extInst) const;
  92. // Writes the extended operand with the given type and first encoded word
  93. // to the *extInst parameter.
  94. // Returns SPV_SUCCESS if the value could be found.
  95. spv_result_t lookupExtInst(spv_ext_inst_type_t type, uint32_t firstWord,
  96. spv_ext_inst_desc* extInst) const;
  97. // Inserts the operands expected after the given typed mask onto the end
  98. // of the given pattern.
  99. //
  100. // Each set bit in the mask represents zero or more operand types that
  101. // should be appended onto the pattern. Operands for a less significant
  102. // bit must always match before operands for a more significant bit, so
  103. // the operands for a less significant bit must appear closer to the end
  104. // of the pattern stack.
  105. //
  106. // If a set bit is unknown, then we assume it has no operands.
  107. void pushOperandTypesForMask(const spv_operand_type_t type,
  108. const uint32_t mask,
  109. spv_operand_pattern_t* pattern) const;
  110. private:
  111. const spv_target_env target_env_;
  112. const spv_operand_table operandTable_;
  113. const spv_opcode_table opcodeTable_;
  114. const spv_ext_inst_table extInstTable_;
  115. };
  116. } // namespace spvtools
  117. #endif // SOURCE_ASSEMBLY_GRAMMAR_H_