assembly_grammar.cpp 8.8 KB

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