blob: 8946cdee318cebd1681e03335326011d0ee08aa5 [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:
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100590 case OpTypeArray:
591 case OpTypeRuntimeArray:
592 case OpTypeStruct:
593 case OpTypeOpaque:
594 case OpTypePointer:
595 case OpTypeFunction:
596 case OpTypeEvent:
597 case OpTypeDeviceEvent:
598 case OpTypeReserveId:
599 case OpTypeQueue:
600 case OpTypePipe:
601 return true;
602 default:
603 return false;
604 }
605}
606
607int32_t spvOpcodeIsScalarType(const Op opcode) {
608 switch (opcode) {
609 case OpTypeInt:
610 case OpTypeFloat:
611 return true;
612 default:
613 return false;
614 }
615}
616
617int32_t spvOpcodeIsConstant(const Op opcode) {
618 switch (opcode) {
619 case OpConstantTrue:
620 case OpConstantFalse:
621 case OpConstant:
622 case OpConstantComposite:
623 case OpConstantSampler:
624 // case OpConstantNull:
625 case OpConstantNull:
626 case OpSpecConstantTrue:
627 case OpSpecConstantFalse:
628 case OpSpecConstant:
629 case OpSpecConstantComposite:
630 // case OpSpecConstantOp:
631 return true;
632 default:
633 return false;
634 }
635}
636
637int32_t spvOpcodeIsComposite(const Op opcode) {
638 switch (opcode) {
639 case OpTypeVector:
640 case OpTypeMatrix:
641 case OpTypeArray:
642 case OpTypeStruct:
643 return true;
644 default:
645 return false;
646 }
647}
648
649int32_t spvOpcodeAreTypesEqual(const spv_instruction_t *pTypeInst0,
650 const spv_instruction_t *pTypeInst1) {
Lei Zhang40056702015-09-11 14:31:27 -0400651 if (pTypeInst0->opcode != pTypeInst1->opcode) return false;
652 if (pTypeInst0->words[1] != pTypeInst1->words[1]) return false;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100653 return true;
654}
655
656int32_t spvOpcodeIsPointer(const Op opcode) {
657 switch (opcode) {
658 case OpVariable:
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100659 case OpAccessChain:
660 case OpInBoundsAccessChain:
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100661 case OpFunctionParameter:
662 return true;
663 default:
664 return false;
665 }
666}
667
668int32_t spvOpcodeIsObject(const Op opcode) {
669 switch (opcode) {
670 case OpConstantTrue:
671 case OpConstantFalse:
672 case OpConstant:
673 case OpConstantComposite:
674 // TODO: case OpConstantSampler:
675 case OpConstantNull:
676 case OpSpecConstantTrue:
677 case OpSpecConstantFalse:
678 case OpSpecConstant:
679 case OpSpecConstantComposite:
680 // TODO: case OpSpecConstantOp:
681 case OpVariable:
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100682 case OpAccessChain:
683 case OpInBoundsAccessChain:
684 case OpConvertFToU:
685 case OpConvertFToS:
686 case OpConvertSToF:
687 case OpConvertUToF:
688 case OpUConvert:
689 case OpSConvert:
690 case OpFConvert:
691 case OpConvertPtrToU:
692 // TODO: case OpConvertUToPtr:
693 case OpPtrCastToGeneric:
694 // TODO: case OpGenericCastToPtr:
695 case OpBitcast:
696 // TODO: case OpGenericCastToPtrExplicit:
697 case OpSatConvertSToU:
698 case OpSatConvertUToS:
699 case OpVectorExtractDynamic:
700 case OpCompositeConstruct:
701 case OpCompositeExtract:
702 case OpCopyObject:
703 case OpTranspose:
704 case OpSNegate:
705 case OpFNegate:
706 case OpNot:
707 case OpIAdd:
708 case OpFAdd:
709 case OpISub:
710 case OpFSub:
711 case OpIMul:
712 case OpFMul:
713 case OpUDiv:
714 case OpSDiv:
715 case OpFDiv:
716 case OpUMod:
717 case OpSRem:
718 case OpSMod:
719 case OpVectorTimesScalar:
720 case OpMatrixTimesScalar:
721 case OpVectorTimesMatrix:
722 case OpMatrixTimesVector:
723 case OpMatrixTimesMatrix:
724 case OpOuterProduct:
725 case OpDot:
726 case OpShiftRightLogical:
727 case OpShiftRightArithmetic:
728 case OpShiftLeftLogical:
729 case OpBitwiseOr:
730 case OpBitwiseXor:
731 case OpBitwiseAnd:
732 case OpAny:
733 case OpAll:
734 case OpIsNan:
735 case OpIsInf:
736 case OpIsFinite:
737 case OpIsNormal:
738 case OpSignBitSet:
739 case OpLessOrGreater:
740 case OpOrdered:
741 case OpUnordered:
742 case OpLogicalOr:
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100743 case OpLogicalAnd:
744 case OpSelect:
745 case OpIEqual:
746 case OpFOrdEqual:
747 case OpFUnordEqual:
748 case OpINotEqual:
749 case OpFOrdNotEqual:
750 case OpFUnordNotEqual:
751 case OpULessThan:
752 case OpSLessThan:
753 case OpFOrdLessThan:
754 case OpFUnordLessThan:
755 case OpUGreaterThan:
756 case OpSGreaterThan:
757 case OpFOrdGreaterThan:
758 case OpFUnordGreaterThan:
759 case OpULessThanEqual:
760 case OpSLessThanEqual:
761 case OpFOrdLessThanEqual:
762 case OpFUnordLessThanEqual:
763 case OpUGreaterThanEqual:
764 case OpSGreaterThanEqual:
765 case OpFOrdGreaterThanEqual:
766 case OpFUnordGreaterThanEqual:
767 case OpDPdx:
768 case OpDPdy:
769 case OpFwidth:
770 case OpDPdxFine:
771 case OpDPdyFine:
772 case OpFwidthFine:
773 case OpDPdxCoarse:
774 case OpDPdyCoarse:
775 case OpFwidthCoarse:
776 case OpReturnValue:
777 return true;
778 default:
779 return false;
780 }
781}
782
783int32_t spvOpcodeIsBasicTypeNullable(Op opcode) {
784 switch (opcode) {
785 case OpTypeBool:
786 case OpTypeInt:
787 case OpTypeFloat:
788 case OpTypePointer:
789 case OpTypeEvent:
790 case OpTypeDeviceEvent:
791 case OpTypeReserveId:
792 case OpTypeQueue:
793 return true;
794 default:
795 return false;
796 }
797}
798
799int32_t spvInstructionIsInBasicBlock(const spv_instruction_t *pFirstInst,
800 const spv_instruction_t *pInst) {
801 while (pFirstInst != pInst) {
Lei Zhang40056702015-09-11 14:31:27 -0400802 if (OpFunction == pInst->opcode) break;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100803 pInst--;
804 }
Lei Zhang40056702015-09-11 14:31:27 -0400805 if (OpFunction != pInst->opcode) return false;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100806 return true;
807}
808
809int32_t spvOpcodeIsValue(Op opcode) {
Lei Zhang40056702015-09-11 14:31:27 -0400810 if (spvOpcodeIsPointer(opcode)) return true;
811 if (spvOpcodeIsConstant(opcode)) return true;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100812 switch (opcode) {
813 case OpLoad:
814 // TODO: Other Opcode's resulting in a value
815 return true;
816 default:
817 return false;
818 }
819}
Andrew Woloszyn537e7762015-09-29 11:28:34 -0400820
821int32_t spvOpcodeGeneratesType(Op op) {
822 switch(op) {
823 case OpTypeVoid:
824 case OpTypeBool:
825 case OpTypeInt:
826 case OpTypeFloat:
827 case OpTypeVector:
828 case OpTypeMatrix:
829 case OpTypeImage:
830 case OpTypeSampler:
831 case OpTypeSampledImage:
832 case OpTypeArray:
833 case OpTypeRuntimeArray:
834 case OpTypeStruct:
835 case OpTypeOpaque:
836 case OpTypePointer:
837 case OpTypeFunction:
838 case OpTypeEvent:
839 case OpTypeDeviceEvent:
840 case OpTypeReserveId:
841 case OpTypeQueue:
842 case OpTypePipe:
843 case OpTypeForwardPointer:
844 return true;
845 default:;
846 }
847 return 0;
848}