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 | |
| 27 | #include <libspirv/libspirv.h> |
David Neto | db901b6 | 2015-10-27 16:14:40 -0400 | [diff] [blame] | 28 | #include "endian.h" |
David Neto | b5dc8fc | 2015-10-06 16:22:00 -0400 | [diff] [blame] | 29 | #include "instruction.h" |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 30 | #include "opcode.h" |
| 31 | |
| 32 | #include <assert.h> |
| 33 | #include <string.h> |
| 34 | |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 35 | namespace { |
| 36 | |
| 37 | // Descriptions of each opcode. Each entry describes the format of the |
| 38 | // instruction that follows a particular opcode. |
| 39 | // |
| 40 | // Most fields are initialized statically by including an automatically |
| 41 | // generated file. |
| 42 | // The operandTypes fields are initialized during spvOpcodeInitialize(). |
| 43 | // |
| 44 | // TODO(dneto): Some of the macros are quite unreadable. We could make |
| 45 | // good use of constexpr functions, but some compilers don't support that yet. |
| 46 | spv_opcode_desc_t opcodeTableEntries[] = { |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame^] | 47 | #define EmptyList \ |
| 48 | {} |
| 49 | #define List(...) \ |
| 50 | { __VA_ARGS__ } |
Lei Zhang | b36e704 | 2015-10-28 13:40:52 -0400 | [diff] [blame] | 51 | #define Capability(X) SPV_CAPABILITY_AS_MASK(SpvCapability##X) |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame^] | 52 | #define Capability2(X, Y) Capability(X) | Capability(Y) |
| 53 | #define SpvCapabilityNone \ |
| 54 | 0 // Needed so Capability(None) still expands to valid syntax. |
| 55 | #define Instruction(Name, HasResult, HasType, NumLogicalOperands, \ |
| 56 | NumCapabilities, CapabilityRequired, IsVariable, \ |
| 57 | LogicalArgsList) \ |
| 58 | { \ |
| 59 | #Name, SpvOp##Name, \ |
| 60 | (NumCapabilities) ? (CapabilityRequired) : 0, 0, \ |
| 61 | {}, /* Filled in later. Operand list, including \ |
| 62 | result id and type id, if needed */ \ |
| 63 | HasResult, HasType, LogicalArgsList \ |
| 64 | } \ |
| 65 | , |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 66 | #include "opcode.inc" |
| 67 | #undef EmptyList |
| 68 | #undef List |
| 69 | #undef Capability |
Dejan Mircevski | 205408b | 2015-09-30 16:42:34 -0400 | [diff] [blame] | 70 | #undef Capability2 |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 71 | #undef CapabilityNone |
| 72 | #undef Instruction |
| 73 | }; |
| 74 | |
| 75 | // Has the opcodeTableEntries table been fully elaborated? |
| 76 | // That is, are the operandTypes fields initialized? |
| 77 | bool opcodeTableInitialized = false; |
| 78 | |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 79 | // Opcode API |
| 80 | |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 81 | // Converts the given operand class enum (from the SPIR-V document generation |
| 82 | // logic) to the operand type required by the parser. |
| 83 | // This only applies to logical operands. |
Lei Zhang | b36e704 | 2015-10-28 13:40:52 -0400 | [diff] [blame] | 84 | spv_operand_type_t convertOperandClassToType(SpvOp opcode, |
| 85 | OperandClass operandClass) { |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 86 | // The spec document generator uses OptionalOperandLiteral for several kinds |
| 87 | // of repeating values. Our parser needs more specific information about |
| 88 | // what is being repeated. |
| 89 | if (operandClass == OperandOptionalLiteral) { |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 90 | switch (opcode) { |
Lei Zhang | b36e704 | 2015-10-28 13:40:52 -0400 | [diff] [blame] | 91 | case SpvOpLoad: |
| 92 | case SpvOpStore: |
| 93 | case SpvOpCopyMemory: |
| 94 | case SpvOpCopyMemorySized: |
David Neto | 3fca4cd | 2015-09-17 17:39:45 -0400 | [diff] [blame] | 95 | // Expect an optional mask. When the Aligned bit is set in the mask, |
| 96 | // we will later add the expectation of a literal number operand. |
| 97 | return SPV_OPERAND_TYPE_OPTIONAL_MEMORY_ACCESS; |
Lei Zhang | b36e704 | 2015-10-28 13:40:52 -0400 | [diff] [blame] | 98 | case SpvOpExecutionMode: |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 99 | return SPV_OPERAND_TYPE_VARIABLE_EXECUTION_MODE; |
| 100 | default: |
| 101 | break; |
| 102 | } |
Lei Zhang | b41d150 | 2015-09-14 15:22:23 -0400 | [diff] [blame] | 103 | } else if (operandClass == OperandVariableLiterals) { |
Lei Zhang | b36e704 | 2015-10-28 13:40:52 -0400 | [diff] [blame] | 104 | if (opcode == SpvOpConstant || opcode == SpvOpSpecConstant) |
Lei Zhang | b41d150 | 2015-09-14 15:22:23 -0400 | [diff] [blame] | 105 | return SPV_OPERAND_TYPE_MULTIWORD_LITERAL_NUMBER; |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 106 | } |
| 107 | |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame^] | 108 | switch (operandClass) { |
| 109 | case OperandNone: |
| 110 | return SPV_OPERAND_TYPE_NONE; |
| 111 | case OperandId: |
| 112 | return SPV_OPERAND_TYPE_ID; |
| 113 | case OperandOptionalId: |
| 114 | return SPV_OPERAND_TYPE_OPTIONAL_ID; |
| 115 | case OperandOptionalImage: |
| 116 | return SPV_OPERAND_TYPE_OPTIONAL_IMAGE; |
| 117 | case OperandVariableIds: |
| 118 | return SPV_OPERAND_TYPE_VARIABLE_ID; |
David Neto | 561dc4e | 2015-09-25 14:23:29 -0400 | [diff] [blame] | 119 | // The spec only uses OptionalLiteral for an optional literal number. |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame^] | 120 | case OperandOptionalLiteral: |
| 121 | return SPV_OPERAND_TYPE_OPTIONAL_LITERAL_INTEGER; |
| 122 | case OperandOptionalLiteralString: |
| 123 | return SPV_OPERAND_TYPE_OPTIONAL_LITERAL_STRING; |
David Neto | 561dc4e | 2015-09-25 14:23:29 -0400 | [diff] [blame] | 124 | // This is only used for sequences of literal numbers. |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame^] | 125 | case OperandVariableLiterals: |
| 126 | return SPV_OPERAND_TYPE_VARIABLE_LITERAL_INTEGER; |
David Neto | 445ce44 | 2015-10-15 15:22:06 -0400 | [diff] [blame] | 127 | case OperandLiteralNumber: |
Lei Zhang | b36e704 | 2015-10-28 13:40:52 -0400 | [diff] [blame] | 128 | if (opcode == SpvOpExtInst) { |
David Neto | 445ce44 | 2015-10-15 15:22:06 -0400 | [diff] [blame] | 129 | // We use a special operand type for the extension instruction number. |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame^] | 130 | // For now, we assume there is only one LiteraNumber argument to |
| 131 | // OpExtInst, and it is the extension instruction argument. |
David Neto | 445ce44 | 2015-10-15 15:22:06 -0400 | [diff] [blame] | 132 | // See the ExtInst entry in opcode.inc |
| 133 | // TODO(dneto): Use a function to confirm the assumption, and to verify |
| 134 | // that the index into the operandClass is 1, as expected. |
| 135 | return SPV_OPERAND_TYPE_EXTENSION_INSTRUCTION_NUMBER; |
| 136 | } |
| 137 | return SPV_OPERAND_TYPE_LITERAL_INTEGER; |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame^] | 138 | case OperandLiteralString: |
| 139 | return SPV_OPERAND_TYPE_LITERAL_STRING; |
| 140 | case OperandSource: |
| 141 | return SPV_OPERAND_TYPE_SOURCE_LANGUAGE; |
| 142 | case OperandExecutionModel: |
| 143 | return SPV_OPERAND_TYPE_EXECUTION_MODEL; |
| 144 | case OperandAddressing: |
| 145 | return SPV_OPERAND_TYPE_ADDRESSING_MODEL; |
| 146 | case OperandMemory: |
| 147 | return SPV_OPERAND_TYPE_MEMORY_MODEL; |
| 148 | case OperandExecutionMode: |
| 149 | return SPV_OPERAND_TYPE_EXECUTION_MODE; |
| 150 | case OperandStorage: |
| 151 | return SPV_OPERAND_TYPE_STORAGE_CLASS; |
| 152 | case OperandDimensionality: |
| 153 | return SPV_OPERAND_TYPE_DIMENSIONALITY; |
| 154 | case OperandSamplerAddressingMode: |
| 155 | return SPV_OPERAND_TYPE_SAMPLER_ADDRESSING_MODE; |
| 156 | case OperandSamplerFilterMode: |
| 157 | return SPV_OPERAND_TYPE_SAMPLER_FILTER_MODE; |
| 158 | case OperandSamplerImageFormat: |
| 159 | return SPV_OPERAND_TYPE_SAMPLER_IMAGE_FORMAT; |
David Neto | 7cefb23 | 2015-09-28 15:33:49 -0400 | [diff] [blame] | 160 | case OperandImageChannelOrder: |
| 161 | // This is only used to describe the value generated by OpImageQueryOrder. |
| 162 | // It is not used as an operand. |
| 163 | break; |
| 164 | case OperandImageChannelDataType: |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame^] | 165 | // This is only used to describe the value generated by |
| 166 | // OpImageQueryFormat. It is not used as an operand. |
David Neto | 7cefb23 | 2015-09-28 15:33:49 -0400 | [diff] [blame] | 167 | break; |
| 168 | case OperandImageOperands: |
Dejan Mircevski | f8e091a | 2015-10-13 14:41:31 -0400 | [diff] [blame] | 169 | // This is not used in opcode.inc. It only exists to generate the |
| 170 | // corresponding spec section. In parsing, image operands meld into the |
| 171 | // OperandOptionalImage case. |
David Neto | 7cefb23 | 2015-09-28 15:33:49 -0400 | [diff] [blame] | 172 | break; |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame^] | 173 | case OperandFPFastMath: |
| 174 | return SPV_OPERAND_TYPE_FP_FAST_MATH_MODE; |
| 175 | case OperandFPRoundingMode: |
| 176 | return SPV_OPERAND_TYPE_FP_ROUNDING_MODE; |
| 177 | case OperandLinkageType: |
| 178 | return SPV_OPERAND_TYPE_LINKAGE_TYPE; |
| 179 | case OperandAccessQualifier: |
| 180 | return SPV_OPERAND_TYPE_ACCESS_QUALIFIER; |
| 181 | case OperandFuncParamAttr: |
| 182 | return SPV_OPERAND_TYPE_FUNCTION_PARAMETER_ATTRIBUTE; |
| 183 | case OperandDecoration: |
| 184 | return SPV_OPERAND_TYPE_DECORATION; |
| 185 | case OperandBuiltIn: |
| 186 | return SPV_OPERAND_TYPE_BUILT_IN; |
| 187 | case OperandSelect: |
| 188 | return SPV_OPERAND_TYPE_SELECTION_CONTROL; |
| 189 | case OperandLoop: |
| 190 | return SPV_OPERAND_TYPE_LOOP_CONTROL; |
| 191 | case OperandFunction: |
| 192 | return SPV_OPERAND_TYPE_FUNCTION_CONTROL; |
| 193 | case OperandMemorySemantics: |
| 194 | return SPV_OPERAND_TYPE_MEMORY_SEMANTICS; |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 195 | case OperandMemoryAccess: |
David Neto | 7cefb23 | 2015-09-28 15:33:49 -0400 | [diff] [blame] | 196 | // This case does not occur in the table for SPIR-V 0.99 Rev 32. |
David Neto | 3fca4cd | 2015-09-17 17:39:45 -0400 | [diff] [blame] | 197 | // We expect that it will become SPV_OPERAND_TYPE_OPTIONAL_MEMORY_ACCESS, |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 198 | // and we can remove the special casing above for memory operation |
| 199 | // instructions. |
| 200 | break; |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame^] | 201 | case OperandScope: |
| 202 | return SPV_OPERAND_TYPE_EXECUTION_SCOPE; |
| 203 | case OperandGroupOperation: |
| 204 | return SPV_OPERAND_TYPE_GROUP_OPERATION; |
| 205 | case OperandKernelEnqueueFlags: |
| 206 | return SPV_OPERAND_TYPE_KERNEL_ENQ_FLAGS; |
| 207 | case OperandKernelProfilingInfo: |
| 208 | return SPV_OPERAND_TYPE_KERNEL_PROFILING_INFO; |
| 209 | case OperandCapability: |
| 210 | return SPV_OPERAND_TYPE_CAPABILITY; |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 211 | |
| 212 | // Used by GroupMemberDecorate |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame^] | 213 | case OperandVariableIdLiteral: |
| 214 | return SPV_OPERAND_TYPE_VARIABLE_ID_LITERAL_INTEGER; |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 215 | |
| 216 | // Used by Switch |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame^] | 217 | case OperandVariableLiteralId: |
| 218 | return SPV_OPERAND_TYPE_VARIABLE_LITERAL_INTEGER_ID; |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 219 | |
| 220 | // These exceptional cases shouldn't occur. |
| 221 | case OperandCount: |
| 222 | default: |
| 223 | break; |
| 224 | } |
| 225 | assert(0 && "Unexpected operand class"); |
| 226 | return SPV_OPERAND_TYPE_NONE; |
| 227 | } |
| 228 | |
Lei Zhang | a94701d | 2015-09-14 10:05:37 -0400 | [diff] [blame] | 229 | } // anonymous namespace |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 230 | |
| 231 | // Finish populating the opcodeTableEntries array. |
| 232 | void spvOpcodeTableInitialize() { |
| 233 | // Compute the operandTypes field for each entry. |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame^] | 234 | for (auto& opcode : opcodeTableEntries) { |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 235 | opcode.numTypes = 0; |
| 236 | // Type ID always comes first, if present. |
| 237 | if (opcode.hasType) |
Andrew Woloszyn | 537e776 | 2015-09-29 11:28:34 -0400 | [diff] [blame] | 238 | opcode.operandTypes[opcode.numTypes++] = SPV_OPERAND_TYPE_TYPE_ID; |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 239 | // Result ID always comes next, if present |
| 240 | if (opcode.hasResult) |
| 241 | opcode.operandTypes[opcode.numTypes++] = SPV_OPERAND_TYPE_RESULT_ID; |
Lei Zhang | a94701d | 2015-09-14 10:05:37 -0400 | [diff] [blame] | 242 | const uint16_t maxNumOperands = |
| 243 | sizeof(opcode.operandTypes) / sizeof(opcode.operandTypes[0]); |
| 244 | const uint16_t maxNumClasses = |
| 245 | sizeof(opcode.operandClass) / sizeof(opcode.operandClass[0]); |
| 246 | for (uint16_t classIndex = 0; |
| 247 | opcode.numTypes < maxNumOperands && classIndex < maxNumClasses; |
| 248 | classIndex++) { |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 249 | const OperandClass operandClass = opcode.operandClass[classIndex]; |
Lei Zhang | a94701d | 2015-09-14 10:05:37 -0400 | [diff] [blame] | 250 | opcode.operandTypes[opcode.numTypes++] = |
| 251 | convertOperandClassToType(opcode.opcode, operandClass); |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 252 | // The OperandNone value is not explicitly represented in the .inc file. |
| 253 | // However, it is the zero value, and is created via implicit value |
| 254 | // initialization. |
| 255 | if (operandClass == OperandNone) { |
| 256 | opcode.numTypes--; |
| 257 | break; |
| 258 | } |
| 259 | } |
| 260 | // We should have written the terminating SPV_OPERAND_TYPE_NONE entry, but |
| 261 | // also without overflowing. |
Lei Zhang | a94701d | 2015-09-14 10:05:37 -0400 | [diff] [blame] | 262 | assert((opcode.numTypes < maxNumOperands) && |
| 263 | "Operand class list is too long. Expand " |
| 264 | "spv_opcode_desc_t.operandClass"); |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 265 | } |
| 266 | opcodeTableInitialized = true; |
| 267 | } |
| 268 | |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame^] | 269 | const char* spvGeneratorStr(uint32_t generator) { |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 270 | switch (generator) { |
| 271 | case SPV_GENERATOR_KHRONOS: |
| 272 | return "Khronos"; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 273 | case SPV_GENERATOR_LUNARG: |
| 274 | return "LunarG"; |
David Neto | 1780fc4 | 2015-10-26 15:43:12 -0400 | [diff] [blame] | 275 | case SPV_GENERATOR_VALVE: |
| 276 | return "Valve"; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 277 | case SPV_GENERATOR_CODEPLAY: |
| 278 | return "Codeplay Software Ltd."; |
David Neto | 1780fc4 | 2015-10-26 15:43:12 -0400 | [diff] [blame] | 279 | case SPV_GENERATOR_NVIDIA: |
| 280 | return "NVIDIA"; |
| 281 | case SPV_GENERATOR_ARM: |
| 282 | return "ARM"; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 283 | default: |
| 284 | return "Unknown"; |
| 285 | } |
| 286 | } |
| 287 | |
Lei Zhang | b36e704 | 2015-10-28 13:40:52 -0400 | [diff] [blame] | 288 | uint32_t spvOpcodeMake(uint16_t wordCount, SpvOp opcode) { |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 289 | return ((uint32_t)opcode) | (((uint32_t)wordCount) << 16); |
| 290 | } |
| 291 | |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame^] | 292 | void spvOpcodeSplit(const uint32_t word, uint16_t* pWordCount, SpvOp* pOpcode) { |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 293 | if (pWordCount) { |
| 294 | *pWordCount = (uint16_t)((0xffff0000 & word) >> 16); |
| 295 | } |
| 296 | if (pOpcode) { |
Lei Zhang | b36e704 | 2015-10-28 13:40:52 -0400 | [diff] [blame] | 297 | *pOpcode = (SpvOp)(0x0000ffff & word); |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 298 | } |
| 299 | } |
| 300 | |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame^] | 301 | spv_result_t spvOpcodeTableGet(spv_opcode_table* pInstTable) { |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 302 | if (!pInstTable) return SPV_ERROR_INVALID_POINTER; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 303 | |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 304 | static spv_opcode_table_t table = { |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 305 | sizeof(opcodeTableEntries) / sizeof(spv_opcode_desc_t), |
| 306 | opcodeTableEntries}; |
| 307 | |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 308 | // TODO(dneto): Consider thread safety of initialization. |
| 309 | // That is, ordering effects of the flag vs. the table updates. |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 310 | if (!opcodeTableInitialized) spvOpcodeTableInitialize(); |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 311 | |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 312 | *pInstTable = &table; |
| 313 | |
| 314 | return SPV_SUCCESS; |
| 315 | } |
| 316 | |
| 317 | spv_result_t spvOpcodeTableNameLookup(const spv_opcode_table table, |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame^] | 318 | const char* name, |
| 319 | spv_opcode_desc* pEntry) { |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 320 | if (!name || !pEntry) return SPV_ERROR_INVALID_POINTER; |
| 321 | if (!table) return SPV_ERROR_INVALID_TABLE; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 322 | |
| 323 | // TODO: This lookup of the Opcode table is suboptimal! Binary sort would be |
| 324 | // preferable but the table requires sorting on the Opcode name, but it's |
| 325 | // static |
| 326 | // const initialized and matches the order of the spec. |
| 327 | const size_t nameLength = strlen(name); |
| 328 | for (uint64_t opcodeIndex = 0; opcodeIndex < table->count; ++opcodeIndex) { |
| 329 | if (nameLength == strlen(table->entries[opcodeIndex].name) && |
| 330 | !strncmp(name, table->entries[opcodeIndex].name, nameLength)) { |
| 331 | // NOTE: Found out Opcode! |
| 332 | *pEntry = &table->entries[opcodeIndex]; |
| 333 | return SPV_SUCCESS; |
| 334 | } |
| 335 | } |
| 336 | |
| 337 | return SPV_ERROR_INVALID_LOOKUP; |
| 338 | } |
| 339 | |
| 340 | spv_result_t spvOpcodeTableValueLookup(const spv_opcode_table table, |
Lei Zhang | b36e704 | 2015-10-28 13:40:52 -0400 | [diff] [blame] | 341 | const SpvOp opcode, |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame^] | 342 | spv_opcode_desc* pEntry) { |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 343 | if (!table) return SPV_ERROR_INVALID_TABLE; |
| 344 | if (!pEntry) return SPV_ERROR_INVALID_POINTER; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 345 | |
| 346 | // TODO: As above this lookup is not optimal. |
| 347 | for (uint64_t opcodeIndex = 0; opcodeIndex < table->count; ++opcodeIndex) { |
| 348 | if (opcode == table->entries[opcodeIndex].opcode) { |
| 349 | // NOTE: Found the Opcode! |
| 350 | *pEntry = &table->entries[opcodeIndex]; |
| 351 | return SPV_SUCCESS; |
| 352 | } |
| 353 | } |
| 354 | |
| 355 | return SPV_ERROR_INVALID_LOOKUP; |
| 356 | } |
| 357 | |
Lei Zhang | dfc5008 | 2015-08-21 11:50:55 -0400 | [diff] [blame] | 358 | int16_t spvOpcodeResultIdIndex(spv_opcode_desc entry) { |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 359 | for (int16_t i = 0; i < entry->numTypes; ++i) { |
Lei Zhang | dfc5008 | 2015-08-21 11:50:55 -0400 | [diff] [blame] | 360 | if (SPV_OPERAND_TYPE_RESULT_ID == entry->operandTypes[i]) return i; |
| 361 | } |
| 362 | return SPV_OPERAND_INVALID_RESULT_ID_INDEX; |
| 363 | } |
| 364 | |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 365 | int32_t spvOpcodeRequiresCapabilities(spv_opcode_desc entry) { |
David Neto | 9db3a53 | 2015-10-07 16:58:38 -0400 | [diff] [blame] | 366 | return entry->capabilities != 0; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 367 | } |
| 368 | |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame^] | 369 | void spvInstructionCopy(const uint32_t* words, const SpvOp opcode, |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 370 | const uint16_t wordCount, const spv_endianness_t endian, |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame^] | 371 | spv_instruction_t* pInst) { |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 372 | pInst->opcode = opcode; |
David Neto | b5dc8fc | 2015-10-06 16:22:00 -0400 | [diff] [blame] | 373 | pInst->words.resize(wordCount); |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 374 | for (uint16_t wordIndex = 0; wordIndex < wordCount; ++wordIndex) { |
| 375 | pInst->words[wordIndex] = spvFixWord(words[wordIndex], endian); |
| 376 | if (!wordIndex) { |
| 377 | uint16_t thisWordCount; |
Lei Zhang | b36e704 | 2015-10-28 13:40:52 -0400 | [diff] [blame] | 378 | SpvOp thisOpcode; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 379 | spvOpcodeSplit(pInst->words[wordIndex], &thisWordCount, &thisOpcode); |
| 380 | assert(opcode == thisOpcode && wordCount == thisWordCount && |
| 381 | "Endianness failed!"); |
| 382 | } |
| 383 | } |
| 384 | } |
| 385 | |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame^] | 386 | const char* spvOpcodeString(const SpvOp opcode) { |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 387 | #define CASE(OPCODE) \ |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame^] | 388 | case Spv##OPCODE: \ |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 389 | return #OPCODE; |
| 390 | switch (opcode) { |
| 391 | CASE(OpNop) |
| 392 | CASE(OpSource) |
| 393 | CASE(OpSourceExtension) |
| 394 | CASE(OpExtension) |
| 395 | CASE(OpExtInstImport) |
| 396 | CASE(OpMemoryModel) |
| 397 | CASE(OpEntryPoint) |
| 398 | CASE(OpExecutionMode) |
| 399 | CASE(OpTypeVoid) |
| 400 | CASE(OpTypeBool) |
| 401 | CASE(OpTypeInt) |
| 402 | CASE(OpTypeFloat) |
| 403 | CASE(OpTypeVector) |
| 404 | CASE(OpTypeMatrix) |
| 405 | CASE(OpTypeSampler) |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 406 | CASE(OpTypeArray) |
| 407 | CASE(OpTypeRuntimeArray) |
| 408 | CASE(OpTypeStruct) |
| 409 | CASE(OpTypeOpaque) |
| 410 | CASE(OpTypePointer) |
| 411 | CASE(OpTypeFunction) |
| 412 | CASE(OpTypeEvent) |
| 413 | CASE(OpTypeDeviceEvent) |
| 414 | CASE(OpTypeReserveId) |
| 415 | CASE(OpTypeQueue) |
| 416 | CASE(OpTypePipe) |
| 417 | CASE(OpConstantTrue) |
| 418 | CASE(OpConstantFalse) |
| 419 | CASE(OpConstant) |
| 420 | CASE(OpConstantComposite) |
| 421 | CASE(OpConstantSampler) |
| 422 | CASE(OpConstantNull) |
| 423 | CASE(OpSpecConstantTrue) |
| 424 | CASE(OpSpecConstantFalse) |
| 425 | CASE(OpSpecConstant) |
| 426 | CASE(OpSpecConstantComposite) |
| 427 | CASE(OpVariable) |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 428 | CASE(OpFunction) |
| 429 | CASE(OpFunctionParameter) |
| 430 | CASE(OpFunctionEnd) |
| 431 | CASE(OpFunctionCall) |
| 432 | CASE(OpExtInst) |
| 433 | CASE(OpUndef) |
| 434 | CASE(OpLoad) |
| 435 | CASE(OpStore) |
| 436 | CASE(OpPhi) |
| 437 | CASE(OpDecorationGroup) |
| 438 | CASE(OpDecorate) |
| 439 | CASE(OpMemberDecorate) |
| 440 | CASE(OpGroupDecorate) |
| 441 | CASE(OpGroupMemberDecorate) |
| 442 | CASE(OpName) |
| 443 | CASE(OpMemberName) |
| 444 | CASE(OpString) |
| 445 | CASE(OpLine) |
| 446 | CASE(OpVectorExtractDynamic) |
| 447 | CASE(OpVectorInsertDynamic) |
| 448 | CASE(OpVectorShuffle) |
| 449 | CASE(OpCompositeConstruct) |
| 450 | CASE(OpCompositeExtract) |
| 451 | CASE(OpCompositeInsert) |
| 452 | CASE(OpCopyObject) |
| 453 | CASE(OpCopyMemory) |
| 454 | CASE(OpCopyMemorySized) |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 455 | CASE(OpAccessChain) |
| 456 | CASE(OpInBoundsAccessChain) |
| 457 | CASE(OpSNegate) |
| 458 | CASE(OpFNegate) |
| 459 | CASE(OpNot) |
| 460 | CASE(OpAny) |
| 461 | CASE(OpAll) |
| 462 | CASE(OpConvertFToU) |
| 463 | CASE(OpConvertFToS) |
| 464 | CASE(OpConvertSToF) |
| 465 | CASE(OpConvertUToF) |
| 466 | CASE(OpUConvert) |
| 467 | CASE(OpSConvert) |
| 468 | CASE(OpFConvert) |
| 469 | CASE(OpConvertPtrToU) |
| 470 | CASE(OpConvertUToPtr) |
| 471 | CASE(OpPtrCastToGeneric) |
| 472 | CASE(OpGenericCastToPtr) |
| 473 | CASE(OpBitcast) |
| 474 | CASE(OpTranspose) |
| 475 | CASE(OpIsNan) |
| 476 | CASE(OpIsInf) |
| 477 | CASE(OpIsFinite) |
| 478 | CASE(OpIsNormal) |
| 479 | CASE(OpSignBitSet) |
| 480 | CASE(OpLessOrGreater) |
| 481 | CASE(OpOrdered) |
| 482 | CASE(OpUnordered) |
| 483 | CASE(OpArrayLength) |
| 484 | CASE(OpIAdd) |
| 485 | CASE(OpFAdd) |
| 486 | CASE(OpISub) |
| 487 | CASE(OpFSub) |
| 488 | CASE(OpIMul) |
| 489 | CASE(OpFMul) |
| 490 | CASE(OpUDiv) |
| 491 | CASE(OpSDiv) |
| 492 | CASE(OpFDiv) |
| 493 | CASE(OpUMod) |
| 494 | CASE(OpSRem) |
| 495 | CASE(OpSMod) |
| 496 | CASE(OpFRem) |
| 497 | CASE(OpFMod) |
| 498 | CASE(OpVectorTimesScalar) |
| 499 | CASE(OpMatrixTimesScalar) |
| 500 | CASE(OpVectorTimesMatrix) |
| 501 | CASE(OpMatrixTimesVector) |
| 502 | CASE(OpMatrixTimesMatrix) |
| 503 | CASE(OpOuterProduct) |
| 504 | CASE(OpDot) |
| 505 | CASE(OpShiftRightLogical) |
| 506 | CASE(OpShiftRightArithmetic) |
| 507 | CASE(OpShiftLeftLogical) |
| 508 | CASE(OpLogicalOr) |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 509 | CASE(OpLogicalAnd) |
| 510 | CASE(OpBitwiseOr) |
| 511 | CASE(OpBitwiseXor) |
| 512 | CASE(OpBitwiseAnd) |
| 513 | CASE(OpSelect) |
| 514 | CASE(OpIEqual) |
| 515 | CASE(OpFOrdEqual) |
| 516 | CASE(OpFUnordEqual) |
| 517 | CASE(OpINotEqual) |
| 518 | CASE(OpFOrdNotEqual) |
| 519 | CASE(OpFUnordNotEqual) |
| 520 | CASE(OpULessThan) |
| 521 | CASE(OpSLessThan) |
| 522 | CASE(OpFOrdLessThan) |
| 523 | CASE(OpFUnordLessThan) |
| 524 | CASE(OpUGreaterThan) |
| 525 | CASE(OpSGreaterThan) |
| 526 | CASE(OpFOrdGreaterThan) |
| 527 | CASE(OpFUnordGreaterThan) |
| 528 | CASE(OpULessThanEqual) |
| 529 | CASE(OpSLessThanEqual) |
| 530 | CASE(OpFOrdLessThanEqual) |
| 531 | CASE(OpFUnordLessThanEqual) |
| 532 | CASE(OpUGreaterThanEqual) |
| 533 | CASE(OpSGreaterThanEqual) |
| 534 | CASE(OpFOrdGreaterThanEqual) |
| 535 | CASE(OpFUnordGreaterThanEqual) |
| 536 | CASE(OpDPdx) |
| 537 | CASE(OpDPdy) |
| 538 | CASE(OpFwidth) |
| 539 | CASE(OpDPdxFine) |
| 540 | CASE(OpDPdyFine) |
| 541 | CASE(OpFwidthFine) |
| 542 | CASE(OpDPdxCoarse) |
| 543 | CASE(OpDPdyCoarse) |
| 544 | CASE(OpFwidthCoarse) |
| 545 | CASE(OpEmitVertex) |
| 546 | CASE(OpEndPrimitive) |
| 547 | CASE(OpEmitStreamVertex) |
| 548 | CASE(OpEndStreamPrimitive) |
| 549 | CASE(OpControlBarrier) |
| 550 | CASE(OpMemoryBarrier) |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 551 | CASE(OpAtomicLoad) |
| 552 | CASE(OpAtomicStore) |
| 553 | CASE(OpAtomicExchange) |
| 554 | CASE(OpAtomicCompareExchange) |
| 555 | CASE(OpAtomicCompareExchangeWeak) |
| 556 | CASE(OpAtomicIIncrement) |
| 557 | CASE(OpAtomicIDecrement) |
| 558 | CASE(OpAtomicIAdd) |
| 559 | CASE(OpAtomicISub) |
| 560 | CASE(OpAtomicUMin) |
| 561 | CASE(OpAtomicUMax) |
| 562 | CASE(OpAtomicAnd) |
| 563 | CASE(OpAtomicOr) |
| 564 | CASE(OpAtomicXor) |
| 565 | CASE(OpLoopMerge) |
| 566 | CASE(OpSelectionMerge) |
| 567 | CASE(OpLabel) |
| 568 | CASE(OpBranch) |
| 569 | CASE(OpBranchConditional) |
| 570 | CASE(OpSwitch) |
| 571 | CASE(OpKill) |
| 572 | CASE(OpReturn) |
| 573 | CASE(OpReturnValue) |
| 574 | CASE(OpUnreachable) |
| 575 | CASE(OpLifetimeStart) |
| 576 | CASE(OpLifetimeStop) |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 577 | CASE(OpAsyncGroupCopy) |
| 578 | CASE(OpWaitGroupEvents) |
| 579 | CASE(OpGroupAll) |
| 580 | CASE(OpGroupAny) |
| 581 | CASE(OpGroupBroadcast) |
| 582 | CASE(OpGroupIAdd) |
| 583 | CASE(OpGroupFAdd) |
| 584 | CASE(OpGroupFMin) |
| 585 | CASE(OpGroupUMin) |
| 586 | CASE(OpGroupSMin) |
| 587 | CASE(OpGroupFMax) |
| 588 | CASE(OpGroupUMax) |
| 589 | CASE(OpGroupSMax) |
| 590 | CASE(OpGenericCastToPtrExplicit) |
| 591 | CASE(OpGenericPtrMemSemantics) |
| 592 | CASE(OpReadPipe) |
| 593 | CASE(OpWritePipe) |
| 594 | CASE(OpReservedReadPipe) |
| 595 | CASE(OpReservedWritePipe) |
| 596 | CASE(OpReserveReadPipePackets) |
| 597 | CASE(OpReserveWritePipePackets) |
| 598 | CASE(OpCommitReadPipe) |
| 599 | CASE(OpCommitWritePipe) |
| 600 | CASE(OpIsValidReserveId) |
| 601 | CASE(OpGetNumPipePackets) |
| 602 | CASE(OpGetMaxPipePackets) |
| 603 | CASE(OpGroupReserveReadPipePackets) |
| 604 | CASE(OpGroupReserveWritePipePackets) |
| 605 | CASE(OpGroupCommitReadPipe) |
| 606 | CASE(OpGroupCommitWritePipe) |
| 607 | CASE(OpEnqueueMarker) |
| 608 | CASE(OpEnqueueKernel) |
| 609 | CASE(OpGetKernelNDrangeSubGroupCount) |
| 610 | CASE(OpGetKernelNDrangeMaxSubGroupSize) |
| 611 | CASE(OpGetKernelWorkGroupSize) |
| 612 | CASE(OpGetKernelPreferredWorkGroupSizeMultiple) |
| 613 | CASE(OpRetainEvent) |
| 614 | CASE(OpReleaseEvent) |
| 615 | CASE(OpCreateUserEvent) |
| 616 | CASE(OpIsValidEvent) |
| 617 | CASE(OpSetUserEventStatus) |
| 618 | CASE(OpCaptureEventProfilingInfo) |
| 619 | CASE(OpGetDefaultQueue) |
| 620 | CASE(OpBuildNDRange) |
| 621 | default: |
| 622 | assert(0 && "Unreachable!"); |
| 623 | } |
| 624 | #undef CASE |
| 625 | return "unknown"; |
| 626 | } |
| 627 | |
Lei Zhang | b36e704 | 2015-10-28 13:40:52 -0400 | [diff] [blame] | 628 | int32_t spvOpcodeIsType(const SpvOp opcode) { |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 629 | switch (opcode) { |
Lei Zhang | b36e704 | 2015-10-28 13:40:52 -0400 | [diff] [blame] | 630 | case SpvOpTypeVoid: |
| 631 | case SpvOpTypeBool: |
| 632 | case SpvOpTypeInt: |
| 633 | case SpvOpTypeFloat: |
| 634 | case SpvOpTypeVector: |
| 635 | case SpvOpTypeMatrix: |
| 636 | case SpvOpTypeSampler: |
| 637 | case SpvOpTypeSampledImage: |
| 638 | case SpvOpTypeArray: |
| 639 | case SpvOpTypeRuntimeArray: |
| 640 | case SpvOpTypeStruct: |
| 641 | case SpvOpTypeOpaque: |
| 642 | case SpvOpTypePointer: |
| 643 | case SpvOpTypeFunction: |
| 644 | case SpvOpTypeEvent: |
| 645 | case SpvOpTypeDeviceEvent: |
| 646 | case SpvOpTypeReserveId: |
| 647 | case SpvOpTypeQueue: |
| 648 | case SpvOpTypePipe: |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 649 | return true; |
| 650 | default: |
| 651 | return false; |
| 652 | } |
| 653 | } |
| 654 | |
Lei Zhang | b36e704 | 2015-10-28 13:40:52 -0400 | [diff] [blame] | 655 | int32_t spvOpcodeIsScalarType(const SpvOp opcode) { |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 656 | switch (opcode) { |
Lei Zhang | b36e704 | 2015-10-28 13:40:52 -0400 | [diff] [blame] | 657 | case SpvOpTypeInt: |
| 658 | case SpvOpTypeFloat: |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 659 | return true; |
| 660 | default: |
| 661 | return false; |
| 662 | } |
| 663 | } |
| 664 | |
Lei Zhang | b36e704 | 2015-10-28 13:40:52 -0400 | [diff] [blame] | 665 | int32_t spvOpcodeIsConstant(const SpvOp opcode) { |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 666 | switch (opcode) { |
Lei Zhang | b36e704 | 2015-10-28 13:40:52 -0400 | [diff] [blame] | 667 | case SpvOpConstantTrue: |
| 668 | case SpvOpConstantFalse: |
| 669 | case SpvOpConstant: |
| 670 | case SpvOpConstantComposite: |
| 671 | case SpvOpConstantSampler: |
| 672 | // case SpvOpConstantNull: |
| 673 | case SpvOpConstantNull: |
| 674 | case SpvOpSpecConstantTrue: |
| 675 | case SpvOpSpecConstantFalse: |
| 676 | case SpvOpSpecConstant: |
| 677 | case SpvOpSpecConstantComposite: |
| 678 | // case SpvOpSpecConstantOp: |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 679 | return true; |
| 680 | default: |
| 681 | return false; |
| 682 | } |
| 683 | } |
| 684 | |
Lei Zhang | b36e704 | 2015-10-28 13:40:52 -0400 | [diff] [blame] | 685 | int32_t spvOpcodeIsComposite(const SpvOp opcode) { |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 686 | switch (opcode) { |
Lei Zhang | b36e704 | 2015-10-28 13:40:52 -0400 | [diff] [blame] | 687 | case SpvOpTypeVector: |
| 688 | case SpvOpTypeMatrix: |
| 689 | case SpvOpTypeArray: |
| 690 | case SpvOpTypeStruct: |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 691 | return true; |
| 692 | default: |
| 693 | return false; |
| 694 | } |
| 695 | } |
| 696 | |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame^] | 697 | int32_t spvOpcodeAreTypesEqual(const spv_instruction_t* pTypeInst0, |
| 698 | const spv_instruction_t* pTypeInst1) { |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 699 | if (pTypeInst0->opcode != pTypeInst1->opcode) return false; |
| 700 | if (pTypeInst0->words[1] != pTypeInst1->words[1]) return false; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 701 | return true; |
| 702 | } |
| 703 | |
Lei Zhang | b36e704 | 2015-10-28 13:40:52 -0400 | [diff] [blame] | 704 | int32_t spvOpcodeIsPointer(const SpvOp opcode) { |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 705 | switch (opcode) { |
Lei Zhang | b36e704 | 2015-10-28 13:40:52 -0400 | [diff] [blame] | 706 | case SpvOpVariable: |
| 707 | case SpvOpAccessChain: |
| 708 | case SpvOpInBoundsAccessChain: |
| 709 | case SpvOpFunctionParameter: |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 710 | return true; |
| 711 | default: |
| 712 | return false; |
| 713 | } |
| 714 | } |
| 715 | |
Lei Zhang | b36e704 | 2015-10-28 13:40:52 -0400 | [diff] [blame] | 716 | int32_t spvOpcodeIsObject(const SpvOp opcode) { |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 717 | switch (opcode) { |
Lei Zhang | b36e704 | 2015-10-28 13:40:52 -0400 | [diff] [blame] | 718 | case SpvOpConstantTrue: |
| 719 | case SpvOpConstantFalse: |
| 720 | case SpvOpConstant: |
| 721 | case SpvOpConstantComposite: |
| 722 | // TODO: case SpvOpConstantSampler: |
| 723 | case SpvOpConstantNull: |
| 724 | case SpvOpSpecConstantTrue: |
| 725 | case SpvOpSpecConstantFalse: |
| 726 | case SpvOpSpecConstant: |
| 727 | case SpvOpSpecConstantComposite: |
| 728 | // TODO: case SpvOpSpecConstantOp: |
| 729 | case SpvOpVariable: |
| 730 | case SpvOpAccessChain: |
| 731 | case SpvOpInBoundsAccessChain: |
| 732 | case SpvOpConvertFToU: |
| 733 | case SpvOpConvertFToS: |
| 734 | case SpvOpConvertSToF: |
| 735 | case SpvOpConvertUToF: |
| 736 | case SpvOpUConvert: |
| 737 | case SpvOpSConvert: |
| 738 | case SpvOpFConvert: |
| 739 | case SpvOpConvertPtrToU: |
| 740 | // TODO: case SpvOpConvertUToPtr: |
| 741 | case SpvOpPtrCastToGeneric: |
| 742 | // TODO: case SpvOpGenericCastToPtr: |
| 743 | case SpvOpBitcast: |
| 744 | // TODO: case SpvOpGenericCastToPtrExplicit: |
| 745 | case SpvOpSatConvertSToU: |
| 746 | case SpvOpSatConvertUToS: |
| 747 | case SpvOpVectorExtractDynamic: |
| 748 | case SpvOpCompositeConstruct: |
| 749 | case SpvOpCompositeExtract: |
| 750 | case SpvOpCopyObject: |
| 751 | case SpvOpTranspose: |
| 752 | case SpvOpSNegate: |
| 753 | case SpvOpFNegate: |
| 754 | case SpvOpNot: |
| 755 | case SpvOpIAdd: |
| 756 | case SpvOpFAdd: |
| 757 | case SpvOpISub: |
| 758 | case SpvOpFSub: |
| 759 | case SpvOpIMul: |
| 760 | case SpvOpFMul: |
| 761 | case SpvOpUDiv: |
| 762 | case SpvOpSDiv: |
| 763 | case SpvOpFDiv: |
| 764 | case SpvOpUMod: |
| 765 | case SpvOpSRem: |
| 766 | case SpvOpSMod: |
| 767 | case SpvOpVectorTimesScalar: |
| 768 | case SpvOpMatrixTimesScalar: |
| 769 | case SpvOpVectorTimesMatrix: |
| 770 | case SpvOpMatrixTimesVector: |
| 771 | case SpvOpMatrixTimesMatrix: |
| 772 | case SpvOpOuterProduct: |
| 773 | case SpvOpDot: |
| 774 | case SpvOpShiftRightLogical: |
| 775 | case SpvOpShiftRightArithmetic: |
| 776 | case SpvOpShiftLeftLogical: |
| 777 | case SpvOpBitwiseOr: |
| 778 | case SpvOpBitwiseXor: |
| 779 | case SpvOpBitwiseAnd: |
| 780 | case SpvOpAny: |
| 781 | case SpvOpAll: |
| 782 | case SpvOpIsNan: |
| 783 | case SpvOpIsInf: |
| 784 | case SpvOpIsFinite: |
| 785 | case SpvOpIsNormal: |
| 786 | case SpvOpSignBitSet: |
| 787 | case SpvOpLessOrGreater: |
| 788 | case SpvOpOrdered: |
| 789 | case SpvOpUnordered: |
| 790 | case SpvOpLogicalOr: |
| 791 | case SpvOpLogicalAnd: |
| 792 | case SpvOpSelect: |
| 793 | case SpvOpIEqual: |
| 794 | case SpvOpFOrdEqual: |
| 795 | case SpvOpFUnordEqual: |
| 796 | case SpvOpINotEqual: |
| 797 | case SpvOpFOrdNotEqual: |
| 798 | case SpvOpFUnordNotEqual: |
| 799 | case SpvOpULessThan: |
| 800 | case SpvOpSLessThan: |
| 801 | case SpvOpFOrdLessThan: |
| 802 | case SpvOpFUnordLessThan: |
| 803 | case SpvOpUGreaterThan: |
| 804 | case SpvOpSGreaterThan: |
| 805 | case SpvOpFOrdGreaterThan: |
| 806 | case SpvOpFUnordGreaterThan: |
| 807 | case SpvOpULessThanEqual: |
| 808 | case SpvOpSLessThanEqual: |
| 809 | case SpvOpFOrdLessThanEqual: |
| 810 | case SpvOpFUnordLessThanEqual: |
| 811 | case SpvOpUGreaterThanEqual: |
| 812 | case SpvOpSGreaterThanEqual: |
| 813 | case SpvOpFOrdGreaterThanEqual: |
| 814 | case SpvOpFUnordGreaterThanEqual: |
| 815 | case SpvOpDPdx: |
| 816 | case SpvOpDPdy: |
| 817 | case SpvOpFwidth: |
| 818 | case SpvOpDPdxFine: |
| 819 | case SpvOpDPdyFine: |
| 820 | case SpvOpFwidthFine: |
| 821 | case SpvOpDPdxCoarse: |
| 822 | case SpvOpDPdyCoarse: |
| 823 | case SpvOpFwidthCoarse: |
| 824 | case SpvOpReturnValue: |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 825 | return true; |
| 826 | default: |
| 827 | return false; |
| 828 | } |
| 829 | } |
| 830 | |
Lei Zhang | b36e704 | 2015-10-28 13:40:52 -0400 | [diff] [blame] | 831 | int32_t spvOpcodeIsBasicTypeNullable(SpvOp opcode) { |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 832 | switch (opcode) { |
Lei Zhang | b36e704 | 2015-10-28 13:40:52 -0400 | [diff] [blame] | 833 | case SpvOpTypeBool: |
| 834 | case SpvOpTypeInt: |
| 835 | case SpvOpTypeFloat: |
| 836 | case SpvOpTypePointer: |
| 837 | case SpvOpTypeEvent: |
| 838 | case SpvOpTypeDeviceEvent: |
| 839 | case SpvOpTypeReserveId: |
| 840 | case SpvOpTypeQueue: |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 841 | return true; |
| 842 | default: |
| 843 | return false; |
| 844 | } |
| 845 | } |
| 846 | |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame^] | 847 | int32_t spvInstructionIsInBasicBlock(const spv_instruction_t* pFirstInst, |
| 848 | const spv_instruction_t* pInst) { |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 849 | while (pFirstInst != pInst) { |
Lei Zhang | b36e704 | 2015-10-28 13:40:52 -0400 | [diff] [blame] | 850 | if (SpvOpFunction == pInst->opcode) break; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 851 | pInst--; |
| 852 | } |
Lei Zhang | b36e704 | 2015-10-28 13:40:52 -0400 | [diff] [blame] | 853 | if (SpvOpFunction != pInst->opcode) return false; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 854 | return true; |
| 855 | } |
| 856 | |
Lei Zhang | b36e704 | 2015-10-28 13:40:52 -0400 | [diff] [blame] | 857 | int32_t spvOpcodeIsValue(SpvOp opcode) { |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 858 | if (spvOpcodeIsPointer(opcode)) return true; |
| 859 | if (spvOpcodeIsConstant(opcode)) return true; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 860 | switch (opcode) { |
Lei Zhang | b36e704 | 2015-10-28 13:40:52 -0400 | [diff] [blame] | 861 | case SpvOpLoad: |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 862 | // TODO: Other Opcode's resulting in a value |
| 863 | return true; |
| 864 | default: |
| 865 | return false; |
| 866 | } |
| 867 | } |
Andrew Woloszyn | 537e776 | 2015-09-29 11:28:34 -0400 | [diff] [blame] | 868 | |
Lei Zhang | b36e704 | 2015-10-28 13:40:52 -0400 | [diff] [blame] | 869 | int32_t spvOpcodeGeneratesType(SpvOp op) { |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame^] | 870 | switch (op) { |
Lei Zhang | b36e704 | 2015-10-28 13:40:52 -0400 | [diff] [blame] | 871 | case SpvOpTypeVoid: |
| 872 | case SpvOpTypeBool: |
| 873 | case SpvOpTypeInt: |
| 874 | case SpvOpTypeFloat: |
| 875 | case SpvOpTypeVector: |
| 876 | case SpvOpTypeMatrix: |
| 877 | case SpvOpTypeImage: |
| 878 | case SpvOpTypeSampler: |
| 879 | case SpvOpTypeSampledImage: |
| 880 | case SpvOpTypeArray: |
| 881 | case SpvOpTypeRuntimeArray: |
| 882 | case SpvOpTypeStruct: |
| 883 | case SpvOpTypeOpaque: |
| 884 | case SpvOpTypePointer: |
| 885 | case SpvOpTypeFunction: |
| 886 | case SpvOpTypeEvent: |
| 887 | case SpvOpTypeDeviceEvent: |
| 888 | case SpvOpTypeReserveId: |
| 889 | case SpvOpTypeQueue: |
| 890 | case SpvOpTypePipe: |
| 891 | case SpvOpTypeForwardPointer: |
Andrew Woloszyn | 537e776 | 2015-09-29 11:28:34 -0400 | [diff] [blame] | 892 | return true; |
| 893 | default:; |
| 894 | } |
| 895 | return 0; |
| 896 | } |