operand.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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_OPERAND_H_
  15. #define SOURCE_OPERAND_H_
  16. #include <functional>
  17. #include <vector>
  18. #include "source/table.h"
  19. #include "spirv-tools/libspirv.h"
  20. // A sequence of operand types.
  21. //
  22. // A SPIR-V parser uses an operand pattern to describe what is expected
  23. // next on the input.
  24. //
  25. // As we parse an instruction in text or binary form from left to right,
  26. // we pop and push at the end of the pattern vector. Symbols later in the
  27. // pattern vector are matched against the input before symbols earlier in the
  28. // pattern vector are matched.
  29. // Using a vector in this way reduces memory traffic, which is good for
  30. // performance.
  31. using spv_operand_pattern_t = std::vector<spv_operand_type_t>;
  32. // Finds the named operand in the table. The type parameter specifies the
  33. // operand's group. A handle of the operand table entry for this operand will
  34. // be written into *entry.
  35. spv_result_t spvOperandTableNameLookup(spv_target_env,
  36. const spv_operand_table table,
  37. const spv_operand_type_t type,
  38. const char* name,
  39. const size_t name_length,
  40. spv_operand_desc* entry);
  41. // Finds the operand with value in the table. The type parameter specifies the
  42. // operand's group. A handle of the operand table entry for this operand will
  43. // be written into *entry.
  44. spv_result_t spvOperandTableValueLookup(spv_target_env,
  45. const spv_operand_table table,
  46. const spv_operand_type_t type,
  47. const uint32_t value,
  48. spv_operand_desc* entry);
  49. // Gets the name string of the non-variable operand type.
  50. const char* spvOperandTypeStr(spv_operand_type_t type);
  51. // Returns true if the given type is concrete.
  52. bool spvOperandIsConcrete(spv_operand_type_t type);
  53. // Returns true if the given type is concrete and also a mask.
  54. bool spvOperandIsConcreteMask(spv_operand_type_t type);
  55. // Returns true if an operand of the given type is optional.
  56. bool spvOperandIsOptional(spv_operand_type_t type);
  57. // Returns true if an operand type represents zero or more logical operands.
  58. //
  59. // Note that a single logical operand may still be a variable number of words.
  60. // For example, a literal string may be many words, but is just one logical
  61. // operand.
  62. bool spvOperandIsVariable(spv_operand_type_t type);
  63. // Append a list of operand types to the end of the pattern vector.
  64. // The types parameter specifies the source array of types, ending with
  65. // SPV_OPERAND_TYPE_NONE.
  66. void spvPushOperandTypes(const spv_operand_type_t* types,
  67. spv_operand_pattern_t* pattern);
  68. // Appends the operands expected after the given typed mask onto the
  69. // end of the given pattern.
  70. //
  71. // Each set bit in the mask represents zero or more operand types that should
  72. // be appended onto the pattern. Operands for a less significant bit always
  73. // appear after operands for a more significant bit.
  74. //
  75. // If a set bit is unknown, then we assume it has no operands.
  76. void spvPushOperandTypesForMask(spv_target_env,
  77. const spv_operand_table operand_table,
  78. const spv_operand_type_t mask_type,
  79. const uint32_t mask,
  80. spv_operand_pattern_t* pattern);
  81. // Expands an operand type representing zero or more logical operands,
  82. // exactly once.
  83. //
  84. // If the given type represents potentially several logical operands,
  85. // then prepend the given pattern with the first expansion of the logical
  86. // operands, followed by original type. Otherwise, don't modify the pattern.
  87. //
  88. // For example, the SPV_OPERAND_TYPE_VARIABLE_ID represents zero or more
  89. // IDs. In that case we would prepend the pattern with SPV_OPERAND_TYPE_ID
  90. // followed by SPV_OPERAND_TYPE_VARIABLE_ID again.
  91. //
  92. // This also applies to zero or more tuples of logical operands. In that case
  93. // we prepend pattern with for the members of the tuple, followed by the
  94. // original type argument. The pattern must encode the fact that if any part
  95. // of the tuple is present, then all tuple members should be. So the first
  96. // member of the tuple must be optional, and the remaining members
  97. // non-optional.
  98. //
  99. // Returns true if we modified the pattern.
  100. bool spvExpandOperandSequenceOnce(spv_operand_type_t type,
  101. spv_operand_pattern_t* pattern);
  102. // Expands the first element in the pattern until it is a matchable operand
  103. // type, then pops it off the front and returns it. The pattern must not be
  104. // empty.
  105. //
  106. // A matchable operand type is anything other than a zero-or-more-items
  107. // operand type.
  108. spv_operand_type_t spvTakeFirstMatchableOperand(spv_operand_pattern_t* pattern);
  109. // Calculates the corresponding post-immediate alternate pattern, which allows
  110. // a limited set of operand types.
  111. spv_operand_pattern_t spvAlternatePatternFollowingImmediate(
  112. const spv_operand_pattern_t& pattern);
  113. // Is the operand an ID?
  114. bool spvIsIdType(spv_operand_type_t type);
  115. // Is the operand an input ID?
  116. bool spvIsInIdType(spv_operand_type_t type);
  117. // Takes the opcode of an instruction and returns
  118. // a function object that will return true if the index
  119. // of the operand can be forward declared. This function will
  120. // used in the SSA validation stage of the pipeline
  121. std::function<bool(unsigned)> spvOperandCanBeForwardDeclaredFunction(
  122. SpvOp opcode);
  123. #endif // SOURCE_OPERAND_H_