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