blob: c8f6851634df9b37a813037d5e2ca56db93306e5 [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
27#include <libspirv/libspirv.h>
David Netodb901b62015-10-27 16:14:40 -040028#include "endian.h"
David Netob5dc8fc2015-10-06 16:22:00 -040029#include "instruction.h"
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010030#include "opcode.h"
31
32#include <assert.h>
33#include <string.h>
34
David Neto78c3b432015-08-27 13:03:52 -040035namespace {
36
37// Descriptions of each opcode. Each entry describes the format of the
38// instruction that follows a particular opcode.
39//
40// Most fields are initialized statically by including an automatically
41// generated file.
42// The operandTypes fields are initialized during spvOpcodeInitialize().
43//
44// TODO(dneto): Some of the macros are quite unreadable. We could make
45// good use of constexpr functions, but some compilers don't support that yet.
46spv_opcode_desc_t opcodeTableEntries[] = {
Lei Zhang1a0334e2015-11-02 09:41:20 -050047#define EmptyList \
48 {}
49#define List(...) \
50 { __VA_ARGS__ }
Lei Zhangb36e7042015-10-28 13:40:52 -040051#define Capability(X) SPV_CAPABILITY_AS_MASK(SpvCapability##X)
Lei Zhang1a0334e2015-11-02 09:41:20 -050052#define Capability2(X, Y) Capability(X) | Capability(Y)
53#define SpvCapabilityNone \
54 0 // Needed so Capability(None) still expands to valid syntax.
David Neto201caf72015-11-04 17:38:17 -050055#define Instruction(Name, HasResult, HasType, NumLogicalOperands, \
56 NumCapabilities, CapabilityRequired, IsVariable, \
57 LogicalArgsList) \
58 {#Name, SpvOp##Name, (NumCapabilities) ? (CapabilityRequired) : 0, \
59 0, {}, /* Filled in later. Operand list, including \
60 result id and type id, if needed */ \
61 HasResult, HasType, LogicalArgsList},
David Neto78c3b432015-08-27 13:03:52 -040062#include "opcode.inc"
63#undef EmptyList
64#undef List
65#undef Capability
Dejan Mircevski205408b2015-09-30 16:42:34 -040066#undef Capability2
David Neto78c3b432015-08-27 13:03:52 -040067#undef CapabilityNone
68#undef Instruction
69};
70
71// Has the opcodeTableEntries table been fully elaborated?
72// That is, are the operandTypes fields initialized?
73bool opcodeTableInitialized = false;
74
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010075// Opcode API
76
David Neto78c3b432015-08-27 13:03:52 -040077// Converts the given operand class enum (from the SPIR-V document generation
David Neto0f166be2015-11-11 01:56:49 -050078// logic) to the operand type required by the parser. The SPV_OPERAND_TYPE_NONE
79// value indicates there is no current operand and no further operands.
David Neto78c3b432015-08-27 13:03:52 -040080// This only applies to logical operands.
Lei Zhangb36e7042015-10-28 13:40:52 -040081spv_operand_type_t convertOperandClassToType(SpvOp opcode,
82 OperandClass operandClass) {
David Neto78c3b432015-08-27 13:03:52 -040083 // The spec document generator uses OptionalOperandLiteral for several kinds
84 // of repeating values. Our parser needs more specific information about
85 // what is being repeated.
86 if (operandClass == OperandOptionalLiteral) {
Lei Zhang40056702015-09-11 14:31:27 -040087 switch (opcode) {
Lei Zhangb36e7042015-10-28 13:40:52 -040088 case SpvOpLoad:
89 case SpvOpStore:
90 case SpvOpCopyMemory:
91 case SpvOpCopyMemorySized:
David Neto3fca4cd2015-09-17 17:39:45 -040092 // Expect an optional mask. When the Aligned bit is set in the mask,
93 // we will later add the expectation of a literal number operand.
94 return SPV_OPERAND_TYPE_OPTIONAL_MEMORY_ACCESS;
Lei Zhangb36e7042015-10-28 13:40:52 -040095 case SpvOpExecutionMode:
David Neto78c3b432015-08-27 13:03:52 -040096 return SPV_OPERAND_TYPE_VARIABLE_EXECUTION_MODE;
97 default:
98 break;
99 }
Lei Zhangb41d1502015-09-14 15:22:23 -0400100 } else if (operandClass == OperandVariableLiterals) {
David Neto201caf72015-11-04 17:38:17 -0500101 if (opcode == SpvOpConstant || opcode == SpvOpSpecConstant) {
102 // The number type is determined by the type Id operand.
103 return SPV_OPERAND_TYPE_TYPED_LITERAL_NUMBER;
104 }
David Neto78c3b432015-08-27 13:03:52 -0400105 }
106
Lei Zhang1a0334e2015-11-02 09:41:20 -0500107 switch (operandClass) {
108 case OperandNone:
109 return SPV_OPERAND_TYPE_NONE;
110 case OperandId:
111 return SPV_OPERAND_TYPE_ID;
112 case OperandOptionalId:
113 return SPV_OPERAND_TYPE_OPTIONAL_ID;
114 case OperandOptionalImage:
115 return SPV_OPERAND_TYPE_OPTIONAL_IMAGE;
116 case OperandVariableIds:
David Neto0f166be2015-11-11 01:56:49 -0500117 if (opcode == SpvOpSpecConstantOp) {
118 // These are the operands to the specialization constant opcode.
119 // The assembler and binary parser set up the extra Id and literal
120 // arguments when processing the opcode operand. So don't add
121 // an operand type for them here.
122 return SPV_OPERAND_TYPE_NONE;
123 }
Lei Zhang1a0334e2015-11-02 09:41:20 -0500124 return SPV_OPERAND_TYPE_VARIABLE_ID;
David Neto561dc4e2015-09-25 14:23:29 -0400125 // The spec only uses OptionalLiteral for an optional literal number.
Lei Zhang1a0334e2015-11-02 09:41:20 -0500126 case OperandOptionalLiteral:
127 return SPV_OPERAND_TYPE_OPTIONAL_LITERAL_INTEGER;
128 case OperandOptionalLiteralString:
129 return SPV_OPERAND_TYPE_OPTIONAL_LITERAL_STRING;
David Neto561dc4e2015-09-25 14:23:29 -0400130 // This is only used for sequences of literal numbers.
Lei Zhang1a0334e2015-11-02 09:41:20 -0500131 case OperandVariableLiterals:
132 return SPV_OPERAND_TYPE_VARIABLE_LITERAL_INTEGER;
David Neto445ce442015-10-15 15:22:06 -0400133 case OperandLiteralNumber:
Lei Zhangb36e7042015-10-28 13:40:52 -0400134 if (opcode == SpvOpExtInst) {
David Neto445ce442015-10-15 15:22:06 -0400135 // We use a special operand type for the extension instruction number.
Lei Zhang1a0334e2015-11-02 09:41:20 -0500136 // For now, we assume there is only one LiteraNumber argument to
137 // OpExtInst, and it is the extension instruction argument.
David Neto445ce442015-10-15 15:22:06 -0400138 // See the ExtInst entry in opcode.inc
139 // TODO(dneto): Use a function to confirm the assumption, and to verify
140 // that the index into the operandClass is 1, as expected.
141 return SPV_OPERAND_TYPE_EXTENSION_INSTRUCTION_NUMBER;
David Neto0f166be2015-11-11 01:56:49 -0500142 } else if (opcode == SpvOpSpecConstantOp) {
143 // Use a special operand type for the opcode operand, so we can
144 // use mnemonic names instead of the numbers. For example, the
145 // assembler should accept "IAdd" instead of the numeric value of
146 // SpvOpIAdd.
147 return SPV_OPERAND_TYPE_SPEC_CONSTANT_OP_NUMBER;
David Neto445ce442015-10-15 15:22:06 -0400148 }
149 return SPV_OPERAND_TYPE_LITERAL_INTEGER;
Lei Zhang1a0334e2015-11-02 09:41:20 -0500150 case OperandLiteralString:
151 return SPV_OPERAND_TYPE_LITERAL_STRING;
152 case OperandSource:
153 return SPV_OPERAND_TYPE_SOURCE_LANGUAGE;
154 case OperandExecutionModel:
155 return SPV_OPERAND_TYPE_EXECUTION_MODEL;
156 case OperandAddressing:
157 return SPV_OPERAND_TYPE_ADDRESSING_MODEL;
158 case OperandMemory:
159 return SPV_OPERAND_TYPE_MEMORY_MODEL;
160 case OperandExecutionMode:
161 return SPV_OPERAND_TYPE_EXECUTION_MODE;
162 case OperandStorage:
163 return SPV_OPERAND_TYPE_STORAGE_CLASS;
164 case OperandDimensionality:
165 return SPV_OPERAND_TYPE_DIMENSIONALITY;
166 case OperandSamplerAddressingMode:
167 return SPV_OPERAND_TYPE_SAMPLER_ADDRESSING_MODE;
168 case OperandSamplerFilterMode:
169 return SPV_OPERAND_TYPE_SAMPLER_FILTER_MODE;
170 case OperandSamplerImageFormat:
171 return SPV_OPERAND_TYPE_SAMPLER_IMAGE_FORMAT;
David Neto7cefb232015-09-28 15:33:49 -0400172 case OperandImageChannelOrder:
173 // This is only used to describe the value generated by OpImageQueryOrder.
174 // It is not used as an operand.
175 break;
176 case OperandImageChannelDataType:
Lei Zhang1a0334e2015-11-02 09:41:20 -0500177 // This is only used to describe the value generated by
178 // OpImageQueryFormat. It is not used as an operand.
David Neto7cefb232015-09-28 15:33:49 -0400179 break;
180 case OperandImageOperands:
Dejan Mircevskif8e091a2015-10-13 14:41:31 -0400181 // This is not used in opcode.inc. It only exists to generate the
182 // corresponding spec section. In parsing, image operands meld into the
183 // OperandOptionalImage case.
David Neto7cefb232015-09-28 15:33:49 -0400184 break;
Lei Zhang1a0334e2015-11-02 09:41:20 -0500185 case OperandFPFastMath:
186 return SPV_OPERAND_TYPE_FP_FAST_MATH_MODE;
187 case OperandFPRoundingMode:
188 return SPV_OPERAND_TYPE_FP_ROUNDING_MODE;
189 case OperandLinkageType:
190 return SPV_OPERAND_TYPE_LINKAGE_TYPE;
191 case OperandAccessQualifier:
192 return SPV_OPERAND_TYPE_ACCESS_QUALIFIER;
193 case OperandFuncParamAttr:
194 return SPV_OPERAND_TYPE_FUNCTION_PARAMETER_ATTRIBUTE;
195 case OperandDecoration:
196 return SPV_OPERAND_TYPE_DECORATION;
197 case OperandBuiltIn:
198 return SPV_OPERAND_TYPE_BUILT_IN;
199 case OperandSelect:
200 return SPV_OPERAND_TYPE_SELECTION_CONTROL;
201 case OperandLoop:
202 return SPV_OPERAND_TYPE_LOOP_CONTROL;
203 case OperandFunction:
204 return SPV_OPERAND_TYPE_FUNCTION_CONTROL;
205 case OperandMemorySemantics:
206 return SPV_OPERAND_TYPE_MEMORY_SEMANTICS;
David Neto78c3b432015-08-27 13:03:52 -0400207 case OperandMemoryAccess:
David Neto7cefb232015-09-28 15:33:49 -0400208 // This case does not occur in the table for SPIR-V 0.99 Rev 32.
David Neto3fca4cd2015-09-17 17:39:45 -0400209 // We expect that it will become SPV_OPERAND_TYPE_OPTIONAL_MEMORY_ACCESS,
David Neto78c3b432015-08-27 13:03:52 -0400210 // and we can remove the special casing above for memory operation
211 // instructions.
212 break;
Lei Zhang1a0334e2015-11-02 09:41:20 -0500213 case OperandScope:
214 return SPV_OPERAND_TYPE_EXECUTION_SCOPE;
215 case OperandGroupOperation:
216 return SPV_OPERAND_TYPE_GROUP_OPERATION;
217 case OperandKernelEnqueueFlags:
218 return SPV_OPERAND_TYPE_KERNEL_ENQ_FLAGS;
219 case OperandKernelProfilingInfo:
220 return SPV_OPERAND_TYPE_KERNEL_PROFILING_INFO;
221 case OperandCapability:
222 return SPV_OPERAND_TYPE_CAPABILITY;
David Neto78c3b432015-08-27 13:03:52 -0400223
224 // Used by GroupMemberDecorate
Lei Zhang1a0334e2015-11-02 09:41:20 -0500225 case OperandVariableIdLiteral:
226 return SPV_OPERAND_TYPE_VARIABLE_ID_LITERAL_INTEGER;
David Neto78c3b432015-08-27 13:03:52 -0400227
228 // Used by Switch
Lei Zhang1a0334e2015-11-02 09:41:20 -0500229 case OperandVariableLiteralId:
230 return SPV_OPERAND_TYPE_VARIABLE_LITERAL_INTEGER_ID;
David Neto78c3b432015-08-27 13:03:52 -0400231
232 // These exceptional cases shouldn't occur.
233 case OperandCount:
234 default:
235 break;
236 }
237 assert(0 && "Unexpected operand class");
238 return SPV_OPERAND_TYPE_NONE;
239}
240
Lei Zhanga94701d2015-09-14 10:05:37 -0400241} // anonymous namespace
David Neto78c3b432015-08-27 13:03:52 -0400242
243// Finish populating the opcodeTableEntries array.
244void spvOpcodeTableInitialize() {
245 // Compute the operandTypes field for each entry.
Lei Zhang1a0334e2015-11-02 09:41:20 -0500246 for (auto& opcode : opcodeTableEntries) {
David Neto78c3b432015-08-27 13:03:52 -0400247 opcode.numTypes = 0;
248 // Type ID always comes first, if present.
249 if (opcode.hasType)
Andrew Woloszyn537e7762015-09-29 11:28:34 -0400250 opcode.operandTypes[opcode.numTypes++] = SPV_OPERAND_TYPE_TYPE_ID;
David Neto78c3b432015-08-27 13:03:52 -0400251 // Result ID always comes next, if present
252 if (opcode.hasResult)
253 opcode.operandTypes[opcode.numTypes++] = SPV_OPERAND_TYPE_RESULT_ID;
Lei Zhanga94701d2015-09-14 10:05:37 -0400254 const uint16_t maxNumOperands =
255 sizeof(opcode.operandTypes) / sizeof(opcode.operandTypes[0]);
256 const uint16_t maxNumClasses =
257 sizeof(opcode.operandClass) / sizeof(opcode.operandClass[0]);
258 for (uint16_t classIndex = 0;
259 opcode.numTypes < maxNumOperands && classIndex < maxNumClasses;
260 classIndex++) {
David Neto78c3b432015-08-27 13:03:52 -0400261 const OperandClass operandClass = opcode.operandClass[classIndex];
David Neto0f166be2015-11-11 01:56:49 -0500262 const auto operandType =
Lei Zhanga94701d2015-09-14 10:05:37 -0400263 convertOperandClassToType(opcode.opcode, operandClass);
David Neto0f166be2015-11-11 01:56:49 -0500264 opcode.operandTypes[opcode.numTypes++] = operandType;
David Neto78c3b432015-08-27 13:03:52 -0400265 // The OperandNone value is not explicitly represented in the .inc file.
266 // However, it is the zero value, and is created via implicit value
David Neto0f166be2015-11-11 01:56:49 -0500267 // initialization. It converts to SPV_OPERAND_TYPE_NONE.
268 // The SPV_OPERAND_TYPE_NONE operand type indicates no current or futher
269 // operands.
270 if (operandType == SPV_OPERAND_TYPE_NONE) {
David Neto78c3b432015-08-27 13:03:52 -0400271 opcode.numTypes--;
272 break;
273 }
274 }
David Neto0f166be2015-11-11 01:56:49 -0500275
David Neto78c3b432015-08-27 13:03:52 -0400276 // We should have written the terminating SPV_OPERAND_TYPE_NONE entry, but
277 // also without overflowing.
Lei Zhanga94701d2015-09-14 10:05:37 -0400278 assert((opcode.numTypes < maxNumOperands) &&
279 "Operand class list is too long. Expand "
280 "spv_opcode_desc_t.operandClass");
David Neto78c3b432015-08-27 13:03:52 -0400281 }
282 opcodeTableInitialized = true;
283}
284
Lei Zhang1a0334e2015-11-02 09:41:20 -0500285const char* spvGeneratorStr(uint32_t generator) {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100286 switch (generator) {
287 case SPV_GENERATOR_KHRONOS:
288 return "Khronos";
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100289 case SPV_GENERATOR_LUNARG:
290 return "LunarG";
David Neto1780fc42015-10-26 15:43:12 -0400291 case SPV_GENERATOR_VALVE:
292 return "Valve";
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100293 case SPV_GENERATOR_CODEPLAY:
294 return "Codeplay Software Ltd.";
David Neto1780fc42015-10-26 15:43:12 -0400295 case SPV_GENERATOR_NVIDIA:
296 return "NVIDIA";
297 case SPV_GENERATOR_ARM:
298 return "ARM";
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100299 default:
300 return "Unknown";
301 }
302}
303
Lei Zhangb36e7042015-10-28 13:40:52 -0400304uint32_t spvOpcodeMake(uint16_t wordCount, SpvOp opcode) {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100305 return ((uint32_t)opcode) | (((uint32_t)wordCount) << 16);
306}
307
Lei Zhang1a0334e2015-11-02 09:41:20 -0500308void spvOpcodeSplit(const uint32_t word, uint16_t* pWordCount, SpvOp* pOpcode) {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100309 if (pWordCount) {
310 *pWordCount = (uint16_t)((0xffff0000 & word) >> 16);
311 }
312 if (pOpcode) {
Lei Zhangb36e7042015-10-28 13:40:52 -0400313 *pOpcode = (SpvOp)(0x0000ffff & word);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100314 }
315}
316
Lei Zhang1a0334e2015-11-02 09:41:20 -0500317spv_result_t spvOpcodeTableGet(spv_opcode_table* pInstTable) {
Lei Zhang40056702015-09-11 14:31:27 -0400318 if (!pInstTable) return SPV_ERROR_INVALID_POINTER;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100319
David Neto78c3b432015-08-27 13:03:52 -0400320 static spv_opcode_table_t table = {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100321 sizeof(opcodeTableEntries) / sizeof(spv_opcode_desc_t),
322 opcodeTableEntries};
323
David Neto78c3b432015-08-27 13:03:52 -0400324 // TODO(dneto): Consider thread safety of initialization.
325 // That is, ordering effects of the flag vs. the table updates.
Lei Zhang40056702015-09-11 14:31:27 -0400326 if (!opcodeTableInitialized) spvOpcodeTableInitialize();
David Neto78c3b432015-08-27 13:03:52 -0400327
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100328 *pInstTable = &table;
329
330 return SPV_SUCCESS;
331}
332
333spv_result_t spvOpcodeTableNameLookup(const spv_opcode_table table,
Lei Zhang1a0334e2015-11-02 09:41:20 -0500334 const char* name,
335 spv_opcode_desc* pEntry) {
Lei Zhang40056702015-09-11 14:31:27 -0400336 if (!name || !pEntry) return SPV_ERROR_INVALID_POINTER;
337 if (!table) return SPV_ERROR_INVALID_TABLE;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100338
339 // TODO: This lookup of the Opcode table is suboptimal! Binary sort would be
340 // preferable but the table requires sorting on the Opcode name, but it's
341 // static
342 // const initialized and matches the order of the spec.
343 const size_t nameLength = strlen(name);
344 for (uint64_t opcodeIndex = 0; opcodeIndex < table->count; ++opcodeIndex) {
345 if (nameLength == strlen(table->entries[opcodeIndex].name) &&
346 !strncmp(name, table->entries[opcodeIndex].name, nameLength)) {
347 // NOTE: Found out Opcode!
348 *pEntry = &table->entries[opcodeIndex];
349 return SPV_SUCCESS;
350 }
351 }
352
353 return SPV_ERROR_INVALID_LOOKUP;
354}
355
356spv_result_t spvOpcodeTableValueLookup(const spv_opcode_table table,
Lei Zhangb36e7042015-10-28 13:40:52 -0400357 const SpvOp opcode,
Lei Zhang1a0334e2015-11-02 09:41:20 -0500358 spv_opcode_desc* pEntry) {
Lei Zhang40056702015-09-11 14:31:27 -0400359 if (!table) return SPV_ERROR_INVALID_TABLE;
360 if (!pEntry) return SPV_ERROR_INVALID_POINTER;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100361
362 // TODO: As above this lookup is not optimal.
363 for (uint64_t opcodeIndex = 0; opcodeIndex < table->count; ++opcodeIndex) {
364 if (opcode == table->entries[opcodeIndex].opcode) {
365 // NOTE: Found the Opcode!
366 *pEntry = &table->entries[opcodeIndex];
367 return SPV_SUCCESS;
368 }
369 }
370
371 return SPV_ERROR_INVALID_LOOKUP;
372}
373
Lei Zhangdfc50082015-08-21 11:50:55 -0400374int16_t spvOpcodeResultIdIndex(spv_opcode_desc entry) {
David Neto78c3b432015-08-27 13:03:52 -0400375 for (int16_t i = 0; i < entry->numTypes; ++i) {
Lei Zhangdfc50082015-08-21 11:50:55 -0400376 if (SPV_OPERAND_TYPE_RESULT_ID == entry->operandTypes[i]) return i;
377 }
378 return SPV_OPERAND_INVALID_RESULT_ID_INDEX;
379}
380
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100381int32_t spvOpcodeRequiresCapabilities(spv_opcode_desc entry) {
David Neto9db3a532015-10-07 16:58:38 -0400382 return entry->capabilities != 0;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100383}
384
Lei Zhang1a0334e2015-11-02 09:41:20 -0500385void spvInstructionCopy(const uint32_t* words, const SpvOp opcode,
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100386 const uint16_t wordCount, const spv_endianness_t endian,
Lei Zhang1a0334e2015-11-02 09:41:20 -0500387 spv_instruction_t* pInst) {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100388 pInst->opcode = opcode;
David Netob5dc8fc2015-10-06 16:22:00 -0400389 pInst->words.resize(wordCount);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100390 for (uint16_t wordIndex = 0; wordIndex < wordCount; ++wordIndex) {
391 pInst->words[wordIndex] = spvFixWord(words[wordIndex], endian);
392 if (!wordIndex) {
393 uint16_t thisWordCount;
Lei Zhangb36e7042015-10-28 13:40:52 -0400394 SpvOp thisOpcode;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100395 spvOpcodeSplit(pInst->words[wordIndex], &thisWordCount, &thisOpcode);
396 assert(opcode == thisOpcode && wordCount == thisWordCount &&
397 "Endianness failed!");
398 }
399 }
400}
401
Lei Zhang1a0334e2015-11-02 09:41:20 -0500402const char* spvOpcodeString(const SpvOp opcode) {
David Neto201caf72015-11-04 17:38:17 -0500403// Use the syntax table so it's sure to be complete.
David Neto1bcd3d12015-11-02 16:03:12 -0500404#define Instruction(Name, ...) \
405 case SpvOp##Name: \
406 return #Name;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100407 switch (opcode) {
David Neto1bcd3d12015-11-02 16:03:12 -0500408#include "opcode.inc"
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100409 default:
410 assert(0 && "Unreachable!");
411 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100412 return "unknown";
David Neto1bcd3d12015-11-02 16:03:12 -0500413#undef Instruction
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100414}
415
Lei Zhangb36e7042015-10-28 13:40:52 -0400416int32_t spvOpcodeIsScalarType(const SpvOp opcode) {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100417 switch (opcode) {
Lei Zhangb36e7042015-10-28 13:40:52 -0400418 case SpvOpTypeInt:
419 case SpvOpTypeFloat:
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100420 return true;
421 default:
422 return false;
423 }
424}
425
Lei Zhangb36e7042015-10-28 13:40:52 -0400426int32_t spvOpcodeIsConstant(const SpvOp opcode) {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100427 switch (opcode) {
Lei Zhangb36e7042015-10-28 13:40:52 -0400428 case SpvOpConstantTrue:
429 case SpvOpConstantFalse:
430 case SpvOpConstant:
431 case SpvOpConstantComposite:
432 case SpvOpConstantSampler:
433 // case SpvOpConstantNull:
434 case SpvOpConstantNull:
435 case SpvOpSpecConstantTrue:
436 case SpvOpSpecConstantFalse:
437 case SpvOpSpecConstant:
438 case SpvOpSpecConstantComposite:
439 // case SpvOpSpecConstantOp:
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100440 return true;
441 default:
442 return false;
443 }
444}
445
Lei Zhangb36e7042015-10-28 13:40:52 -0400446int32_t spvOpcodeIsComposite(const SpvOp opcode) {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100447 switch (opcode) {
Lei Zhangb36e7042015-10-28 13:40:52 -0400448 case SpvOpTypeVector:
449 case SpvOpTypeMatrix:
450 case SpvOpTypeArray:
451 case SpvOpTypeStruct:
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100452 return true;
453 default:
454 return false;
455 }
456}
457
Lei Zhang1a0334e2015-11-02 09:41:20 -0500458int32_t spvOpcodeAreTypesEqual(const spv_instruction_t* pTypeInst0,
459 const spv_instruction_t* pTypeInst1) {
Lei Zhang40056702015-09-11 14:31:27 -0400460 if (pTypeInst0->opcode != pTypeInst1->opcode) return false;
461 if (pTypeInst0->words[1] != pTypeInst1->words[1]) return false;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100462 return true;
463}
464
Lei Zhangb36e7042015-10-28 13:40:52 -0400465int32_t spvOpcodeIsPointer(const SpvOp opcode) {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100466 switch (opcode) {
Lei Zhangb36e7042015-10-28 13:40:52 -0400467 case SpvOpVariable:
468 case SpvOpAccessChain:
469 case SpvOpInBoundsAccessChain:
470 case SpvOpFunctionParameter:
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100471 return true;
472 default:
473 return false;
474 }
475}
476
Lei Zhangb36e7042015-10-28 13:40:52 -0400477int32_t spvOpcodeIsObject(const SpvOp opcode) {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100478 switch (opcode) {
Lei Zhangb36e7042015-10-28 13:40:52 -0400479 case SpvOpConstantTrue:
480 case SpvOpConstantFalse:
481 case SpvOpConstant:
482 case SpvOpConstantComposite:
483 // TODO: case SpvOpConstantSampler:
484 case SpvOpConstantNull:
485 case SpvOpSpecConstantTrue:
486 case SpvOpSpecConstantFalse:
487 case SpvOpSpecConstant:
488 case SpvOpSpecConstantComposite:
489 // TODO: case SpvOpSpecConstantOp:
490 case SpvOpVariable:
491 case SpvOpAccessChain:
492 case SpvOpInBoundsAccessChain:
493 case SpvOpConvertFToU:
494 case SpvOpConvertFToS:
495 case SpvOpConvertSToF:
496 case SpvOpConvertUToF:
497 case SpvOpUConvert:
498 case SpvOpSConvert:
499 case SpvOpFConvert:
500 case SpvOpConvertPtrToU:
501 // TODO: case SpvOpConvertUToPtr:
502 case SpvOpPtrCastToGeneric:
503 // TODO: case SpvOpGenericCastToPtr:
504 case SpvOpBitcast:
505 // TODO: case SpvOpGenericCastToPtrExplicit:
506 case SpvOpSatConvertSToU:
507 case SpvOpSatConvertUToS:
508 case SpvOpVectorExtractDynamic:
509 case SpvOpCompositeConstruct:
510 case SpvOpCompositeExtract:
511 case SpvOpCopyObject:
512 case SpvOpTranspose:
513 case SpvOpSNegate:
514 case SpvOpFNegate:
515 case SpvOpNot:
516 case SpvOpIAdd:
517 case SpvOpFAdd:
518 case SpvOpISub:
519 case SpvOpFSub:
520 case SpvOpIMul:
521 case SpvOpFMul:
522 case SpvOpUDiv:
523 case SpvOpSDiv:
524 case SpvOpFDiv:
525 case SpvOpUMod:
526 case SpvOpSRem:
527 case SpvOpSMod:
528 case SpvOpVectorTimesScalar:
529 case SpvOpMatrixTimesScalar:
530 case SpvOpVectorTimesMatrix:
531 case SpvOpMatrixTimesVector:
532 case SpvOpMatrixTimesMatrix:
533 case SpvOpOuterProduct:
534 case SpvOpDot:
535 case SpvOpShiftRightLogical:
536 case SpvOpShiftRightArithmetic:
537 case SpvOpShiftLeftLogical:
538 case SpvOpBitwiseOr:
539 case SpvOpBitwiseXor:
540 case SpvOpBitwiseAnd:
541 case SpvOpAny:
542 case SpvOpAll:
543 case SpvOpIsNan:
544 case SpvOpIsInf:
545 case SpvOpIsFinite:
546 case SpvOpIsNormal:
547 case SpvOpSignBitSet:
548 case SpvOpLessOrGreater:
549 case SpvOpOrdered:
550 case SpvOpUnordered:
551 case SpvOpLogicalOr:
552 case SpvOpLogicalAnd:
553 case SpvOpSelect:
554 case SpvOpIEqual:
555 case SpvOpFOrdEqual:
556 case SpvOpFUnordEqual:
557 case SpvOpINotEqual:
558 case SpvOpFOrdNotEqual:
559 case SpvOpFUnordNotEqual:
560 case SpvOpULessThan:
561 case SpvOpSLessThan:
562 case SpvOpFOrdLessThan:
563 case SpvOpFUnordLessThan:
564 case SpvOpUGreaterThan:
565 case SpvOpSGreaterThan:
566 case SpvOpFOrdGreaterThan:
567 case SpvOpFUnordGreaterThan:
568 case SpvOpULessThanEqual:
569 case SpvOpSLessThanEqual:
570 case SpvOpFOrdLessThanEqual:
571 case SpvOpFUnordLessThanEqual:
572 case SpvOpUGreaterThanEqual:
573 case SpvOpSGreaterThanEqual:
574 case SpvOpFOrdGreaterThanEqual:
575 case SpvOpFUnordGreaterThanEqual:
576 case SpvOpDPdx:
577 case SpvOpDPdy:
578 case SpvOpFwidth:
579 case SpvOpDPdxFine:
580 case SpvOpDPdyFine:
581 case SpvOpFwidthFine:
582 case SpvOpDPdxCoarse:
583 case SpvOpDPdyCoarse:
584 case SpvOpFwidthCoarse:
585 case SpvOpReturnValue:
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100586 return true;
587 default:
588 return false;
589 }
590}
591
Lei Zhangb36e7042015-10-28 13:40:52 -0400592int32_t spvOpcodeIsBasicTypeNullable(SpvOp opcode) {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100593 switch (opcode) {
Lei Zhangb36e7042015-10-28 13:40:52 -0400594 case SpvOpTypeBool:
595 case SpvOpTypeInt:
596 case SpvOpTypeFloat:
597 case SpvOpTypePointer:
598 case SpvOpTypeEvent:
599 case SpvOpTypeDeviceEvent:
600 case SpvOpTypeReserveId:
601 case SpvOpTypeQueue:
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100602 return true;
603 default:
604 return false;
605 }
606}
607
Lei Zhang1a0334e2015-11-02 09:41:20 -0500608int32_t spvInstructionIsInBasicBlock(const spv_instruction_t* pFirstInst,
609 const spv_instruction_t* pInst) {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100610 while (pFirstInst != pInst) {
Lei Zhangb36e7042015-10-28 13:40:52 -0400611 if (SpvOpFunction == pInst->opcode) break;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100612 pInst--;
613 }
Lei Zhangb36e7042015-10-28 13:40:52 -0400614 if (SpvOpFunction != pInst->opcode) return false;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100615 return true;
616}
617
Lei Zhangb36e7042015-10-28 13:40:52 -0400618int32_t spvOpcodeIsValue(SpvOp opcode) {
Lei Zhang40056702015-09-11 14:31:27 -0400619 if (spvOpcodeIsPointer(opcode)) return true;
620 if (spvOpcodeIsConstant(opcode)) return true;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100621 switch (opcode) {
Lei Zhangb36e7042015-10-28 13:40:52 -0400622 case SpvOpLoad:
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100623 // TODO: Other Opcode's resulting in a value
624 return true;
625 default:
626 return false;
627 }
628}
Andrew Woloszyn537e7762015-09-29 11:28:34 -0400629
Lei Zhangb36e7042015-10-28 13:40:52 -0400630int32_t spvOpcodeGeneratesType(SpvOp op) {
Lei Zhang1a0334e2015-11-02 09:41:20 -0500631 switch (op) {
Lei Zhangb36e7042015-10-28 13:40:52 -0400632 case SpvOpTypeVoid:
633 case SpvOpTypeBool:
634 case SpvOpTypeInt:
635 case SpvOpTypeFloat:
636 case SpvOpTypeVector:
637 case SpvOpTypeMatrix:
638 case SpvOpTypeImage:
639 case SpvOpTypeSampler:
640 case SpvOpTypeSampledImage:
641 case SpvOpTypeArray:
642 case SpvOpTypeRuntimeArray:
643 case SpvOpTypeStruct:
644 case SpvOpTypeOpaque:
645 case SpvOpTypePointer:
646 case SpvOpTypeFunction:
647 case SpvOpTypeEvent:
648 case SpvOpTypeDeviceEvent:
649 case SpvOpTypeReserveId:
650 case SpvOpTypeQueue:
651 case SpvOpTypePipe:
Andrew Woloszyn537e7762015-09-29 11:28:34 -0400652 return true;
David Netoaef608c2015-11-02 14:59:02 -0500653 default:
654 // In particular, OpTypeForwardPointer does not generate a type,
655 // but declares a storage class for a pointer type generated
656 // by a different instruction.
657 break;
Andrew Woloszyn537e7762015-09-29 11:28:34 -0400658 }
659 return 0;
660}