assembly_grammar.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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. #include "source/assembly_grammar.h"
  15. #include <algorithm>
  16. #include <cassert>
  17. #include <cstring>
  18. #include "source/ext_inst.h"
  19. #include "source/opcode.h"
  20. #include "source/operand.h"
  21. #include "source/spirv_target_env.h"
  22. #include "source/table.h"
  23. namespace spvtools {
  24. namespace {
  25. /// @brief Parses a mask expression string for the given operand type.
  26. ///
  27. /// A mask expression is a sequence of one or more terms separated by '|',
  28. /// where each term a named enum value for the given type. No whitespace
  29. /// is permitted.
  30. ///
  31. /// On success, the value is written to pValue.
  32. ///
  33. /// @param[in] operandTable operand lookup table
  34. /// @param[in] type of the operand
  35. /// @param[in] textValue word of text to be parsed
  36. /// @param[out] pValue where the resulting value is written
  37. ///
  38. /// @return result code
  39. spv_result_t spvTextParseMaskOperand(spv_target_env env,
  40. const spv_operand_table operandTable,
  41. const spv_operand_type_t type,
  42. const char* textValue, uint32_t* pValue) {
  43. if (textValue == nullptr) return SPV_ERROR_INVALID_TEXT;
  44. size_t text_length = strlen(textValue);
  45. if (text_length == 0) return SPV_ERROR_INVALID_TEXT;
  46. const char* text_end = textValue + text_length;
  47. // We only support mask expressions in ASCII, so the separator value is a
  48. // char.
  49. const char separator = '|';
  50. // Accumulate the result by interpreting one word at a time, scanning
  51. // from left to right.
  52. uint32_t value = 0;
  53. const char* begin = textValue; // The left end of the current word.
  54. const char* end = nullptr; // One character past the end of the current word.
  55. do {
  56. end = std::find(begin, text_end, separator);
  57. spv_operand_desc entry = nullptr;
  58. if (auto error = spvOperandTableNameLookup(env, operandTable, type, begin,
  59. end - begin, &entry)) {
  60. return error;
  61. }
  62. value |= entry->value;
  63. // Advance to the next word by skipping over the separator.
  64. begin = end + 1;
  65. } while (end != text_end);
  66. *pValue = value;
  67. return SPV_SUCCESS;
  68. }
  69. // Associates an opcode with its name.
  70. struct SpecConstantOpcodeEntry {
  71. spv::Op opcode;
  72. const char* name;
  73. };
  74. // All the opcodes allowed as the operation for OpSpecConstantOp.
  75. // The name does not have the usual "Op" prefix. For example opcode
  76. // spv::Op::IAdd is associated with the name "IAdd".
  77. //
  78. // clang-format off
  79. #define CASE(NAME) { spv::Op::Op##NAME, #NAME }
  80. const SpecConstantOpcodeEntry kOpSpecConstantOpcodes[] = {
  81. // Conversion
  82. CASE(SConvert),
  83. CASE(FConvert),
  84. CASE(ConvertFToS),
  85. CASE(ConvertSToF),
  86. CASE(ConvertFToU),
  87. CASE(ConvertUToF),
  88. CASE(UConvert),
  89. CASE(ConvertPtrToU),
  90. CASE(ConvertUToPtr),
  91. CASE(GenericCastToPtr),
  92. CASE(PtrCastToGeneric),
  93. CASE(Bitcast),
  94. CASE(QuantizeToF16),
  95. // Arithmetic
  96. CASE(SNegate),
  97. CASE(Not),
  98. CASE(IAdd),
  99. CASE(ISub),
  100. CASE(IMul),
  101. CASE(UDiv),
  102. CASE(SDiv),
  103. CASE(UMod),
  104. CASE(SRem),
  105. CASE(SMod),
  106. CASE(ShiftRightLogical),
  107. CASE(ShiftRightArithmetic),
  108. CASE(ShiftLeftLogical),
  109. CASE(BitwiseOr),
  110. CASE(BitwiseAnd),
  111. CASE(BitwiseXor),
  112. CASE(FNegate),
  113. CASE(FAdd),
  114. CASE(FSub),
  115. CASE(FMul),
  116. CASE(FDiv),
  117. CASE(FRem),
  118. CASE(FMod),
  119. // Composite
  120. CASE(VectorShuffle),
  121. CASE(CompositeExtract),
  122. CASE(CompositeInsert),
  123. // Logical
  124. CASE(LogicalOr),
  125. CASE(LogicalAnd),
  126. CASE(LogicalNot),
  127. CASE(LogicalEqual),
  128. CASE(LogicalNotEqual),
  129. CASE(Select),
  130. // Comparison
  131. CASE(IEqual),
  132. CASE(INotEqual),
  133. CASE(ULessThan),
  134. CASE(SLessThan),
  135. CASE(UGreaterThan),
  136. CASE(SGreaterThan),
  137. CASE(ULessThanEqual),
  138. CASE(SLessThanEqual),
  139. CASE(UGreaterThanEqual),
  140. CASE(SGreaterThanEqual),
  141. // Memory
  142. CASE(AccessChain),
  143. CASE(InBoundsAccessChain),
  144. CASE(PtrAccessChain),
  145. CASE(InBoundsPtrAccessChain),
  146. CASE(CooperativeMatrixLengthNV),
  147. CASE(CooperativeMatrixLengthKHR)
  148. };
  149. // The 60 is determined by counting the opcodes listed in the spec.
  150. static_assert(61 == sizeof(kOpSpecConstantOpcodes)/sizeof(kOpSpecConstantOpcodes[0]),
  151. "OpSpecConstantOp opcode table is incomplete");
  152. #undef CASE
  153. // clang-format on
  154. const size_t kNumOpSpecConstantOpcodes =
  155. sizeof(kOpSpecConstantOpcodes) / sizeof(kOpSpecConstantOpcodes[0]);
  156. } // namespace
  157. bool AssemblyGrammar::isValid() const {
  158. return operandTable_ && opcodeTable_ && extInstTable_;
  159. }
  160. CapabilitySet AssemblyGrammar::filterCapsAgainstTargetEnv(
  161. const spv::Capability* cap_array, uint32_t count) const {
  162. CapabilitySet cap_set;
  163. const auto version = spvVersionForTargetEnv(target_env_);
  164. for (uint32_t i = 0; i < count; ++i) {
  165. spv_operand_desc entry = {};
  166. if (SPV_SUCCESS == lookupOperand(SPV_OPERAND_TYPE_CAPABILITY,
  167. static_cast<uint32_t>(cap_array[i]),
  168. &entry)) {
  169. // This token is visible in this environment if it's in an appropriate
  170. // core version, or it is enabled by a capability or an extension.
  171. if ((version >= entry->minVersion && version <= entry->lastVersion) ||
  172. entry->numExtensions > 0u || entry->numCapabilities > 0u) {
  173. cap_set.insert(cap_array[i]);
  174. }
  175. }
  176. }
  177. return cap_set;
  178. }
  179. spv_result_t AssemblyGrammar::lookupOpcode(const char* name,
  180. spv_opcode_desc* desc) const {
  181. return spvOpcodeTableNameLookup(target_env_, opcodeTable_, name, desc);
  182. }
  183. spv_result_t AssemblyGrammar::lookupOpcode(spv::Op opcode,
  184. spv_opcode_desc* desc) const {
  185. return spvOpcodeTableValueLookup(target_env_, opcodeTable_, opcode, desc);
  186. }
  187. spv_result_t AssemblyGrammar::lookupOperand(spv_operand_type_t type,
  188. const char* name, size_t name_len,
  189. spv_operand_desc* desc) const {
  190. return spvOperandTableNameLookup(target_env_, operandTable_, type, name,
  191. name_len, desc);
  192. }
  193. spv_result_t AssemblyGrammar::lookupOperand(spv_operand_type_t type,
  194. uint32_t operand,
  195. spv_operand_desc* desc) const {
  196. return spvOperandTableValueLookup(target_env_, operandTable_, type, operand,
  197. desc);
  198. }
  199. spv_result_t AssemblyGrammar::lookupSpecConstantOpcode(const char* name,
  200. spv::Op* opcode) const {
  201. const auto* last = kOpSpecConstantOpcodes + kNumOpSpecConstantOpcodes;
  202. const auto* found =
  203. std::find_if(kOpSpecConstantOpcodes, last,
  204. [name](const SpecConstantOpcodeEntry& entry) {
  205. return 0 == strcmp(name, entry.name);
  206. });
  207. if (found == last) return SPV_ERROR_INVALID_LOOKUP;
  208. *opcode = found->opcode;
  209. return SPV_SUCCESS;
  210. }
  211. spv_result_t AssemblyGrammar::lookupSpecConstantOpcode(spv::Op opcode) const {
  212. const auto* last = kOpSpecConstantOpcodes + kNumOpSpecConstantOpcodes;
  213. const auto* found =
  214. std::find_if(kOpSpecConstantOpcodes, last,
  215. [opcode](const SpecConstantOpcodeEntry& entry) {
  216. return opcode == entry.opcode;
  217. });
  218. if (found == last) return SPV_ERROR_INVALID_LOOKUP;
  219. return SPV_SUCCESS;
  220. }
  221. spv_result_t AssemblyGrammar::parseMaskOperand(const spv_operand_type_t type,
  222. const char* textValue,
  223. uint32_t* pValue) const {
  224. return spvTextParseMaskOperand(target_env_, operandTable_, type, textValue,
  225. pValue);
  226. }
  227. spv_result_t AssemblyGrammar::lookupExtInst(spv_ext_inst_type_t type,
  228. const char* textValue,
  229. spv_ext_inst_desc* extInst) const {
  230. return spvExtInstTableNameLookup(extInstTable_, type, textValue, extInst);
  231. }
  232. spv_result_t AssemblyGrammar::lookupExtInst(spv_ext_inst_type_t type,
  233. uint32_t firstWord,
  234. spv_ext_inst_desc* extInst) const {
  235. return spvExtInstTableValueLookup(extInstTable_, type, firstWord, extInst);
  236. }
  237. void AssemblyGrammar::pushOperandTypesForMask(
  238. const spv_operand_type_t type, const uint32_t mask,
  239. spv_operand_pattern_t* pattern) const {
  240. spvPushOperandTypesForMask(target_env_, operandTable_, type, mask, pattern);
  241. }
  242. } // namespace spvtools