Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [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 | |
David Neto | 9f79d78 | 2015-10-27 16:27:05 -0400 | [diff] [blame] | 27 | #ifndef LIBSPIRV_OPCODE_H_ |
| 28 | #define LIBSPIRV_OPCODE_H_ |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 29 | |
David Neto | b5dc8fc | 2015-10-06 16:22:00 -0400 | [diff] [blame] | 30 | #include "instruction.h" |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 31 | #include <libspirv/libspirv.h> |
| 32 | |
| 33 | // Functions |
| 34 | |
David Neto | 1780fc4 | 2015-10-26 15:43:12 -0400 | [diff] [blame] | 35 | /// @brief Get the name of the SPIR-V generator. |
| 36 | /// |
| 37 | /// See the registry at |
| 38 | /// https://www.khronos.org/registry/spir-v/api/spir-v.xml |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 39 | /// |
| 40 | /// @param[in] generator Khronos SPIR-V generator ID |
| 41 | /// |
| 42 | /// @return string name |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame] | 43 | const char* spvGeneratorStr(uint32_t generator); |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 44 | |
| 45 | /// @brief Combine word count and Opcode enumerant in single word |
| 46 | /// |
| 47 | /// @param[in] wordCount Opcode consumes |
| 48 | /// @param[in] opcode enumerant value |
| 49 | /// |
| 50 | /// @return Opcode word |
Lei Zhang | b36e704 | 2015-10-28 13:40:52 -0400 | [diff] [blame] | 51 | uint32_t spvOpcodeMake(uint16_t wordCount, SpvOp opcode); |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 52 | |
| 53 | /// @brief Split the binary opcode into its constituent parts |
| 54 | /// |
| 55 | /// @param[in] word binary opcode to split |
| 56 | /// @param[out] wordCount the returned number of words (optional) |
| 57 | /// @param[out] opcode the returned opcode enumerant (optional) |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame] | 58 | void spvOpcodeSplit(const uint32_t word, uint16_t* wordCount, SpvOp* opcode); |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 59 | |
| 60 | /// @brief Find the named Opcode in the table |
| 61 | /// |
| 62 | /// @param[in] table to lookup |
| 63 | /// @param[in] name name of Opcode to find |
| 64 | /// @param[out] pEntry returned Opcode table entry |
| 65 | /// |
| 66 | /// @return result code |
| 67 | spv_result_t spvOpcodeTableNameLookup(const spv_opcode_table table, |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame] | 68 | const char* name, |
| 69 | spv_opcode_desc* pEntry); |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 70 | |
| 71 | /// @brief Find the opcode ID in the table |
| 72 | /// |
| 73 | /// @param[out] table to lookup |
| 74 | /// @param[in] opcode value of Opcode to fine |
| 75 | /// @param[out] pEntry return Opcode table entry |
| 76 | /// |
| 77 | /// @return result code |
| 78 | spv_result_t spvOpcodeTableValueLookup(const spv_opcode_table table, |
Lei Zhang | b36e704 | 2015-10-28 13:40:52 -0400 | [diff] [blame] | 79 | const SpvOp opcode, |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame] | 80 | spv_opcode_desc* pEntry); |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 81 | |
Lei Zhang | dfc5008 | 2015-08-21 11:50:55 -0400 | [diff] [blame] | 82 | /// @brief Get the argument index for the <result-id> operand, if any. |
| 83 | /// |
| 84 | /// @param[in] entry the Opcode entry |
| 85 | /// |
| 86 | /// @return index for the <result-id> operand, or |
| 87 | /// SPV_OPERAND_INVALID_RESULT_ID_INDEX if the given opcode |
| 88 | /// doesn't have a <result-id> operand. |
| 89 | // |
| 90 | /// For example, 0 means <result-id> is the first argument, i.e. right after |
| 91 | /// the wordcount/opcode word. |
| 92 | int16_t spvOpcodeResultIdIndex(spv_opcode_desc entry); |
| 93 | |
David Neto | 9db3a53 | 2015-10-07 16:58:38 -0400 | [diff] [blame] | 94 | /// @brief Determine if the Opcode has capability requirements. |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 95 | /// |
| 96 | /// This function does not check if @a entry is valid. |
| 97 | /// |
| 98 | /// @param[in] entry the Opcode entry |
| 99 | /// |
| 100 | /// @return zero if false, non-zero otherwise |
| 101 | int32_t spvOpcodeRequiresCapabilities(spv_opcode_desc entry); |
| 102 | |
| 103 | /// @brief Copy an instructions word and fix the endianness |
| 104 | /// |
| 105 | /// @param[in] words the input instruction stream |
| 106 | /// @param[in] opcode the instructions Opcode |
| 107 | /// @param[in] wordCount the number of words to copy |
| 108 | /// @param[in] endian the endianness of the stream |
| 109 | /// @param[out] pInst the returned instruction |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame] | 110 | void spvInstructionCopy(const uint32_t* words, const SpvOp opcode, |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 111 | const uint16_t wordCount, const spv_endianness_t endian, |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame] | 112 | spv_instruction_t* pInst); |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 113 | |
David Neto | 1bcd3d1 | 2015-11-02 16:03:12 -0500 | [diff] [blame^] | 114 | /// @brief Get the name of an instruction, without the "Op" prefix. |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 115 | /// |
| 116 | /// @param[in] opcode the opcode |
| 117 | /// |
| 118 | /// @return the opcode string |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame] | 119 | const char* spvOpcodeString(const SpvOp opcode); |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 120 | |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 121 | /// @brief Determine if the OpCode is a scalar type |
| 122 | /// |
| 123 | /// @param[in] opcode the opcode |
| 124 | /// |
| 125 | /// @return zero if false, non-zero otherwise |
Lei Zhang | b36e704 | 2015-10-28 13:40:52 -0400 | [diff] [blame] | 126 | int32_t spvOpcodeIsScalarType(const SpvOp opcode); |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 127 | |
| 128 | /// @brief Determine if the Opcode is a constant |
| 129 | /// |
| 130 | /// @param[in] opcode the opcode |
| 131 | /// |
| 132 | /// @return zero if false, non-zero otherwise |
Lei Zhang | b36e704 | 2015-10-28 13:40:52 -0400 | [diff] [blame] | 133 | int32_t spvOpcodeIsConstant(const SpvOp opcode); |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 134 | |
| 135 | /// @brief Determine if the Opcode is a composite type |
| 136 | /// |
| 137 | /// @param[in] opcode the opcode |
| 138 | /// |
| 139 | /// @return zero if false, non-zero otherwise |
Lei Zhang | b36e704 | 2015-10-28 13:40:52 -0400 | [diff] [blame] | 140 | int32_t spvOpcodeIsComposite(const SpvOp opcode); |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 141 | |
| 142 | /// @brief Deep comparison of type declaration instructions |
| 143 | /// |
| 144 | /// @param[in] pTypeInst0 type definition zero |
| 145 | /// @param[in] pTypeInst1 type definition one |
| 146 | /// |
| 147 | /// @return zero if false, non-zero otherwise |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame] | 148 | int32_t spvOpcodeAreTypesEqual(const spv_instruction_t* pTypeInst0, |
| 149 | const spv_instruction_t* pTypeInst1); |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 150 | |
| 151 | /// @brief Determine if the Opcode results in a pointer |
| 152 | /// |
| 153 | /// @param[in] opcode the opcode |
| 154 | /// |
| 155 | /// @return zero if false, non-zero otherwise |
Lei Zhang | b36e704 | 2015-10-28 13:40:52 -0400 | [diff] [blame] | 156 | int32_t spvOpcodeIsPointer(const SpvOp opcode); |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 157 | |
| 158 | /// @brief Determine if the Opcode results in a instantation of a non-void type |
| 159 | /// |
| 160 | /// @param[in] opcode the opcode |
| 161 | /// |
| 162 | /// @return zero if false, non-zero otherwise |
Lei Zhang | b36e704 | 2015-10-28 13:40:52 -0400 | [diff] [blame] | 163 | int32_t spvOpcodeIsObject(const SpvOp opcode); |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 164 | |
| 165 | /// @brief Determine if the scalar type Opcode is nullable |
| 166 | /// |
| 167 | /// @param[in] opcode the opcode |
| 168 | /// |
| 169 | /// @return zero if false, non-zero otherwise |
Lei Zhang | b36e704 | 2015-10-28 13:40:52 -0400 | [diff] [blame] | 170 | int32_t spvOpcodeIsBasicTypeNullable(SpvOp opcode); |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 171 | |
| 172 | /// @brief Determine if instruction is in a basic block |
| 173 | /// |
| 174 | /// @param[in] pFirstInst first instruction in the stream |
| 175 | /// @param[in] pInst current instruction |
| 176 | /// |
| 177 | /// @return zero if false, non-zero otherwise |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame] | 178 | int32_t spvInstructionIsInBasicBlock(const spv_instruction_t* pFirstInst, |
| 179 | const spv_instruction_t* pInst); |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 180 | |
| 181 | /// @brief Determine if the Opcode contains a value |
| 182 | /// |
| 183 | /// @param[in] opcode the opcode |
| 184 | /// |
| 185 | /// @return zero if false, non-zero otherwise |
Lei Zhang | b36e704 | 2015-10-28 13:40:52 -0400 | [diff] [blame] | 186 | int32_t spvOpcodeIsValue(SpvOp opcode); |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 187 | |
Andrew Woloszyn | 537e776 | 2015-09-29 11:28:34 -0400 | [diff] [blame] | 188 | /// @brief Determine if the Opcode generates a type |
| 189 | /// |
| 190 | /// @param[in] opcode the opcode |
| 191 | /// |
| 192 | /// @return zero if false, non-zero otherwise |
Lei Zhang | b36e704 | 2015-10-28 13:40:52 -0400 | [diff] [blame] | 193 | int32_t spvOpcodeGeneratesType(SpvOp op); |
Andrew Woloszyn | 537e776 | 2015-09-29 11:28:34 -0400 | [diff] [blame] | 194 | |
David Neto | 9f79d78 | 2015-10-27 16:27:05 -0400 | [diff] [blame] | 195 | #endif // LIBSPIRV_OPCODE_H_ |