blob: c26e9b6ea6864d4f3def0a3341927a698fd76afb [file] [log] [blame]
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +01001// 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)83e5a292015-05-22 18:26:19 +010027#include "opcode.h"
28
29#include <assert.h>
30#include <string.h>
31
Lei Zhang972788b2015-11-12 13:48:30 -050032#include <cstdlib>
33
Lei Zhang923f6c12015-11-11 12:45:23 -050034#include "endian.h"
35#include "instruction.h"
36#include "libspirv/libspirv.h"
Lei Zhangaa056cd2015-11-11 14:24:04 -050037#include "spirv_constant.h"
Lei Zhang4ac601a2015-11-11 17:29:23 -050038#include "spirv_operands.h"
Lei Zhang923f6c12015-11-11 12:45:23 -050039
David Neto78c3b432015-08-27 13:03:52 -040040namespace {
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 Zhang972788b2015-11-12 13:48:30 -050051const spv_opcode_desc_t opcodeTableEntries[] = {
Lei Zhang1a0334e2015-11-02 09:41:20 -050052#define EmptyList \
53 {}
54#define List(...) \
55 { __VA_ARGS__ }
Lei Zhangb36e7042015-10-28 13:40:52 -040056#define Capability(X) SPV_CAPABILITY_AS_MASK(SpvCapability##X)
Lei Zhang1a0334e2015-11-02 09:41:20 -050057#define Capability2(X, Y) Capability(X) | Capability(Y)
58#define SpvCapabilityNone \
59 0 // Needed so Capability(None) still expands to valid syntax.
David Neto201caf72015-11-04 17:38:17 -050060#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 Neto78c3b432015-08-27 13:03:52 -040067#include "opcode.inc"
68#undef EmptyList
69#undef List
70#undef Capability
Dejan Mircevski205408b2015-09-30 16:42:34 -040071#undef Capability2
David Neto78c3b432015-08-27 13:03:52 -040072#undef CapabilityNone
73#undef Instruction
74};
75
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010076// Opcode API
77
David Neto78c3b432015-08-27 13:03:52 -040078// Converts the given operand class enum (from the SPIR-V document generation
David Neto0f166be2015-11-11 01:56:49 -050079// 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 Neto78c3b432015-08-27 13:03:52 -040081// This only applies to logical operands.
Lei Zhangb36e7042015-10-28 13:40:52 -040082spv_operand_type_t convertOperandClassToType(SpvOp opcode,
83 OperandClass operandClass) {
David Neto78c3b432015-08-27 13:03:52 -040084 // 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 Zhang40056702015-09-11 14:31:27 -040088 switch (opcode) {
Lei Zhangb36e7042015-10-28 13:40:52 -040089 case SpvOpLoad:
90 case SpvOpStore:
91 case SpvOpCopyMemory:
92 case SpvOpCopyMemorySized:
David Neto3fca4cd2015-09-17 17:39:45 -040093 // 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 Zhangb36e7042015-10-28 13:40:52 -040096 case SpvOpExecutionMode:
David Neto78c3b432015-08-27 13:03:52 -040097 return SPV_OPERAND_TYPE_VARIABLE_EXECUTION_MODE;
98 default:
99 break;
100 }
Lei Zhangb41d1502015-09-14 15:22:23 -0400101 } else if (operandClass == OperandVariableLiterals) {
David Neto201caf72015-11-04 17:38:17 -0500102 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 Neto78c3b432015-08-27 13:03:52 -0400106 }
107
Lei Zhang1a0334e2015-11-02 09:41:20 -0500108 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 Neto0f166be2015-11-11 01:56:49 -0500118 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 Zhang1a0334e2015-11-02 09:41:20 -0500125 return SPV_OPERAND_TYPE_VARIABLE_ID;
David Neto561dc4e2015-09-25 14:23:29 -0400126 // The spec only uses OptionalLiteral for an optional literal number.
Lei Zhang1a0334e2015-11-02 09:41:20 -0500127 case OperandOptionalLiteral:
128 return SPV_OPERAND_TYPE_OPTIONAL_LITERAL_INTEGER;
129 case OperandOptionalLiteralString:
130 return SPV_OPERAND_TYPE_OPTIONAL_LITERAL_STRING;
David Neto561dc4e2015-09-25 14:23:29 -0400131 // This is only used for sequences of literal numbers.
Lei Zhang1a0334e2015-11-02 09:41:20 -0500132 case OperandVariableLiterals:
133 return SPV_OPERAND_TYPE_VARIABLE_LITERAL_INTEGER;
David Neto445ce442015-10-15 15:22:06 -0400134 case OperandLiteralNumber:
Lei Zhangb36e7042015-10-28 13:40:52 -0400135 if (opcode == SpvOpExtInst) {
David Neto445ce442015-10-15 15:22:06 -0400136 // We use a special operand type for the extension instruction number.
Lei Zhang1a0334e2015-11-02 09:41:20 -0500137 // For now, we assume there is only one LiteraNumber argument to
138 // OpExtInst, and it is the extension instruction argument.
David Neto445ce442015-10-15 15:22:06 -0400139 // 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 Neto0f166be2015-11-11 01:56:49 -0500143 } 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 Neto445ce442015-10-15 15:22:06 -0400149 }
150 return SPV_OPERAND_TYPE_LITERAL_INTEGER;
Lei Zhang1a0334e2015-11-02 09:41:20 -0500151 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 Neto7cefb232015-09-28 15:33:49 -0400173 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 Zhang1a0334e2015-11-02 09:41:20 -0500178 // This is only used to describe the value generated by
179 // OpImageQueryFormat. It is not used as an operand.
David Neto7cefb232015-09-28 15:33:49 -0400180 break;
181 case OperandImageOperands:
Dejan Mircevskif8e091a2015-10-13 14:41:31 -0400182 // 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 Neto7cefb232015-09-28 15:33:49 -0400185 break;
Lei Zhang1a0334e2015-11-02 09:41:20 -0500186 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 Neto78c3b432015-08-27 13:03:52 -0400208 case OperandMemoryAccess:
David Neto7cefb232015-09-28 15:33:49 -0400209 // This case does not occur in the table for SPIR-V 0.99 Rev 32.
David Neto3fca4cd2015-09-17 17:39:45 -0400210 // We expect that it will become SPV_OPERAND_TYPE_OPTIONAL_MEMORY_ACCESS,
David Neto78c3b432015-08-27 13:03:52 -0400211 // and we can remove the special casing above for memory operation
212 // instructions.
213 break;
Lei Zhang1a0334e2015-11-02 09:41:20 -0500214 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 Neto78c3b432015-08-27 13:03:52 -0400224
225 // Used by GroupMemberDecorate
Lei Zhang1a0334e2015-11-02 09:41:20 -0500226 case OperandVariableIdLiteral:
227 return SPV_OPERAND_TYPE_VARIABLE_ID_LITERAL_INTEGER;
David Neto78c3b432015-08-27 13:03:52 -0400228
229 // Used by Switch
Lei Zhang1a0334e2015-11-02 09:41:20 -0500230 case OperandVariableLiteralId:
231 return SPV_OPERAND_TYPE_VARIABLE_LITERAL_INTEGER_ID;
David Neto78c3b432015-08-27 13:03:52 -0400232
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 Zhanga94701d2015-09-14 10:05:37 -0400242} // anonymous namespace
David Neto78c3b432015-08-27 13:03:52 -0400243
244// Finish populating the opcodeTableEntries array.
Lei Zhang972788b2015-11-12 13:48:30 -0500245void spvOpcodeTableInitialize(spv_opcode_desc_t* entries,
246 uint32_t num_entries) {
David Neto78c3b432015-08-27 13:03:52 -0400247 // Compute the operandTypes field for each entry.
Lei Zhang972788b2015-11-12 13:48:30 -0500248 for (uint32_t i = 0; i < num_entries; ++i) {
249 spv_opcode_desc_t& opcode = entries[i];
David Neto78c3b432015-08-27 13:03:52 -0400250 opcode.numTypes = 0;
251 // Type ID always comes first, if present.
252 if (opcode.hasType)
Andrew Woloszyn537e7762015-09-29 11:28:34 -0400253 opcode.operandTypes[opcode.numTypes++] = SPV_OPERAND_TYPE_TYPE_ID;
David Neto78c3b432015-08-27 13:03:52 -0400254 // Result ID always comes next, if present
255 if (opcode.hasResult)
256 opcode.operandTypes[opcode.numTypes++] = SPV_OPERAND_TYPE_RESULT_ID;
Lei Zhanga94701d2015-09-14 10:05:37 -0400257 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 Neto78c3b432015-08-27 13:03:52 -0400264 const OperandClass operandClass = opcode.operandClass[classIndex];
David Neto0f166be2015-11-11 01:56:49 -0500265 const auto operandType =
Lei Zhanga94701d2015-09-14 10:05:37 -0400266 convertOperandClassToType(opcode.opcode, operandClass);
David Neto0f166be2015-11-11 01:56:49 -0500267 opcode.operandTypes[opcode.numTypes++] = operandType;
David Neto78c3b432015-08-27 13:03:52 -0400268 // 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 Neto0f166be2015-11-11 01:56:49 -0500270 // 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 Neto78c3b432015-08-27 13:03:52 -0400274 opcode.numTypes--;
275 break;
276 }
277 }
David Neto0f166be2015-11-11 01:56:49 -0500278
David Neto78c3b432015-08-27 13:03:52 -0400279 // We should have written the terminating SPV_OPERAND_TYPE_NONE entry, but
280 // also without overflowing.
Lei Zhanga94701d2015-09-14 10:05:37 -0400281 assert((opcode.numTypes < maxNumOperands) &&
282 "Operand class list is too long. Expand "
283 "spv_opcode_desc_t.operandClass");
David Neto78c3b432015-08-27 13:03:52 -0400284 }
David Neto78c3b432015-08-27 13:03:52 -0400285}
286
Lei Zhang1a0334e2015-11-02 09:41:20 -0500287const char* spvGeneratorStr(uint32_t generator) {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100288 switch (generator) {
289 case SPV_GENERATOR_KHRONOS:
290 return "Khronos";
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100291 case SPV_GENERATOR_LUNARG:
292 return "LunarG";
David Neto1780fc42015-10-26 15:43:12 -0400293 case SPV_GENERATOR_VALVE:
294 return "Valve";
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100295 case SPV_GENERATOR_CODEPLAY:
296 return "Codeplay Software Ltd.";
David Neto1780fc42015-10-26 15:43:12 -0400297 case SPV_GENERATOR_NVIDIA:
298 return "NVIDIA";
299 case SPV_GENERATOR_ARM:
300 return "ARM";
David Neto14b93e42015-11-12 18:33:47 -0500301 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";
David Neto2266ba12015-11-13 12:03:28 -0600305 case SPV_GENERATOR_KHRONOS_GLSLANG:
306 return "Khronos Glslang Reference Front End";
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100307 default:
308 return "Unknown";
309 }
310}
311
Lei Zhangb36e7042015-10-28 13:40:52 -0400312uint32_t spvOpcodeMake(uint16_t wordCount, SpvOp opcode) {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100313 return ((uint32_t)opcode) | (((uint32_t)wordCount) << 16);
314}
315
Lei Zhang1a0334e2015-11-02 09:41:20 -0500316void spvOpcodeSplit(const uint32_t word, uint16_t* pWordCount, SpvOp* pOpcode) {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100317 if (pWordCount) {
318 *pWordCount = (uint16_t)((0xffff0000 & word) >> 16);
319 }
320 if (pOpcode) {
Lei Zhangb36e7042015-10-28 13:40:52 -0400321 *pOpcode = (SpvOp)(0x0000ffff & word);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100322 }
323}
324
Lei Zhang1a0334e2015-11-02 09:41:20 -0500325spv_result_t spvOpcodeTableGet(spv_opcode_table* pInstTable) {
Lei Zhang40056702015-09-11 14:31:27 -0400326 if (!pInstTable) return SPV_ERROR_INVALID_POINTER;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100327
Lei Zhang972788b2015-11-12 13:48:30 -0500328 const uint32_t size = sizeof(opcodeTableEntries);
329 spv_opcode_desc_t* copied_entries =
330 static_cast<spv_opcode_desc_t*>(::malloc(size));
331 if (!copied_entries) return SPV_ERROR_OUT_OF_MEMORY;
332 ::memcpy(copied_entries, opcodeTableEntries, size);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100333
Lei Zhang972788b2015-11-12 13:48:30 -0500334 const uint32_t count = sizeof(opcodeTableEntries) / sizeof(spv_opcode_desc_t);
335 spv_opcode_table_t* table = new spv_opcode_table_t{count, copied_entries};
David Neto78c3b432015-08-27 13:03:52 -0400336
Lei Zhang972788b2015-11-12 13:48:30 -0500337 spvOpcodeTableInitialize(copied_entries, count);
338
339 *pInstTable = table;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100340
341 return SPV_SUCCESS;
342}
343
344spv_result_t spvOpcodeTableNameLookup(const spv_opcode_table table,
Lei Zhang1a0334e2015-11-02 09:41:20 -0500345 const char* name,
346 spv_opcode_desc* pEntry) {
Lei Zhang40056702015-09-11 14:31:27 -0400347 if (!name || !pEntry) return SPV_ERROR_INVALID_POINTER;
348 if (!table) return SPV_ERROR_INVALID_TABLE;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100349
350 // TODO: This lookup of the Opcode table is suboptimal! Binary sort would be
351 // preferable but the table requires sorting on the Opcode name, but it's
352 // static
353 // const initialized and matches the order of the spec.
354 const size_t nameLength = strlen(name);
355 for (uint64_t opcodeIndex = 0; opcodeIndex < table->count; ++opcodeIndex) {
356 if (nameLength == strlen(table->entries[opcodeIndex].name) &&
357 !strncmp(name, table->entries[opcodeIndex].name, nameLength)) {
358 // NOTE: Found out Opcode!
359 *pEntry = &table->entries[opcodeIndex];
360 return SPV_SUCCESS;
361 }
362 }
363
364 return SPV_ERROR_INVALID_LOOKUP;
365}
366
367spv_result_t spvOpcodeTableValueLookup(const spv_opcode_table table,
Lei Zhangb36e7042015-10-28 13:40:52 -0400368 const SpvOp opcode,
Lei Zhang1a0334e2015-11-02 09:41:20 -0500369 spv_opcode_desc* pEntry) {
Lei Zhang40056702015-09-11 14:31:27 -0400370 if (!table) return SPV_ERROR_INVALID_TABLE;
371 if (!pEntry) return SPV_ERROR_INVALID_POINTER;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100372
373 // TODO: As above this lookup is not optimal.
374 for (uint64_t opcodeIndex = 0; opcodeIndex < table->count; ++opcodeIndex) {
375 if (opcode == table->entries[opcodeIndex].opcode) {
376 // NOTE: Found the Opcode!
377 *pEntry = &table->entries[opcodeIndex];
378 return SPV_SUCCESS;
379 }
380 }
381
382 return SPV_ERROR_INVALID_LOOKUP;
383}
384
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100385int32_t spvOpcodeRequiresCapabilities(spv_opcode_desc entry) {
David Neto9db3a532015-10-07 16:58:38 -0400386 return entry->capabilities != 0;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100387}
388
Lei Zhang1a0334e2015-11-02 09:41:20 -0500389void spvInstructionCopy(const uint32_t* words, const SpvOp opcode,
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100390 const uint16_t wordCount, const spv_endianness_t endian,
Lei Zhang1a0334e2015-11-02 09:41:20 -0500391 spv_instruction_t* pInst) {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100392 pInst->opcode = opcode;
David Netob5dc8fc2015-10-06 16:22:00 -0400393 pInst->words.resize(wordCount);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100394 for (uint16_t wordIndex = 0; wordIndex < wordCount; ++wordIndex) {
395 pInst->words[wordIndex] = spvFixWord(words[wordIndex], endian);
396 if (!wordIndex) {
397 uint16_t thisWordCount;
Lei Zhangb36e7042015-10-28 13:40:52 -0400398 SpvOp thisOpcode;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100399 spvOpcodeSplit(pInst->words[wordIndex], &thisWordCount, &thisOpcode);
400 assert(opcode == thisOpcode && wordCount == thisWordCount &&
401 "Endianness failed!");
402 }
403 }
404}
405
Lei Zhang1a0334e2015-11-02 09:41:20 -0500406const char* spvOpcodeString(const SpvOp opcode) {
David Neto201caf72015-11-04 17:38:17 -0500407// Use the syntax table so it's sure to be complete.
David Neto1bcd3d12015-11-02 16:03:12 -0500408#define Instruction(Name, ...) \
409 case SpvOp##Name: \
410 return #Name;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100411 switch (opcode) {
David Neto1bcd3d12015-11-02 16:03:12 -0500412#include "opcode.inc"
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100413 default:
414 assert(0 && "Unreachable!");
415 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100416 return "unknown";
David Neto1bcd3d12015-11-02 16:03:12 -0500417#undef Instruction
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100418}
419
Lei Zhangb36e7042015-10-28 13:40:52 -0400420int32_t spvOpcodeIsScalarType(const SpvOp opcode) {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100421 switch (opcode) {
Lei Zhangb36e7042015-10-28 13:40:52 -0400422 case SpvOpTypeInt:
423 case SpvOpTypeFloat:
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100424 return true;
425 default:
426 return false;
427 }
428}
429
Lei Zhangb36e7042015-10-28 13:40:52 -0400430int32_t spvOpcodeIsConstant(const SpvOp opcode) {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100431 switch (opcode) {
Lei Zhangb36e7042015-10-28 13:40:52 -0400432 case SpvOpConstantTrue:
433 case SpvOpConstantFalse:
434 case SpvOpConstant:
435 case SpvOpConstantComposite:
436 case SpvOpConstantSampler:
437 // case SpvOpConstantNull:
438 case SpvOpConstantNull:
439 case SpvOpSpecConstantTrue:
440 case SpvOpSpecConstantFalse:
441 case SpvOpSpecConstant:
442 case SpvOpSpecConstantComposite:
443 // case SpvOpSpecConstantOp:
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100444 return true;
445 default:
446 return false;
447 }
448}
449
Lei Zhangb36e7042015-10-28 13:40:52 -0400450int32_t spvOpcodeIsComposite(const SpvOp opcode) {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100451 switch (opcode) {
Lei Zhangb36e7042015-10-28 13:40:52 -0400452 case SpvOpTypeVector:
453 case SpvOpTypeMatrix:
454 case SpvOpTypeArray:
455 case SpvOpTypeStruct:
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100456 return true;
457 default:
458 return false;
459 }
460}
461
Lei Zhang1a0334e2015-11-02 09:41:20 -0500462int32_t spvOpcodeAreTypesEqual(const spv_instruction_t* pTypeInst0,
463 const spv_instruction_t* pTypeInst1) {
Lei Zhang40056702015-09-11 14:31:27 -0400464 if (pTypeInst0->opcode != pTypeInst1->opcode) return false;
465 if (pTypeInst0->words[1] != pTypeInst1->words[1]) return false;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100466 return true;
467}
468
Lei Zhangb36e7042015-10-28 13:40:52 -0400469int32_t spvOpcodeIsPointer(const SpvOp opcode) {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100470 switch (opcode) {
Lei Zhangb36e7042015-10-28 13:40:52 -0400471 case SpvOpVariable:
472 case SpvOpAccessChain:
473 case SpvOpInBoundsAccessChain:
474 case SpvOpFunctionParameter:
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100475 return true;
476 default:
477 return false;
478 }
479}
480
Lei Zhangb36e7042015-10-28 13:40:52 -0400481int32_t spvOpcodeIsObject(const SpvOp opcode) {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100482 switch (opcode) {
Lei Zhangb36e7042015-10-28 13:40:52 -0400483 case SpvOpConstantTrue:
484 case SpvOpConstantFalse:
485 case SpvOpConstant:
486 case SpvOpConstantComposite:
487 // TODO: case SpvOpConstantSampler:
488 case SpvOpConstantNull:
489 case SpvOpSpecConstantTrue:
490 case SpvOpSpecConstantFalse:
491 case SpvOpSpecConstant:
492 case SpvOpSpecConstantComposite:
493 // TODO: case SpvOpSpecConstantOp:
494 case SpvOpVariable:
495 case SpvOpAccessChain:
496 case SpvOpInBoundsAccessChain:
497 case SpvOpConvertFToU:
498 case SpvOpConvertFToS:
499 case SpvOpConvertSToF:
500 case SpvOpConvertUToF:
501 case SpvOpUConvert:
502 case SpvOpSConvert:
503 case SpvOpFConvert:
504 case SpvOpConvertPtrToU:
505 // TODO: case SpvOpConvertUToPtr:
506 case SpvOpPtrCastToGeneric:
507 // TODO: case SpvOpGenericCastToPtr:
508 case SpvOpBitcast:
509 // TODO: case SpvOpGenericCastToPtrExplicit:
510 case SpvOpSatConvertSToU:
511 case SpvOpSatConvertUToS:
512 case SpvOpVectorExtractDynamic:
513 case SpvOpCompositeConstruct:
514 case SpvOpCompositeExtract:
515 case SpvOpCopyObject:
516 case SpvOpTranspose:
517 case SpvOpSNegate:
518 case SpvOpFNegate:
519 case SpvOpNot:
520 case SpvOpIAdd:
521 case SpvOpFAdd:
522 case SpvOpISub:
523 case SpvOpFSub:
524 case SpvOpIMul:
525 case SpvOpFMul:
526 case SpvOpUDiv:
527 case SpvOpSDiv:
528 case SpvOpFDiv:
529 case SpvOpUMod:
530 case SpvOpSRem:
531 case SpvOpSMod:
532 case SpvOpVectorTimesScalar:
533 case SpvOpMatrixTimesScalar:
534 case SpvOpVectorTimesMatrix:
535 case SpvOpMatrixTimesVector:
536 case SpvOpMatrixTimesMatrix:
537 case SpvOpOuterProduct:
538 case SpvOpDot:
539 case SpvOpShiftRightLogical:
540 case SpvOpShiftRightArithmetic:
541 case SpvOpShiftLeftLogical:
542 case SpvOpBitwiseOr:
543 case SpvOpBitwiseXor:
544 case SpvOpBitwiseAnd:
545 case SpvOpAny:
546 case SpvOpAll:
547 case SpvOpIsNan:
548 case SpvOpIsInf:
549 case SpvOpIsFinite:
550 case SpvOpIsNormal:
551 case SpvOpSignBitSet:
552 case SpvOpLessOrGreater:
553 case SpvOpOrdered:
554 case SpvOpUnordered:
555 case SpvOpLogicalOr:
556 case SpvOpLogicalAnd:
557 case SpvOpSelect:
558 case SpvOpIEqual:
559 case SpvOpFOrdEqual:
560 case SpvOpFUnordEqual:
561 case SpvOpINotEqual:
562 case SpvOpFOrdNotEqual:
563 case SpvOpFUnordNotEqual:
564 case SpvOpULessThan:
565 case SpvOpSLessThan:
566 case SpvOpFOrdLessThan:
567 case SpvOpFUnordLessThan:
568 case SpvOpUGreaterThan:
569 case SpvOpSGreaterThan:
570 case SpvOpFOrdGreaterThan:
571 case SpvOpFUnordGreaterThan:
572 case SpvOpULessThanEqual:
573 case SpvOpSLessThanEqual:
574 case SpvOpFOrdLessThanEqual:
575 case SpvOpFUnordLessThanEqual:
576 case SpvOpUGreaterThanEqual:
577 case SpvOpSGreaterThanEqual:
578 case SpvOpFOrdGreaterThanEqual:
579 case SpvOpFUnordGreaterThanEqual:
580 case SpvOpDPdx:
581 case SpvOpDPdy:
582 case SpvOpFwidth:
583 case SpvOpDPdxFine:
584 case SpvOpDPdyFine:
585 case SpvOpFwidthFine:
586 case SpvOpDPdxCoarse:
587 case SpvOpDPdyCoarse:
588 case SpvOpFwidthCoarse:
589 case SpvOpReturnValue:
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100590 return true;
591 default:
592 return false;
593 }
594}
595
Lei Zhangb36e7042015-10-28 13:40:52 -0400596int32_t spvOpcodeIsBasicTypeNullable(SpvOp opcode) {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100597 switch (opcode) {
Lei Zhangb36e7042015-10-28 13:40:52 -0400598 case SpvOpTypeBool:
599 case SpvOpTypeInt:
600 case SpvOpTypeFloat:
601 case SpvOpTypePointer:
602 case SpvOpTypeEvent:
603 case SpvOpTypeDeviceEvent:
604 case SpvOpTypeReserveId:
605 case SpvOpTypeQueue:
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100606 return true;
607 default:
608 return false;
609 }
610}
611
Lei Zhang1a0334e2015-11-02 09:41:20 -0500612int32_t spvInstructionIsInBasicBlock(const spv_instruction_t* pFirstInst,
613 const spv_instruction_t* pInst) {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100614 while (pFirstInst != pInst) {
Lei Zhangb36e7042015-10-28 13:40:52 -0400615 if (SpvOpFunction == pInst->opcode) break;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100616 pInst--;
617 }
Lei Zhangb36e7042015-10-28 13:40:52 -0400618 if (SpvOpFunction != pInst->opcode) return false;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100619 return true;
620}
621
Lei Zhangb36e7042015-10-28 13:40:52 -0400622int32_t spvOpcodeIsValue(SpvOp opcode) {
Lei Zhang40056702015-09-11 14:31:27 -0400623 if (spvOpcodeIsPointer(opcode)) return true;
624 if (spvOpcodeIsConstant(opcode)) return true;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100625 switch (opcode) {
Lei Zhangb36e7042015-10-28 13:40:52 -0400626 case SpvOpLoad:
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100627 // TODO: Other Opcode's resulting in a value
628 return true;
629 default:
630 return false;
631 }
632}
Andrew Woloszyn537e7762015-09-29 11:28:34 -0400633
Lei Zhangb36e7042015-10-28 13:40:52 -0400634int32_t spvOpcodeGeneratesType(SpvOp op) {
Lei Zhang1a0334e2015-11-02 09:41:20 -0500635 switch (op) {
Lei Zhangb36e7042015-10-28 13:40:52 -0400636 case SpvOpTypeVoid:
637 case SpvOpTypeBool:
638 case SpvOpTypeInt:
639 case SpvOpTypeFloat:
640 case SpvOpTypeVector:
641 case SpvOpTypeMatrix:
642 case SpvOpTypeImage:
643 case SpvOpTypeSampler:
644 case SpvOpTypeSampledImage:
645 case SpvOpTypeArray:
646 case SpvOpTypeRuntimeArray:
647 case SpvOpTypeStruct:
648 case SpvOpTypeOpaque:
649 case SpvOpTypePointer:
650 case SpvOpTypeFunction:
651 case SpvOpTypeEvent:
652 case SpvOpTypeDeviceEvent:
653 case SpvOpTypeReserveId:
654 case SpvOpTypeQueue:
655 case SpvOpTypePipe:
Andrew Woloszyn537e7762015-09-29 11:28:34 -0400656 return true;
David Netoaef608c2015-11-02 14:59:02 -0500657 default:
658 // In particular, OpTypeForwardPointer does not generate a type,
659 // but declares a storage class for a pointer type generated
660 // by a different instruction.
661 break;
Andrew Woloszyn537e7762015-09-29 11:28:34 -0400662 }
663 return 0;
664}