Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 1 | // Copyright (c) 2015 The Khronos Group Inc. |
| 2 | // |
| 3 | // Permission is hereby granted, free of charge, to any person obtaining a |
| 4 | // copy of this software and/or associated documentation files (the |
| 5 | // "Materials"), to deal in the Materials without restriction, including |
| 6 | // without limitation the rights to use, copy, modify, merge, publish, |
| 7 | // distribute, sublicense, and/or sell copies of the Materials, and to |
| 8 | // permit persons to whom the Materials are furnished to do so, subject to |
| 9 | // the following conditions: |
| 10 | // |
| 11 | // The above copyright notice and this permission notice shall be included |
| 12 | // in all copies or substantial portions of the Materials. |
| 13 | // |
| 14 | // MODIFICATIONS TO THIS FILE MAY MEAN IT NO LONGER ACCURATELY REFLECTS |
| 15 | // KHRONOS STANDARDS. THE UNMODIFIED, NORMATIVE VERSIONS OF KHRONOS |
| 16 | // SPECIFICATIONS AND HEADER INFORMATION ARE LOCATED AT |
| 17 | // https://www.khronos.org/registry/ |
| 18 | // |
| 19 | // THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 20 | // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 21 | // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 22 | // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
| 23 | // CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
| 24 | // TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
| 25 | // MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS. |
| 26 | |
| 27 | #ifndef _LIBSPIRV_UTIL_TEXT_HANDLER_H_ |
| 28 | #define _LIBSPIRV_UTIL_TEXT_HANDLER_H_ |
| 29 | |
| 30 | #include <libspirv/libspirv.h> |
| 31 | #include <unordered_map> |
| 32 | |
| 33 | #include "diagnostic.h" |
David Neto | b5dc8fc | 2015-10-06 16:22:00 -0400 | [diff] [blame] | 34 | #include "instruction.h" |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 35 | #include "operand.h" |
| 36 | |
| 37 | namespace libspirv { |
| 38 | // Structures |
| 39 | |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 40 | // This is a lattice for tracking types. |
| 41 | enum class IdTypeClass { |
| 42 | kBottom, // We have no information yet. |
| 43 | kScalarIntegerType, |
| 44 | kScalarFloatType, |
| 45 | kOtherType |
| 46 | }; |
| 47 | |
| 48 | |
| 49 | // Contains ID type information that needs to be tracked across all Ids. |
| 50 | // Bitwidth is only valid when type_class is kScalarIntegerType or |
| 51 | // kScalarFloatType. |
| 52 | struct IdType { |
| 53 | uint32_t bitwidth; // Safe to assume that we will not have > 2^32 bits. |
| 54 | IdTypeClass type_class; |
| 55 | }; |
| 56 | |
Andrew Woloszyn | 537e776 | 2015-09-29 11:28:34 -0400 | [diff] [blame] | 57 | // Returns true if the type is a scalar integer type. |
| 58 | inline bool isScalarIntegral(const IdType& type) { |
| 59 | return type.type_class == IdTypeClass::kScalarIntegerType; |
| 60 | } |
| 61 | |
| 62 | // Returns true if the type is a scalar floating point type. |
| 63 | inline bool isScalarFloating(const IdType& type) { |
| 64 | return type.type_class == IdTypeClass::kScalarFloatType; |
| 65 | } |
| 66 | |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 67 | // Encapsulates the grammar to use for SPIR-V assembly. |
| 68 | // Contains methods to query for valid instructions and operands. |
| 69 | class AssemblyGrammar { |
| 70 | public: |
| 71 | AssemblyGrammar(const spv_operand_table operand_table, |
| 72 | const spv_opcode_table opcode_table, |
| 73 | const spv_ext_inst_table ext_inst_table) |
| 74 | : operandTable_(operand_table), |
| 75 | opcodeTable_(opcode_table), |
| 76 | extInstTable_(ext_inst_table) {} |
| 77 | |
| 78 | // Returns true if the compilation_data has been initialized with valid data. |
| 79 | bool isValid() const; |
| 80 | |
| 81 | // Fills in the desc parameter with the information about the opcode |
| 82 | // of the given name. Returns SPV_SUCCESS if the opcode was found, and |
| 83 | // SPV_ERROR_INVALID_LOOKUP if the opcode does not exist. |
| 84 | spv_result_t lookupOpcode(const char *name, spv_opcode_desc *desc) const; |
| 85 | |
Andrew Woloszyn | 537e776 | 2015-09-29 11:28:34 -0400 | [diff] [blame] | 86 | // Fills in the desc parameter with the information about the opcode |
| 87 | // of the valid. Returns SPV_SUCCESS if the opcode was found, and |
| 88 | // SPV_ERROR_INVALID_LOOKUP if the opcode does not exist. |
| 89 | spv_result_t lookupOpcode(Op opcode, spv_opcode_desc *desc) const; |
| 90 | |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 91 | // Fills in the desc parameter with the information about the given |
| 92 | // operand. Returns SPV_SUCCESS if the operand was found, and |
| 93 | // SPV_ERROR_INVALID_LOOKUP otherwise. |
| 94 | spv_result_t lookupOperand(spv_operand_type_t type, const char *name, |
| 95 | size_t name_len, spv_operand_desc *desc) const; |
| 96 | |
| 97 | // Parses a mask expression string for the given operand type. |
| 98 | // |
| 99 | // A mask expression is a sequence of one or more terms separated by '|', |
| 100 | // where each term is a named enum value for a given type. No whitespace |
| 101 | // is permitted. |
| 102 | // |
| 103 | // On success, the value is written to pValue, and SPV_SUCCESS is returend. |
| 104 | // The operand type is defined by the type parameter, and the text to be |
| 105 | // parsed is defined by the textValue parameter. |
| 106 | spv_result_t parseMaskOperand(const spv_operand_type_t type, |
| 107 | const char *textValue, uint32_t *pValue) const; |
| 108 | |
| 109 | |
| 110 | // Writes the extended operand with the given type and text to the *extInst |
| 111 | // parameter. |
| 112 | // Returns SPV_SUCCESS if the value could be found. |
| 113 | spv_result_t lookupExtInst(spv_ext_inst_type_t type, const char *textValue, |
| 114 | spv_ext_inst_desc *extInst) const; |
| 115 | |
| 116 | // Inserts the operands expected after the given typed mask onto the front |
| 117 | // of the given pattern. |
| 118 | // |
| 119 | // Each set bit in the mask represents zero or more operand types that should |
| 120 | // be prepended onto the pattern. Opearnds for a less significant bit always |
| 121 | // appear before operands for a more significatn bit. |
| 122 | // |
| 123 | // If a set bit is unknown, then we assume it has no operands. |
| 124 | void prependOperandTypesForMask(const spv_operand_type_t type, |
| 125 | const uint32_t mask, |
| 126 | spv_operand_pattern_t *pattern) const; |
| 127 | |
| 128 | private: |
| 129 | const spv_operand_table operandTable_; |
| 130 | const spv_opcode_table opcodeTable_; |
| 131 | const spv_ext_inst_table extInstTable_; |
| 132 | }; |
| 133 | |
| 134 | // Encapsulates the data used during the assembly of a SPIR-V module. |
| 135 | class AssemblyContext { |
| 136 | public: |
| 137 | AssemblyContext(spv_text text, spv_diagnostic *diagnostic) |
| 138 | : current_position_({}), |
| 139 | pDiagnostic_(diagnostic), |
| 140 | text_(text), |
| 141 | bound_(1) {} |
| 142 | |
| 143 | // Assigns a new integer value to the given text ID, or returns the previously |
| 144 | // assigned integer value if the ID has been seen before. |
| 145 | uint32_t spvNamedIdAssignOrGet(const char *textValue); |
| 146 | |
| 147 | // Returns the largest largest numeric ID that has been assigned. |
| 148 | uint32_t getBound() const; |
| 149 | |
| 150 | // Advances position to point to the next word in the input stream. |
| 151 | // Returns SPV_SUCCESS on success. |
| 152 | spv_result_t advance(); |
| 153 | |
| 154 | // Sets word to the next word in the input text. Fills endPosition with |
| 155 | // the next location past the end of the word. |
| 156 | spv_result_t getWord(std::string &word, spv_position endPosition); |
| 157 | |
| 158 | // Returns the next word in the input stream. It is invalid to call this |
| 159 | // method if position has been set to a location in the stream that does not |
| 160 | // exist. If there are no subsequent words, the empty string will be returend. |
| 161 | std::string getWord() const; |
| 162 | |
| 163 | // Returns true if the next word in the input is the start of a new Opcode. |
| 164 | bool startsWithOp(); |
| 165 | |
| 166 | // Returns true if the next word in the input is the start of a new |
| 167 | // instruction. |
| 168 | bool isStartOfNewInst(); |
| 169 | |
| 170 | // Returns a diagnostic object initialized with current position in the input |
| 171 | // stream. Any data written to this object will show up in pDiagnsotic on |
| 172 | // destruction. |
| 173 | DiagnosticStream diagnostic() { |
| 174 | return DiagnosticStream(¤t_position_, pDiagnostic_); |
| 175 | } |
| 176 | |
| 177 | // Returns then next characted in the input stream. |
| 178 | char peek() const; |
| 179 | |
| 180 | // Returns true if there is more text in the input stream. |
| 181 | bool hasText() const; |
| 182 | |
| 183 | // Seeks the input stream forward by 'size' characters. |
| 184 | void seekForward(uint32_t size); |
| 185 | |
| 186 | // Sets the current position in the input stream to the given position. |
| 187 | void setPosition(const spv_position_t &newPosition) { |
| 188 | current_position_ = newPosition; |
| 189 | } |
| 190 | |
| 191 | // Returns the current position in the input stream. |
| 192 | const spv_position_t &position() const { return current_position_; } |
| 193 | |
| 194 | // Appends the given 32-bit value to the given instruction. |
| 195 | // Returns SPV_SUCCESS if the value could be correctly inserted in the the |
| 196 | // instruction. |
| 197 | spv_result_t binaryEncodeU32(const uint32_t value, spv_instruction_t *pInst); |
| 198 | // Appends the given 64-bit value to the given instruction. |
| 199 | // Returns SPV_SUCCESS if the value could be correctly inserted in the the |
| 200 | // instruction. |
| 201 | spv_result_t binaryEncodeU64(const uint64_t value, spv_instruction_t *pInst); |
| 202 | // Appends the given string to the given instruction. |
| 203 | // Returns SPV_SUCCESS if the value could be correctly inserted in the the |
| 204 | // instruction. |
| 205 | spv_result_t binaryEncodeString(const char *value, spv_instruction_t *pInst); |
| 206 | |
Andrew Woloszyn | 537e776 | 2015-09-29 11:28:34 -0400 | [diff] [blame] | 207 | // Returns the IdType associated with this type-generating value. |
| 208 | // If the type has not been previously recorded with recordTypeDefinition, |
| 209 | // { 0, IdTypeClass::kBottom } will be returned. |
| 210 | IdType getTypeOfTypeGeneratingValue(uint32_t value) const; |
| 211 | |
| 212 | // Returns the IdType that represents the return value of this Value |
| 213 | // generating instruction. |
| 214 | // If the value has not been recorded with recordTypeIdForValue, or the type |
| 215 | // could not be determined { 0, IdTypeClass::kBottom } will be returned. |
| 216 | IdType getTypeOfValueInstruction(uint32_t value) const; |
| 217 | |
| 218 | // Tracks the type-defining instruction. The result of the tracking can |
| 219 | // later be queried using getValueType. |
| 220 | // pInst is expected to be completely filled in by the time this instruction |
| 221 | // is called. |
| 222 | // Returns SPV_SUCCESS on success, or SPV_ERROR_INVALID_VALUE on error. |
| 223 | spv_result_t recordTypeDefinition(const spv_instruction_t* pInst); |
| 224 | |
| 225 | // Tracks the relationship between the value and its type. |
| 226 | spv_result_t recordTypeIdForValue(uint32_t value, uint32_t type); |
| 227 | |
Andrew Woloszyn | 71fc055 | 2015-09-24 10:26:51 -0400 | [diff] [blame] | 228 | private: |
| 229 | // Maps ID names to their corresponding numerical ids. |
| 230 | using spv_named_id_table = std::unordered_map<std::string, uint32_t>; |
| 231 | // Maps type-defining IDs to their IdType. |
| 232 | using spv_id_to_type_map = std::unordered_map<uint32_t, IdType>; |
| 233 | // Maps Ids to the id of their type. |
| 234 | using spv_id_to_type_id = std::unordered_map<uint32_t, uint32_t>; |
| 235 | |
| 236 | spv_named_id_table named_ids_; |
| 237 | spv_id_to_type_map types_; |
| 238 | spv_id_to_type_id value_types_; |
| 239 | spv_position_t current_position_; |
| 240 | spv_diagnostic *pDiagnostic_; |
| 241 | spv_text text_; |
| 242 | uint32_t bound_; |
| 243 | }; |
| 244 | } |
| 245 | #endif // _LIBSPIRV_UTIL_TEXT_HANDLER_H_ |
| 246 | |