blob: 2421610a59859adbc8a9c1b53ade9b748985287b [file] [log] [blame]
Dejan Mircevskib6fe02f2016-01-07 13:44:22 -05001// Copyright (c) 2015-2016 The Khronos Group Inc.
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +01002//
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 "instruction.h"
David Neto5a703352016-02-17 14:44:00 -050035#include "spirv-tools/libspirv.h"
Lei Zhangaa056cd2015-11-11 14:24:04 -050036#include "spirv_constant.h"
David Neto4c215712015-12-22 15:08:41 -050037#include "spirv_endian.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 SpvOpExecutionMode:
David Neto6836e172016-03-29 14:49:05 -040090 // An OpExecutionMode only takes a single ExecutionMode operand and the
91 // operands for that execution mode. The OperandOptionalLiteral in the
92 // grammar from the spec is only used to generate the text "Optional
93 // literal(s)". But we've already recorded the
94 // SPV_OPERAND_TYPE_EXECUTION_MODE which will absorb those extra
95 // literals. Use a NONE operand type here to terminate the operands
96 // to the instruction.
97 return SPV_OPERAND_TYPE_NONE;
David Neto78c3b432015-08-27 13:03:52 -040098 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;
Lei Zhang1a0334e2015-11-02 09:41:20 -0500124 case OperandVariableIds:
David Neto0f166be2015-11-11 01:56:49 -0500125 if (opcode == SpvOpSpecConstantOp) {
126 // These are the operands to the specialization constant opcode.
127 // The assembler and binary parser set up the extra Id and literal
128 // arguments when processing the opcode operand. So don't add
129 // an operand type for them here.
130 return SPV_OPERAND_TYPE_NONE;
131 }
Lei Zhang1a0334e2015-11-02 09:41:20 -0500132 return SPV_OPERAND_TYPE_VARIABLE_ID;
David Neto561dc4e2015-09-25 14:23:29 -0400133 // The spec only uses OptionalLiteral for an optional literal number.
Lei Zhang1a0334e2015-11-02 09:41:20 -0500134 case OperandOptionalLiteral:
135 return SPV_OPERAND_TYPE_OPTIONAL_LITERAL_INTEGER;
136 case OperandOptionalLiteralString:
137 return SPV_OPERAND_TYPE_OPTIONAL_LITERAL_STRING;
David Neto561dc4e2015-09-25 14:23:29 -0400138 // This is only used for sequences of literal numbers.
Lei Zhang1a0334e2015-11-02 09:41:20 -0500139 case OperandVariableLiterals:
140 return SPV_OPERAND_TYPE_VARIABLE_LITERAL_INTEGER;
David Neto445ce442015-10-15 15:22:06 -0400141 case OperandLiteralNumber:
Lei Zhangb36e7042015-10-28 13:40:52 -0400142 if (opcode == SpvOpExtInst) {
David Neto445ce442015-10-15 15:22:06 -0400143 // We use a special operand type for the extension instruction number.
Lei Zhang1a0334e2015-11-02 09:41:20 -0500144 // For now, we assume there is only one LiteraNumber argument to
145 // OpExtInst, and it is the extension instruction argument.
David Neto445ce442015-10-15 15:22:06 -0400146 // See the ExtInst entry in opcode.inc
147 // TODO(dneto): Use a function to confirm the assumption, and to verify
148 // that the index into the operandClass is 1, as expected.
149 return SPV_OPERAND_TYPE_EXTENSION_INSTRUCTION_NUMBER;
David Neto0f166be2015-11-11 01:56:49 -0500150 } else if (opcode == SpvOpSpecConstantOp) {
151 // Use a special operand type for the opcode operand, so we can
152 // use mnemonic names instead of the numbers. For example, the
153 // assembler should accept "IAdd" instead of the numeric value of
154 // SpvOpIAdd.
155 return SPV_OPERAND_TYPE_SPEC_CONSTANT_OP_NUMBER;
David Neto445ce442015-10-15 15:22:06 -0400156 }
157 return SPV_OPERAND_TYPE_LITERAL_INTEGER;
Lei Zhang1a0334e2015-11-02 09:41:20 -0500158 case OperandLiteralString:
159 return SPV_OPERAND_TYPE_LITERAL_STRING;
160 case OperandSource:
161 return SPV_OPERAND_TYPE_SOURCE_LANGUAGE;
162 case OperandExecutionModel:
163 return SPV_OPERAND_TYPE_EXECUTION_MODEL;
164 case OperandAddressing:
165 return SPV_OPERAND_TYPE_ADDRESSING_MODEL;
166 case OperandMemory:
167 return SPV_OPERAND_TYPE_MEMORY_MODEL;
168 case OperandExecutionMode:
169 return SPV_OPERAND_TYPE_EXECUTION_MODE;
170 case OperandStorage:
171 return SPV_OPERAND_TYPE_STORAGE_CLASS;
172 case OperandDimensionality:
173 return SPV_OPERAND_TYPE_DIMENSIONALITY;
174 case OperandSamplerAddressingMode:
175 return SPV_OPERAND_TYPE_SAMPLER_ADDRESSING_MODE;
176 case OperandSamplerFilterMode:
177 return SPV_OPERAND_TYPE_SAMPLER_FILTER_MODE;
178 case OperandSamplerImageFormat:
179 return SPV_OPERAND_TYPE_SAMPLER_IMAGE_FORMAT;
David Neto7cefb232015-09-28 15:33:49 -0400180 case OperandImageChannelOrder:
181 // This is only used to describe the value generated by OpImageQueryOrder.
182 // It is not used as an operand.
183 break;
184 case OperandImageChannelDataType:
Lei Zhang1a0334e2015-11-02 09:41:20 -0500185 // This is only used to describe the value generated by
186 // OpImageQueryFormat. It is not used as an operand.
David Neto7cefb232015-09-28 15:33:49 -0400187 break;
188 case OperandImageOperands:
David Neto2889a0c2016-02-15 13:50:00 -0500189 return SPV_OPERAND_TYPE_IMAGE;
190 case OperandOptionalImageOperands:
191 return SPV_OPERAND_TYPE_OPTIONAL_IMAGE;
Lei Zhang1a0334e2015-11-02 09:41:20 -0500192 case OperandFPFastMath:
193 return SPV_OPERAND_TYPE_FP_FAST_MATH_MODE;
194 case OperandFPRoundingMode:
195 return SPV_OPERAND_TYPE_FP_ROUNDING_MODE;
196 case OperandLinkageType:
197 return SPV_OPERAND_TYPE_LINKAGE_TYPE;
198 case OperandAccessQualifier:
199 return SPV_OPERAND_TYPE_ACCESS_QUALIFIER;
David Neto2889a0c2016-02-15 13:50:00 -0500200 case OperandOptionalAccessQualifier:
201 return SPV_OPERAND_TYPE_OPTIONAL_ACCESS_QUALIFIER;
Lei Zhang1a0334e2015-11-02 09:41:20 -0500202 case OperandFuncParamAttr:
203 return SPV_OPERAND_TYPE_FUNCTION_PARAMETER_ATTRIBUTE;
204 case OperandDecoration:
205 return SPV_OPERAND_TYPE_DECORATION;
206 case OperandBuiltIn:
207 return SPV_OPERAND_TYPE_BUILT_IN;
208 case OperandSelect:
209 return SPV_OPERAND_TYPE_SELECTION_CONTROL;
210 case OperandLoop:
211 return SPV_OPERAND_TYPE_LOOP_CONTROL;
212 case OperandFunction:
213 return SPV_OPERAND_TYPE_FUNCTION_CONTROL;
214 case OperandMemorySemantics:
David Neto64a9be92015-11-18 15:48:32 -0500215 return SPV_OPERAND_TYPE_MEMORY_SEMANTICS_ID;
David Neto78c3b432015-08-27 13:03:52 -0400216 case OperandMemoryAccess:
David Neto7cefb232015-09-28 15:33:49 -0400217 // This case does not occur in the table for SPIR-V 0.99 Rev 32.
David Neto3fca4cd2015-09-17 17:39:45 -0400218 // We expect that it will become SPV_OPERAND_TYPE_OPTIONAL_MEMORY_ACCESS,
David Neto78c3b432015-08-27 13:03:52 -0400219 // and we can remove the special casing above for memory operation
220 // instructions.
221 break;
David Neto2889a0c2016-02-15 13:50:00 -0500222 case OperandOptionalMemoryAccess:
223 // Expect an optional mask. When the Aligned bit is set in the mask,
224 // we will later add the expectation of a literal number operand.
225 return SPV_OPERAND_TYPE_OPTIONAL_MEMORY_ACCESS;
Lei Zhang1a0334e2015-11-02 09:41:20 -0500226 case OperandScope:
David Neto64a9be92015-11-18 15:48:32 -0500227 return SPV_OPERAND_TYPE_SCOPE_ID;
Lei Zhang1a0334e2015-11-02 09:41:20 -0500228 case OperandGroupOperation:
229 return SPV_OPERAND_TYPE_GROUP_OPERATION;
230 case OperandKernelEnqueueFlags:
231 return SPV_OPERAND_TYPE_KERNEL_ENQ_FLAGS;
232 case OperandKernelProfilingInfo:
233 return SPV_OPERAND_TYPE_KERNEL_PROFILING_INFO;
234 case OperandCapability:
235 return SPV_OPERAND_TYPE_CAPABILITY;
David Neto78c3b432015-08-27 13:03:52 -0400236
237 // Used by GroupMemberDecorate
Lei Zhang1a0334e2015-11-02 09:41:20 -0500238 case OperandVariableIdLiteral:
239 return SPV_OPERAND_TYPE_VARIABLE_ID_LITERAL_INTEGER;
David Neto78c3b432015-08-27 13:03:52 -0400240
241 // Used by Switch
Lei Zhang1a0334e2015-11-02 09:41:20 -0500242 case OperandVariableLiteralId:
243 return SPV_OPERAND_TYPE_VARIABLE_LITERAL_INTEGER_ID;
David Neto78c3b432015-08-27 13:03:52 -0400244
245 // These exceptional cases shouldn't occur.
246 case OperandCount:
247 default:
248 break;
249 }
250 assert(0 && "Unexpected operand class");
251 return SPV_OPERAND_TYPE_NONE;
252}
253
Lei Zhanga94701d2015-09-14 10:05:37 -0400254} // anonymous namespace
David Neto78c3b432015-08-27 13:03:52 -0400255
256// Finish populating the opcodeTableEntries array.
Lei Zhang972788b2015-11-12 13:48:30 -0500257void spvOpcodeTableInitialize(spv_opcode_desc_t* entries,
258 uint32_t num_entries) {
David Neto78c3b432015-08-27 13:03:52 -0400259 // Compute the operandTypes field for each entry.
Lei Zhang972788b2015-11-12 13:48:30 -0500260 for (uint32_t i = 0; i < num_entries; ++i) {
261 spv_opcode_desc_t& opcode = entries[i];
David Neto78c3b432015-08-27 13:03:52 -0400262 opcode.numTypes = 0;
263 // Type ID always comes first, if present.
264 if (opcode.hasType)
Andrew Woloszyn537e7762015-09-29 11:28:34 -0400265 opcode.operandTypes[opcode.numTypes++] = SPV_OPERAND_TYPE_TYPE_ID;
David Neto78c3b432015-08-27 13:03:52 -0400266 // Result ID always comes next, if present
267 if (opcode.hasResult)
268 opcode.operandTypes[opcode.numTypes++] = SPV_OPERAND_TYPE_RESULT_ID;
David Netoba73a7c2016-01-06 13:08:39 -0500269 const uint16_t maxNumOperands = static_cast<uint16_t>(
270 sizeof(opcode.operandTypes) / sizeof(opcode.operandTypes[0]));
271 const uint16_t maxNumClasses = static_cast<uint16_t>(
272 sizeof(opcode.operandClass) / sizeof(opcode.operandClass[0]));
Lei Zhanga94701d2015-09-14 10:05:37 -0400273 for (uint16_t classIndex = 0;
274 opcode.numTypes < maxNumOperands && classIndex < maxNumClasses;
275 classIndex++) {
David Neto78c3b432015-08-27 13:03:52 -0400276 const OperandClass operandClass = opcode.operandClass[classIndex];
David Neto0f166be2015-11-11 01:56:49 -0500277 const auto operandType =
Lei Zhanga94701d2015-09-14 10:05:37 -0400278 convertOperandClassToType(opcode.opcode, operandClass);
David Neto0f166be2015-11-11 01:56:49 -0500279 opcode.operandTypes[opcode.numTypes++] = operandType;
David Neto78c3b432015-08-27 13:03:52 -0400280 // The OperandNone value is not explicitly represented in the .inc file.
281 // However, it is the zero value, and is created via implicit value
David Neto0f166be2015-11-11 01:56:49 -0500282 // initialization. It converts to SPV_OPERAND_TYPE_NONE.
283 // The SPV_OPERAND_TYPE_NONE operand type indicates no current or futher
284 // operands.
285 if (operandType == SPV_OPERAND_TYPE_NONE) {
David Neto78c3b432015-08-27 13:03:52 -0400286 opcode.numTypes--;
287 break;
288 }
289 }
David Neto0f166be2015-11-11 01:56:49 -0500290
David Neto78c3b432015-08-27 13:03:52 -0400291 // We should have written the terminating SPV_OPERAND_TYPE_NONE entry, but
292 // also without overflowing.
Lei Zhanga94701d2015-09-14 10:05:37 -0400293 assert((opcode.numTypes < maxNumOperands) &&
294 "Operand class list is too long. Expand "
295 "spv_opcode_desc_t.operandClass");
David Neto78c3b432015-08-27 13:03:52 -0400296 }
David Neto78c3b432015-08-27 13:03:52 -0400297}
298
Lei Zhang1a0334e2015-11-02 09:41:20 -0500299const char* spvGeneratorStr(uint32_t generator) {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100300 switch (generator) {
301 case SPV_GENERATOR_KHRONOS:
302 return "Khronos";
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100303 case SPV_GENERATOR_LUNARG:
304 return "LunarG";
David Neto1780fc42015-10-26 15:43:12 -0400305 case SPV_GENERATOR_VALVE:
306 return "Valve";
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100307 case SPV_GENERATOR_CODEPLAY:
308 return "Codeplay Software Ltd.";
David Neto1780fc42015-10-26 15:43:12 -0400309 case SPV_GENERATOR_NVIDIA:
310 return "NVIDIA";
311 case SPV_GENERATOR_ARM:
312 return "ARM";
David Neto14b93e42015-11-12 18:33:47 -0500313 case SPV_GENERATOR_KHRONOS_LLVM_TRANSLATOR:
314 return "Khronos LLVM/SPIR-V Translator";
315 case SPV_GENERATOR_KHRONOS_ASSEMBLER:
316 return "Khronos SPIR-V Tools Assembler";
David Neto2266ba12015-11-13 12:03:28 -0600317 case SPV_GENERATOR_KHRONOS_GLSLANG:
318 return "Khronos Glslang Reference Front End";
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100319 default:
320 return "Unknown";
321 }
322}
323
Lei Zhangb36e7042015-10-28 13:40:52 -0400324uint32_t spvOpcodeMake(uint16_t wordCount, SpvOp opcode) {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100325 return ((uint32_t)opcode) | (((uint32_t)wordCount) << 16);
326}
327
Lei Zhang1a0334e2015-11-02 09:41:20 -0500328void spvOpcodeSplit(const uint32_t word, uint16_t* pWordCount, SpvOp* pOpcode) {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100329 if (pWordCount) {
330 *pWordCount = (uint16_t)((0xffff0000 & word) >> 16);
331 }
332 if (pOpcode) {
Lei Zhangb36e7042015-10-28 13:40:52 -0400333 *pOpcode = (SpvOp)(0x0000ffff & word);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100334 }
335}
336
Lei Zhang1a0334e2015-11-02 09:41:20 -0500337spv_result_t spvOpcodeTableGet(spv_opcode_table* pInstTable) {
Lei Zhang40056702015-09-11 14:31:27 -0400338 if (!pInstTable) return SPV_ERROR_INVALID_POINTER;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100339
Lei Zhang972788b2015-11-12 13:48:30 -0500340 const uint32_t size = sizeof(opcodeTableEntries);
341 spv_opcode_desc_t* copied_entries =
342 static_cast<spv_opcode_desc_t*>(::malloc(size));
343 if (!copied_entries) return SPV_ERROR_OUT_OF_MEMORY;
344 ::memcpy(copied_entries, opcodeTableEntries, size);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100345
David Netoba73a7c2016-01-06 13:08:39 -0500346 const uint32_t count = static_cast<uint32_t>(sizeof(opcodeTableEntries) /
347 sizeof(spv_opcode_desc_t));
Lei Zhang972788b2015-11-12 13:48:30 -0500348 spv_opcode_table_t* table = new spv_opcode_table_t{count, copied_entries};
David Neto78c3b432015-08-27 13:03:52 -0400349
Lei Zhang972788b2015-11-12 13:48:30 -0500350 spvOpcodeTableInitialize(copied_entries, count);
351
352 *pInstTable = table;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100353
354 return SPV_SUCCESS;
355}
356
357spv_result_t spvOpcodeTableNameLookup(const spv_opcode_table table,
Lei Zhang1a0334e2015-11-02 09:41:20 -0500358 const char* name,
359 spv_opcode_desc* pEntry) {
Lei Zhang40056702015-09-11 14:31:27 -0400360 if (!name || !pEntry) return SPV_ERROR_INVALID_POINTER;
361 if (!table) return SPV_ERROR_INVALID_TABLE;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100362
363 // TODO: This lookup of the Opcode table is suboptimal! Binary sort would be
364 // preferable but the table requires sorting on the Opcode name, but it's
365 // static
366 // const initialized and matches the order of the spec.
367 const size_t nameLength = strlen(name);
368 for (uint64_t opcodeIndex = 0; opcodeIndex < table->count; ++opcodeIndex) {
369 if (nameLength == strlen(table->entries[opcodeIndex].name) &&
370 !strncmp(name, table->entries[opcodeIndex].name, nameLength)) {
371 // NOTE: Found out Opcode!
372 *pEntry = &table->entries[opcodeIndex];
373 return SPV_SUCCESS;
374 }
375 }
376
377 return SPV_ERROR_INVALID_LOOKUP;
378}
379
380spv_result_t spvOpcodeTableValueLookup(const spv_opcode_table table,
Lei Zhangb36e7042015-10-28 13:40:52 -0400381 const SpvOp opcode,
Lei Zhang1a0334e2015-11-02 09:41:20 -0500382 spv_opcode_desc* pEntry) {
Lei Zhang40056702015-09-11 14:31:27 -0400383 if (!table) return SPV_ERROR_INVALID_TABLE;
384 if (!pEntry) return SPV_ERROR_INVALID_POINTER;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100385
386 // TODO: As above this lookup is not optimal.
387 for (uint64_t opcodeIndex = 0; opcodeIndex < table->count; ++opcodeIndex) {
388 if (opcode == table->entries[opcodeIndex].opcode) {
389 // NOTE: Found the Opcode!
390 *pEntry = &table->entries[opcodeIndex];
391 return SPV_SUCCESS;
392 }
393 }
394
395 return SPV_ERROR_INVALID_LOOKUP;
396}
397
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100398int32_t spvOpcodeRequiresCapabilities(spv_opcode_desc entry) {
David Neto9db3a532015-10-07 16:58:38 -0400399 return entry->capabilities != 0;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100400}
401
Lei Zhang1a0334e2015-11-02 09:41:20 -0500402void spvInstructionCopy(const uint32_t* words, const SpvOp opcode,
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100403 const uint16_t wordCount, const spv_endianness_t endian,
Lei Zhang1a0334e2015-11-02 09:41:20 -0500404 spv_instruction_t* pInst) {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100405 pInst->opcode = opcode;
David Netob5dc8fc2015-10-06 16:22:00 -0400406 pInst->words.resize(wordCount);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100407 for (uint16_t wordIndex = 0; wordIndex < wordCount; ++wordIndex) {
408 pInst->words[wordIndex] = spvFixWord(words[wordIndex], endian);
409 if (!wordIndex) {
410 uint16_t thisWordCount;
Lei Zhangb36e7042015-10-28 13:40:52 -0400411 SpvOp thisOpcode;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100412 spvOpcodeSplit(pInst->words[wordIndex], &thisWordCount, &thisOpcode);
413 assert(opcode == thisOpcode && wordCount == thisWordCount &&
414 "Endianness failed!");
415 }
416 }
417}
418
Lei Zhang1a0334e2015-11-02 09:41:20 -0500419const char* spvOpcodeString(const SpvOp opcode) {
David Neto201caf72015-11-04 17:38:17 -0500420// Use the syntax table so it's sure to be complete.
David Neto1bcd3d12015-11-02 16:03:12 -0500421#define Instruction(Name, ...) \
422 case SpvOp##Name: \
423 return #Name;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100424 switch (opcode) {
David Neto1bcd3d12015-11-02 16:03:12 -0500425#include "opcode.inc"
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100426 default:
427 assert(0 && "Unreachable!");
428 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100429 return "unknown";
David Neto1bcd3d12015-11-02 16:03:12 -0500430#undef Instruction
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100431}
432
Lei Zhangb36e7042015-10-28 13:40:52 -0400433int32_t spvOpcodeIsScalarType(const SpvOp opcode) {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100434 switch (opcode) {
Lei Zhangb36e7042015-10-28 13:40:52 -0400435 case SpvOpTypeInt:
436 case SpvOpTypeFloat:
Dejan Mircevski276a7242016-01-21 15:55:43 -0500437 case SpvOpTypeBool:
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100438 return true;
439 default:
440 return false;
441 }
442}
443
Lei Zhangb36e7042015-10-28 13:40:52 -0400444int32_t spvOpcodeIsConstant(const SpvOp opcode) {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100445 switch (opcode) {
Lei Zhangb36e7042015-10-28 13:40:52 -0400446 case SpvOpConstantTrue:
447 case SpvOpConstantFalse:
448 case SpvOpConstant:
449 case SpvOpConstantComposite:
450 case SpvOpConstantSampler:
451 // case SpvOpConstantNull:
452 case SpvOpConstantNull:
453 case SpvOpSpecConstantTrue:
454 case SpvOpSpecConstantFalse:
455 case SpvOpSpecConstant:
456 case SpvOpSpecConstantComposite:
457 // case SpvOpSpecConstantOp:
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100458 return true;
459 default:
460 return false;
461 }
462}
463
Lei Zhangb36e7042015-10-28 13:40:52 -0400464int32_t spvOpcodeIsComposite(const SpvOp opcode) {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100465 switch (opcode) {
Lei Zhangb36e7042015-10-28 13:40:52 -0400466 case SpvOpTypeVector:
467 case SpvOpTypeMatrix:
468 case SpvOpTypeArray:
469 case SpvOpTypeStruct:
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100470 return true;
471 default:
472 return false;
473 }
474}
475
Lei Zhangb36e7042015-10-28 13:40:52 -0400476int32_t spvOpcodeIsPointer(const SpvOp opcode) {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100477 switch (opcode) {
Lei Zhangb36e7042015-10-28 13:40:52 -0400478 case SpvOpVariable:
479 case SpvOpAccessChain:
Florian Ziesche680f9b72016-03-01 19:56:14 +0100480 case SpvOpPtrAccessChain:
Lei Zhangb36e7042015-10-28 13:40:52 -0400481 case SpvOpInBoundsAccessChain:
Florian Ziesche680f9b72016-03-01 19:56:14 +0100482 case SpvOpInBoundsPtrAccessChain:
Lei Zhangb36e7042015-10-28 13:40:52 -0400483 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 spvOpcodeGeneratesType(SpvOp op) {
Lei Zhang1a0334e2015-11-02 09:41:20 -0500491 switch (op) {
Lei Zhangb36e7042015-10-28 13:40:52 -0400492 case SpvOpTypeVoid:
493 case SpvOpTypeBool:
494 case SpvOpTypeInt:
495 case SpvOpTypeFloat:
496 case SpvOpTypeVector:
497 case SpvOpTypeMatrix:
498 case SpvOpTypeImage:
499 case SpvOpTypeSampler:
500 case SpvOpTypeSampledImage:
501 case SpvOpTypeArray:
502 case SpvOpTypeRuntimeArray:
503 case SpvOpTypeStruct:
504 case SpvOpTypeOpaque:
505 case SpvOpTypePointer:
506 case SpvOpTypeFunction:
507 case SpvOpTypeEvent:
508 case SpvOpTypeDeviceEvent:
509 case SpvOpTypeReserveId:
510 case SpvOpTypeQueue:
511 case SpvOpTypePipe:
Andrew Woloszyn537e7762015-09-29 11:28:34 -0400512 return true;
David Netoaef608c2015-11-02 14:59:02 -0500513 default:
514 // In particular, OpTypeForwardPointer does not generate a type,
515 // but declares a storage class for a pointer type generated
516 // by a different instruction.
517 break;
Andrew Woloszyn537e7762015-09-29 11:28:34 -0400518 }
519 return 0;
520}