blob: 3344a98c29d595c8625b8a9beef9b9315abb8158 [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"
29#include "opcode.h"
30
31#include <assert.h>
32#include <string.h>
33
David Neto78c3b432015-08-27 13:03:52 -040034namespace {
35
36// Descriptions of each opcode. Each entry describes the format of the
37// instruction that follows a particular opcode.
38//
39// Most fields are initialized statically by including an automatically
40// generated file.
41// The operandTypes fields are initialized during spvOpcodeInitialize().
42//
43// TODO(dneto): Some of the macros are quite unreadable. We could make
44// good use of constexpr functions, but some compilers don't support that yet.
45spv_opcode_desc_t opcodeTableEntries[] = {
46#define EmptyList {}
47#define List(...) {__VA_ARGS__}
Dejan Mircevski205408b2015-09-30 16:42:34 -040048#define Capability(X) SPV_CAPABILITY_AS_MASK(Capability##X)
49#define Capability2(X,Y) Capability(X)|Capability(Y)
50#define CapabilityNone 0 // Needed so Capability(None) still expands to valid syntax.
David Neto78c3b432015-08-27 13:03:52 -040051#define Instruction(Name,HasResult,HasType,NumLogicalOperands,NumCapabilities,CapabilityRequired,IsVariable,LogicalArgsList) \
52 { #Name, \
53 Op##Name, \
Dejan Mircevski205408b2015-09-30 16:42:34 -040054 (NumCapabilities) ? SPV_OPCODE_FLAGS_CAPABILITIES : 0, \
55 (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.
109 case OperandOptionalLiteral: return SPV_OPERAND_TYPE_OPTIONAL_LITERAL_NUMBER;
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.
112 case OperandVariableLiterals: return SPV_OPERAND_TYPE_VARIABLE_LITERAL_NUMBER;
David Neto78c3b432015-08-27 13:03:52 -0400113 case OperandLiteralNumber: return SPV_OPERAND_TYPE_LITERAL_NUMBER;
114 case OperandLiteralString: return SPV_OPERAND_TYPE_LITERAL_STRING;
115 case OperandSource: return SPV_OPERAND_TYPE_SOURCE_LANGUAGE;
116 case OperandExecutionModel: return SPV_OPERAND_TYPE_EXECUTION_MODEL;
117 case OperandAddressing: return SPV_OPERAND_TYPE_ADDRESSING_MODEL;
118 case OperandMemory: return SPV_OPERAND_TYPE_MEMORY_MODEL;
119 case OperandExecutionMode: return SPV_OPERAND_TYPE_EXECUTION_MODE;
120 case OperandStorage: return SPV_OPERAND_TYPE_STORAGE_CLASS;
121 case OperandDimensionality: return SPV_OPERAND_TYPE_DIMENSIONALITY;
122 case OperandSamplerAddressingMode: return SPV_OPERAND_TYPE_SAMPLER_ADDRESSING_MODE;
123 case OperandSamplerFilterMode: return SPV_OPERAND_TYPE_SAMPLER_FILTER_MODE;
David Netob30a0c52015-09-16 15:56:43 -0400124 case OperandSamplerImageFormat: return SPV_OPERAND_TYPE_SAMPLER_IMAGE_FORMAT;
David Neto7cefb232015-09-28 15:33:49 -0400125 case OperandImageChannelOrder:
126 // This is only used to describe the value generated by OpImageQueryOrder.
127 // It is not used as an operand.
128 break;
129 case OperandImageChannelDataType:
130 // This is only used to describe the value generated by OpImageQueryFormat.
131 // It is not used as an operand.
132 break;
133 case OperandImageOperands:
134 // This is not used. It's only an artifact of spec generation.
135 break;
David Neto78c3b432015-08-27 13:03:52 -0400136 case OperandFPFastMath: return SPV_OPERAND_TYPE_FP_FAST_MATH_MODE;
137 case OperandFPRoundingMode: return SPV_OPERAND_TYPE_FP_ROUNDING_MODE;
138 case OperandLinkageType: return SPV_OPERAND_TYPE_LINKAGE_TYPE;
139 case OperandAccessQualifier: return SPV_OPERAND_TYPE_ACCESS_QUALIFIER;
140 case OperandFuncParamAttr: return SPV_OPERAND_TYPE_FUNCTION_PARAMETER_ATTRIBUTE;
141 case OperandDecoration: return SPV_OPERAND_TYPE_DECORATION;
142 case OperandBuiltIn: return SPV_OPERAND_TYPE_BUILT_IN;
143 case OperandSelect: return SPV_OPERAND_TYPE_SELECTION_CONTROL;
144 case OperandLoop: return SPV_OPERAND_TYPE_LOOP_CONTROL;
145 case OperandFunction: return SPV_OPERAND_TYPE_FUNCTION_CONTROL;
146 case OperandMemorySemantics: return SPV_OPERAND_TYPE_MEMORY_SEMANTICS;
147 case OperandMemoryAccess:
David Neto7cefb232015-09-28 15:33:49 -0400148 // This case does not occur in the table for SPIR-V 0.99 Rev 32.
David Neto3fca4cd2015-09-17 17:39:45 -0400149 // We expect that it will become SPV_OPERAND_TYPE_OPTIONAL_MEMORY_ACCESS,
David Neto78c3b432015-08-27 13:03:52 -0400150 // and we can remove the special casing above for memory operation
151 // instructions.
152 break;
153 case OperandScope: return SPV_OPERAND_TYPE_EXECUTION_SCOPE;
154 case OperandGroupOperation: return SPV_OPERAND_TYPE_GROUP_OPERATION;
155 case OperandKernelEnqueueFlags: return SPV_OPERAND_TYPE_KERNEL_ENQ_FLAGS;
156 case OperandKernelProfilingInfo: return SPV_OPERAND_TYPE_KERNEL_PROFILING_INFO;
157 case OperandCapability: return SPV_OPERAND_TYPE_CAPABILITY;
158
159 // Used by GroupMemberDecorate
David Neto561dc4e2015-09-25 14:23:29 -0400160 case OperandVariableIdLiteral: return SPV_OPERAND_TYPE_VARIABLE_ID_LITERAL_NUMBER;
David Neto78c3b432015-08-27 13:03:52 -0400161
162 // Used by Switch
David Neto561dc4e2015-09-25 14:23:29 -0400163 case OperandVariableLiteralId: return SPV_OPERAND_TYPE_VARIABLE_LITERAL_NUMBER_ID;
David Neto78c3b432015-08-27 13:03:52 -0400164
165 // These exceptional cases shouldn't occur.
166 case OperandCount:
167 default:
168 break;
169 }
170 assert(0 && "Unexpected operand class");
171 return SPV_OPERAND_TYPE_NONE;
172}
173
Lei Zhanga94701d2015-09-14 10:05:37 -0400174} // anonymous namespace
David Neto78c3b432015-08-27 13:03:52 -0400175
176// Finish populating the opcodeTableEntries array.
177void spvOpcodeTableInitialize() {
178 // Compute the operandTypes field for each entry.
Lei Zhanga94701d2015-09-14 10:05:37 -0400179 for (auto &opcode : opcodeTableEntries) {
David Neto78c3b432015-08-27 13:03:52 -0400180 opcode.numTypes = 0;
181 // Type ID always comes first, if present.
182 if (opcode.hasType)
183 opcode.operandTypes[opcode.numTypes++] = SPV_OPERAND_TYPE_ID;
184 // Result ID always comes next, if present
185 if (opcode.hasResult)
186 opcode.operandTypes[opcode.numTypes++] = SPV_OPERAND_TYPE_RESULT_ID;
Lei Zhanga94701d2015-09-14 10:05:37 -0400187 const uint16_t maxNumOperands =
188 sizeof(opcode.operandTypes) / sizeof(opcode.operandTypes[0]);
189 const uint16_t maxNumClasses =
190 sizeof(opcode.operandClass) / sizeof(opcode.operandClass[0]);
191 for (uint16_t classIndex = 0;
192 opcode.numTypes < maxNumOperands && classIndex < maxNumClasses;
193 classIndex++) {
David Neto78c3b432015-08-27 13:03:52 -0400194 const OperandClass operandClass = opcode.operandClass[classIndex];
Lei Zhanga94701d2015-09-14 10:05:37 -0400195 opcode.operandTypes[opcode.numTypes++] =
196 convertOperandClassToType(opcode.opcode, operandClass);
David Neto78c3b432015-08-27 13:03:52 -0400197 // The OperandNone value is not explicitly represented in the .inc file.
198 // However, it is the zero value, and is created via implicit value
199 // initialization.
200 if (operandClass == OperandNone) {
201 opcode.numTypes--;
202 break;
203 }
204 }
205 // We should have written the terminating SPV_OPERAND_TYPE_NONE entry, but
206 // also without overflowing.
Lei Zhanga94701d2015-09-14 10:05:37 -0400207 assert((opcode.numTypes < maxNumOperands) &&
208 "Operand class list is too long. Expand "
209 "spv_opcode_desc_t.operandClass");
David Neto78c3b432015-08-27 13:03:52 -0400210 }
211 opcodeTableInitialized = true;
212}
213
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100214const char *spvGeneratorStr(uint32_t generator) {
215 switch (generator) {
216 case SPV_GENERATOR_KHRONOS:
217 return "Khronos";
218 case SPV_GENERATOR_VALVE:
219 return "Valve";
220 case SPV_GENERATOR_LUNARG:
221 return "LunarG";
222 case SPV_GENERATOR_CODEPLAY:
223 return "Codeplay Software Ltd.";
224 default:
225 return "Unknown";
226 }
227}
228
229uint32_t spvOpcodeMake(uint16_t wordCount, Op opcode) {
230 return ((uint32_t)opcode) | (((uint32_t)wordCount) << 16);
231}
232
233void spvOpcodeSplit(const uint32_t word, uint16_t *pWordCount, Op *pOpcode) {
234 if (pWordCount) {
235 *pWordCount = (uint16_t)((0xffff0000 & word) >> 16);
236 }
237 if (pOpcode) {
238 *pOpcode = (Op)(0x0000ffff & word);
239 }
240}
241
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100242spv_result_t spvOpcodeTableGet(spv_opcode_table *pInstTable) {
Lei Zhang40056702015-09-11 14:31:27 -0400243 if (!pInstTable) return SPV_ERROR_INVALID_POINTER;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100244
David Neto78c3b432015-08-27 13:03:52 -0400245 static spv_opcode_table_t table = {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100246 sizeof(opcodeTableEntries) / sizeof(spv_opcode_desc_t),
247 opcodeTableEntries};
248
David Neto78c3b432015-08-27 13:03:52 -0400249 // TODO(dneto): Consider thread safety of initialization.
250 // That is, ordering effects of the flag vs. the table updates.
Lei Zhang40056702015-09-11 14:31:27 -0400251 if (!opcodeTableInitialized) spvOpcodeTableInitialize();
David Neto78c3b432015-08-27 13:03:52 -0400252
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100253 *pInstTable = &table;
254
255 return SPV_SUCCESS;
256}
257
258spv_result_t spvOpcodeTableNameLookup(const spv_opcode_table table,
259 const char *name,
260 spv_opcode_desc *pEntry) {
Lei Zhang40056702015-09-11 14:31:27 -0400261 if (!name || !pEntry) return SPV_ERROR_INVALID_POINTER;
262 if (!table) return SPV_ERROR_INVALID_TABLE;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100263
264 // TODO: This lookup of the Opcode table is suboptimal! Binary sort would be
265 // preferable but the table requires sorting on the Opcode name, but it's
266 // static
267 // const initialized and matches the order of the spec.
268 const size_t nameLength = strlen(name);
269 for (uint64_t opcodeIndex = 0; opcodeIndex < table->count; ++opcodeIndex) {
270 if (nameLength == strlen(table->entries[opcodeIndex].name) &&
271 !strncmp(name, table->entries[opcodeIndex].name, nameLength)) {
272 // NOTE: Found out Opcode!
273 *pEntry = &table->entries[opcodeIndex];
274 return SPV_SUCCESS;
275 }
276 }
277
278 return SPV_ERROR_INVALID_LOOKUP;
279}
280
281spv_result_t spvOpcodeTableValueLookup(const spv_opcode_table table,
282 const Op opcode,
283 spv_opcode_desc *pEntry) {
Lei Zhang40056702015-09-11 14:31:27 -0400284 if (!table) return SPV_ERROR_INVALID_TABLE;
285 if (!pEntry) return SPV_ERROR_INVALID_POINTER;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100286
287 // TODO: As above this lookup is not optimal.
288 for (uint64_t opcodeIndex = 0; opcodeIndex < table->count; ++opcodeIndex) {
289 if (opcode == table->entries[opcodeIndex].opcode) {
290 // NOTE: Found the Opcode!
291 *pEntry = &table->entries[opcodeIndex];
292 return SPV_SUCCESS;
293 }
294 }
295
296 return SPV_ERROR_INVALID_LOOKUP;
297}
298
Lei Zhangdfc50082015-08-21 11:50:55 -0400299int16_t spvOpcodeResultIdIndex(spv_opcode_desc entry) {
David Neto78c3b432015-08-27 13:03:52 -0400300 for (int16_t i = 0; i < entry->numTypes; ++i) {
Lei Zhangdfc50082015-08-21 11:50:55 -0400301 if (SPV_OPERAND_TYPE_RESULT_ID == entry->operandTypes[i]) return i;
302 }
303 return SPV_OPERAND_INVALID_RESULT_ID_INDEX;
304}
305
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100306int32_t spvOpcodeRequiresCapabilities(spv_opcode_desc entry) {
307 return SPV_OPCODE_FLAGS_CAPABILITIES ==
308 (SPV_OPCODE_FLAGS_CAPABILITIES & entry->flags);
309}
310
311void spvInstructionCopy(const uint32_t *words, const Op opcode,
312 const uint16_t wordCount, const spv_endianness_t endian,
313 spv_instruction_t *pInst) {
314 pInst->opcode = opcode;
315 pInst->wordCount = wordCount;
316 for (uint16_t wordIndex = 0; wordIndex < wordCount; ++wordIndex) {
317 pInst->words[wordIndex] = spvFixWord(words[wordIndex], endian);
318 if (!wordIndex) {
319 uint16_t thisWordCount;
320 Op thisOpcode;
321 spvOpcodeSplit(pInst->words[wordIndex], &thisWordCount, &thisOpcode);
322 assert(opcode == thisOpcode && wordCount == thisWordCount &&
323 "Endianness failed!");
324 }
325 }
326}
327
328const char *spvOpcodeString(const Op opcode) {
329#define CASE(OPCODE) \
330 case OPCODE: \
331 return #OPCODE;
332 switch (opcode) {
333 CASE(OpNop)
334 CASE(OpSource)
335 CASE(OpSourceExtension)
336 CASE(OpExtension)
337 CASE(OpExtInstImport)
338 CASE(OpMemoryModel)
339 CASE(OpEntryPoint)
340 CASE(OpExecutionMode)
341 CASE(OpTypeVoid)
342 CASE(OpTypeBool)
343 CASE(OpTypeInt)
344 CASE(OpTypeFloat)
345 CASE(OpTypeVector)
346 CASE(OpTypeMatrix)
347 CASE(OpTypeSampler)
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100348 CASE(OpTypeArray)
349 CASE(OpTypeRuntimeArray)
350 CASE(OpTypeStruct)
351 CASE(OpTypeOpaque)
352 CASE(OpTypePointer)
353 CASE(OpTypeFunction)
354 CASE(OpTypeEvent)
355 CASE(OpTypeDeviceEvent)
356 CASE(OpTypeReserveId)
357 CASE(OpTypeQueue)
358 CASE(OpTypePipe)
359 CASE(OpConstantTrue)
360 CASE(OpConstantFalse)
361 CASE(OpConstant)
362 CASE(OpConstantComposite)
363 CASE(OpConstantSampler)
364 CASE(OpConstantNull)
365 CASE(OpSpecConstantTrue)
366 CASE(OpSpecConstantFalse)
367 CASE(OpSpecConstant)
368 CASE(OpSpecConstantComposite)
369 CASE(OpVariable)
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100370 CASE(OpFunction)
371 CASE(OpFunctionParameter)
372 CASE(OpFunctionEnd)
373 CASE(OpFunctionCall)
374 CASE(OpExtInst)
375 CASE(OpUndef)
376 CASE(OpLoad)
377 CASE(OpStore)
378 CASE(OpPhi)
379 CASE(OpDecorationGroup)
380 CASE(OpDecorate)
381 CASE(OpMemberDecorate)
382 CASE(OpGroupDecorate)
383 CASE(OpGroupMemberDecorate)
384 CASE(OpName)
385 CASE(OpMemberName)
386 CASE(OpString)
387 CASE(OpLine)
388 CASE(OpVectorExtractDynamic)
389 CASE(OpVectorInsertDynamic)
390 CASE(OpVectorShuffle)
391 CASE(OpCompositeConstruct)
392 CASE(OpCompositeExtract)
393 CASE(OpCompositeInsert)
394 CASE(OpCopyObject)
395 CASE(OpCopyMemory)
396 CASE(OpCopyMemorySized)
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100397 CASE(OpAccessChain)
398 CASE(OpInBoundsAccessChain)
399 CASE(OpSNegate)
400 CASE(OpFNegate)
401 CASE(OpNot)
402 CASE(OpAny)
403 CASE(OpAll)
404 CASE(OpConvertFToU)
405 CASE(OpConvertFToS)
406 CASE(OpConvertSToF)
407 CASE(OpConvertUToF)
408 CASE(OpUConvert)
409 CASE(OpSConvert)
410 CASE(OpFConvert)
411 CASE(OpConvertPtrToU)
412 CASE(OpConvertUToPtr)
413 CASE(OpPtrCastToGeneric)
414 CASE(OpGenericCastToPtr)
415 CASE(OpBitcast)
416 CASE(OpTranspose)
417 CASE(OpIsNan)
418 CASE(OpIsInf)
419 CASE(OpIsFinite)
420 CASE(OpIsNormal)
421 CASE(OpSignBitSet)
422 CASE(OpLessOrGreater)
423 CASE(OpOrdered)
424 CASE(OpUnordered)
425 CASE(OpArrayLength)
426 CASE(OpIAdd)
427 CASE(OpFAdd)
428 CASE(OpISub)
429 CASE(OpFSub)
430 CASE(OpIMul)
431 CASE(OpFMul)
432 CASE(OpUDiv)
433 CASE(OpSDiv)
434 CASE(OpFDiv)
435 CASE(OpUMod)
436 CASE(OpSRem)
437 CASE(OpSMod)
438 CASE(OpFRem)
439 CASE(OpFMod)
440 CASE(OpVectorTimesScalar)
441 CASE(OpMatrixTimesScalar)
442 CASE(OpVectorTimesMatrix)
443 CASE(OpMatrixTimesVector)
444 CASE(OpMatrixTimesMatrix)
445 CASE(OpOuterProduct)
446 CASE(OpDot)
447 CASE(OpShiftRightLogical)
448 CASE(OpShiftRightArithmetic)
449 CASE(OpShiftLeftLogical)
450 CASE(OpLogicalOr)
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100451 CASE(OpLogicalAnd)
452 CASE(OpBitwiseOr)
453 CASE(OpBitwiseXor)
454 CASE(OpBitwiseAnd)
455 CASE(OpSelect)
456 CASE(OpIEqual)
457 CASE(OpFOrdEqual)
458 CASE(OpFUnordEqual)
459 CASE(OpINotEqual)
460 CASE(OpFOrdNotEqual)
461 CASE(OpFUnordNotEqual)
462 CASE(OpULessThan)
463 CASE(OpSLessThan)
464 CASE(OpFOrdLessThan)
465 CASE(OpFUnordLessThan)
466 CASE(OpUGreaterThan)
467 CASE(OpSGreaterThan)
468 CASE(OpFOrdGreaterThan)
469 CASE(OpFUnordGreaterThan)
470 CASE(OpULessThanEqual)
471 CASE(OpSLessThanEqual)
472 CASE(OpFOrdLessThanEqual)
473 CASE(OpFUnordLessThanEqual)
474 CASE(OpUGreaterThanEqual)
475 CASE(OpSGreaterThanEqual)
476 CASE(OpFOrdGreaterThanEqual)
477 CASE(OpFUnordGreaterThanEqual)
478 CASE(OpDPdx)
479 CASE(OpDPdy)
480 CASE(OpFwidth)
481 CASE(OpDPdxFine)
482 CASE(OpDPdyFine)
483 CASE(OpFwidthFine)
484 CASE(OpDPdxCoarse)
485 CASE(OpDPdyCoarse)
486 CASE(OpFwidthCoarse)
487 CASE(OpEmitVertex)
488 CASE(OpEndPrimitive)
489 CASE(OpEmitStreamVertex)
490 CASE(OpEndStreamPrimitive)
491 CASE(OpControlBarrier)
492 CASE(OpMemoryBarrier)
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100493 CASE(OpAtomicLoad)
494 CASE(OpAtomicStore)
495 CASE(OpAtomicExchange)
496 CASE(OpAtomicCompareExchange)
497 CASE(OpAtomicCompareExchangeWeak)
498 CASE(OpAtomicIIncrement)
499 CASE(OpAtomicIDecrement)
500 CASE(OpAtomicIAdd)
501 CASE(OpAtomicISub)
502 CASE(OpAtomicUMin)
503 CASE(OpAtomicUMax)
504 CASE(OpAtomicAnd)
505 CASE(OpAtomicOr)
506 CASE(OpAtomicXor)
507 CASE(OpLoopMerge)
508 CASE(OpSelectionMerge)
509 CASE(OpLabel)
510 CASE(OpBranch)
511 CASE(OpBranchConditional)
512 CASE(OpSwitch)
513 CASE(OpKill)
514 CASE(OpReturn)
515 CASE(OpReturnValue)
516 CASE(OpUnreachable)
517 CASE(OpLifetimeStart)
518 CASE(OpLifetimeStop)
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100519 CASE(OpAsyncGroupCopy)
520 CASE(OpWaitGroupEvents)
521 CASE(OpGroupAll)
522 CASE(OpGroupAny)
523 CASE(OpGroupBroadcast)
524 CASE(OpGroupIAdd)
525 CASE(OpGroupFAdd)
526 CASE(OpGroupFMin)
527 CASE(OpGroupUMin)
528 CASE(OpGroupSMin)
529 CASE(OpGroupFMax)
530 CASE(OpGroupUMax)
531 CASE(OpGroupSMax)
532 CASE(OpGenericCastToPtrExplicit)
533 CASE(OpGenericPtrMemSemantics)
534 CASE(OpReadPipe)
535 CASE(OpWritePipe)
536 CASE(OpReservedReadPipe)
537 CASE(OpReservedWritePipe)
538 CASE(OpReserveReadPipePackets)
539 CASE(OpReserveWritePipePackets)
540 CASE(OpCommitReadPipe)
541 CASE(OpCommitWritePipe)
542 CASE(OpIsValidReserveId)
543 CASE(OpGetNumPipePackets)
544 CASE(OpGetMaxPipePackets)
545 CASE(OpGroupReserveReadPipePackets)
546 CASE(OpGroupReserveWritePipePackets)
547 CASE(OpGroupCommitReadPipe)
548 CASE(OpGroupCommitWritePipe)
549 CASE(OpEnqueueMarker)
550 CASE(OpEnqueueKernel)
551 CASE(OpGetKernelNDrangeSubGroupCount)
552 CASE(OpGetKernelNDrangeMaxSubGroupSize)
553 CASE(OpGetKernelWorkGroupSize)
554 CASE(OpGetKernelPreferredWorkGroupSizeMultiple)
555 CASE(OpRetainEvent)
556 CASE(OpReleaseEvent)
557 CASE(OpCreateUserEvent)
558 CASE(OpIsValidEvent)
559 CASE(OpSetUserEventStatus)
560 CASE(OpCaptureEventProfilingInfo)
561 CASE(OpGetDefaultQueue)
562 CASE(OpBuildNDRange)
563 default:
564 assert(0 && "Unreachable!");
565 }
566#undef CASE
567 return "unknown";
568}
569
570int32_t spvOpcodeIsType(const Op opcode) {
571 switch (opcode) {
572 case OpTypeVoid:
573 case OpTypeBool:
574 case OpTypeInt:
575 case OpTypeFloat:
576 case OpTypeVector:
577 case OpTypeMatrix:
578 case OpTypeSampler:
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100579 case OpTypeArray:
580 case OpTypeRuntimeArray:
581 case OpTypeStruct:
582 case OpTypeOpaque:
583 case OpTypePointer:
584 case OpTypeFunction:
585 case OpTypeEvent:
586 case OpTypeDeviceEvent:
587 case OpTypeReserveId:
588 case OpTypeQueue:
589 case OpTypePipe:
590 return true;
591 default:
592 return false;
593 }
594}
595
596int32_t spvOpcodeIsScalarType(const Op opcode) {
597 switch (opcode) {
598 case OpTypeInt:
599 case OpTypeFloat:
600 return true;
601 default:
602 return false;
603 }
604}
605
606int32_t spvOpcodeIsConstant(const Op opcode) {
607 switch (opcode) {
608 case OpConstantTrue:
609 case OpConstantFalse:
610 case OpConstant:
611 case OpConstantComposite:
612 case OpConstantSampler:
613 // case OpConstantNull:
614 case OpConstantNull:
615 case OpSpecConstantTrue:
616 case OpSpecConstantFalse:
617 case OpSpecConstant:
618 case OpSpecConstantComposite:
619 // case OpSpecConstantOp:
620 return true;
621 default:
622 return false;
623 }
624}
625
626int32_t spvOpcodeIsComposite(const Op opcode) {
627 switch (opcode) {
628 case OpTypeVector:
629 case OpTypeMatrix:
630 case OpTypeArray:
631 case OpTypeStruct:
632 return true;
633 default:
634 return false;
635 }
636}
637
638int32_t spvOpcodeAreTypesEqual(const spv_instruction_t *pTypeInst0,
639 const spv_instruction_t *pTypeInst1) {
Lei Zhang40056702015-09-11 14:31:27 -0400640 if (pTypeInst0->opcode != pTypeInst1->opcode) return false;
641 if (pTypeInst0->words[1] != pTypeInst1->words[1]) return false;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100642 return true;
643}
644
645int32_t spvOpcodeIsPointer(const Op opcode) {
646 switch (opcode) {
647 case OpVariable:
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100648 case OpAccessChain:
649 case OpInBoundsAccessChain:
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100650 case OpFunctionParameter:
651 return true;
652 default:
653 return false;
654 }
655}
656
657int32_t spvOpcodeIsObject(const Op opcode) {
658 switch (opcode) {
659 case OpConstantTrue:
660 case OpConstantFalse:
661 case OpConstant:
662 case OpConstantComposite:
663 // TODO: case OpConstantSampler:
664 case OpConstantNull:
665 case OpSpecConstantTrue:
666 case OpSpecConstantFalse:
667 case OpSpecConstant:
668 case OpSpecConstantComposite:
669 // TODO: case OpSpecConstantOp:
670 case OpVariable:
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100671 case OpAccessChain:
672 case OpInBoundsAccessChain:
673 case OpConvertFToU:
674 case OpConvertFToS:
675 case OpConvertSToF:
676 case OpConvertUToF:
677 case OpUConvert:
678 case OpSConvert:
679 case OpFConvert:
680 case OpConvertPtrToU:
681 // TODO: case OpConvertUToPtr:
682 case OpPtrCastToGeneric:
683 // TODO: case OpGenericCastToPtr:
684 case OpBitcast:
685 // TODO: case OpGenericCastToPtrExplicit:
686 case OpSatConvertSToU:
687 case OpSatConvertUToS:
688 case OpVectorExtractDynamic:
689 case OpCompositeConstruct:
690 case OpCompositeExtract:
691 case OpCopyObject:
692 case OpTranspose:
693 case OpSNegate:
694 case OpFNegate:
695 case OpNot:
696 case OpIAdd:
697 case OpFAdd:
698 case OpISub:
699 case OpFSub:
700 case OpIMul:
701 case OpFMul:
702 case OpUDiv:
703 case OpSDiv:
704 case OpFDiv:
705 case OpUMod:
706 case OpSRem:
707 case OpSMod:
708 case OpVectorTimesScalar:
709 case OpMatrixTimesScalar:
710 case OpVectorTimesMatrix:
711 case OpMatrixTimesVector:
712 case OpMatrixTimesMatrix:
713 case OpOuterProduct:
714 case OpDot:
715 case OpShiftRightLogical:
716 case OpShiftRightArithmetic:
717 case OpShiftLeftLogical:
718 case OpBitwiseOr:
719 case OpBitwiseXor:
720 case OpBitwiseAnd:
721 case OpAny:
722 case OpAll:
723 case OpIsNan:
724 case OpIsInf:
725 case OpIsFinite:
726 case OpIsNormal:
727 case OpSignBitSet:
728 case OpLessOrGreater:
729 case OpOrdered:
730 case OpUnordered:
731 case OpLogicalOr:
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100732 case OpLogicalAnd:
733 case OpSelect:
734 case OpIEqual:
735 case OpFOrdEqual:
736 case OpFUnordEqual:
737 case OpINotEqual:
738 case OpFOrdNotEqual:
739 case OpFUnordNotEqual:
740 case OpULessThan:
741 case OpSLessThan:
742 case OpFOrdLessThan:
743 case OpFUnordLessThan:
744 case OpUGreaterThan:
745 case OpSGreaterThan:
746 case OpFOrdGreaterThan:
747 case OpFUnordGreaterThan:
748 case OpULessThanEqual:
749 case OpSLessThanEqual:
750 case OpFOrdLessThanEqual:
751 case OpFUnordLessThanEqual:
752 case OpUGreaterThanEqual:
753 case OpSGreaterThanEqual:
754 case OpFOrdGreaterThanEqual:
755 case OpFUnordGreaterThanEqual:
756 case OpDPdx:
757 case OpDPdy:
758 case OpFwidth:
759 case OpDPdxFine:
760 case OpDPdyFine:
761 case OpFwidthFine:
762 case OpDPdxCoarse:
763 case OpDPdyCoarse:
764 case OpFwidthCoarse:
765 case OpReturnValue:
766 return true;
767 default:
768 return false;
769 }
770}
771
772int32_t spvOpcodeIsBasicTypeNullable(Op opcode) {
773 switch (opcode) {
774 case OpTypeBool:
775 case OpTypeInt:
776 case OpTypeFloat:
777 case OpTypePointer:
778 case OpTypeEvent:
779 case OpTypeDeviceEvent:
780 case OpTypeReserveId:
781 case OpTypeQueue:
782 return true;
783 default:
784 return false;
785 }
786}
787
788int32_t spvInstructionIsInBasicBlock(const spv_instruction_t *pFirstInst,
789 const spv_instruction_t *pInst) {
790 while (pFirstInst != pInst) {
Lei Zhang40056702015-09-11 14:31:27 -0400791 if (OpFunction == pInst->opcode) break;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100792 pInst--;
793 }
Lei Zhang40056702015-09-11 14:31:27 -0400794 if (OpFunction != pInst->opcode) return false;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100795 return true;
796}
797
798int32_t spvOpcodeIsValue(Op opcode) {
Lei Zhang40056702015-09-11 14:31:27 -0400799 if (spvOpcodeIsPointer(opcode)) return true;
800 if (spvOpcodeIsConstant(opcode)) return true;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100801 switch (opcode) {
802 case OpLoad:
803 // TODO: Other Opcode's resulting in a value
804 return true;
805 default:
806 return false;
807 }
808}