blob: f1a2200fe6382788bf7729b9c1a2b7dc174016f4 [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>
28#include "binary.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[] = {
47#define EmptyList {}
48#define List(...) {__VA_ARGS__}
Dejan Mircevski205408b2015-09-30 16:42:34 -040049#define Capability(X) SPV_CAPABILITY_AS_MASK(Capability##X)
50#define Capability2(X,Y) Capability(X)|Capability(Y)
51#define CapabilityNone 0 // Needed so Capability(None) still expands to valid syntax.
David Neto78c3b432015-08-27 13:03:52 -040052#define Instruction(Name,HasResult,HasType,NumLogicalOperands,NumCapabilities,CapabilityRequired,IsVariable,LogicalArgsList) \
53 { #Name, \
54 Op##Name, \
Dejan Mircevski205408b2015-09-30 16:42:34 -040055 (NumCapabilities) ? (CapabilityRequired) : 0, \
David Neto78c3b432015-08-27 13:03:52 -040056 0, {}, /* Filled in later. Operand list, including result id and type id, if needed */ \
57 HasResult, \
58 HasType, \
59 LogicalArgsList },
60#include "opcode.inc"
61#undef EmptyList
62#undef List
63#undef Capability
Dejan Mircevski205408b2015-09-30 16:42:34 -040064#undef Capability2
David Neto78c3b432015-08-27 13:03:52 -040065#undef CapabilityNone
66#undef Instruction
67};
68
69// Has the opcodeTableEntries table been fully elaborated?
70// That is, are the operandTypes fields initialized?
71bool opcodeTableInitialized = false;
72
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010073// Opcode API
74
David Neto78c3b432015-08-27 13:03:52 -040075// Converts the given operand class enum (from the SPIR-V document generation
76// logic) to the operand type required by the parser.
77// This only applies to logical operands.
Lei Zhang40056702015-09-11 14:31:27 -040078spv_operand_type_t convertOperandClassToType(spv::Op opcode,
79 spv::OperandClass operandClass) {
David Neto78c3b432015-08-27 13:03:52 -040080 // The spec document generator uses OptionalOperandLiteral for several kinds
81 // of repeating values. Our parser needs more specific information about
82 // what is being repeated.
83 if (operandClass == OperandOptionalLiteral) {
Lei Zhang40056702015-09-11 14:31:27 -040084 switch (opcode) {
David Neto78c3b432015-08-27 13:03:52 -040085 case spv::OpLoad:
86 case spv::OpStore:
87 case spv::OpCopyMemory:
88 case spv::OpCopyMemorySized:
David Neto3fca4cd2015-09-17 17:39:45 -040089 // Expect an optional mask. When the Aligned bit is set in the mask,
90 // we will later add the expectation of a literal number operand.
91 return SPV_OPERAND_TYPE_OPTIONAL_MEMORY_ACCESS;
David Neto78c3b432015-08-27 13:03:52 -040092 case spv::OpExecutionMode:
93 return SPV_OPERAND_TYPE_VARIABLE_EXECUTION_MODE;
94 default:
95 break;
96 }
Lei Zhangb41d1502015-09-14 15:22:23 -040097 } else if (operandClass == OperandVariableLiterals) {
98 if (opcode == spv::OpConstant || opcode == spv::OpSpecConstant)
99 return SPV_OPERAND_TYPE_MULTIWORD_LITERAL_NUMBER;
David Neto78c3b432015-08-27 13:03:52 -0400100 }
101
102 switch(operandClass) {
103 case OperandNone: return SPV_OPERAND_TYPE_NONE;
104 case OperandId: return SPV_OPERAND_TYPE_ID;
105 case OperandOptionalId: return SPV_OPERAND_TYPE_OPTIONAL_ID;
David Neto561dc4e2015-09-25 14:23:29 -0400106 case OperandOptionalImage: return SPV_OPERAND_TYPE_OPTIONAL_IMAGE;
David Neto78c3b432015-08-27 13:03:52 -0400107 case OperandVariableIds: return SPV_OPERAND_TYPE_VARIABLE_ID;
David Neto561dc4e2015-09-25 14:23:29 -0400108 // The spec only uses OptionalLiteral for an optional literal number.
Lei Zhang6483bd72015-10-14 17:02:39 -0400109 case OperandOptionalLiteral: return SPV_OPERAND_TYPE_OPTIONAL_LITERAL_INTEGER;
David Neto78c3b432015-08-27 13:03:52 -0400110 case OperandOptionalLiteralString: return SPV_OPERAND_TYPE_OPTIONAL_LITERAL_STRING;
David Neto561dc4e2015-09-25 14:23:29 -0400111 // This is only used for sequences of literal numbers.
Lei Zhang6483bd72015-10-14 17:02:39 -0400112 case OperandVariableLiterals: return SPV_OPERAND_TYPE_VARIABLE_LITERAL_INTEGER;
David Neto445ce442015-10-15 15:22:06 -0400113 case OperandLiteralNumber:
114 if (opcode == spv::OpExtInst) {
115 // We use a special operand type for the extension instruction number.
116 // For now, we assume there is only one LiteraNumber argument to OpExtInst,
117 // and it is the extension instruction argument.
118 // See the ExtInst entry in opcode.inc
119 // TODO(dneto): Use a function to confirm the assumption, and to verify
120 // that the index into the operandClass is 1, as expected.
121 return SPV_OPERAND_TYPE_EXTENSION_INSTRUCTION_NUMBER;
122 }
123 return SPV_OPERAND_TYPE_LITERAL_INTEGER;
David Neto78c3b432015-08-27 13:03:52 -0400124 case OperandLiteralString: return SPV_OPERAND_TYPE_LITERAL_STRING;
125 case OperandSource: return SPV_OPERAND_TYPE_SOURCE_LANGUAGE;
126 case OperandExecutionModel: return SPV_OPERAND_TYPE_EXECUTION_MODEL;
127 case OperandAddressing: return SPV_OPERAND_TYPE_ADDRESSING_MODEL;
128 case OperandMemory: return SPV_OPERAND_TYPE_MEMORY_MODEL;
129 case OperandExecutionMode: return SPV_OPERAND_TYPE_EXECUTION_MODE;
130 case OperandStorage: return SPV_OPERAND_TYPE_STORAGE_CLASS;
131 case OperandDimensionality: return SPV_OPERAND_TYPE_DIMENSIONALITY;
132 case OperandSamplerAddressingMode: return SPV_OPERAND_TYPE_SAMPLER_ADDRESSING_MODE;
133 case OperandSamplerFilterMode: return SPV_OPERAND_TYPE_SAMPLER_FILTER_MODE;
David Netob30a0c52015-09-16 15:56:43 -0400134 case OperandSamplerImageFormat: return SPV_OPERAND_TYPE_SAMPLER_IMAGE_FORMAT;
David Neto7cefb232015-09-28 15:33:49 -0400135 case OperandImageChannelOrder:
136 // This is only used to describe the value generated by OpImageQueryOrder.
137 // It is not used as an operand.
138 break;
139 case OperandImageChannelDataType:
140 // This is only used to describe the value generated by OpImageQueryFormat.
141 // It is not used as an operand.
142 break;
143 case OperandImageOperands:
Dejan Mircevskif8e091a2015-10-13 14:41:31 -0400144 // This is not used in opcode.inc. It only exists to generate the
145 // corresponding spec section. In parsing, image operands meld into the
146 // OperandOptionalImage case.
David Neto7cefb232015-09-28 15:33:49 -0400147 break;
David Neto78c3b432015-08-27 13:03:52 -0400148 case OperandFPFastMath: return SPV_OPERAND_TYPE_FP_FAST_MATH_MODE;
149 case OperandFPRoundingMode: return SPV_OPERAND_TYPE_FP_ROUNDING_MODE;
150 case OperandLinkageType: return SPV_OPERAND_TYPE_LINKAGE_TYPE;
151 case OperandAccessQualifier: return SPV_OPERAND_TYPE_ACCESS_QUALIFIER;
152 case OperandFuncParamAttr: return SPV_OPERAND_TYPE_FUNCTION_PARAMETER_ATTRIBUTE;
153 case OperandDecoration: return SPV_OPERAND_TYPE_DECORATION;
154 case OperandBuiltIn: return SPV_OPERAND_TYPE_BUILT_IN;
155 case OperandSelect: return SPV_OPERAND_TYPE_SELECTION_CONTROL;
156 case OperandLoop: return SPV_OPERAND_TYPE_LOOP_CONTROL;
157 case OperandFunction: return SPV_OPERAND_TYPE_FUNCTION_CONTROL;
158 case OperandMemorySemantics: return SPV_OPERAND_TYPE_MEMORY_SEMANTICS;
159 case OperandMemoryAccess:
David Neto7cefb232015-09-28 15:33:49 -0400160 // This case does not occur in the table for SPIR-V 0.99 Rev 32.
David Neto3fca4cd2015-09-17 17:39:45 -0400161 // We expect that it will become SPV_OPERAND_TYPE_OPTIONAL_MEMORY_ACCESS,
David Neto78c3b432015-08-27 13:03:52 -0400162 // and we can remove the special casing above for memory operation
163 // instructions.
164 break;
165 case OperandScope: return SPV_OPERAND_TYPE_EXECUTION_SCOPE;
166 case OperandGroupOperation: return SPV_OPERAND_TYPE_GROUP_OPERATION;
167 case OperandKernelEnqueueFlags: return SPV_OPERAND_TYPE_KERNEL_ENQ_FLAGS;
168 case OperandKernelProfilingInfo: return SPV_OPERAND_TYPE_KERNEL_PROFILING_INFO;
169 case OperandCapability: return SPV_OPERAND_TYPE_CAPABILITY;
170
171 // Used by GroupMemberDecorate
Lei Zhang6483bd72015-10-14 17:02:39 -0400172 case OperandVariableIdLiteral: return SPV_OPERAND_TYPE_VARIABLE_ID_LITERAL_INTEGER;
David Neto78c3b432015-08-27 13:03:52 -0400173
174 // Used by Switch
Lei Zhang6483bd72015-10-14 17:02:39 -0400175 case OperandVariableLiteralId: return SPV_OPERAND_TYPE_VARIABLE_LITERAL_INTEGER_ID;
David Neto78c3b432015-08-27 13:03:52 -0400176
177 // These exceptional cases shouldn't occur.
178 case OperandCount:
179 default:
180 break;
181 }
182 assert(0 && "Unexpected operand class");
183 return SPV_OPERAND_TYPE_NONE;
184}
185
Lei Zhanga94701d2015-09-14 10:05:37 -0400186} // anonymous namespace
David Neto78c3b432015-08-27 13:03:52 -0400187
188// Finish populating the opcodeTableEntries array.
189void spvOpcodeTableInitialize() {
190 // Compute the operandTypes field for each entry.
Lei Zhanga94701d2015-09-14 10:05:37 -0400191 for (auto &opcode : opcodeTableEntries) {
David Neto78c3b432015-08-27 13:03:52 -0400192 opcode.numTypes = 0;
193 // Type ID always comes first, if present.
194 if (opcode.hasType)
Andrew Woloszyn537e7762015-09-29 11:28:34 -0400195 opcode.operandTypes[opcode.numTypes++] = SPV_OPERAND_TYPE_TYPE_ID;
David Neto78c3b432015-08-27 13:03:52 -0400196 // Result ID always comes next, if present
197 if (opcode.hasResult)
198 opcode.operandTypes[opcode.numTypes++] = SPV_OPERAND_TYPE_RESULT_ID;
Lei Zhanga94701d2015-09-14 10:05:37 -0400199 const uint16_t maxNumOperands =
200 sizeof(opcode.operandTypes) / sizeof(opcode.operandTypes[0]);
201 const uint16_t maxNumClasses =
202 sizeof(opcode.operandClass) / sizeof(opcode.operandClass[0]);
203 for (uint16_t classIndex = 0;
204 opcode.numTypes < maxNumOperands && classIndex < maxNumClasses;
205 classIndex++) {
David Neto78c3b432015-08-27 13:03:52 -0400206 const OperandClass operandClass = opcode.operandClass[classIndex];
Lei Zhanga94701d2015-09-14 10:05:37 -0400207 opcode.operandTypes[opcode.numTypes++] =
208 convertOperandClassToType(opcode.opcode, operandClass);
David Neto78c3b432015-08-27 13:03:52 -0400209 // The OperandNone value is not explicitly represented in the .inc file.
210 // However, it is the zero value, and is created via implicit value
211 // initialization.
212 if (operandClass == OperandNone) {
213 opcode.numTypes--;
214 break;
215 }
216 }
217 // We should have written the terminating SPV_OPERAND_TYPE_NONE entry, but
218 // also without overflowing.
Lei Zhanga94701d2015-09-14 10:05:37 -0400219 assert((opcode.numTypes < maxNumOperands) &&
220 "Operand class list is too long. Expand "
221 "spv_opcode_desc_t.operandClass");
David Neto78c3b432015-08-27 13:03:52 -0400222 }
223 opcodeTableInitialized = true;
224}
225
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100226const char *spvGeneratorStr(uint32_t generator) {
227 switch (generator) {
228 case SPV_GENERATOR_KHRONOS:
229 return "Khronos";
230 case SPV_GENERATOR_VALVE:
231 return "Valve";
232 case SPV_GENERATOR_LUNARG:
233 return "LunarG";
234 case SPV_GENERATOR_CODEPLAY:
235 return "Codeplay Software Ltd.";
236 default:
237 return "Unknown";
238 }
239}
240
241uint32_t spvOpcodeMake(uint16_t wordCount, Op opcode) {
242 return ((uint32_t)opcode) | (((uint32_t)wordCount) << 16);
243}
244
245void spvOpcodeSplit(const uint32_t word, uint16_t *pWordCount, Op *pOpcode) {
246 if (pWordCount) {
247 *pWordCount = (uint16_t)((0xffff0000 & word) >> 16);
248 }
249 if (pOpcode) {
250 *pOpcode = (Op)(0x0000ffff & word);
251 }
252}
253
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100254spv_result_t spvOpcodeTableGet(spv_opcode_table *pInstTable) {
Lei Zhang40056702015-09-11 14:31:27 -0400255 if (!pInstTable) return SPV_ERROR_INVALID_POINTER;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100256
David Neto78c3b432015-08-27 13:03:52 -0400257 static spv_opcode_table_t table = {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100258 sizeof(opcodeTableEntries) / sizeof(spv_opcode_desc_t),
259 opcodeTableEntries};
260
David Neto78c3b432015-08-27 13:03:52 -0400261 // TODO(dneto): Consider thread safety of initialization.
262 // That is, ordering effects of the flag vs. the table updates.
Lei Zhang40056702015-09-11 14:31:27 -0400263 if (!opcodeTableInitialized) spvOpcodeTableInitialize();
David Neto78c3b432015-08-27 13:03:52 -0400264
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100265 *pInstTable = &table;
266
267 return SPV_SUCCESS;
268}
269
270spv_result_t spvOpcodeTableNameLookup(const spv_opcode_table table,
271 const char *name,
272 spv_opcode_desc *pEntry) {
Lei Zhang40056702015-09-11 14:31:27 -0400273 if (!name || !pEntry) return SPV_ERROR_INVALID_POINTER;
274 if (!table) return SPV_ERROR_INVALID_TABLE;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100275
276 // TODO: This lookup of the Opcode table is suboptimal! Binary sort would be
277 // preferable but the table requires sorting on the Opcode name, but it's
278 // static
279 // const initialized and matches the order of the spec.
280 const size_t nameLength = strlen(name);
281 for (uint64_t opcodeIndex = 0; opcodeIndex < table->count; ++opcodeIndex) {
282 if (nameLength == strlen(table->entries[opcodeIndex].name) &&
283 !strncmp(name, table->entries[opcodeIndex].name, nameLength)) {
284 // NOTE: Found out Opcode!
285 *pEntry = &table->entries[opcodeIndex];
286 return SPV_SUCCESS;
287 }
288 }
289
290 return SPV_ERROR_INVALID_LOOKUP;
291}
292
293spv_result_t spvOpcodeTableValueLookup(const spv_opcode_table table,
294 const Op opcode,
295 spv_opcode_desc *pEntry) {
Lei Zhang40056702015-09-11 14:31:27 -0400296 if (!table) return SPV_ERROR_INVALID_TABLE;
297 if (!pEntry) return SPV_ERROR_INVALID_POINTER;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100298
299 // TODO: As above this lookup is not optimal.
300 for (uint64_t opcodeIndex = 0; opcodeIndex < table->count; ++opcodeIndex) {
301 if (opcode == table->entries[opcodeIndex].opcode) {
302 // NOTE: Found the Opcode!
303 *pEntry = &table->entries[opcodeIndex];
304 return SPV_SUCCESS;
305 }
306 }
307
308 return SPV_ERROR_INVALID_LOOKUP;
309}
310
Lei Zhangdfc50082015-08-21 11:50:55 -0400311int16_t spvOpcodeResultIdIndex(spv_opcode_desc entry) {
David Neto78c3b432015-08-27 13:03:52 -0400312 for (int16_t i = 0; i < entry->numTypes; ++i) {
Lei Zhangdfc50082015-08-21 11:50:55 -0400313 if (SPV_OPERAND_TYPE_RESULT_ID == entry->operandTypes[i]) return i;
314 }
315 return SPV_OPERAND_INVALID_RESULT_ID_INDEX;
316}
317
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100318int32_t spvOpcodeRequiresCapabilities(spv_opcode_desc entry) {
David Neto9db3a532015-10-07 16:58:38 -0400319 return entry->capabilities != 0;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100320}
321
322void spvInstructionCopy(const uint32_t *words, const Op opcode,
323 const uint16_t wordCount, const spv_endianness_t endian,
324 spv_instruction_t *pInst) {
325 pInst->opcode = opcode;
David Netob5dc8fc2015-10-06 16:22:00 -0400326 pInst->words.resize(wordCount);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100327 for (uint16_t wordIndex = 0; wordIndex < wordCount; ++wordIndex) {
328 pInst->words[wordIndex] = spvFixWord(words[wordIndex], endian);
329 if (!wordIndex) {
330 uint16_t thisWordCount;
331 Op thisOpcode;
332 spvOpcodeSplit(pInst->words[wordIndex], &thisWordCount, &thisOpcode);
333 assert(opcode == thisOpcode && wordCount == thisWordCount &&
334 "Endianness failed!");
335 }
336 }
337}
338
339const char *spvOpcodeString(const Op opcode) {
340#define CASE(OPCODE) \
341 case OPCODE: \
342 return #OPCODE;
343 switch (opcode) {
344 CASE(OpNop)
345 CASE(OpSource)
346 CASE(OpSourceExtension)
347 CASE(OpExtension)
348 CASE(OpExtInstImport)
349 CASE(OpMemoryModel)
350 CASE(OpEntryPoint)
351 CASE(OpExecutionMode)
352 CASE(OpTypeVoid)
353 CASE(OpTypeBool)
354 CASE(OpTypeInt)
355 CASE(OpTypeFloat)
356 CASE(OpTypeVector)
357 CASE(OpTypeMatrix)
358 CASE(OpTypeSampler)
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100359 CASE(OpTypeArray)
360 CASE(OpTypeRuntimeArray)
361 CASE(OpTypeStruct)
362 CASE(OpTypeOpaque)
363 CASE(OpTypePointer)
364 CASE(OpTypeFunction)
365 CASE(OpTypeEvent)
366 CASE(OpTypeDeviceEvent)
367 CASE(OpTypeReserveId)
368 CASE(OpTypeQueue)
369 CASE(OpTypePipe)
370 CASE(OpConstantTrue)
371 CASE(OpConstantFalse)
372 CASE(OpConstant)
373 CASE(OpConstantComposite)
374 CASE(OpConstantSampler)
375 CASE(OpConstantNull)
376 CASE(OpSpecConstantTrue)
377 CASE(OpSpecConstantFalse)
378 CASE(OpSpecConstant)
379 CASE(OpSpecConstantComposite)
380 CASE(OpVariable)
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100381 CASE(OpFunction)
382 CASE(OpFunctionParameter)
383 CASE(OpFunctionEnd)
384 CASE(OpFunctionCall)
385 CASE(OpExtInst)
386 CASE(OpUndef)
387 CASE(OpLoad)
388 CASE(OpStore)
389 CASE(OpPhi)
390 CASE(OpDecorationGroup)
391 CASE(OpDecorate)
392 CASE(OpMemberDecorate)
393 CASE(OpGroupDecorate)
394 CASE(OpGroupMemberDecorate)
395 CASE(OpName)
396 CASE(OpMemberName)
397 CASE(OpString)
398 CASE(OpLine)
399 CASE(OpVectorExtractDynamic)
400 CASE(OpVectorInsertDynamic)
401 CASE(OpVectorShuffle)
402 CASE(OpCompositeConstruct)
403 CASE(OpCompositeExtract)
404 CASE(OpCompositeInsert)
405 CASE(OpCopyObject)
406 CASE(OpCopyMemory)
407 CASE(OpCopyMemorySized)
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100408 CASE(OpAccessChain)
409 CASE(OpInBoundsAccessChain)
410 CASE(OpSNegate)
411 CASE(OpFNegate)
412 CASE(OpNot)
413 CASE(OpAny)
414 CASE(OpAll)
415 CASE(OpConvertFToU)
416 CASE(OpConvertFToS)
417 CASE(OpConvertSToF)
418 CASE(OpConvertUToF)
419 CASE(OpUConvert)
420 CASE(OpSConvert)
421 CASE(OpFConvert)
422 CASE(OpConvertPtrToU)
423 CASE(OpConvertUToPtr)
424 CASE(OpPtrCastToGeneric)
425 CASE(OpGenericCastToPtr)
426 CASE(OpBitcast)
427 CASE(OpTranspose)
428 CASE(OpIsNan)
429 CASE(OpIsInf)
430 CASE(OpIsFinite)
431 CASE(OpIsNormal)
432 CASE(OpSignBitSet)
433 CASE(OpLessOrGreater)
434 CASE(OpOrdered)
435 CASE(OpUnordered)
436 CASE(OpArrayLength)
437 CASE(OpIAdd)
438 CASE(OpFAdd)
439 CASE(OpISub)
440 CASE(OpFSub)
441 CASE(OpIMul)
442 CASE(OpFMul)
443 CASE(OpUDiv)
444 CASE(OpSDiv)
445 CASE(OpFDiv)
446 CASE(OpUMod)
447 CASE(OpSRem)
448 CASE(OpSMod)
449 CASE(OpFRem)
450 CASE(OpFMod)
451 CASE(OpVectorTimesScalar)
452 CASE(OpMatrixTimesScalar)
453 CASE(OpVectorTimesMatrix)
454 CASE(OpMatrixTimesVector)
455 CASE(OpMatrixTimesMatrix)
456 CASE(OpOuterProduct)
457 CASE(OpDot)
458 CASE(OpShiftRightLogical)
459 CASE(OpShiftRightArithmetic)
460 CASE(OpShiftLeftLogical)
461 CASE(OpLogicalOr)
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100462 CASE(OpLogicalAnd)
463 CASE(OpBitwiseOr)
464 CASE(OpBitwiseXor)
465 CASE(OpBitwiseAnd)
466 CASE(OpSelect)
467 CASE(OpIEqual)
468 CASE(OpFOrdEqual)
469 CASE(OpFUnordEqual)
470 CASE(OpINotEqual)
471 CASE(OpFOrdNotEqual)
472 CASE(OpFUnordNotEqual)
473 CASE(OpULessThan)
474 CASE(OpSLessThan)
475 CASE(OpFOrdLessThan)
476 CASE(OpFUnordLessThan)
477 CASE(OpUGreaterThan)
478 CASE(OpSGreaterThan)
479 CASE(OpFOrdGreaterThan)
480 CASE(OpFUnordGreaterThan)
481 CASE(OpULessThanEqual)
482 CASE(OpSLessThanEqual)
483 CASE(OpFOrdLessThanEqual)
484 CASE(OpFUnordLessThanEqual)
485 CASE(OpUGreaterThanEqual)
486 CASE(OpSGreaterThanEqual)
487 CASE(OpFOrdGreaterThanEqual)
488 CASE(OpFUnordGreaterThanEqual)
489 CASE(OpDPdx)
490 CASE(OpDPdy)
491 CASE(OpFwidth)
492 CASE(OpDPdxFine)
493 CASE(OpDPdyFine)
494 CASE(OpFwidthFine)
495 CASE(OpDPdxCoarse)
496 CASE(OpDPdyCoarse)
497 CASE(OpFwidthCoarse)
498 CASE(OpEmitVertex)
499 CASE(OpEndPrimitive)
500 CASE(OpEmitStreamVertex)
501 CASE(OpEndStreamPrimitive)
502 CASE(OpControlBarrier)
503 CASE(OpMemoryBarrier)
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100504 CASE(OpAtomicLoad)
505 CASE(OpAtomicStore)
506 CASE(OpAtomicExchange)
507 CASE(OpAtomicCompareExchange)
508 CASE(OpAtomicCompareExchangeWeak)
509 CASE(OpAtomicIIncrement)
510 CASE(OpAtomicIDecrement)
511 CASE(OpAtomicIAdd)
512 CASE(OpAtomicISub)
513 CASE(OpAtomicUMin)
514 CASE(OpAtomicUMax)
515 CASE(OpAtomicAnd)
516 CASE(OpAtomicOr)
517 CASE(OpAtomicXor)
518 CASE(OpLoopMerge)
519 CASE(OpSelectionMerge)
520 CASE(OpLabel)
521 CASE(OpBranch)
522 CASE(OpBranchConditional)
523 CASE(OpSwitch)
524 CASE(OpKill)
525 CASE(OpReturn)
526 CASE(OpReturnValue)
527 CASE(OpUnreachable)
528 CASE(OpLifetimeStart)
529 CASE(OpLifetimeStop)
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100530 CASE(OpAsyncGroupCopy)
531 CASE(OpWaitGroupEvents)
532 CASE(OpGroupAll)
533 CASE(OpGroupAny)
534 CASE(OpGroupBroadcast)
535 CASE(OpGroupIAdd)
536 CASE(OpGroupFAdd)
537 CASE(OpGroupFMin)
538 CASE(OpGroupUMin)
539 CASE(OpGroupSMin)
540 CASE(OpGroupFMax)
541 CASE(OpGroupUMax)
542 CASE(OpGroupSMax)
543 CASE(OpGenericCastToPtrExplicit)
544 CASE(OpGenericPtrMemSemantics)
545 CASE(OpReadPipe)
546 CASE(OpWritePipe)
547 CASE(OpReservedReadPipe)
548 CASE(OpReservedWritePipe)
549 CASE(OpReserveReadPipePackets)
550 CASE(OpReserveWritePipePackets)
551 CASE(OpCommitReadPipe)
552 CASE(OpCommitWritePipe)
553 CASE(OpIsValidReserveId)
554 CASE(OpGetNumPipePackets)
555 CASE(OpGetMaxPipePackets)
556 CASE(OpGroupReserveReadPipePackets)
557 CASE(OpGroupReserveWritePipePackets)
558 CASE(OpGroupCommitReadPipe)
559 CASE(OpGroupCommitWritePipe)
560 CASE(OpEnqueueMarker)
561 CASE(OpEnqueueKernel)
562 CASE(OpGetKernelNDrangeSubGroupCount)
563 CASE(OpGetKernelNDrangeMaxSubGroupSize)
564 CASE(OpGetKernelWorkGroupSize)
565 CASE(OpGetKernelPreferredWorkGroupSizeMultiple)
566 CASE(OpRetainEvent)
567 CASE(OpReleaseEvent)
568 CASE(OpCreateUserEvent)
569 CASE(OpIsValidEvent)
570 CASE(OpSetUserEventStatus)
571 CASE(OpCaptureEventProfilingInfo)
572 CASE(OpGetDefaultQueue)
573 CASE(OpBuildNDRange)
574 default:
575 assert(0 && "Unreachable!");
576 }
577#undef CASE
578 return "unknown";
579}
580
581int32_t spvOpcodeIsType(const Op opcode) {
582 switch (opcode) {
583 case OpTypeVoid:
584 case OpTypeBool:
585 case OpTypeInt:
586 case OpTypeFloat:
587 case OpTypeVector:
588 case OpTypeMatrix:
589 case OpTypeSampler:
Andrew Woloszyn157e41b2015-10-16 15:11:00 -0400590 case OpTypeSampledImage:
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100591 case OpTypeArray:
592 case OpTypeRuntimeArray:
593 case OpTypeStruct:
594 case OpTypeOpaque:
595 case OpTypePointer:
596 case OpTypeFunction:
597 case OpTypeEvent:
598 case OpTypeDeviceEvent:
599 case OpTypeReserveId:
600 case OpTypeQueue:
601 case OpTypePipe:
602 return true;
603 default:
604 return false;
605 }
606}
607
608int32_t spvOpcodeIsScalarType(const Op opcode) {
609 switch (opcode) {
610 case OpTypeInt:
611 case OpTypeFloat:
612 return true;
613 default:
614 return false;
615 }
616}
617
618int32_t spvOpcodeIsConstant(const Op opcode) {
619 switch (opcode) {
620 case OpConstantTrue:
621 case OpConstantFalse:
622 case OpConstant:
623 case OpConstantComposite:
624 case OpConstantSampler:
625 // case OpConstantNull:
626 case OpConstantNull:
627 case OpSpecConstantTrue:
628 case OpSpecConstantFalse:
629 case OpSpecConstant:
630 case OpSpecConstantComposite:
631 // case OpSpecConstantOp:
632 return true;
633 default:
634 return false;
635 }
636}
637
638int32_t spvOpcodeIsComposite(const Op opcode) {
639 switch (opcode) {
640 case OpTypeVector:
641 case OpTypeMatrix:
642 case OpTypeArray:
643 case OpTypeStruct:
644 return true;
645 default:
646 return false;
647 }
648}
649
650int32_t spvOpcodeAreTypesEqual(const spv_instruction_t *pTypeInst0,
651 const spv_instruction_t *pTypeInst1) {
Lei Zhang40056702015-09-11 14:31:27 -0400652 if (pTypeInst0->opcode != pTypeInst1->opcode) return false;
653 if (pTypeInst0->words[1] != pTypeInst1->words[1]) return false;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100654 return true;
655}
656
657int32_t spvOpcodeIsPointer(const Op opcode) {
658 switch (opcode) {
659 case OpVariable:
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100660 case OpAccessChain:
661 case OpInBoundsAccessChain:
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100662 case OpFunctionParameter:
663 return true;
664 default:
665 return false;
666 }
667}
668
669int32_t spvOpcodeIsObject(const Op opcode) {
670 switch (opcode) {
671 case OpConstantTrue:
672 case OpConstantFalse:
673 case OpConstant:
674 case OpConstantComposite:
675 // TODO: case OpConstantSampler:
676 case OpConstantNull:
677 case OpSpecConstantTrue:
678 case OpSpecConstantFalse:
679 case OpSpecConstant:
680 case OpSpecConstantComposite:
681 // TODO: case OpSpecConstantOp:
682 case OpVariable:
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100683 case OpAccessChain:
684 case OpInBoundsAccessChain:
685 case OpConvertFToU:
686 case OpConvertFToS:
687 case OpConvertSToF:
688 case OpConvertUToF:
689 case OpUConvert:
690 case OpSConvert:
691 case OpFConvert:
692 case OpConvertPtrToU:
693 // TODO: case OpConvertUToPtr:
694 case OpPtrCastToGeneric:
695 // TODO: case OpGenericCastToPtr:
696 case OpBitcast:
697 // TODO: case OpGenericCastToPtrExplicit:
698 case OpSatConvertSToU:
699 case OpSatConvertUToS:
700 case OpVectorExtractDynamic:
701 case OpCompositeConstruct:
702 case OpCompositeExtract:
703 case OpCopyObject:
704 case OpTranspose:
705 case OpSNegate:
706 case OpFNegate:
707 case OpNot:
708 case OpIAdd:
709 case OpFAdd:
710 case OpISub:
711 case OpFSub:
712 case OpIMul:
713 case OpFMul:
714 case OpUDiv:
715 case OpSDiv:
716 case OpFDiv:
717 case OpUMod:
718 case OpSRem:
719 case OpSMod:
720 case OpVectorTimesScalar:
721 case OpMatrixTimesScalar:
722 case OpVectorTimesMatrix:
723 case OpMatrixTimesVector:
724 case OpMatrixTimesMatrix:
725 case OpOuterProduct:
726 case OpDot:
727 case OpShiftRightLogical:
728 case OpShiftRightArithmetic:
729 case OpShiftLeftLogical:
730 case OpBitwiseOr:
731 case OpBitwiseXor:
732 case OpBitwiseAnd:
733 case OpAny:
734 case OpAll:
735 case OpIsNan:
736 case OpIsInf:
737 case OpIsFinite:
738 case OpIsNormal:
739 case OpSignBitSet:
740 case OpLessOrGreater:
741 case OpOrdered:
742 case OpUnordered:
743 case OpLogicalOr:
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100744 case OpLogicalAnd:
745 case OpSelect:
746 case OpIEqual:
747 case OpFOrdEqual:
748 case OpFUnordEqual:
749 case OpINotEqual:
750 case OpFOrdNotEqual:
751 case OpFUnordNotEqual:
752 case OpULessThan:
753 case OpSLessThan:
754 case OpFOrdLessThan:
755 case OpFUnordLessThan:
756 case OpUGreaterThan:
757 case OpSGreaterThan:
758 case OpFOrdGreaterThan:
759 case OpFUnordGreaterThan:
760 case OpULessThanEqual:
761 case OpSLessThanEqual:
762 case OpFOrdLessThanEqual:
763 case OpFUnordLessThanEqual:
764 case OpUGreaterThanEqual:
765 case OpSGreaterThanEqual:
766 case OpFOrdGreaterThanEqual:
767 case OpFUnordGreaterThanEqual:
768 case OpDPdx:
769 case OpDPdy:
770 case OpFwidth:
771 case OpDPdxFine:
772 case OpDPdyFine:
773 case OpFwidthFine:
774 case OpDPdxCoarse:
775 case OpDPdyCoarse:
776 case OpFwidthCoarse:
777 case OpReturnValue:
778 return true;
779 default:
780 return false;
781 }
782}
783
784int32_t spvOpcodeIsBasicTypeNullable(Op opcode) {
785 switch (opcode) {
786 case OpTypeBool:
787 case OpTypeInt:
788 case OpTypeFloat:
789 case OpTypePointer:
790 case OpTypeEvent:
791 case OpTypeDeviceEvent:
792 case OpTypeReserveId:
793 case OpTypeQueue:
794 return true;
795 default:
796 return false;
797 }
798}
799
800int32_t spvInstructionIsInBasicBlock(const spv_instruction_t *pFirstInst,
801 const spv_instruction_t *pInst) {
802 while (pFirstInst != pInst) {
Lei Zhang40056702015-09-11 14:31:27 -0400803 if (OpFunction == pInst->opcode) break;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100804 pInst--;
805 }
Lei Zhang40056702015-09-11 14:31:27 -0400806 if (OpFunction != pInst->opcode) return false;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100807 return true;
808}
809
810int32_t spvOpcodeIsValue(Op opcode) {
Lei Zhang40056702015-09-11 14:31:27 -0400811 if (spvOpcodeIsPointer(opcode)) return true;
812 if (spvOpcodeIsConstant(opcode)) return true;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100813 switch (opcode) {
814 case OpLoad:
815 // TODO: Other Opcode's resulting in a value
816 return true;
817 default:
818 return false;
819 }
820}
Andrew Woloszyn537e7762015-09-29 11:28:34 -0400821
822int32_t spvOpcodeGeneratesType(Op op) {
823 switch(op) {
824 case OpTypeVoid:
825 case OpTypeBool:
826 case OpTypeInt:
827 case OpTypeFloat:
828 case OpTypeVector:
829 case OpTypeMatrix:
830 case OpTypeImage:
831 case OpTypeSampler:
832 case OpTypeSampledImage:
833 case OpTypeArray:
834 case OpTypeRuntimeArray:
835 case OpTypeStruct:
836 case OpTypeOpaque:
837 case OpTypePointer:
838 case OpTypeFunction:
839 case OpTypeEvent:
840 case OpTypeDeviceEvent:
841 case OpTypeReserveId:
842 case OpTypeQueue:
843 case OpTypePipe:
844 case OpTypeForwardPointer:
845 return true;
846 default:;
847 }
848 return 0;
849}