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 | 923f6c1 | 2015-11-11 12:45:23 -0500 | [diff] [blame] | 38 | |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 39 | namespace { |
| 40 | |
| 41 | // Descriptions of each opcode. Each entry describes the format of the |
| 42 | // instruction that follows a particular opcode. |
| 43 | // |
| 44 | // Most fields are initialized statically by including an automatically |
| 45 | // generated file. |
| 46 | // The operandTypes fields are initialized during spvOpcodeInitialize(). |
| 47 | // |
| 48 | // TODO(dneto): Some of the macros are quite unreadable. We could make |
| 49 | // 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] | 50 | const spv_opcode_desc_t opcodeTableEntries[] = { |
Lei Zhang | 80e416c | 2016-04-04 14:30:56 -0400 | [diff] [blame^] | 51 | #include "core.insts.inc" |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 52 | }; |
| 53 | |
Lei Zhang | a94701d | 2015-09-14 10:05:37 -0400 | [diff] [blame] | 54 | } // anonymous namespace |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 55 | |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame] | 56 | const char* spvGeneratorStr(uint32_t generator) { |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 57 | switch (generator) { |
| 58 | case SPV_GENERATOR_KHRONOS: |
| 59 | return "Khronos"; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 60 | case SPV_GENERATOR_LUNARG: |
| 61 | return "LunarG"; |
David Neto | 1780fc4 | 2015-10-26 15:43:12 -0400 | [diff] [blame] | 62 | case SPV_GENERATOR_VALVE: |
| 63 | return "Valve"; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 64 | case SPV_GENERATOR_CODEPLAY: |
| 65 | return "Codeplay Software Ltd."; |
David Neto | 1780fc4 | 2015-10-26 15:43:12 -0400 | [diff] [blame] | 66 | case SPV_GENERATOR_NVIDIA: |
| 67 | return "NVIDIA"; |
| 68 | case SPV_GENERATOR_ARM: |
| 69 | return "ARM"; |
David Neto | 14b93e4 | 2015-11-12 18:33:47 -0500 | [diff] [blame] | 70 | case SPV_GENERATOR_KHRONOS_LLVM_TRANSLATOR: |
| 71 | return "Khronos LLVM/SPIR-V Translator"; |
| 72 | case SPV_GENERATOR_KHRONOS_ASSEMBLER: |
| 73 | return "Khronos SPIR-V Tools Assembler"; |
David Neto | 2266ba1 | 2015-11-13 12:03:28 -0600 | [diff] [blame] | 74 | case SPV_GENERATOR_KHRONOS_GLSLANG: |
| 75 | return "Khronos Glslang Reference Front End"; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 76 | default: |
| 77 | return "Unknown"; |
| 78 | } |
| 79 | } |
| 80 | |
Lei Zhang | b36e704 | 2015-10-28 13:40:52 -0400 | [diff] [blame] | 81 | uint32_t spvOpcodeMake(uint16_t wordCount, SpvOp opcode) { |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 82 | return ((uint32_t)opcode) | (((uint32_t)wordCount) << 16); |
| 83 | } |
| 84 | |
Lei Zhang | 6fa3f8a | 2016-03-31 17:26:31 -0400 | [diff] [blame] | 85 | void spvOpcodeSplit(const uint32_t word, uint16_t* pWordCount, |
| 86 | uint16_t* pOpcode) { |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 87 | if (pWordCount) { |
| 88 | *pWordCount = (uint16_t)((0xffff0000 & word) >> 16); |
| 89 | } |
| 90 | if (pOpcode) { |
Lei Zhang | 6fa3f8a | 2016-03-31 17:26:31 -0400 | [diff] [blame] | 91 | *pOpcode = 0x0000ffff & word; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 92 | } |
| 93 | } |
| 94 | |
Lei Zhang | 4f293b7 | 2016-03-21 16:36:14 -0400 | [diff] [blame] | 95 | // Evaluates to the number of elements of array A. |
| 96 | // If we could use constexpr, then we could make this a template function. |
| 97 | // If the source arrays were std::array, then we could have used |
| 98 | // std::array::size. |
| 99 | #define ARRAY_SIZE(A) (static_cast<uint32_t>(sizeof(A) / sizeof(A[0]))) |
| 100 | |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame] | 101 | spv_result_t spvOpcodeTableGet(spv_opcode_table* pInstTable) { |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 102 | if (!pInstTable) return SPV_ERROR_INVALID_POINTER; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 103 | |
Lei Zhang | 4f293b7 | 2016-03-21 16:36:14 -0400 | [diff] [blame] | 104 | static const spv_opcode_table_t table = {ARRAY_SIZE(opcodeTableEntries), |
| 105 | opcodeTableEntries}; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 106 | |
Lei Zhang | 4f293b7 | 2016-03-21 16:36:14 -0400 | [diff] [blame] | 107 | *pInstTable = &table; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 108 | |
| 109 | return SPV_SUCCESS; |
| 110 | } |
| 111 | |
| 112 | spv_result_t spvOpcodeTableNameLookup(const spv_opcode_table table, |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame] | 113 | const char* name, |
| 114 | spv_opcode_desc* pEntry) { |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 115 | if (!name || !pEntry) return SPV_ERROR_INVALID_POINTER; |
| 116 | if (!table) return SPV_ERROR_INVALID_TABLE; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 117 | |
| 118 | // TODO: This lookup of the Opcode table is suboptimal! Binary sort would be |
| 119 | // preferable but the table requires sorting on the Opcode name, but it's |
| 120 | // static |
| 121 | // const initialized and matches the order of the spec. |
| 122 | const size_t nameLength = strlen(name); |
| 123 | for (uint64_t opcodeIndex = 0; opcodeIndex < table->count; ++opcodeIndex) { |
| 124 | if (nameLength == strlen(table->entries[opcodeIndex].name) && |
| 125 | !strncmp(name, table->entries[opcodeIndex].name, nameLength)) { |
| 126 | // NOTE: Found out Opcode! |
| 127 | *pEntry = &table->entries[opcodeIndex]; |
| 128 | return SPV_SUCCESS; |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | return SPV_ERROR_INVALID_LOOKUP; |
| 133 | } |
| 134 | |
| 135 | spv_result_t spvOpcodeTableValueLookup(const spv_opcode_table table, |
Lei Zhang | b36e704 | 2015-10-28 13:40:52 -0400 | [diff] [blame] | 136 | const SpvOp opcode, |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame] | 137 | spv_opcode_desc* pEntry) { |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 138 | if (!table) return SPV_ERROR_INVALID_TABLE; |
| 139 | if (!pEntry) return SPV_ERROR_INVALID_POINTER; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 140 | |
| 141 | // TODO: As above this lookup is not optimal. |
| 142 | for (uint64_t opcodeIndex = 0; opcodeIndex < table->count; ++opcodeIndex) { |
| 143 | if (opcode == table->entries[opcodeIndex].opcode) { |
| 144 | // NOTE: Found the Opcode! |
| 145 | *pEntry = &table->entries[opcodeIndex]; |
| 146 | return SPV_SUCCESS; |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | return SPV_ERROR_INVALID_LOOKUP; |
| 151 | } |
| 152 | |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 153 | int32_t spvOpcodeRequiresCapabilities(spv_opcode_desc entry) { |
David Neto | 9db3a53 | 2015-10-07 16:58:38 -0400 | [diff] [blame] | 154 | return entry->capabilities != 0; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 155 | } |
| 156 | |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame] | 157 | void spvInstructionCopy(const uint32_t* words, const SpvOp opcode, |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 158 | const uint16_t wordCount, const spv_endianness_t endian, |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame] | 159 | spv_instruction_t* pInst) { |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 160 | pInst->opcode = opcode; |
David Neto | b5dc8fc | 2015-10-06 16:22:00 -0400 | [diff] [blame] | 161 | pInst->words.resize(wordCount); |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 162 | for (uint16_t wordIndex = 0; wordIndex < wordCount; ++wordIndex) { |
| 163 | pInst->words[wordIndex] = spvFixWord(words[wordIndex], endian); |
| 164 | if (!wordIndex) { |
| 165 | uint16_t thisWordCount; |
Lei Zhang | 6fa3f8a | 2016-03-31 17:26:31 -0400 | [diff] [blame] | 166 | uint16_t thisOpcode; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 167 | spvOpcodeSplit(pInst->words[wordIndex], &thisWordCount, &thisOpcode); |
Lei Zhang | 6fa3f8a | 2016-03-31 17:26:31 -0400 | [diff] [blame] | 168 | assert(opcode == static_cast<SpvOp>(thisOpcode) && |
| 169 | wordCount == thisWordCount && "Endianness failed!"); |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 170 | } |
| 171 | } |
| 172 | } |
| 173 | |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame] | 174 | const char* spvOpcodeString(const SpvOp opcode) { |
Lei Zhang | 4f293b7 | 2016-03-21 16:36:14 -0400 | [diff] [blame] | 175 | for (uint32_t i = 0; |
| 176 | i < sizeof(opcodeTableEntries) / sizeof(spv_opcode_desc_t); ++i) { |
| 177 | if (opcodeTableEntries[i].opcode == opcode) |
| 178 | return opcodeTableEntries[i].name; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 179 | } |
Lei Zhang | 4f293b7 | 2016-03-21 16:36:14 -0400 | [diff] [blame] | 180 | assert(0 && "Unreachable!"); |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 181 | return "unknown"; |
| 182 | } |
| 183 | |
Lei Zhang | b36e704 | 2015-10-28 13:40:52 -0400 | [diff] [blame] | 184 | int32_t spvOpcodeIsScalarType(const SpvOp opcode) { |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 185 | switch (opcode) { |
Lei Zhang | b36e704 | 2015-10-28 13:40:52 -0400 | [diff] [blame] | 186 | case SpvOpTypeInt: |
| 187 | case SpvOpTypeFloat: |
Dejan Mircevski | 276a724 | 2016-01-21 15:55:43 -0500 | [diff] [blame] | 188 | case SpvOpTypeBool: |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 189 | return true; |
| 190 | default: |
| 191 | return false; |
| 192 | } |
| 193 | } |
| 194 | |
Lei Zhang | b36e704 | 2015-10-28 13:40:52 -0400 | [diff] [blame] | 195 | int32_t spvOpcodeIsConstant(const SpvOp opcode) { |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 196 | switch (opcode) { |
Lei Zhang | b36e704 | 2015-10-28 13:40:52 -0400 | [diff] [blame] | 197 | case SpvOpConstantTrue: |
| 198 | case SpvOpConstantFalse: |
| 199 | case SpvOpConstant: |
| 200 | case SpvOpConstantComposite: |
| 201 | case SpvOpConstantSampler: |
Lei Zhang | b36e704 | 2015-10-28 13:40:52 -0400 | [diff] [blame] | 202 | case SpvOpConstantNull: |
| 203 | case SpvOpSpecConstantTrue: |
| 204 | case SpvOpSpecConstantFalse: |
| 205 | case SpvOpSpecConstant: |
| 206 | case SpvOpSpecConstantComposite: |
Dejan Mircevski | 3fb2676 | 2016-04-04 15:55:05 -0400 | [diff] [blame] | 207 | case SpvOpSpecConstantOp: |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 208 | return true; |
| 209 | default: |
| 210 | return false; |
| 211 | } |
| 212 | } |
| 213 | |
Lei Zhang | b36e704 | 2015-10-28 13:40:52 -0400 | [diff] [blame] | 214 | int32_t spvOpcodeIsComposite(const SpvOp opcode) { |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 215 | switch (opcode) { |
Lei Zhang | b36e704 | 2015-10-28 13:40:52 -0400 | [diff] [blame] | 216 | case SpvOpTypeVector: |
| 217 | case SpvOpTypeMatrix: |
| 218 | case SpvOpTypeArray: |
| 219 | case SpvOpTypeStruct: |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 220 | return true; |
| 221 | default: |
| 222 | return false; |
| 223 | } |
| 224 | } |
| 225 | |
Lei Zhang | b36e704 | 2015-10-28 13:40:52 -0400 | [diff] [blame] | 226 | int32_t spvOpcodeIsPointer(const SpvOp opcode) { |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 227 | switch (opcode) { |
Lei Zhang | b36e704 | 2015-10-28 13:40:52 -0400 | [diff] [blame] | 228 | case SpvOpVariable: |
| 229 | case SpvOpAccessChain: |
Florian Ziesche | 680f9b7 | 2016-03-01 19:56:14 +0100 | [diff] [blame] | 230 | case SpvOpPtrAccessChain: |
Lei Zhang | b36e704 | 2015-10-28 13:40:52 -0400 | [diff] [blame] | 231 | case SpvOpInBoundsAccessChain: |
Florian Ziesche | 680f9b7 | 2016-03-01 19:56:14 +0100 | [diff] [blame] | 232 | case SpvOpInBoundsPtrAccessChain: |
Lei Zhang | b36e704 | 2015-10-28 13:40:52 -0400 | [diff] [blame] | 233 | case SpvOpFunctionParameter: |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 234 | return true; |
| 235 | default: |
| 236 | return false; |
| 237 | } |
| 238 | } |
| 239 | |
Lei Zhang | b36e704 | 2015-10-28 13:40:52 -0400 | [diff] [blame] | 240 | int32_t spvOpcodeGeneratesType(SpvOp op) { |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame] | 241 | switch (op) { |
Lei Zhang | b36e704 | 2015-10-28 13:40:52 -0400 | [diff] [blame] | 242 | case SpvOpTypeVoid: |
| 243 | case SpvOpTypeBool: |
| 244 | case SpvOpTypeInt: |
| 245 | case SpvOpTypeFloat: |
| 246 | case SpvOpTypeVector: |
| 247 | case SpvOpTypeMatrix: |
| 248 | case SpvOpTypeImage: |
| 249 | case SpvOpTypeSampler: |
| 250 | case SpvOpTypeSampledImage: |
| 251 | case SpvOpTypeArray: |
| 252 | case SpvOpTypeRuntimeArray: |
| 253 | case SpvOpTypeStruct: |
| 254 | case SpvOpTypeOpaque: |
| 255 | case SpvOpTypePointer: |
| 256 | case SpvOpTypeFunction: |
| 257 | case SpvOpTypeEvent: |
| 258 | case SpvOpTypeDeviceEvent: |
| 259 | case SpvOpTypeReserveId: |
| 260 | case SpvOpTypeQueue: |
| 261 | case SpvOpTypePipe: |
Andrew Woloszyn | 537e776 | 2015-09-29 11:28:34 -0400 | [diff] [blame] | 262 | return true; |
David Neto | aef608c | 2015-11-02 14:59:02 -0500 | [diff] [blame] | 263 | default: |
| 264 | // In particular, OpTypeForwardPointer does not generate a type, |
| 265 | // but declares a storage class for a pointer type generated |
| 266 | // by a different instruction. |
| 267 | break; |
Andrew Woloszyn | 537e776 | 2015-09-29 11:28:34 -0400 | [diff] [blame] | 268 | } |
| 269 | return 0; |
| 270 | } |