blob: 3027bcda9fe0f077ad989a5efc7b1b8913179f16 [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 Neto39fa1482015-11-30 14:39:31 -0500102 switch (opcode) {
103 case SpvOpConstant:
104 case SpvOpSpecConstant:
105 // The number type is determined by the type Id operand.
106 return SPV_OPERAND_TYPE_TYPED_LITERAL_NUMBER;
107 case SpvOpDecorate:
108 case SpvOpMemberDecorate:
109 // The operand types at the end of the instruction are
110 // determined instead by the decoration kind.
111 return SPV_OPERAND_TYPE_NONE;
112 default:
113 break;
David Neto201caf72015-11-04 17:38:17 -0500114 }
David Neto78c3b432015-08-27 13:03:52 -0400115 }
116
Lei Zhang1a0334e2015-11-02 09:41:20 -0500117 switch (operandClass) {
118 case OperandNone:
119 return SPV_OPERAND_TYPE_NONE;
120 case OperandId:
121 return SPV_OPERAND_TYPE_ID;
122 case OperandOptionalId:
123 return SPV_OPERAND_TYPE_OPTIONAL_ID;
124 case OperandOptionalImage:
125 return SPV_OPERAND_TYPE_OPTIONAL_IMAGE;
126 case OperandVariableIds:
David Neto0f166be2015-11-11 01:56:49 -0500127 if (opcode == SpvOpSpecConstantOp) {
128 // These are the operands to the specialization constant opcode.
129 // The assembler and binary parser set up the extra Id and literal
130 // arguments when processing the opcode operand. So don't add
131 // an operand type for them here.
132 return SPV_OPERAND_TYPE_NONE;
133 }
Lei Zhang1a0334e2015-11-02 09:41:20 -0500134 return SPV_OPERAND_TYPE_VARIABLE_ID;
David Neto561dc4e2015-09-25 14:23:29 -0400135 // The spec only uses OptionalLiteral for an optional literal number.
Lei Zhang1a0334e2015-11-02 09:41:20 -0500136 case OperandOptionalLiteral:
137 return SPV_OPERAND_TYPE_OPTIONAL_LITERAL_INTEGER;
138 case OperandOptionalLiteralString:
139 return SPV_OPERAND_TYPE_OPTIONAL_LITERAL_STRING;
David Neto561dc4e2015-09-25 14:23:29 -0400140 // This is only used for sequences of literal numbers.
Lei Zhang1a0334e2015-11-02 09:41:20 -0500141 case OperandVariableLiterals:
142 return SPV_OPERAND_TYPE_VARIABLE_LITERAL_INTEGER;
David Neto445ce442015-10-15 15:22:06 -0400143 case OperandLiteralNumber:
Lei Zhangb36e7042015-10-28 13:40:52 -0400144 if (opcode == SpvOpExtInst) {
David Neto445ce442015-10-15 15:22:06 -0400145 // We use a special operand type for the extension instruction number.
Lei Zhang1a0334e2015-11-02 09:41:20 -0500146 // For now, we assume there is only one LiteraNumber argument to
147 // OpExtInst, and it is the extension instruction argument.
David Neto445ce442015-10-15 15:22:06 -0400148 // See the ExtInst entry in opcode.inc
149 // TODO(dneto): Use a function to confirm the assumption, and to verify
150 // that the index into the operandClass is 1, as expected.
151 return SPV_OPERAND_TYPE_EXTENSION_INSTRUCTION_NUMBER;
David Neto0f166be2015-11-11 01:56:49 -0500152 } else if (opcode == SpvOpSpecConstantOp) {
153 // Use a special operand type for the opcode operand, so we can
154 // use mnemonic names instead of the numbers. For example, the
155 // assembler should accept "IAdd" instead of the numeric value of
156 // SpvOpIAdd.
157 return SPV_OPERAND_TYPE_SPEC_CONSTANT_OP_NUMBER;
David Neto445ce442015-10-15 15:22:06 -0400158 }
159 return SPV_OPERAND_TYPE_LITERAL_INTEGER;
Lei Zhang1a0334e2015-11-02 09:41:20 -0500160 case OperandLiteralString:
161 return SPV_OPERAND_TYPE_LITERAL_STRING;
162 case OperandSource:
163 return SPV_OPERAND_TYPE_SOURCE_LANGUAGE;
164 case OperandExecutionModel:
165 return SPV_OPERAND_TYPE_EXECUTION_MODEL;
166 case OperandAddressing:
167 return SPV_OPERAND_TYPE_ADDRESSING_MODEL;
168 case OperandMemory:
169 return SPV_OPERAND_TYPE_MEMORY_MODEL;
170 case OperandExecutionMode:
171 return SPV_OPERAND_TYPE_EXECUTION_MODE;
172 case OperandStorage:
173 return SPV_OPERAND_TYPE_STORAGE_CLASS;
174 case OperandDimensionality:
175 return SPV_OPERAND_TYPE_DIMENSIONALITY;
176 case OperandSamplerAddressingMode:
177 return SPV_OPERAND_TYPE_SAMPLER_ADDRESSING_MODE;
178 case OperandSamplerFilterMode:
179 return SPV_OPERAND_TYPE_SAMPLER_FILTER_MODE;
180 case OperandSamplerImageFormat:
181 return SPV_OPERAND_TYPE_SAMPLER_IMAGE_FORMAT;
David Neto7cefb232015-09-28 15:33:49 -0400182 case OperandImageChannelOrder:
183 // This is only used to describe the value generated by OpImageQueryOrder.
184 // It is not used as an operand.
185 break;
186 case OperandImageChannelDataType:
Lei Zhang1a0334e2015-11-02 09:41:20 -0500187 // This is only used to describe the value generated by
188 // OpImageQueryFormat. It is not used as an operand.
David Neto7cefb232015-09-28 15:33:49 -0400189 break;
190 case OperandImageOperands:
Dejan Mircevskif8e091a2015-10-13 14:41:31 -0400191 // This is not used in opcode.inc. It only exists to generate the
192 // corresponding spec section. In parsing, image operands meld into the
193 // OperandOptionalImage case.
David Neto7cefb232015-09-28 15:33:49 -0400194 break;
Lei Zhang1a0334e2015-11-02 09:41:20 -0500195 case OperandFPFastMath:
196 return SPV_OPERAND_TYPE_FP_FAST_MATH_MODE;
197 case OperandFPRoundingMode:
198 return SPV_OPERAND_TYPE_FP_ROUNDING_MODE;
199 case OperandLinkageType:
200 return SPV_OPERAND_TYPE_LINKAGE_TYPE;
201 case OperandAccessQualifier:
202 return SPV_OPERAND_TYPE_ACCESS_QUALIFIER;
203 case OperandFuncParamAttr:
204 return SPV_OPERAND_TYPE_FUNCTION_PARAMETER_ATTRIBUTE;
205 case OperandDecoration:
206 return SPV_OPERAND_TYPE_DECORATION;
207 case OperandBuiltIn:
208 return SPV_OPERAND_TYPE_BUILT_IN;
209 case OperandSelect:
210 return SPV_OPERAND_TYPE_SELECTION_CONTROL;
211 case OperandLoop:
212 return SPV_OPERAND_TYPE_LOOP_CONTROL;
213 case OperandFunction:
214 return SPV_OPERAND_TYPE_FUNCTION_CONTROL;
215 case OperandMemorySemantics:
David Neto64a9be92015-11-18 15:48:32 -0500216 return SPV_OPERAND_TYPE_MEMORY_SEMANTICS_ID;
David Neto78c3b432015-08-27 13:03:52 -0400217 case OperandMemoryAccess:
David Neto7cefb232015-09-28 15:33:49 -0400218 // This case does not occur in the table for SPIR-V 0.99 Rev 32.
David Neto3fca4cd2015-09-17 17:39:45 -0400219 // We expect that it will become SPV_OPERAND_TYPE_OPTIONAL_MEMORY_ACCESS,
David Neto78c3b432015-08-27 13:03:52 -0400220 // and we can remove the special casing above for memory operation
221 // instructions.
222 break;
Lei Zhang1a0334e2015-11-02 09:41:20 -0500223 case OperandScope:
David Neto64a9be92015-11-18 15:48:32 -0500224 return SPV_OPERAND_TYPE_SCOPE_ID;
Lei Zhang1a0334e2015-11-02 09:41:20 -0500225 case OperandGroupOperation:
226 return SPV_OPERAND_TYPE_GROUP_OPERATION;
227 case OperandKernelEnqueueFlags:
228 return SPV_OPERAND_TYPE_KERNEL_ENQ_FLAGS;
229 case OperandKernelProfilingInfo:
230 return SPV_OPERAND_TYPE_KERNEL_PROFILING_INFO;
231 case OperandCapability:
232 return SPV_OPERAND_TYPE_CAPABILITY;
David Neto78c3b432015-08-27 13:03:52 -0400233
234 // Used by GroupMemberDecorate
Lei Zhang1a0334e2015-11-02 09:41:20 -0500235 case OperandVariableIdLiteral:
236 return SPV_OPERAND_TYPE_VARIABLE_ID_LITERAL_INTEGER;
David Neto78c3b432015-08-27 13:03:52 -0400237
238 // Used by Switch
Lei Zhang1a0334e2015-11-02 09:41:20 -0500239 case OperandVariableLiteralId:
240 return SPV_OPERAND_TYPE_VARIABLE_LITERAL_INTEGER_ID;
David Neto78c3b432015-08-27 13:03:52 -0400241
242 // These exceptional cases shouldn't occur.
243 case OperandCount:
244 default:
245 break;
246 }
247 assert(0 && "Unexpected operand class");
248 return SPV_OPERAND_TYPE_NONE;
249}
250
Lei Zhanga94701d2015-09-14 10:05:37 -0400251} // anonymous namespace
David Neto78c3b432015-08-27 13:03:52 -0400252
253// Finish populating the opcodeTableEntries array.
Lei Zhang972788b2015-11-12 13:48:30 -0500254void spvOpcodeTableInitialize(spv_opcode_desc_t* entries,
255 uint32_t num_entries) {
David Neto78c3b432015-08-27 13:03:52 -0400256 // Compute the operandTypes field for each entry.
Lei Zhang972788b2015-11-12 13:48:30 -0500257 for (uint32_t i = 0; i < num_entries; ++i) {
258 spv_opcode_desc_t& opcode = entries[i];
David Neto78c3b432015-08-27 13:03:52 -0400259 opcode.numTypes = 0;
260 // Type ID always comes first, if present.
261 if (opcode.hasType)
Andrew Woloszyn537e7762015-09-29 11:28:34 -0400262 opcode.operandTypes[opcode.numTypes++] = SPV_OPERAND_TYPE_TYPE_ID;
David Neto78c3b432015-08-27 13:03:52 -0400263 // Result ID always comes next, if present
264 if (opcode.hasResult)
265 opcode.operandTypes[opcode.numTypes++] = SPV_OPERAND_TYPE_RESULT_ID;
Lei Zhanga94701d2015-09-14 10:05:37 -0400266 const uint16_t maxNumOperands =
267 sizeof(opcode.operandTypes) / sizeof(opcode.operandTypes[0]);
268 const uint16_t maxNumClasses =
269 sizeof(opcode.operandClass) / sizeof(opcode.operandClass[0]);
270 for (uint16_t classIndex = 0;
271 opcode.numTypes < maxNumOperands && classIndex < maxNumClasses;
272 classIndex++) {
David Neto78c3b432015-08-27 13:03:52 -0400273 const OperandClass operandClass = opcode.operandClass[classIndex];
David Neto0f166be2015-11-11 01:56:49 -0500274 const auto operandType =
Lei Zhanga94701d2015-09-14 10:05:37 -0400275 convertOperandClassToType(opcode.opcode, operandClass);
David Neto0f166be2015-11-11 01:56:49 -0500276 opcode.operandTypes[opcode.numTypes++] = operandType;
David Neto78c3b432015-08-27 13:03:52 -0400277 // The OperandNone value is not explicitly represented in the .inc file.
278 // However, it is the zero value, and is created via implicit value
David Neto0f166be2015-11-11 01:56:49 -0500279 // initialization. It converts to SPV_OPERAND_TYPE_NONE.
280 // The SPV_OPERAND_TYPE_NONE operand type indicates no current or futher
281 // operands.
282 if (operandType == SPV_OPERAND_TYPE_NONE) {
David Neto78c3b432015-08-27 13:03:52 -0400283 opcode.numTypes--;
284 break;
285 }
286 }
David Neto0f166be2015-11-11 01:56:49 -0500287
David Neto78c3b432015-08-27 13:03:52 -0400288 // We should have written the terminating SPV_OPERAND_TYPE_NONE entry, but
289 // also without overflowing.
Lei Zhanga94701d2015-09-14 10:05:37 -0400290 assert((opcode.numTypes < maxNumOperands) &&
291 "Operand class list is too long. Expand "
292 "spv_opcode_desc_t.operandClass");
David Neto78c3b432015-08-27 13:03:52 -0400293 }
David Neto78c3b432015-08-27 13:03:52 -0400294}
295
Lei Zhang1a0334e2015-11-02 09:41:20 -0500296const char* spvGeneratorStr(uint32_t generator) {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100297 switch (generator) {
298 case SPV_GENERATOR_KHRONOS:
299 return "Khronos";
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100300 case SPV_GENERATOR_LUNARG:
301 return "LunarG";
David Neto1780fc42015-10-26 15:43:12 -0400302 case SPV_GENERATOR_VALVE:
303 return "Valve";
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100304 case SPV_GENERATOR_CODEPLAY:
305 return "Codeplay Software Ltd.";
David Neto1780fc42015-10-26 15:43:12 -0400306 case SPV_GENERATOR_NVIDIA:
307 return "NVIDIA";
308 case SPV_GENERATOR_ARM:
309 return "ARM";
David Neto14b93e42015-11-12 18:33:47 -0500310 case SPV_GENERATOR_KHRONOS_LLVM_TRANSLATOR:
311 return "Khronos LLVM/SPIR-V Translator";
312 case SPV_GENERATOR_KHRONOS_ASSEMBLER:
313 return "Khronos SPIR-V Tools Assembler";
David Neto2266ba12015-11-13 12:03:28 -0600314 case SPV_GENERATOR_KHRONOS_GLSLANG:
315 return "Khronos Glslang Reference Front End";
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100316 default:
317 return "Unknown";
318 }
319}
320
Lei Zhangb36e7042015-10-28 13:40:52 -0400321uint32_t spvOpcodeMake(uint16_t wordCount, SpvOp opcode) {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100322 return ((uint32_t)opcode) | (((uint32_t)wordCount) << 16);
323}
324
Lei Zhang1a0334e2015-11-02 09:41:20 -0500325void spvOpcodeSplit(const uint32_t word, uint16_t* pWordCount, SpvOp* pOpcode) {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100326 if (pWordCount) {
327 *pWordCount = (uint16_t)((0xffff0000 & word) >> 16);
328 }
329 if (pOpcode) {
Lei Zhangb36e7042015-10-28 13:40:52 -0400330 *pOpcode = (SpvOp)(0x0000ffff & word);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100331 }
332}
333
Lei Zhang1a0334e2015-11-02 09:41:20 -0500334spv_result_t spvOpcodeTableGet(spv_opcode_table* pInstTable) {
Lei Zhang40056702015-09-11 14:31:27 -0400335 if (!pInstTable) return SPV_ERROR_INVALID_POINTER;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100336
Lei Zhang972788b2015-11-12 13:48:30 -0500337 const uint32_t size = sizeof(opcodeTableEntries);
338 spv_opcode_desc_t* copied_entries =
339 static_cast<spv_opcode_desc_t*>(::malloc(size));
340 if (!copied_entries) return SPV_ERROR_OUT_OF_MEMORY;
341 ::memcpy(copied_entries, opcodeTableEntries, size);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100342
Lei Zhang972788b2015-11-12 13:48:30 -0500343 const uint32_t count = sizeof(opcodeTableEntries) / sizeof(spv_opcode_desc_t);
344 spv_opcode_table_t* table = new spv_opcode_table_t{count, copied_entries};
David Neto78c3b432015-08-27 13:03:52 -0400345
Lei Zhang972788b2015-11-12 13:48:30 -0500346 spvOpcodeTableInitialize(copied_entries, count);
347
348 *pInstTable = table;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100349
350 return SPV_SUCCESS;
351}
352
353spv_result_t spvOpcodeTableNameLookup(const spv_opcode_table table,
Lei Zhang1a0334e2015-11-02 09:41:20 -0500354 const char* name,
355 spv_opcode_desc* pEntry) {
Lei Zhang40056702015-09-11 14:31:27 -0400356 if (!name || !pEntry) return SPV_ERROR_INVALID_POINTER;
357 if (!table) return SPV_ERROR_INVALID_TABLE;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100358
359 // TODO: This lookup of the Opcode table is suboptimal! Binary sort would be
360 // preferable but the table requires sorting on the Opcode name, but it's
361 // static
362 // const initialized and matches the order of the spec.
363 const size_t nameLength = strlen(name);
364 for (uint64_t opcodeIndex = 0; opcodeIndex < table->count; ++opcodeIndex) {
365 if (nameLength == strlen(table->entries[opcodeIndex].name) &&
366 !strncmp(name, table->entries[opcodeIndex].name, nameLength)) {
367 // NOTE: Found out Opcode!
368 *pEntry = &table->entries[opcodeIndex];
369 return SPV_SUCCESS;
370 }
371 }
372
373 return SPV_ERROR_INVALID_LOOKUP;
374}
375
376spv_result_t spvOpcodeTableValueLookup(const spv_opcode_table table,
Lei Zhangb36e7042015-10-28 13:40:52 -0400377 const SpvOp opcode,
Lei Zhang1a0334e2015-11-02 09:41:20 -0500378 spv_opcode_desc* pEntry) {
Lei Zhang40056702015-09-11 14:31:27 -0400379 if (!table) return SPV_ERROR_INVALID_TABLE;
380 if (!pEntry) return SPV_ERROR_INVALID_POINTER;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100381
382 // TODO: As above this lookup is not optimal.
383 for (uint64_t opcodeIndex = 0; opcodeIndex < table->count; ++opcodeIndex) {
384 if (opcode == table->entries[opcodeIndex].opcode) {
385 // NOTE: Found the Opcode!
386 *pEntry = &table->entries[opcodeIndex];
387 return SPV_SUCCESS;
388 }
389 }
390
391 return SPV_ERROR_INVALID_LOOKUP;
392}
393
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100394int32_t spvOpcodeRequiresCapabilities(spv_opcode_desc entry) {
David Neto9db3a532015-10-07 16:58:38 -0400395 return entry->capabilities != 0;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100396}
397
Lei Zhang1a0334e2015-11-02 09:41:20 -0500398void spvInstructionCopy(const uint32_t* words, const SpvOp opcode,
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100399 const uint16_t wordCount, const spv_endianness_t endian,
Lei Zhang1a0334e2015-11-02 09:41:20 -0500400 spv_instruction_t* pInst) {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100401 pInst->opcode = opcode;
David Netob5dc8fc2015-10-06 16:22:00 -0400402 pInst->words.resize(wordCount);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100403 for (uint16_t wordIndex = 0; wordIndex < wordCount; ++wordIndex) {
404 pInst->words[wordIndex] = spvFixWord(words[wordIndex], endian);
405 if (!wordIndex) {
406 uint16_t thisWordCount;
Lei Zhangb36e7042015-10-28 13:40:52 -0400407 SpvOp thisOpcode;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100408 spvOpcodeSplit(pInst->words[wordIndex], &thisWordCount, &thisOpcode);
409 assert(opcode == thisOpcode && wordCount == thisWordCount &&
410 "Endianness failed!");
411 }
412 }
413}
414
Lei Zhang1a0334e2015-11-02 09:41:20 -0500415const char* spvOpcodeString(const SpvOp opcode) {
David Neto201caf72015-11-04 17:38:17 -0500416// Use the syntax table so it's sure to be complete.
David Neto1bcd3d12015-11-02 16:03:12 -0500417#define Instruction(Name, ...) \
418 case SpvOp##Name: \
419 return #Name;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100420 switch (opcode) {
David Neto1bcd3d12015-11-02 16:03:12 -0500421#include "opcode.inc"
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100422 default:
423 assert(0 && "Unreachable!");
424 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100425 return "unknown";
David Neto1bcd3d12015-11-02 16:03:12 -0500426#undef Instruction
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100427}
428
Lei Zhangb36e7042015-10-28 13:40:52 -0400429int32_t spvOpcodeIsScalarType(const SpvOp opcode) {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100430 switch (opcode) {
Lei Zhangb36e7042015-10-28 13:40:52 -0400431 case SpvOpTypeInt:
432 case SpvOpTypeFloat:
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100433 return true;
434 default:
435 return false;
436 }
437}
438
Lei Zhangb36e7042015-10-28 13:40:52 -0400439int32_t spvOpcodeIsConstant(const SpvOp opcode) {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100440 switch (opcode) {
Lei Zhangb36e7042015-10-28 13:40:52 -0400441 case SpvOpConstantTrue:
442 case SpvOpConstantFalse:
443 case SpvOpConstant:
444 case SpvOpConstantComposite:
445 case SpvOpConstantSampler:
446 // case SpvOpConstantNull:
447 case SpvOpConstantNull:
448 case SpvOpSpecConstantTrue:
449 case SpvOpSpecConstantFalse:
450 case SpvOpSpecConstant:
451 case SpvOpSpecConstantComposite:
452 // case SpvOpSpecConstantOp:
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100453 return true;
454 default:
455 return false;
456 }
457}
458
Lei Zhangb36e7042015-10-28 13:40:52 -0400459int32_t spvOpcodeIsComposite(const SpvOp opcode) {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100460 switch (opcode) {
Lei Zhangb36e7042015-10-28 13:40:52 -0400461 case SpvOpTypeVector:
462 case SpvOpTypeMatrix:
463 case SpvOpTypeArray:
464 case SpvOpTypeStruct:
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100465 return true;
466 default:
467 return false;
468 }
469}
470
Lei Zhang1a0334e2015-11-02 09:41:20 -0500471int32_t spvOpcodeAreTypesEqual(const spv_instruction_t* pTypeInst0,
472 const spv_instruction_t* pTypeInst1) {
Lei Zhang40056702015-09-11 14:31:27 -0400473 if (pTypeInst0->opcode != pTypeInst1->opcode) return false;
474 if (pTypeInst0->words[1] != pTypeInst1->words[1]) return false;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100475 return true;
476}
477
Lei Zhangb36e7042015-10-28 13:40:52 -0400478int32_t spvOpcodeIsPointer(const SpvOp opcode) {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100479 switch (opcode) {
Lei Zhangb36e7042015-10-28 13:40:52 -0400480 case SpvOpVariable:
481 case SpvOpAccessChain:
482 case SpvOpInBoundsAccessChain:
483 case SpvOpFunctionParameter:
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100484 return true;
485 default:
486 return false;
487 }
488}
489
Lei Zhangb36e7042015-10-28 13:40:52 -0400490int32_t spvOpcodeIsObject(const SpvOp opcode) {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100491 switch (opcode) {
Lei Zhangb36e7042015-10-28 13:40:52 -0400492 case SpvOpConstantTrue:
493 case SpvOpConstantFalse:
494 case SpvOpConstant:
495 case SpvOpConstantComposite:
496 // TODO: case SpvOpConstantSampler:
497 case SpvOpConstantNull:
498 case SpvOpSpecConstantTrue:
499 case SpvOpSpecConstantFalse:
500 case SpvOpSpecConstant:
501 case SpvOpSpecConstantComposite:
502 // TODO: case SpvOpSpecConstantOp:
503 case SpvOpVariable:
504 case SpvOpAccessChain:
505 case SpvOpInBoundsAccessChain:
506 case SpvOpConvertFToU:
507 case SpvOpConvertFToS:
508 case SpvOpConvertSToF:
509 case SpvOpConvertUToF:
510 case SpvOpUConvert:
511 case SpvOpSConvert:
512 case SpvOpFConvert:
513 case SpvOpConvertPtrToU:
514 // TODO: case SpvOpConvertUToPtr:
515 case SpvOpPtrCastToGeneric:
516 // TODO: case SpvOpGenericCastToPtr:
517 case SpvOpBitcast:
518 // TODO: case SpvOpGenericCastToPtrExplicit:
519 case SpvOpSatConvertSToU:
520 case SpvOpSatConvertUToS:
521 case SpvOpVectorExtractDynamic:
522 case SpvOpCompositeConstruct:
523 case SpvOpCompositeExtract:
524 case SpvOpCopyObject:
525 case SpvOpTranspose:
526 case SpvOpSNegate:
527 case SpvOpFNegate:
528 case SpvOpNot:
529 case SpvOpIAdd:
530 case SpvOpFAdd:
531 case SpvOpISub:
532 case SpvOpFSub:
533 case SpvOpIMul:
534 case SpvOpFMul:
535 case SpvOpUDiv:
536 case SpvOpSDiv:
537 case SpvOpFDiv:
538 case SpvOpUMod:
539 case SpvOpSRem:
540 case SpvOpSMod:
541 case SpvOpVectorTimesScalar:
542 case SpvOpMatrixTimesScalar:
543 case SpvOpVectorTimesMatrix:
544 case SpvOpMatrixTimesVector:
545 case SpvOpMatrixTimesMatrix:
546 case SpvOpOuterProduct:
547 case SpvOpDot:
548 case SpvOpShiftRightLogical:
549 case SpvOpShiftRightArithmetic:
550 case SpvOpShiftLeftLogical:
551 case SpvOpBitwiseOr:
552 case SpvOpBitwiseXor:
553 case SpvOpBitwiseAnd:
554 case SpvOpAny:
555 case SpvOpAll:
556 case SpvOpIsNan:
557 case SpvOpIsInf:
558 case SpvOpIsFinite:
559 case SpvOpIsNormal:
560 case SpvOpSignBitSet:
561 case SpvOpLessOrGreater:
562 case SpvOpOrdered:
563 case SpvOpUnordered:
564 case SpvOpLogicalOr:
565 case SpvOpLogicalAnd:
566 case SpvOpSelect:
567 case SpvOpIEqual:
568 case SpvOpFOrdEqual:
569 case SpvOpFUnordEqual:
570 case SpvOpINotEqual:
571 case SpvOpFOrdNotEqual:
572 case SpvOpFUnordNotEqual:
573 case SpvOpULessThan:
574 case SpvOpSLessThan:
575 case SpvOpFOrdLessThan:
576 case SpvOpFUnordLessThan:
577 case SpvOpUGreaterThan:
578 case SpvOpSGreaterThan:
579 case SpvOpFOrdGreaterThan:
580 case SpvOpFUnordGreaterThan:
581 case SpvOpULessThanEqual:
582 case SpvOpSLessThanEqual:
583 case SpvOpFOrdLessThanEqual:
584 case SpvOpFUnordLessThanEqual:
585 case SpvOpUGreaterThanEqual:
586 case SpvOpSGreaterThanEqual:
587 case SpvOpFOrdGreaterThanEqual:
588 case SpvOpFUnordGreaterThanEqual:
589 case SpvOpDPdx:
590 case SpvOpDPdy:
591 case SpvOpFwidth:
592 case SpvOpDPdxFine:
593 case SpvOpDPdyFine:
594 case SpvOpFwidthFine:
595 case SpvOpDPdxCoarse:
596 case SpvOpDPdyCoarse:
597 case SpvOpFwidthCoarse:
598 case SpvOpReturnValue:
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100599 return true;
600 default:
601 return false;
602 }
603}
604
Lei Zhangb36e7042015-10-28 13:40:52 -0400605int32_t spvOpcodeIsBasicTypeNullable(SpvOp opcode) {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100606 switch (opcode) {
Lei Zhangb36e7042015-10-28 13:40:52 -0400607 case SpvOpTypeBool:
608 case SpvOpTypeInt:
609 case SpvOpTypeFloat:
610 case SpvOpTypePointer:
611 case SpvOpTypeEvent:
612 case SpvOpTypeDeviceEvent:
613 case SpvOpTypeReserveId:
614 case SpvOpTypeQueue:
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100615 return true;
616 default:
617 return false;
618 }
619}
620
Lei Zhang1a0334e2015-11-02 09:41:20 -0500621int32_t spvInstructionIsInBasicBlock(const spv_instruction_t* pFirstInst,
622 const spv_instruction_t* pInst) {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100623 while (pFirstInst != pInst) {
Lei Zhangb36e7042015-10-28 13:40:52 -0400624 if (SpvOpFunction == pInst->opcode) break;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100625 pInst--;
626 }
Lei Zhangb36e7042015-10-28 13:40:52 -0400627 if (SpvOpFunction != pInst->opcode) return false;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100628 return true;
629}
630
Lei Zhangb36e7042015-10-28 13:40:52 -0400631int32_t spvOpcodeIsValue(SpvOp opcode) {
Lei Zhang40056702015-09-11 14:31:27 -0400632 if (spvOpcodeIsPointer(opcode)) return true;
633 if (spvOpcodeIsConstant(opcode)) return true;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100634 switch (opcode) {
Lei Zhangb36e7042015-10-28 13:40:52 -0400635 case SpvOpLoad:
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100636 // TODO: Other Opcode's resulting in a value
637 return true;
638 default:
639 return false;
640 }
641}
Andrew Woloszyn537e7762015-09-29 11:28:34 -0400642
Lei Zhangb36e7042015-10-28 13:40:52 -0400643int32_t spvOpcodeGeneratesType(SpvOp op) {
Lei Zhang1a0334e2015-11-02 09:41:20 -0500644 switch (op) {
Lei Zhangb36e7042015-10-28 13:40:52 -0400645 case SpvOpTypeVoid:
646 case SpvOpTypeBool:
647 case SpvOpTypeInt:
648 case SpvOpTypeFloat:
649 case SpvOpTypeVector:
650 case SpvOpTypeMatrix:
651 case SpvOpTypeImage:
652 case SpvOpTypeSampler:
653 case SpvOpTypeSampledImage:
654 case SpvOpTypeArray:
655 case SpvOpTypeRuntimeArray:
656 case SpvOpTypeStruct:
657 case SpvOpTypeOpaque:
658 case SpvOpTypePointer:
659 case SpvOpTypeFunction:
660 case SpvOpTypeEvent:
661 case SpvOpTypeDeviceEvent:
662 case SpvOpTypeReserveId:
663 case SpvOpTypeQueue:
664 case SpvOpTypePipe:
Andrew Woloszyn537e7762015-09-29 11:28:34 -0400665 return true;
David Netoaef608c2015-11-02 14:59:02 -0500666 default:
667 // In particular, OpTypeForwardPointer does not generate a type,
668 // but declares a storage class for a pointer type generated
669 // by a different instruction.
670 break;
Andrew Woloszyn537e7762015-09-29 11:28:34 -0400671 }
672 return 0;
673}