Dejan Mircevski | b6fe02f | 2016-01-07 13:44:22 -0500 | [diff] [blame] | 1 | // Copyright (c) 2015-2016 The Khronos Group Inc. |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 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 "instruction.h" |
David Neto | 5a70335 | 2016-02-17 14:44:00 -0500 | [diff] [blame^] | 35 | #include "spirv-tools/libspirv.h" |
Lei Zhang | aa056cd | 2015-11-11 14:24:04 -0500 | [diff] [blame] | 36 | #include "spirv_constant.h" |
David Neto | 4c21571 | 2015-12-22 15:08:41 -0500 | [diff] [blame] | 37 | #include "spirv_endian.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 SpvOpExecutionMode: |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 90 | return SPV_OPERAND_TYPE_VARIABLE_EXECUTION_MODE; |
| 91 | default: |
| 92 | break; |
| 93 | } |
Lei Zhang | b41d150 | 2015-09-14 15:22:23 -0400 | [diff] [blame] | 94 | } else if (operandClass == OperandVariableLiterals) { |
David Neto | 39fa148 | 2015-11-30 14:39:31 -0500 | [diff] [blame] | 95 | switch (opcode) { |
| 96 | case SpvOpConstant: |
| 97 | case SpvOpSpecConstant: |
| 98 | // The number type is determined by the type Id operand. |
| 99 | return SPV_OPERAND_TYPE_TYPED_LITERAL_NUMBER; |
| 100 | case SpvOpDecorate: |
| 101 | case SpvOpMemberDecorate: |
| 102 | // The operand types at the end of the instruction are |
| 103 | // determined instead by the decoration kind. |
| 104 | return SPV_OPERAND_TYPE_NONE; |
| 105 | default: |
| 106 | break; |
David Neto | 201caf7 | 2015-11-04 17:38:17 -0500 | [diff] [blame] | 107 | } |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 108 | } |
| 109 | |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame] | 110 | switch (operandClass) { |
| 111 | case OperandNone: |
| 112 | return SPV_OPERAND_TYPE_NONE; |
| 113 | case OperandId: |
| 114 | return SPV_OPERAND_TYPE_ID; |
| 115 | case OperandOptionalId: |
| 116 | return SPV_OPERAND_TYPE_OPTIONAL_ID; |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame] | 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: |
David Neto | 2889a0c | 2016-02-15 13:50:00 -0500 | [diff] [blame] | 182 | return SPV_OPERAND_TYPE_IMAGE; |
| 183 | case OperandOptionalImageOperands: |
| 184 | return SPV_OPERAND_TYPE_OPTIONAL_IMAGE; |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame] | 185 | case OperandFPFastMath: |
| 186 | return SPV_OPERAND_TYPE_FP_FAST_MATH_MODE; |
| 187 | case OperandFPRoundingMode: |
| 188 | return SPV_OPERAND_TYPE_FP_ROUNDING_MODE; |
| 189 | case OperandLinkageType: |
| 190 | return SPV_OPERAND_TYPE_LINKAGE_TYPE; |
| 191 | case OperandAccessQualifier: |
| 192 | return SPV_OPERAND_TYPE_ACCESS_QUALIFIER; |
David Neto | 2889a0c | 2016-02-15 13:50:00 -0500 | [diff] [blame] | 193 | case OperandOptionalAccessQualifier: |
| 194 | return SPV_OPERAND_TYPE_OPTIONAL_ACCESS_QUALIFIER; |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame] | 195 | case OperandFuncParamAttr: |
| 196 | return SPV_OPERAND_TYPE_FUNCTION_PARAMETER_ATTRIBUTE; |
| 197 | case OperandDecoration: |
| 198 | return SPV_OPERAND_TYPE_DECORATION; |
| 199 | case OperandBuiltIn: |
| 200 | return SPV_OPERAND_TYPE_BUILT_IN; |
| 201 | case OperandSelect: |
| 202 | return SPV_OPERAND_TYPE_SELECTION_CONTROL; |
| 203 | case OperandLoop: |
| 204 | return SPV_OPERAND_TYPE_LOOP_CONTROL; |
| 205 | case OperandFunction: |
| 206 | return SPV_OPERAND_TYPE_FUNCTION_CONTROL; |
| 207 | case OperandMemorySemantics: |
David Neto | 64a9be9 | 2015-11-18 15:48:32 -0500 | [diff] [blame] | 208 | return SPV_OPERAND_TYPE_MEMORY_SEMANTICS_ID; |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 209 | case OperandMemoryAccess: |
David Neto | 7cefb23 | 2015-09-28 15:33:49 -0400 | [diff] [blame] | 210 | // 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] | 211 | // We expect that it will become SPV_OPERAND_TYPE_OPTIONAL_MEMORY_ACCESS, |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 212 | // and we can remove the special casing above for memory operation |
| 213 | // instructions. |
| 214 | break; |
David Neto | 2889a0c | 2016-02-15 13:50:00 -0500 | [diff] [blame] | 215 | case OperandOptionalMemoryAccess: |
| 216 | // Expect an optional mask. When the Aligned bit is set in the mask, |
| 217 | // we will later add the expectation of a literal number operand. |
| 218 | return SPV_OPERAND_TYPE_OPTIONAL_MEMORY_ACCESS; |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame] | 219 | case OperandScope: |
David Neto | 64a9be9 | 2015-11-18 15:48:32 -0500 | [diff] [blame] | 220 | return SPV_OPERAND_TYPE_SCOPE_ID; |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame] | 221 | case OperandGroupOperation: |
| 222 | return SPV_OPERAND_TYPE_GROUP_OPERATION; |
| 223 | case OperandKernelEnqueueFlags: |
| 224 | return SPV_OPERAND_TYPE_KERNEL_ENQ_FLAGS; |
| 225 | case OperandKernelProfilingInfo: |
| 226 | return SPV_OPERAND_TYPE_KERNEL_PROFILING_INFO; |
| 227 | case OperandCapability: |
| 228 | return SPV_OPERAND_TYPE_CAPABILITY; |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 229 | |
| 230 | // Used by GroupMemberDecorate |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame] | 231 | case OperandVariableIdLiteral: |
| 232 | return SPV_OPERAND_TYPE_VARIABLE_ID_LITERAL_INTEGER; |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 233 | |
| 234 | // Used by Switch |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame] | 235 | case OperandVariableLiteralId: |
| 236 | return SPV_OPERAND_TYPE_VARIABLE_LITERAL_INTEGER_ID; |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 237 | |
| 238 | // These exceptional cases shouldn't occur. |
| 239 | case OperandCount: |
| 240 | default: |
| 241 | break; |
| 242 | } |
| 243 | assert(0 && "Unexpected operand class"); |
| 244 | return SPV_OPERAND_TYPE_NONE; |
| 245 | } |
| 246 | |
Lei Zhang | a94701d | 2015-09-14 10:05:37 -0400 | [diff] [blame] | 247 | } // anonymous namespace |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 248 | |
| 249 | // Finish populating the opcodeTableEntries array. |
Lei Zhang | 972788b | 2015-11-12 13:48:30 -0500 | [diff] [blame] | 250 | void spvOpcodeTableInitialize(spv_opcode_desc_t* entries, |
| 251 | uint32_t num_entries) { |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 252 | // Compute the operandTypes field for each entry. |
Lei Zhang | 972788b | 2015-11-12 13:48:30 -0500 | [diff] [blame] | 253 | for (uint32_t i = 0; i < num_entries; ++i) { |
| 254 | spv_opcode_desc_t& opcode = entries[i]; |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 255 | opcode.numTypes = 0; |
| 256 | // Type ID always comes first, if present. |
| 257 | if (opcode.hasType) |
Andrew Woloszyn | 537e776 | 2015-09-29 11:28:34 -0400 | [diff] [blame] | 258 | opcode.operandTypes[opcode.numTypes++] = SPV_OPERAND_TYPE_TYPE_ID; |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 259 | // Result ID always comes next, if present |
| 260 | if (opcode.hasResult) |
| 261 | opcode.operandTypes[opcode.numTypes++] = SPV_OPERAND_TYPE_RESULT_ID; |
David Neto | ba73a7c | 2016-01-06 13:08:39 -0500 | [diff] [blame] | 262 | const uint16_t maxNumOperands = static_cast<uint16_t>( |
| 263 | sizeof(opcode.operandTypes) / sizeof(opcode.operandTypes[0])); |
| 264 | const uint16_t maxNumClasses = static_cast<uint16_t>( |
| 265 | sizeof(opcode.operandClass) / sizeof(opcode.operandClass[0])); |
Lei Zhang | a94701d | 2015-09-14 10:05:37 -0400 | [diff] [blame] | 266 | for (uint16_t classIndex = 0; |
| 267 | opcode.numTypes < maxNumOperands && classIndex < maxNumClasses; |
| 268 | classIndex++) { |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 269 | const OperandClass operandClass = opcode.operandClass[classIndex]; |
David Neto | 0f166be | 2015-11-11 01:56:49 -0500 | [diff] [blame] | 270 | const auto operandType = |
Lei Zhang | a94701d | 2015-09-14 10:05:37 -0400 | [diff] [blame] | 271 | convertOperandClassToType(opcode.opcode, operandClass); |
David Neto | 0f166be | 2015-11-11 01:56:49 -0500 | [diff] [blame] | 272 | opcode.operandTypes[opcode.numTypes++] = operandType; |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 273 | // The OperandNone value is not explicitly represented in the .inc file. |
| 274 | // However, it is the zero value, and is created via implicit value |
David Neto | 0f166be | 2015-11-11 01:56:49 -0500 | [diff] [blame] | 275 | // initialization. It converts to SPV_OPERAND_TYPE_NONE. |
| 276 | // The SPV_OPERAND_TYPE_NONE operand type indicates no current or futher |
| 277 | // operands. |
| 278 | if (operandType == SPV_OPERAND_TYPE_NONE) { |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 279 | opcode.numTypes--; |
| 280 | break; |
| 281 | } |
| 282 | } |
David Neto | 0f166be | 2015-11-11 01:56:49 -0500 | [diff] [blame] | 283 | |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 284 | // We should have written the terminating SPV_OPERAND_TYPE_NONE entry, but |
| 285 | // also without overflowing. |
Lei Zhang | a94701d | 2015-09-14 10:05:37 -0400 | [diff] [blame] | 286 | assert((opcode.numTypes < maxNumOperands) && |
| 287 | "Operand class list is too long. Expand " |
| 288 | "spv_opcode_desc_t.operandClass"); |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 289 | } |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 290 | } |
| 291 | |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame] | 292 | const char* spvGeneratorStr(uint32_t generator) { |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 293 | switch (generator) { |
| 294 | case SPV_GENERATOR_KHRONOS: |
| 295 | return "Khronos"; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 296 | case SPV_GENERATOR_LUNARG: |
| 297 | return "LunarG"; |
David Neto | 1780fc4 | 2015-10-26 15:43:12 -0400 | [diff] [blame] | 298 | case SPV_GENERATOR_VALVE: |
| 299 | return "Valve"; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 300 | case SPV_GENERATOR_CODEPLAY: |
| 301 | return "Codeplay Software Ltd."; |
David Neto | 1780fc4 | 2015-10-26 15:43:12 -0400 | [diff] [blame] | 302 | case SPV_GENERATOR_NVIDIA: |
| 303 | return "NVIDIA"; |
| 304 | case SPV_GENERATOR_ARM: |
| 305 | return "ARM"; |
David Neto | 14b93e4 | 2015-11-12 18:33:47 -0500 | [diff] [blame] | 306 | case SPV_GENERATOR_KHRONOS_LLVM_TRANSLATOR: |
| 307 | return "Khronos LLVM/SPIR-V Translator"; |
| 308 | case SPV_GENERATOR_KHRONOS_ASSEMBLER: |
| 309 | return "Khronos SPIR-V Tools Assembler"; |
David Neto | 2266ba1 | 2015-11-13 12:03:28 -0600 | [diff] [blame] | 310 | case SPV_GENERATOR_KHRONOS_GLSLANG: |
| 311 | return "Khronos Glslang Reference Front End"; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 312 | default: |
| 313 | return "Unknown"; |
| 314 | } |
| 315 | } |
| 316 | |
Lei Zhang | b36e704 | 2015-10-28 13:40:52 -0400 | [diff] [blame] | 317 | uint32_t spvOpcodeMake(uint16_t wordCount, SpvOp opcode) { |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 318 | return ((uint32_t)opcode) | (((uint32_t)wordCount) << 16); |
| 319 | } |
| 320 | |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame] | 321 | void spvOpcodeSplit(const uint32_t word, uint16_t* pWordCount, SpvOp* pOpcode) { |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 322 | if (pWordCount) { |
| 323 | *pWordCount = (uint16_t)((0xffff0000 & word) >> 16); |
| 324 | } |
| 325 | if (pOpcode) { |
Lei Zhang | b36e704 | 2015-10-28 13:40:52 -0400 | [diff] [blame] | 326 | *pOpcode = (SpvOp)(0x0000ffff & word); |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 327 | } |
| 328 | } |
| 329 | |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame] | 330 | spv_result_t spvOpcodeTableGet(spv_opcode_table* pInstTable) { |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 331 | if (!pInstTable) return SPV_ERROR_INVALID_POINTER; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 332 | |
Lei Zhang | 972788b | 2015-11-12 13:48:30 -0500 | [diff] [blame] | 333 | const uint32_t size = sizeof(opcodeTableEntries); |
| 334 | spv_opcode_desc_t* copied_entries = |
| 335 | static_cast<spv_opcode_desc_t*>(::malloc(size)); |
| 336 | if (!copied_entries) return SPV_ERROR_OUT_OF_MEMORY; |
| 337 | ::memcpy(copied_entries, opcodeTableEntries, size); |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 338 | |
David Neto | ba73a7c | 2016-01-06 13:08:39 -0500 | [diff] [blame] | 339 | const uint32_t count = static_cast<uint32_t>(sizeof(opcodeTableEntries) / |
| 340 | sizeof(spv_opcode_desc_t)); |
Lei Zhang | 972788b | 2015-11-12 13:48:30 -0500 | [diff] [blame] | 341 | 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] | 342 | |
Lei Zhang | 972788b | 2015-11-12 13:48:30 -0500 | [diff] [blame] | 343 | spvOpcodeTableInitialize(copied_entries, count); |
| 344 | |
| 345 | *pInstTable = table; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 346 | |
| 347 | return SPV_SUCCESS; |
| 348 | } |
| 349 | |
| 350 | spv_result_t spvOpcodeTableNameLookup(const spv_opcode_table table, |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame] | 351 | const char* name, |
| 352 | spv_opcode_desc* pEntry) { |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 353 | if (!name || !pEntry) return SPV_ERROR_INVALID_POINTER; |
| 354 | if (!table) return SPV_ERROR_INVALID_TABLE; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 355 | |
| 356 | // TODO: This lookup of the Opcode table is suboptimal! Binary sort would be |
| 357 | // preferable but the table requires sorting on the Opcode name, but it's |
| 358 | // static |
| 359 | // const initialized and matches the order of the spec. |
| 360 | const size_t nameLength = strlen(name); |
| 361 | for (uint64_t opcodeIndex = 0; opcodeIndex < table->count; ++opcodeIndex) { |
| 362 | if (nameLength == strlen(table->entries[opcodeIndex].name) && |
| 363 | !strncmp(name, table->entries[opcodeIndex].name, nameLength)) { |
| 364 | // NOTE: Found out Opcode! |
| 365 | *pEntry = &table->entries[opcodeIndex]; |
| 366 | return SPV_SUCCESS; |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | return SPV_ERROR_INVALID_LOOKUP; |
| 371 | } |
| 372 | |
| 373 | spv_result_t spvOpcodeTableValueLookup(const spv_opcode_table table, |
Lei Zhang | b36e704 | 2015-10-28 13:40:52 -0400 | [diff] [blame] | 374 | const SpvOp opcode, |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame] | 375 | spv_opcode_desc* pEntry) { |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 376 | if (!table) return SPV_ERROR_INVALID_TABLE; |
| 377 | if (!pEntry) return SPV_ERROR_INVALID_POINTER; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 378 | |
| 379 | // TODO: As above this lookup is not optimal. |
| 380 | for (uint64_t opcodeIndex = 0; opcodeIndex < table->count; ++opcodeIndex) { |
| 381 | if (opcode == table->entries[opcodeIndex].opcode) { |
| 382 | // NOTE: Found the Opcode! |
| 383 | *pEntry = &table->entries[opcodeIndex]; |
| 384 | return SPV_SUCCESS; |
| 385 | } |
| 386 | } |
| 387 | |
| 388 | return SPV_ERROR_INVALID_LOOKUP; |
| 389 | } |
| 390 | |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 391 | int32_t spvOpcodeRequiresCapabilities(spv_opcode_desc entry) { |
David Neto | 9db3a53 | 2015-10-07 16:58:38 -0400 | [diff] [blame] | 392 | return entry->capabilities != 0; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 393 | } |
| 394 | |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame] | 395 | void spvInstructionCopy(const uint32_t* words, const SpvOp opcode, |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 396 | const uint16_t wordCount, const spv_endianness_t endian, |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame] | 397 | spv_instruction_t* pInst) { |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 398 | pInst->opcode = opcode; |
David Neto | b5dc8fc | 2015-10-06 16:22:00 -0400 | [diff] [blame] | 399 | pInst->words.resize(wordCount); |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 400 | for (uint16_t wordIndex = 0; wordIndex < wordCount; ++wordIndex) { |
| 401 | pInst->words[wordIndex] = spvFixWord(words[wordIndex], endian); |
| 402 | if (!wordIndex) { |
| 403 | uint16_t thisWordCount; |
Lei Zhang | b36e704 | 2015-10-28 13:40:52 -0400 | [diff] [blame] | 404 | SpvOp thisOpcode; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 405 | spvOpcodeSplit(pInst->words[wordIndex], &thisWordCount, &thisOpcode); |
| 406 | assert(opcode == thisOpcode && wordCount == thisWordCount && |
| 407 | "Endianness failed!"); |
| 408 | } |
| 409 | } |
| 410 | } |
| 411 | |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame] | 412 | const char* spvOpcodeString(const SpvOp opcode) { |
David Neto | 201caf7 | 2015-11-04 17:38:17 -0500 | [diff] [blame] | 413 | // Use the syntax table so it's sure to be complete. |
David Neto | 1bcd3d1 | 2015-11-02 16:03:12 -0500 | [diff] [blame] | 414 | #define Instruction(Name, ...) \ |
| 415 | case SpvOp##Name: \ |
| 416 | return #Name; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 417 | switch (opcode) { |
David Neto | 1bcd3d1 | 2015-11-02 16:03:12 -0500 | [diff] [blame] | 418 | #include "opcode.inc" |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 419 | default: |
| 420 | assert(0 && "Unreachable!"); |
| 421 | } |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 422 | return "unknown"; |
David Neto | 1bcd3d1 | 2015-11-02 16:03:12 -0500 | [diff] [blame] | 423 | #undef Instruction |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 424 | } |
| 425 | |
Lei Zhang | b36e704 | 2015-10-28 13:40:52 -0400 | [diff] [blame] | 426 | int32_t spvOpcodeIsScalarType(const SpvOp opcode) { |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 427 | switch (opcode) { |
Lei Zhang | b36e704 | 2015-10-28 13:40:52 -0400 | [diff] [blame] | 428 | case SpvOpTypeInt: |
| 429 | case SpvOpTypeFloat: |
Dejan Mircevski | 276a724 | 2016-01-21 15:55:43 -0500 | [diff] [blame] | 430 | case SpvOpTypeBool: |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 431 | return true; |
| 432 | default: |
| 433 | return false; |
| 434 | } |
| 435 | } |
| 436 | |
Lei Zhang | b36e704 | 2015-10-28 13:40:52 -0400 | [diff] [blame] | 437 | int32_t spvOpcodeIsConstant(const SpvOp opcode) { |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 438 | switch (opcode) { |
Lei Zhang | b36e704 | 2015-10-28 13:40:52 -0400 | [diff] [blame] | 439 | case SpvOpConstantTrue: |
| 440 | case SpvOpConstantFalse: |
| 441 | case SpvOpConstant: |
| 442 | case SpvOpConstantComposite: |
| 443 | case SpvOpConstantSampler: |
| 444 | // case SpvOpConstantNull: |
| 445 | case SpvOpConstantNull: |
| 446 | case SpvOpSpecConstantTrue: |
| 447 | case SpvOpSpecConstantFalse: |
| 448 | case SpvOpSpecConstant: |
| 449 | case SpvOpSpecConstantComposite: |
| 450 | // case SpvOpSpecConstantOp: |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 451 | return true; |
| 452 | default: |
| 453 | return false; |
| 454 | } |
| 455 | } |
| 456 | |
Lei Zhang | b36e704 | 2015-10-28 13:40:52 -0400 | [diff] [blame] | 457 | int32_t spvOpcodeIsComposite(const SpvOp opcode) { |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 458 | switch (opcode) { |
Lei Zhang | b36e704 | 2015-10-28 13:40:52 -0400 | [diff] [blame] | 459 | case SpvOpTypeVector: |
| 460 | case SpvOpTypeMatrix: |
| 461 | case SpvOpTypeArray: |
| 462 | case SpvOpTypeStruct: |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 463 | return true; |
| 464 | default: |
| 465 | return false; |
| 466 | } |
| 467 | } |
| 468 | |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame] | 469 | int32_t spvOpcodeAreTypesEqual(const spv_instruction_t* pTypeInst0, |
| 470 | const spv_instruction_t* pTypeInst1) { |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 471 | if (pTypeInst0->opcode != pTypeInst1->opcode) return false; |
| 472 | if (pTypeInst0->words[1] != pTypeInst1->words[1]) return false; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 473 | return true; |
| 474 | } |
| 475 | |
Lei Zhang | b36e704 | 2015-10-28 13:40:52 -0400 | [diff] [blame] | 476 | int32_t spvOpcodeIsPointer(const SpvOp opcode) { |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 477 | switch (opcode) { |
Lei Zhang | b36e704 | 2015-10-28 13:40:52 -0400 | [diff] [blame] | 478 | case SpvOpVariable: |
| 479 | case SpvOpAccessChain: |
| 480 | case SpvOpInBoundsAccessChain: |
| 481 | case SpvOpFunctionParameter: |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 482 | return true; |
| 483 | default: |
| 484 | return false; |
| 485 | } |
| 486 | } |
| 487 | |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame] | 488 | int32_t spvInstructionIsInBasicBlock(const spv_instruction_t* pFirstInst, |
| 489 | const spv_instruction_t* pInst) { |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 490 | while (pFirstInst != pInst) { |
Lei Zhang | b36e704 | 2015-10-28 13:40:52 -0400 | [diff] [blame] | 491 | if (SpvOpFunction == pInst->opcode) break; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 492 | pInst--; |
| 493 | } |
Lei Zhang | b36e704 | 2015-10-28 13:40:52 -0400 | [diff] [blame] | 494 | if (SpvOpFunction != pInst->opcode) return false; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 495 | return true; |
| 496 | } |
| 497 | |
Lei Zhang | b36e704 | 2015-10-28 13:40:52 -0400 | [diff] [blame] | 498 | int32_t spvOpcodeGeneratesType(SpvOp op) { |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame] | 499 | switch (op) { |
Lei Zhang | b36e704 | 2015-10-28 13:40:52 -0400 | [diff] [blame] | 500 | case SpvOpTypeVoid: |
| 501 | case SpvOpTypeBool: |
| 502 | case SpvOpTypeInt: |
| 503 | case SpvOpTypeFloat: |
| 504 | case SpvOpTypeVector: |
| 505 | case SpvOpTypeMatrix: |
| 506 | case SpvOpTypeImage: |
| 507 | case SpvOpTypeSampler: |
| 508 | case SpvOpTypeSampledImage: |
| 509 | case SpvOpTypeArray: |
| 510 | case SpvOpTypeRuntimeArray: |
| 511 | case SpvOpTypeStruct: |
| 512 | case SpvOpTypeOpaque: |
| 513 | case SpvOpTypePointer: |
| 514 | case SpvOpTypeFunction: |
| 515 | case SpvOpTypeEvent: |
| 516 | case SpvOpTypeDeviceEvent: |
| 517 | case SpvOpTypeReserveId: |
| 518 | case SpvOpTypeQueue: |
| 519 | case SpvOpTypePipe: |
Andrew Woloszyn | 537e776 | 2015-09-29 11:28:34 -0400 | [diff] [blame] | 520 | return true; |
David Neto | aef608c | 2015-11-02 14:59:02 -0500 | [diff] [blame] | 521 | default: |
| 522 | // In particular, OpTypeForwardPointer does not generate a type, |
| 523 | // but declares a storage class for a pointer type generated |
| 524 | // by a different instruction. |
| 525 | break; |
Andrew Woloszyn | 537e776 | 2015-09-29 11:28:34 -0400 | [diff] [blame] | 526 | } |
| 527 | return 0; |
| 528 | } |