assembly_grammar.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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 "assembly_grammar.h"
  15. #include <algorithm>
  16. #include <cassert>
  17. #include <cstring>
  18. #include "ext_inst.h"
  19. #include "opcode.h"
  20. #include "operand.h"
  21. #include "table.h"
  22. namespace {
  23. /// @brief Parses a mask expression string for the given operand type.
  24. ///
  25. /// A mask expression is a sequence of one or more terms separated by '|',
  26. /// where each term a named enum value for the given type. No whitespace
  27. /// is permitted.
  28. ///
  29. /// On success, the value is written to pValue.
  30. ///
  31. /// @param[in] operandTable operand lookup table
  32. /// @param[in] type of the operand
  33. /// @param[in] textValue word of text to be parsed
  34. /// @param[out] pValue where the resulting value is written
  35. ///
  36. /// @return result code
  37. spv_result_t spvTextParseMaskOperand(spv_target_env env,
  38. const spv_operand_table operandTable,
  39. const spv_operand_type_t type,
  40. const char* textValue, uint32_t* pValue) {
  41. if (textValue == nullptr) return SPV_ERROR_INVALID_TEXT;
  42. size_t text_length = strlen(textValue);
  43. if (text_length == 0) return SPV_ERROR_INVALID_TEXT;
  44. const char* text_end = textValue + text_length;
  45. // We only support mask expressions in ASCII, so the separator value is a
  46. // char.
  47. const char separator = '|';
  48. // Accumulate the result by interpreting one word at a time, scanning
  49. // from left to right.
  50. uint32_t value = 0;
  51. const char* begin = textValue; // The left end of the current word.
  52. const char* end = nullptr; // One character past the end of the current word.
  53. do {
  54. end = std::find(begin, text_end, separator);
  55. spv_operand_desc entry = nullptr;
  56. if (spvOperandTableNameLookup(env, operandTable, type, begin, end - begin,
  57. &entry)) {
  58. return SPV_ERROR_INVALID_TEXT;
  59. }
  60. value |= entry->value;
  61. // Advance to the next word by skipping over the separator.
  62. begin = end + 1;
  63. } while (end != text_end);
  64. *pValue = value;
  65. return SPV_SUCCESS;
  66. }
  67. // Associates an opcode with its name.
  68. struct SpecConstantOpcodeEntry {
  69. SpvOp opcode;
  70. const char* name;
  71. };
  72. // All the opcodes allowed as the operation for OpSpecConstantOp.
  73. // The name does not have the usual "Op" prefix. For example opcode SpvOpIAdd
  74. // is associated with the name "IAdd".
  75. //
  76. // clang-format off
  77. #define CASE(NAME) { SpvOp##NAME, #NAME }
  78. const SpecConstantOpcodeEntry kOpSpecConstantOpcodes[] = {
  79. // Conversion
  80. CASE(SConvert),
  81. CASE(FConvert),
  82. CASE(ConvertFToS),
  83. CASE(ConvertSToF),
  84. CASE(ConvertFToU),
  85. CASE(ConvertUToF),
  86. CASE(UConvert),
  87. CASE(ConvertPtrToU),
  88. CASE(ConvertUToPtr),
  89. CASE(GenericCastToPtr),
  90. CASE(PtrCastToGeneric),
  91. CASE(Bitcast),
  92. CASE(QuantizeToF16),
  93. // Arithmetic
  94. CASE(SNegate),
  95. CASE(Not),
  96. CASE(IAdd),
  97. CASE(ISub),
  98. CASE(IMul),
  99. CASE(UDiv),
  100. CASE(SDiv),
  101. CASE(UMod),
  102. CASE(SRem),
  103. CASE(SMod),
  104. CASE(ShiftRightLogical),
  105. CASE(ShiftRightArithmetic),
  106. CASE(ShiftLeftLogical),
  107. CASE(BitwiseOr),
  108. CASE(BitwiseAnd),
  109. CASE(BitwiseXor),
  110. CASE(FNegate),
  111. CASE(FAdd),
  112. CASE(FSub),
  113. CASE(FMul),
  114. CASE(FDiv),
  115. CASE(FRem),
  116. CASE(FMod),
  117. // Composite
  118. CASE(VectorShuffle),
  119. CASE(CompositeExtract),
  120. CASE(CompositeInsert),
  121. // Logical
  122. CASE(LogicalOr),
  123. CASE(LogicalAnd),
  124. CASE(LogicalNot),
  125. CASE(LogicalEqual),
  126. CASE(LogicalNotEqual),
  127. CASE(Select),
  128. // Comparison
  129. CASE(IEqual),
  130. CASE(INotEqual),
  131. CASE(ULessThan),
  132. CASE(SLessThan),
  133. CASE(UGreaterThan),
  134. CASE(SGreaterThan),
  135. CASE(ULessThanEqual),
  136. CASE(SLessThanEqual),
  137. CASE(UGreaterThanEqual),
  138. CASE(SGreaterThanEqual),
  139. // Memory
  140. CASE(AccessChain),
  141. CASE(InBoundsAccessChain),
  142. CASE(PtrAccessChain),
  143. CASE(InBoundsPtrAccessChain),
  144. };
  145. // The 59 is determined by counting the opcodes listed in the spec.
  146. static_assert(59 == sizeof(kOpSpecConstantOpcodes)/sizeof(kOpSpecConstantOpcodes[0]),
  147. "OpSpecConstantOp opcode table is incomplete");
  148. #undef CASE
  149. // clang-format on
  150. const size_t kNumOpSpecConstantOpcodes =
  151. sizeof(kOpSpecConstantOpcodes) / sizeof(kOpSpecConstantOpcodes[0]);
  152. } // anonymous namespace
  153. namespace libspirv {
  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 libspirv