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 | |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 27 | #include "opcode.h" |
| 28 | |
| 29 | #include <assert.h> |
| 30 | #include <string.h> |
| 31 | |
Lei Zhang | 972788b | 2015-11-12 13:48:30 -0500 | [diff] [blame] | 32 | #include <cstdlib> |
| 33 | |
Lei Zhang | 923f6c1 | 2015-11-11 12:45:23 -0500 | [diff] [blame] | 34 | #include "endian.h" |
| 35 | #include "instruction.h" |
| 36 | #include "libspirv/libspirv.h" |
Lei Zhang | aa056cd | 2015-11-11 14:24:04 -0500 | [diff] [blame] | 37 | #include "spirv_constant.h" |
Lei Zhang | 4ac601a | 2015-11-11 17:29:23 -0500 | [diff] [blame] | 38 | #include "spirv_operands.h" |
Lei Zhang | 923f6c1 | 2015-11-11 12:45:23 -0500 | [diff] [blame] | 39 | |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 40 | namespace { |
| 41 | |
| 42 | // Descriptions of each opcode. Each entry describes the format of the |
| 43 | // instruction that follows a particular opcode. |
| 44 | // |
| 45 | // Most fields are initialized statically by including an automatically |
| 46 | // generated file. |
| 47 | // The operandTypes fields are initialized during spvOpcodeInitialize(). |
| 48 | // |
| 49 | // TODO(dneto): Some of the macros are quite unreadable. We could make |
| 50 | // good use of constexpr functions, but some compilers don't support that yet. |
Lei Zhang | 972788b | 2015-11-12 13:48:30 -0500 | [diff] [blame] | 51 | const spv_opcode_desc_t opcodeTableEntries[] = { |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame] | 52 | #define EmptyList \ |
| 53 | {} |
| 54 | #define List(...) \ |
| 55 | { __VA_ARGS__ } |
Lei Zhang | b36e704 | 2015-10-28 13:40:52 -0400 | [diff] [blame] | 56 | #define Capability(X) SPV_CAPABILITY_AS_MASK(SpvCapability##X) |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame] | 57 | #define Capability2(X, Y) Capability(X) | Capability(Y) |
| 58 | #define SpvCapabilityNone \ |
| 59 | 0 // Needed so Capability(None) still expands to valid syntax. |
David Neto | 201caf7 | 2015-11-04 17:38:17 -0500 | [diff] [blame] | 60 | #define Instruction(Name, HasResult, HasType, NumLogicalOperands, \ |
| 61 | NumCapabilities, CapabilityRequired, IsVariable, \ |
| 62 | LogicalArgsList) \ |
| 63 | {#Name, SpvOp##Name, (NumCapabilities) ? (CapabilityRequired) : 0, \ |
| 64 | 0, {}, /* Filled in later. Operand list, including \ |
| 65 | result id and type id, if needed */ \ |
| 66 | HasResult, HasType, LogicalArgsList}, |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 67 | #include "opcode.inc" |
| 68 | #undef EmptyList |
| 69 | #undef List |
| 70 | #undef Capability |
Dejan Mircevski | 205408b | 2015-09-30 16:42:34 -0400 | [diff] [blame] | 71 | #undef Capability2 |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 72 | #undef CapabilityNone |
| 73 | #undef Instruction |
| 74 | }; |
| 75 | |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 76 | // Opcode API |
| 77 | |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 78 | // Converts the given operand class enum (from the SPIR-V document generation |
David Neto | 0f166be | 2015-11-11 01:56:49 -0500 | [diff] [blame] | 79 | // logic) to the operand type required by the parser. The SPV_OPERAND_TYPE_NONE |
| 80 | // value indicates there is no current operand and no further operands. |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 81 | // This only applies to logical operands. |
Lei Zhang | b36e704 | 2015-10-28 13:40:52 -0400 | [diff] [blame] | 82 | spv_operand_type_t convertOperandClassToType(SpvOp opcode, |
| 83 | OperandClass operandClass) { |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 84 | // The spec document generator uses OptionalOperandLiteral for several kinds |
| 85 | // of repeating values. Our parser needs more specific information about |
| 86 | // what is being repeated. |
| 87 | if (operandClass == OperandOptionalLiteral) { |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 88 | switch (opcode) { |
Lei Zhang | b36e704 | 2015-10-28 13:40:52 -0400 | [diff] [blame] | 89 | case SpvOpLoad: |
| 90 | case SpvOpStore: |
| 91 | case SpvOpCopyMemory: |
| 92 | case SpvOpCopyMemorySized: |
David Neto | 3fca4cd | 2015-09-17 17:39:45 -0400 | [diff] [blame] | 93 | // Expect an optional mask. When the Aligned bit is set in the mask, |
| 94 | // we will later add the expectation of a literal number operand. |
| 95 | return SPV_OPERAND_TYPE_OPTIONAL_MEMORY_ACCESS; |
Lei Zhang | b36e704 | 2015-10-28 13:40:52 -0400 | [diff] [blame] | 96 | case SpvOpExecutionMode: |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 97 | return SPV_OPERAND_TYPE_VARIABLE_EXECUTION_MODE; |
| 98 | default: |
| 99 | break; |
| 100 | } |
Lei Zhang | b41d150 | 2015-09-14 15:22:23 -0400 | [diff] [blame] | 101 | } else if (operandClass == OperandVariableLiterals) { |
David Neto | 201caf7 | 2015-11-04 17:38:17 -0500 | [diff] [blame] | 102 | if (opcode == SpvOpConstant || opcode == SpvOpSpecConstant) { |
| 103 | // The number type is determined by the type Id operand. |
| 104 | return SPV_OPERAND_TYPE_TYPED_LITERAL_NUMBER; |
| 105 | } |
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: |
David Neto | 0f166be | 2015-11-11 01:56:49 -0500 | [diff] [blame] | 118 | if (opcode == SpvOpSpecConstantOp) { |
| 119 | // These are the operands to the specialization constant opcode. |
| 120 | // The assembler and binary parser set up the extra Id and literal |
| 121 | // arguments when processing the opcode operand. So don't add |
| 122 | // an operand type for them here. |
| 123 | return SPV_OPERAND_TYPE_NONE; |
| 124 | } |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame] | 125 | return SPV_OPERAND_TYPE_VARIABLE_ID; |
David Neto | 561dc4e | 2015-09-25 14:23:29 -0400 | [diff] [blame] | 126 | // The spec only uses OptionalLiteral for an optional literal number. |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame] | 127 | case OperandOptionalLiteral: |
| 128 | return SPV_OPERAND_TYPE_OPTIONAL_LITERAL_INTEGER; |
| 129 | case OperandOptionalLiteralString: |
| 130 | return SPV_OPERAND_TYPE_OPTIONAL_LITERAL_STRING; |
David Neto | 561dc4e | 2015-09-25 14:23:29 -0400 | [diff] [blame] | 131 | // This is only used for sequences of literal numbers. |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame] | 132 | case OperandVariableLiterals: |
| 133 | return SPV_OPERAND_TYPE_VARIABLE_LITERAL_INTEGER; |
David Neto | 445ce44 | 2015-10-15 15:22:06 -0400 | [diff] [blame] | 134 | case OperandLiteralNumber: |
Lei Zhang | b36e704 | 2015-10-28 13:40:52 -0400 | [diff] [blame] | 135 | if (opcode == SpvOpExtInst) { |
David Neto | 445ce44 | 2015-10-15 15:22:06 -0400 | [diff] [blame] | 136 | // We use a special operand type for the extension instruction number. |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame] | 137 | // For now, we assume there is only one LiteraNumber argument to |
| 138 | // OpExtInst, and it is the extension instruction argument. |
David Neto | 445ce44 | 2015-10-15 15:22:06 -0400 | [diff] [blame] | 139 | // See the ExtInst entry in opcode.inc |
| 140 | // TODO(dneto): Use a function to confirm the assumption, and to verify |
| 141 | // that the index into the operandClass is 1, as expected. |
| 142 | return SPV_OPERAND_TYPE_EXTENSION_INSTRUCTION_NUMBER; |
David Neto | 0f166be | 2015-11-11 01:56:49 -0500 | [diff] [blame] | 143 | } else if (opcode == SpvOpSpecConstantOp) { |
| 144 | // Use a special operand type for the opcode operand, so we can |
| 145 | // use mnemonic names instead of the numbers. For example, the |
| 146 | // assembler should accept "IAdd" instead of the numeric value of |
| 147 | // SpvOpIAdd. |
| 148 | return SPV_OPERAND_TYPE_SPEC_CONSTANT_OP_NUMBER; |
David Neto | 445ce44 | 2015-10-15 15:22:06 -0400 | [diff] [blame] | 149 | } |
| 150 | return SPV_OPERAND_TYPE_LITERAL_INTEGER; |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame] | 151 | case OperandLiteralString: |
| 152 | return SPV_OPERAND_TYPE_LITERAL_STRING; |
| 153 | case OperandSource: |
| 154 | return SPV_OPERAND_TYPE_SOURCE_LANGUAGE; |
| 155 | case OperandExecutionModel: |
| 156 | return SPV_OPERAND_TYPE_EXECUTION_MODEL; |
| 157 | case OperandAddressing: |
| 158 | return SPV_OPERAND_TYPE_ADDRESSING_MODEL; |
| 159 | case OperandMemory: |
| 160 | return SPV_OPERAND_TYPE_MEMORY_MODEL; |
| 161 | case OperandExecutionMode: |
| 162 | return SPV_OPERAND_TYPE_EXECUTION_MODE; |
| 163 | case OperandStorage: |
| 164 | return SPV_OPERAND_TYPE_STORAGE_CLASS; |
| 165 | case OperandDimensionality: |
| 166 | return SPV_OPERAND_TYPE_DIMENSIONALITY; |
| 167 | case OperandSamplerAddressingMode: |
| 168 | return SPV_OPERAND_TYPE_SAMPLER_ADDRESSING_MODE; |
| 169 | case OperandSamplerFilterMode: |
| 170 | return SPV_OPERAND_TYPE_SAMPLER_FILTER_MODE; |
| 171 | case OperandSamplerImageFormat: |
| 172 | return SPV_OPERAND_TYPE_SAMPLER_IMAGE_FORMAT; |
David Neto | 7cefb23 | 2015-09-28 15:33:49 -0400 | [diff] [blame] | 173 | case OperandImageChannelOrder: |
| 174 | // This is only used to describe the value generated by OpImageQueryOrder. |
| 175 | // It is not used as an operand. |
| 176 | break; |
| 177 | case OperandImageChannelDataType: |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame] | 178 | // This is only used to describe the value generated by |
| 179 | // OpImageQueryFormat. It is not used as an operand. |
David Neto | 7cefb23 | 2015-09-28 15:33:49 -0400 | [diff] [blame] | 180 | break; |
| 181 | case OperandImageOperands: |
Dejan Mircevski | f8e091a | 2015-10-13 14:41:31 -0400 | [diff] [blame] | 182 | // This is not used in opcode.inc. It only exists to generate the |
| 183 | // corresponding spec section. In parsing, image operands meld into the |
| 184 | // OperandOptionalImage case. |
David Neto | 7cefb23 | 2015-09-28 15:33:49 -0400 | [diff] [blame] | 185 | break; |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame] | 186 | case OperandFPFastMath: |
| 187 | return SPV_OPERAND_TYPE_FP_FAST_MATH_MODE; |
| 188 | case OperandFPRoundingMode: |
| 189 | return SPV_OPERAND_TYPE_FP_ROUNDING_MODE; |
| 190 | case OperandLinkageType: |
| 191 | return SPV_OPERAND_TYPE_LINKAGE_TYPE; |
| 192 | case OperandAccessQualifier: |
| 193 | return SPV_OPERAND_TYPE_ACCESS_QUALIFIER; |
| 194 | case OperandFuncParamAttr: |
| 195 | return SPV_OPERAND_TYPE_FUNCTION_PARAMETER_ATTRIBUTE; |
| 196 | case OperandDecoration: |
| 197 | return SPV_OPERAND_TYPE_DECORATION; |
| 198 | case OperandBuiltIn: |
| 199 | return SPV_OPERAND_TYPE_BUILT_IN; |
| 200 | case OperandSelect: |
| 201 | return SPV_OPERAND_TYPE_SELECTION_CONTROL; |
| 202 | case OperandLoop: |
| 203 | return SPV_OPERAND_TYPE_LOOP_CONTROL; |
| 204 | case OperandFunction: |
| 205 | return SPV_OPERAND_TYPE_FUNCTION_CONTROL; |
| 206 | case OperandMemorySemantics: |
| 207 | return SPV_OPERAND_TYPE_MEMORY_SEMANTICS; |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 208 | case OperandMemoryAccess: |
David Neto | 7cefb23 | 2015-09-28 15:33:49 -0400 | [diff] [blame] | 209 | // 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] | 210 | // We expect that it will become SPV_OPERAND_TYPE_OPTIONAL_MEMORY_ACCESS, |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 211 | // and we can remove the special casing above for memory operation |
| 212 | // instructions. |
| 213 | break; |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame] | 214 | case OperandScope: |
| 215 | return SPV_OPERAND_TYPE_EXECUTION_SCOPE; |
| 216 | case OperandGroupOperation: |
| 217 | return SPV_OPERAND_TYPE_GROUP_OPERATION; |
| 218 | case OperandKernelEnqueueFlags: |
| 219 | return SPV_OPERAND_TYPE_KERNEL_ENQ_FLAGS; |
| 220 | case OperandKernelProfilingInfo: |
| 221 | return SPV_OPERAND_TYPE_KERNEL_PROFILING_INFO; |
| 222 | case OperandCapability: |
| 223 | return SPV_OPERAND_TYPE_CAPABILITY; |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 224 | |
| 225 | // Used by GroupMemberDecorate |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame] | 226 | case OperandVariableIdLiteral: |
| 227 | return SPV_OPERAND_TYPE_VARIABLE_ID_LITERAL_INTEGER; |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 228 | |
| 229 | // Used by Switch |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame] | 230 | case OperandVariableLiteralId: |
| 231 | return SPV_OPERAND_TYPE_VARIABLE_LITERAL_INTEGER_ID; |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 232 | |
| 233 | // These exceptional cases shouldn't occur. |
| 234 | case OperandCount: |
| 235 | default: |
| 236 | break; |
| 237 | } |
| 238 | assert(0 && "Unexpected operand class"); |
| 239 | return SPV_OPERAND_TYPE_NONE; |
| 240 | } |
| 241 | |
Lei Zhang | a94701d | 2015-09-14 10:05:37 -0400 | [diff] [blame] | 242 | } // anonymous namespace |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 243 | |
| 244 | // Finish populating the opcodeTableEntries array. |
Lei Zhang | 972788b | 2015-11-12 13:48:30 -0500 | [diff] [blame] | 245 | void spvOpcodeTableInitialize(spv_opcode_desc_t* entries, |
| 246 | uint32_t num_entries) { |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 247 | // Compute the operandTypes field for each entry. |
Lei Zhang | 972788b | 2015-11-12 13:48:30 -0500 | [diff] [blame] | 248 | for (uint32_t i = 0; i < num_entries; ++i) { |
| 249 | spv_opcode_desc_t& opcode = entries[i]; |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 250 | opcode.numTypes = 0; |
| 251 | // Type ID always comes first, if present. |
| 252 | if (opcode.hasType) |
Andrew Woloszyn | 537e776 | 2015-09-29 11:28:34 -0400 | [diff] [blame] | 253 | opcode.operandTypes[opcode.numTypes++] = SPV_OPERAND_TYPE_TYPE_ID; |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 254 | // Result ID always comes next, if present |
| 255 | if (opcode.hasResult) |
| 256 | opcode.operandTypes[opcode.numTypes++] = SPV_OPERAND_TYPE_RESULT_ID; |
Lei Zhang | a94701d | 2015-09-14 10:05:37 -0400 | [diff] [blame] | 257 | const uint16_t maxNumOperands = |
| 258 | sizeof(opcode.operandTypes) / sizeof(opcode.operandTypes[0]); |
| 259 | const uint16_t maxNumClasses = |
| 260 | sizeof(opcode.operandClass) / sizeof(opcode.operandClass[0]); |
| 261 | for (uint16_t classIndex = 0; |
| 262 | opcode.numTypes < maxNumOperands && classIndex < maxNumClasses; |
| 263 | classIndex++) { |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 264 | const OperandClass operandClass = opcode.operandClass[classIndex]; |
David Neto | 0f166be | 2015-11-11 01:56:49 -0500 | [diff] [blame] | 265 | const auto operandType = |
Lei Zhang | a94701d | 2015-09-14 10:05:37 -0400 | [diff] [blame] | 266 | convertOperandClassToType(opcode.opcode, operandClass); |
David Neto | 0f166be | 2015-11-11 01:56:49 -0500 | [diff] [blame] | 267 | opcode.operandTypes[opcode.numTypes++] = operandType; |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 268 | // The OperandNone value is not explicitly represented in the .inc file. |
| 269 | // However, it is the zero value, and is created via implicit value |
David Neto | 0f166be | 2015-11-11 01:56:49 -0500 | [diff] [blame] | 270 | // initialization. It converts to SPV_OPERAND_TYPE_NONE. |
| 271 | // The SPV_OPERAND_TYPE_NONE operand type indicates no current or futher |
| 272 | // operands. |
| 273 | if (operandType == SPV_OPERAND_TYPE_NONE) { |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 274 | opcode.numTypes--; |
| 275 | break; |
| 276 | } |
| 277 | } |
David Neto | 0f166be | 2015-11-11 01:56:49 -0500 | [diff] [blame] | 278 | |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 279 | // We should have written the terminating SPV_OPERAND_TYPE_NONE entry, but |
| 280 | // also without overflowing. |
Lei Zhang | a94701d | 2015-09-14 10:05:37 -0400 | [diff] [blame] | 281 | assert((opcode.numTypes < maxNumOperands) && |
| 282 | "Operand class list is too long. Expand " |
| 283 | "spv_opcode_desc_t.operandClass"); |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 284 | } |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 285 | } |
| 286 | |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame] | 287 | const char* spvGeneratorStr(uint32_t generator) { |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 288 | switch (generator) { |
| 289 | case SPV_GENERATOR_KHRONOS: |
| 290 | return "Khronos"; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 291 | case SPV_GENERATOR_LUNARG: |
| 292 | return "LunarG"; |
David Neto | 1780fc4 | 2015-10-26 15:43:12 -0400 | [diff] [blame] | 293 | case SPV_GENERATOR_VALVE: |
| 294 | return "Valve"; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 295 | case SPV_GENERATOR_CODEPLAY: |
| 296 | return "Codeplay Software Ltd."; |
David Neto | 1780fc4 | 2015-10-26 15:43:12 -0400 | [diff] [blame] | 297 | case SPV_GENERATOR_NVIDIA: |
| 298 | return "NVIDIA"; |
| 299 | case SPV_GENERATOR_ARM: |
| 300 | return "ARM"; |
David Neto | 14b93e4 | 2015-11-12 18:33:47 -0500 | [diff] [blame^] | 301 | case SPV_GENERATOR_KHRONOS_LLVM_TRANSLATOR: |
| 302 | return "Khronos LLVM/SPIR-V Translator"; |
| 303 | case SPV_GENERATOR_KHRONOS_ASSEMBLER: |
| 304 | return "Khronos SPIR-V Tools Assembler"; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 305 | default: |
| 306 | return "Unknown"; |
| 307 | } |
| 308 | } |
| 309 | |
Lei Zhang | b36e704 | 2015-10-28 13:40:52 -0400 | [diff] [blame] | 310 | uint32_t spvOpcodeMake(uint16_t wordCount, SpvOp opcode) { |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 311 | return ((uint32_t)opcode) | (((uint32_t)wordCount) << 16); |
| 312 | } |
| 313 | |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame] | 314 | void spvOpcodeSplit(const uint32_t word, uint16_t* pWordCount, SpvOp* pOpcode) { |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 315 | if (pWordCount) { |
| 316 | *pWordCount = (uint16_t)((0xffff0000 & word) >> 16); |
| 317 | } |
| 318 | if (pOpcode) { |
Lei Zhang | b36e704 | 2015-10-28 13:40:52 -0400 | [diff] [blame] | 319 | *pOpcode = (SpvOp)(0x0000ffff & word); |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 320 | } |
| 321 | } |
| 322 | |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame] | 323 | spv_result_t spvOpcodeTableGet(spv_opcode_table* pInstTable) { |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 324 | if (!pInstTable) return SPV_ERROR_INVALID_POINTER; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 325 | |
Lei Zhang | 972788b | 2015-11-12 13:48:30 -0500 | [diff] [blame] | 326 | const uint32_t size = sizeof(opcodeTableEntries); |
| 327 | spv_opcode_desc_t* copied_entries = |
| 328 | static_cast<spv_opcode_desc_t*>(::malloc(size)); |
| 329 | if (!copied_entries) return SPV_ERROR_OUT_OF_MEMORY; |
| 330 | ::memcpy(copied_entries, opcodeTableEntries, size); |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 331 | |
Lei Zhang | 972788b | 2015-11-12 13:48:30 -0500 | [diff] [blame] | 332 | const uint32_t count = sizeof(opcodeTableEntries) / sizeof(spv_opcode_desc_t); |
| 333 | spv_opcode_table_t* table = new spv_opcode_table_t{count, copied_entries}; |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 334 | |
Lei Zhang | 972788b | 2015-11-12 13:48:30 -0500 | [diff] [blame] | 335 | spvOpcodeTableInitialize(copied_entries, count); |
| 336 | |
| 337 | *pInstTable = table; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 338 | |
| 339 | return SPV_SUCCESS; |
| 340 | } |
| 341 | |
| 342 | spv_result_t spvOpcodeTableNameLookup(const spv_opcode_table table, |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame] | 343 | const char* name, |
| 344 | spv_opcode_desc* pEntry) { |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 345 | if (!name || !pEntry) return SPV_ERROR_INVALID_POINTER; |
| 346 | if (!table) return SPV_ERROR_INVALID_TABLE; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 347 | |
| 348 | // TODO: This lookup of the Opcode table is suboptimal! Binary sort would be |
| 349 | // preferable but the table requires sorting on the Opcode name, but it's |
| 350 | // static |
| 351 | // const initialized and matches the order of the spec. |
| 352 | const size_t nameLength = strlen(name); |
| 353 | for (uint64_t opcodeIndex = 0; opcodeIndex < table->count; ++opcodeIndex) { |
| 354 | if (nameLength == strlen(table->entries[opcodeIndex].name) && |
| 355 | !strncmp(name, table->entries[opcodeIndex].name, nameLength)) { |
| 356 | // NOTE: Found out Opcode! |
| 357 | *pEntry = &table->entries[opcodeIndex]; |
| 358 | return SPV_SUCCESS; |
| 359 | } |
| 360 | } |
| 361 | |
| 362 | return SPV_ERROR_INVALID_LOOKUP; |
| 363 | } |
| 364 | |
| 365 | spv_result_t spvOpcodeTableValueLookup(const spv_opcode_table table, |
Lei Zhang | b36e704 | 2015-10-28 13:40:52 -0400 | [diff] [blame] | 366 | const SpvOp opcode, |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame] | 367 | spv_opcode_desc* pEntry) { |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 368 | if (!table) return SPV_ERROR_INVALID_TABLE; |
| 369 | if (!pEntry) return SPV_ERROR_INVALID_POINTER; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 370 | |
| 371 | // TODO: As above this lookup is not optimal. |
| 372 | for (uint64_t opcodeIndex = 0; opcodeIndex < table->count; ++opcodeIndex) { |
| 373 | if (opcode == table->entries[opcodeIndex].opcode) { |
| 374 | // NOTE: Found the Opcode! |
| 375 | *pEntry = &table->entries[opcodeIndex]; |
| 376 | return SPV_SUCCESS; |
| 377 | } |
| 378 | } |
| 379 | |
| 380 | return SPV_ERROR_INVALID_LOOKUP; |
| 381 | } |
| 382 | |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 383 | int32_t spvOpcodeRequiresCapabilities(spv_opcode_desc entry) { |
David Neto | 9db3a53 | 2015-10-07 16:58:38 -0400 | [diff] [blame] | 384 | return entry->capabilities != 0; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 385 | } |
| 386 | |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame] | 387 | void spvInstructionCopy(const uint32_t* words, const SpvOp opcode, |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 388 | const uint16_t wordCount, const spv_endianness_t endian, |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame] | 389 | spv_instruction_t* pInst) { |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 390 | pInst->opcode = opcode; |
David Neto | b5dc8fc | 2015-10-06 16:22:00 -0400 | [diff] [blame] | 391 | pInst->words.resize(wordCount); |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 392 | for (uint16_t wordIndex = 0; wordIndex < wordCount; ++wordIndex) { |
| 393 | pInst->words[wordIndex] = spvFixWord(words[wordIndex], endian); |
| 394 | if (!wordIndex) { |
| 395 | uint16_t thisWordCount; |
Lei Zhang | b36e704 | 2015-10-28 13:40:52 -0400 | [diff] [blame] | 396 | SpvOp thisOpcode; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 397 | spvOpcodeSplit(pInst->words[wordIndex], &thisWordCount, &thisOpcode); |
| 398 | assert(opcode == thisOpcode && wordCount == thisWordCount && |
| 399 | "Endianness failed!"); |
| 400 | } |
| 401 | } |
| 402 | } |
| 403 | |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame] | 404 | const char* spvOpcodeString(const SpvOp opcode) { |
David Neto | 201caf7 | 2015-11-04 17:38:17 -0500 | [diff] [blame] | 405 | // Use the syntax table so it's sure to be complete. |
David Neto | 1bcd3d1 | 2015-11-02 16:03:12 -0500 | [diff] [blame] | 406 | #define Instruction(Name, ...) \ |
| 407 | case SpvOp##Name: \ |
| 408 | return #Name; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 409 | switch (opcode) { |
David Neto | 1bcd3d1 | 2015-11-02 16:03:12 -0500 | [diff] [blame] | 410 | #include "opcode.inc" |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 411 | default: |
| 412 | assert(0 && "Unreachable!"); |
| 413 | } |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 414 | return "unknown"; |
David Neto | 1bcd3d1 | 2015-11-02 16:03:12 -0500 | [diff] [blame] | 415 | #undef Instruction |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 416 | } |
| 417 | |
Lei Zhang | b36e704 | 2015-10-28 13:40:52 -0400 | [diff] [blame] | 418 | int32_t spvOpcodeIsScalarType(const SpvOp opcode) { |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 419 | switch (opcode) { |
Lei Zhang | b36e704 | 2015-10-28 13:40:52 -0400 | [diff] [blame] | 420 | case SpvOpTypeInt: |
| 421 | case SpvOpTypeFloat: |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 422 | return true; |
| 423 | default: |
| 424 | return false; |
| 425 | } |
| 426 | } |
| 427 | |
Lei Zhang | b36e704 | 2015-10-28 13:40:52 -0400 | [diff] [blame] | 428 | int32_t spvOpcodeIsConstant(const SpvOp opcode) { |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 429 | switch (opcode) { |
Lei Zhang | b36e704 | 2015-10-28 13:40:52 -0400 | [diff] [blame] | 430 | case SpvOpConstantTrue: |
| 431 | case SpvOpConstantFalse: |
| 432 | case SpvOpConstant: |
| 433 | case SpvOpConstantComposite: |
| 434 | case SpvOpConstantSampler: |
| 435 | // case SpvOpConstantNull: |
| 436 | case SpvOpConstantNull: |
| 437 | case SpvOpSpecConstantTrue: |
| 438 | case SpvOpSpecConstantFalse: |
| 439 | case SpvOpSpecConstant: |
| 440 | case SpvOpSpecConstantComposite: |
| 441 | // case SpvOpSpecConstantOp: |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 442 | return true; |
| 443 | default: |
| 444 | return false; |
| 445 | } |
| 446 | } |
| 447 | |
Lei Zhang | b36e704 | 2015-10-28 13:40:52 -0400 | [diff] [blame] | 448 | int32_t spvOpcodeIsComposite(const SpvOp opcode) { |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 449 | switch (opcode) { |
Lei Zhang | b36e704 | 2015-10-28 13:40:52 -0400 | [diff] [blame] | 450 | case SpvOpTypeVector: |
| 451 | case SpvOpTypeMatrix: |
| 452 | case SpvOpTypeArray: |
| 453 | case SpvOpTypeStruct: |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 454 | return true; |
| 455 | default: |
| 456 | return false; |
| 457 | } |
| 458 | } |
| 459 | |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame] | 460 | int32_t spvOpcodeAreTypesEqual(const spv_instruction_t* pTypeInst0, |
| 461 | const spv_instruction_t* pTypeInst1) { |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 462 | if (pTypeInst0->opcode != pTypeInst1->opcode) return false; |
| 463 | if (pTypeInst0->words[1] != pTypeInst1->words[1]) return false; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 464 | return true; |
| 465 | } |
| 466 | |
Lei Zhang | b36e704 | 2015-10-28 13:40:52 -0400 | [diff] [blame] | 467 | int32_t spvOpcodeIsPointer(const SpvOp opcode) { |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 468 | switch (opcode) { |
Lei Zhang | b36e704 | 2015-10-28 13:40:52 -0400 | [diff] [blame] | 469 | case SpvOpVariable: |
| 470 | case SpvOpAccessChain: |
| 471 | case SpvOpInBoundsAccessChain: |
| 472 | case SpvOpFunctionParameter: |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 473 | return true; |
| 474 | default: |
| 475 | return false; |
| 476 | } |
| 477 | } |
| 478 | |
Lei Zhang | b36e704 | 2015-10-28 13:40:52 -0400 | [diff] [blame] | 479 | int32_t spvOpcodeIsObject(const SpvOp opcode) { |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 480 | switch (opcode) { |
Lei Zhang | b36e704 | 2015-10-28 13:40:52 -0400 | [diff] [blame] | 481 | case SpvOpConstantTrue: |
| 482 | case SpvOpConstantFalse: |
| 483 | case SpvOpConstant: |
| 484 | case SpvOpConstantComposite: |
| 485 | // TODO: case SpvOpConstantSampler: |
| 486 | case SpvOpConstantNull: |
| 487 | case SpvOpSpecConstantTrue: |
| 488 | case SpvOpSpecConstantFalse: |
| 489 | case SpvOpSpecConstant: |
| 490 | case SpvOpSpecConstantComposite: |
| 491 | // TODO: case SpvOpSpecConstantOp: |
| 492 | case SpvOpVariable: |
| 493 | case SpvOpAccessChain: |
| 494 | case SpvOpInBoundsAccessChain: |
| 495 | case SpvOpConvertFToU: |
| 496 | case SpvOpConvertFToS: |
| 497 | case SpvOpConvertSToF: |
| 498 | case SpvOpConvertUToF: |
| 499 | case SpvOpUConvert: |
| 500 | case SpvOpSConvert: |
| 501 | case SpvOpFConvert: |
| 502 | case SpvOpConvertPtrToU: |
| 503 | // TODO: case SpvOpConvertUToPtr: |
| 504 | case SpvOpPtrCastToGeneric: |
| 505 | // TODO: case SpvOpGenericCastToPtr: |
| 506 | case SpvOpBitcast: |
| 507 | // TODO: case SpvOpGenericCastToPtrExplicit: |
| 508 | case SpvOpSatConvertSToU: |
| 509 | case SpvOpSatConvertUToS: |
| 510 | case SpvOpVectorExtractDynamic: |
| 511 | case SpvOpCompositeConstruct: |
| 512 | case SpvOpCompositeExtract: |
| 513 | case SpvOpCopyObject: |
| 514 | case SpvOpTranspose: |
| 515 | case SpvOpSNegate: |
| 516 | case SpvOpFNegate: |
| 517 | case SpvOpNot: |
| 518 | case SpvOpIAdd: |
| 519 | case SpvOpFAdd: |
| 520 | case SpvOpISub: |
| 521 | case SpvOpFSub: |
| 522 | case SpvOpIMul: |
| 523 | case SpvOpFMul: |
| 524 | case SpvOpUDiv: |
| 525 | case SpvOpSDiv: |
| 526 | case SpvOpFDiv: |
| 527 | case SpvOpUMod: |
| 528 | case SpvOpSRem: |
| 529 | case SpvOpSMod: |
| 530 | case SpvOpVectorTimesScalar: |
| 531 | case SpvOpMatrixTimesScalar: |
| 532 | case SpvOpVectorTimesMatrix: |
| 533 | case SpvOpMatrixTimesVector: |
| 534 | case SpvOpMatrixTimesMatrix: |
| 535 | case SpvOpOuterProduct: |
| 536 | case SpvOpDot: |
| 537 | case SpvOpShiftRightLogical: |
| 538 | case SpvOpShiftRightArithmetic: |
| 539 | case SpvOpShiftLeftLogical: |
| 540 | case SpvOpBitwiseOr: |
| 541 | case SpvOpBitwiseXor: |
| 542 | case SpvOpBitwiseAnd: |
| 543 | case SpvOpAny: |
| 544 | case SpvOpAll: |
| 545 | case SpvOpIsNan: |
| 546 | case SpvOpIsInf: |
| 547 | case SpvOpIsFinite: |
| 548 | case SpvOpIsNormal: |
| 549 | case SpvOpSignBitSet: |
| 550 | case SpvOpLessOrGreater: |
| 551 | case SpvOpOrdered: |
| 552 | case SpvOpUnordered: |
| 553 | case SpvOpLogicalOr: |
| 554 | case SpvOpLogicalAnd: |
| 555 | case SpvOpSelect: |
| 556 | case SpvOpIEqual: |
| 557 | case SpvOpFOrdEqual: |
| 558 | case SpvOpFUnordEqual: |
| 559 | case SpvOpINotEqual: |
| 560 | case SpvOpFOrdNotEqual: |
| 561 | case SpvOpFUnordNotEqual: |
| 562 | case SpvOpULessThan: |
| 563 | case SpvOpSLessThan: |
| 564 | case SpvOpFOrdLessThan: |
| 565 | case SpvOpFUnordLessThan: |
| 566 | case SpvOpUGreaterThan: |
| 567 | case SpvOpSGreaterThan: |
| 568 | case SpvOpFOrdGreaterThan: |
| 569 | case SpvOpFUnordGreaterThan: |
| 570 | case SpvOpULessThanEqual: |
| 571 | case SpvOpSLessThanEqual: |
| 572 | case SpvOpFOrdLessThanEqual: |
| 573 | case SpvOpFUnordLessThanEqual: |
| 574 | case SpvOpUGreaterThanEqual: |
| 575 | case SpvOpSGreaterThanEqual: |
| 576 | case SpvOpFOrdGreaterThanEqual: |
| 577 | case SpvOpFUnordGreaterThanEqual: |
| 578 | case SpvOpDPdx: |
| 579 | case SpvOpDPdy: |
| 580 | case SpvOpFwidth: |
| 581 | case SpvOpDPdxFine: |
| 582 | case SpvOpDPdyFine: |
| 583 | case SpvOpFwidthFine: |
| 584 | case SpvOpDPdxCoarse: |
| 585 | case SpvOpDPdyCoarse: |
| 586 | case SpvOpFwidthCoarse: |
| 587 | case SpvOpReturnValue: |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 588 | return true; |
| 589 | default: |
| 590 | return false; |
| 591 | } |
| 592 | } |
| 593 | |
Lei Zhang | b36e704 | 2015-10-28 13:40:52 -0400 | [diff] [blame] | 594 | int32_t spvOpcodeIsBasicTypeNullable(SpvOp opcode) { |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 595 | switch (opcode) { |
Lei Zhang | b36e704 | 2015-10-28 13:40:52 -0400 | [diff] [blame] | 596 | case SpvOpTypeBool: |
| 597 | case SpvOpTypeInt: |
| 598 | case SpvOpTypeFloat: |
| 599 | case SpvOpTypePointer: |
| 600 | case SpvOpTypeEvent: |
| 601 | case SpvOpTypeDeviceEvent: |
| 602 | case SpvOpTypeReserveId: |
| 603 | case SpvOpTypeQueue: |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 604 | return true; |
| 605 | default: |
| 606 | return false; |
| 607 | } |
| 608 | } |
| 609 | |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame] | 610 | int32_t spvInstructionIsInBasicBlock(const spv_instruction_t* pFirstInst, |
| 611 | const spv_instruction_t* pInst) { |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 612 | while (pFirstInst != pInst) { |
Lei Zhang | b36e704 | 2015-10-28 13:40:52 -0400 | [diff] [blame] | 613 | if (SpvOpFunction == pInst->opcode) break; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 614 | pInst--; |
| 615 | } |
Lei Zhang | b36e704 | 2015-10-28 13:40:52 -0400 | [diff] [blame] | 616 | if (SpvOpFunction != pInst->opcode) return false; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 617 | return true; |
| 618 | } |
| 619 | |
Lei Zhang | b36e704 | 2015-10-28 13:40:52 -0400 | [diff] [blame] | 620 | int32_t spvOpcodeIsValue(SpvOp opcode) { |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 621 | if (spvOpcodeIsPointer(opcode)) return true; |
| 622 | if (spvOpcodeIsConstant(opcode)) return true; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 623 | switch (opcode) { |
Lei Zhang | b36e704 | 2015-10-28 13:40:52 -0400 | [diff] [blame] | 624 | case SpvOpLoad: |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 625 | // TODO: Other Opcode's resulting in a value |
| 626 | return true; |
| 627 | default: |
| 628 | return false; |
| 629 | } |
| 630 | } |
Andrew Woloszyn | 537e776 | 2015-09-29 11:28:34 -0400 | [diff] [blame] | 631 | |
Lei Zhang | b36e704 | 2015-10-28 13:40:52 -0400 | [diff] [blame] | 632 | int32_t spvOpcodeGeneratesType(SpvOp op) { |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame] | 633 | switch (op) { |
Lei Zhang | b36e704 | 2015-10-28 13:40:52 -0400 | [diff] [blame] | 634 | case SpvOpTypeVoid: |
| 635 | case SpvOpTypeBool: |
| 636 | case SpvOpTypeInt: |
| 637 | case SpvOpTypeFloat: |
| 638 | case SpvOpTypeVector: |
| 639 | case SpvOpTypeMatrix: |
| 640 | case SpvOpTypeImage: |
| 641 | case SpvOpTypeSampler: |
| 642 | case SpvOpTypeSampledImage: |
| 643 | case SpvOpTypeArray: |
| 644 | case SpvOpTypeRuntimeArray: |
| 645 | case SpvOpTypeStruct: |
| 646 | case SpvOpTypeOpaque: |
| 647 | case SpvOpTypePointer: |
| 648 | case SpvOpTypeFunction: |
| 649 | case SpvOpTypeEvent: |
| 650 | case SpvOpTypeDeviceEvent: |
| 651 | case SpvOpTypeReserveId: |
| 652 | case SpvOpTypeQueue: |
| 653 | case SpvOpTypePipe: |
Andrew Woloszyn | 537e776 | 2015-09-29 11:28:34 -0400 | [diff] [blame] | 654 | return true; |
David Neto | aef608c | 2015-11-02 14:59:02 -0500 | [diff] [blame] | 655 | default: |
| 656 | // In particular, OpTypeForwardPointer does not generate a type, |
| 657 | // but declares a storage class for a pointer type generated |
| 658 | // by a different instruction. |
| 659 | break; |
Andrew Woloszyn | 537e776 | 2015-09-29 11:28:34 -0400 | [diff] [blame] | 660 | } |
| 661 | return 0; |
| 662 | } |