blob: d1550331344f5123d58022f5e3606e8f5cf680dd [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__}
48#define Capability(X) Capability##X
49#define CapabilityNone -1
50#define Instruction(Name,HasResult,HasType,NumLogicalOperands,NumCapabilities,CapabilityRequired,IsVariable,LogicalArgsList) \
51 { #Name, \
52 Op##Name, \
David Netoc9b51522015-09-08 14:59:57 -040053 ((CapabilityRequired != CapabilityNone ? SPV_OPCODE_FLAGS_CAPABILITIES : 0)), \
David Neto78c3b432015-08-27 13:03:52 -040054 uint32_t(CapabilityRequired), \
55 0, {}, /* Filled in later. Operand list, including result id and type id, if needed */ \
56 HasResult, \
57 HasType, \
58 LogicalArgsList },
59#include "opcode.inc"
60#undef EmptyList
61#undef List
62#undef Capability
63#undef CapabilityNone
64#undef Instruction
65};
66
67// Has the opcodeTableEntries table been fully elaborated?
68// That is, are the operandTypes fields initialized?
69bool opcodeTableInitialized = false;
70
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010071// Opcode API
72
David Neto78c3b432015-08-27 13:03:52 -040073// Converts the given operand class enum (from the SPIR-V document generation
74// logic) to the operand type required by the parser.
75// This only applies to logical operands.
Lei Zhang40056702015-09-11 14:31:27 -040076spv_operand_type_t convertOperandClassToType(spv::Op opcode,
77 spv::OperandClass operandClass) {
David Neto78c3b432015-08-27 13:03:52 -040078 // The spec document generator uses OptionalOperandLiteral for several kinds
79 // of repeating values. Our parser needs more specific information about
80 // what is being repeated.
81 if (operandClass == OperandOptionalLiteral) {
Lei Zhang40056702015-09-11 14:31:27 -040082 switch (opcode) {
David Neto78c3b432015-08-27 13:03:52 -040083 case spv::OpLoad:
84 case spv::OpStore:
85 case spv::OpCopyMemory:
86 case spv::OpCopyMemorySized:
87 return SPV_OPERAND_TYPE_VARIABLE_MEMORY_ACCESS;
88 case spv::OpExecutionMode:
89 return SPV_OPERAND_TYPE_VARIABLE_EXECUTION_MODE;
90 default:
91 break;
92 }
93 }
94
95 switch(operandClass) {
96 case OperandNone: return SPV_OPERAND_TYPE_NONE;
97 case OperandId: return SPV_OPERAND_TYPE_ID;
98 case OperandOptionalId: return SPV_OPERAND_TYPE_OPTIONAL_ID;
99 case OperandOptionalImage: return SPV_OPERAND_TYPE_OPTIONAL_IMAGE; // TODO(dneto): This is variable.
100 case OperandVariableIds: return SPV_OPERAND_TYPE_VARIABLE_ID;
101 case OperandOptionalLiteral: return SPV_OPERAND_TYPE_OPTIONAL_LITERAL;
102 case OperandOptionalLiteralString: return SPV_OPERAND_TYPE_OPTIONAL_LITERAL_STRING;
103 case OperandVariableLiterals: return SPV_OPERAND_TYPE_VARIABLE_LITERAL;
104 case OperandLiteralNumber: return SPV_OPERAND_TYPE_LITERAL_NUMBER;
105 case OperandLiteralString: return SPV_OPERAND_TYPE_LITERAL_STRING;
106 case OperandSource: return SPV_OPERAND_TYPE_SOURCE_LANGUAGE;
107 case OperandExecutionModel: return SPV_OPERAND_TYPE_EXECUTION_MODEL;
108 case OperandAddressing: return SPV_OPERAND_TYPE_ADDRESSING_MODEL;
109 case OperandMemory: return SPV_OPERAND_TYPE_MEMORY_MODEL;
110 case OperandExecutionMode: return SPV_OPERAND_TYPE_EXECUTION_MODE;
111 case OperandStorage: return SPV_OPERAND_TYPE_STORAGE_CLASS;
112 case OperandDimensionality: return SPV_OPERAND_TYPE_DIMENSIONALITY;
113 case OperandSamplerAddressingMode: return SPV_OPERAND_TYPE_SAMPLER_ADDRESSING_MODE;
114 case OperandSamplerFilterMode: return SPV_OPERAND_TYPE_SAMPLER_FILTER_MODE;
115 case OperandSamplerImageFormat: return SPV_OPERAND_TYPE_NONE; //TODO
116 case OperandImageChannelOrder: return SPV_OPERAND_TYPE_NONE; //TODO
117 case OperandImageChannelDataType: return SPV_OPERAND_TYPE_NONE; //TODO
118 case OperandImageOperands: return SPV_OPERAND_TYPE_NONE; //TODO
119 case OperandFPFastMath: return SPV_OPERAND_TYPE_FP_FAST_MATH_MODE;
120 case OperandFPRoundingMode: return SPV_OPERAND_TYPE_FP_ROUNDING_MODE;
121 case OperandLinkageType: return SPV_OPERAND_TYPE_LINKAGE_TYPE;
122 case OperandAccessQualifier: return SPV_OPERAND_TYPE_ACCESS_QUALIFIER;
123 case OperandFuncParamAttr: return SPV_OPERAND_TYPE_FUNCTION_PARAMETER_ATTRIBUTE;
124 case OperandDecoration: return SPV_OPERAND_TYPE_DECORATION;
125 case OperandBuiltIn: return SPV_OPERAND_TYPE_BUILT_IN;
126 case OperandSelect: return SPV_OPERAND_TYPE_SELECTION_CONTROL;
127 case OperandLoop: return SPV_OPERAND_TYPE_LOOP_CONTROL;
128 case OperandFunction: return SPV_OPERAND_TYPE_FUNCTION_CONTROL;
129 case OperandMemorySemantics: return SPV_OPERAND_TYPE_MEMORY_SEMANTICS;
130 case OperandMemoryAccess:
131 // This case does not occur in Rev 31.
132 // We expect that it will become SPV_OPERAND_TYPE_VARIABLE_MEMORY_ACCESS,
133 // and we can remove the special casing above for memory operation
134 // instructions.
135 break;
136 case OperandScope: return SPV_OPERAND_TYPE_EXECUTION_SCOPE;
137 case OperandGroupOperation: return SPV_OPERAND_TYPE_GROUP_OPERATION;
138 case OperandKernelEnqueueFlags: return SPV_OPERAND_TYPE_KERNEL_ENQ_FLAGS;
139 case OperandKernelProfilingInfo: return SPV_OPERAND_TYPE_KERNEL_PROFILING_INFO;
140 case OperandCapability: return SPV_OPERAND_TYPE_CAPABILITY;
141
142 // Used by GroupMemberDecorate
143 case OperandVariableIdLiteral: return SPV_OPERAND_TYPE_VARIABLE_ID_LITERAL;
144
145 // Used by Switch
146 case OperandVariableLiteralId: return SPV_OPERAND_TYPE_VARIABLE_LITERAL_ID;
147
148 // These exceptional cases shouldn't occur.
149 case OperandCount:
150 default:
151 break;
152 }
153 assert(0 && "Unexpected operand class");
154 return SPV_OPERAND_TYPE_NONE;
155}
156
157} // anonymous namespace
158
159// Finish populating the opcodeTableEntries array.
160void spvOpcodeTableInitialize() {
161 // Compute the operandTypes field for each entry.
162 for (auto& opcode : opcodeTableEntries) {
163 opcode.numTypes = 0;
164 // Type ID always comes first, if present.
165 if (opcode.hasType)
166 opcode.operandTypes[opcode.numTypes++] = SPV_OPERAND_TYPE_ID;
167 // Result ID always comes next, if present
168 if (opcode.hasResult)
169 opcode.operandTypes[opcode.numTypes++] = SPV_OPERAND_TYPE_RESULT_ID;
170 const uint16_t maxNumOperands = sizeof(opcode.operandTypes)/sizeof(opcode.operandTypes[0]);
171 const uint16_t maxNumClasses = sizeof(opcode.operandClass)/sizeof(opcode.operandClass[0]);
172 for (uint16_t classIndex = 0; opcode.numTypes < maxNumOperands && classIndex < maxNumClasses ; classIndex++ ) {
173 const OperandClass operandClass = opcode.operandClass[classIndex];
174 opcode.operandTypes[opcode.numTypes++] = convertOperandClassToType(opcode.opcode, operandClass);
175 // The OperandNone value is not explicitly represented in the .inc file.
176 // However, it is the zero value, and is created via implicit value
177 // initialization.
178 if (operandClass == OperandNone) {
179 opcode.numTypes--;
180 break;
181 }
182 }
183 // We should have written the terminating SPV_OPERAND_TYPE_NONE entry, but
184 // also without overflowing.
185 assert((opcode.numTypes < maxNumOperands)
186 && "Operand class list is too long. Expand spv_opcode_desc_t.operandClass");
187 }
188 opcodeTableInitialized = true;
189}
190
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100191const char *spvGeneratorStr(uint32_t generator) {
192 switch (generator) {
193 case SPV_GENERATOR_KHRONOS:
194 return "Khronos";
195 case SPV_GENERATOR_VALVE:
196 return "Valve";
197 case SPV_GENERATOR_LUNARG:
198 return "LunarG";
199 case SPV_GENERATOR_CODEPLAY:
200 return "Codeplay Software Ltd.";
201 default:
202 return "Unknown";
203 }
204}
205
206uint32_t spvOpcodeMake(uint16_t wordCount, Op opcode) {
207 return ((uint32_t)opcode) | (((uint32_t)wordCount) << 16);
208}
209
210void spvOpcodeSplit(const uint32_t word, uint16_t *pWordCount, Op *pOpcode) {
211 if (pWordCount) {
212 *pWordCount = (uint16_t)((0xffff0000 & word) >> 16);
213 }
214 if (pOpcode) {
215 *pOpcode = (Op)(0x0000ffff & word);
216 }
217}
218
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100219spv_result_t spvOpcodeTableGet(spv_opcode_table *pInstTable) {
Lei Zhang40056702015-09-11 14:31:27 -0400220 if (!pInstTable) return SPV_ERROR_INVALID_POINTER;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100221
David Neto78c3b432015-08-27 13:03:52 -0400222 static spv_opcode_table_t table = {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100223 sizeof(opcodeTableEntries) / sizeof(spv_opcode_desc_t),
224 opcodeTableEntries};
225
David Neto78c3b432015-08-27 13:03:52 -0400226 // TODO(dneto): Consider thread safety of initialization.
227 // That is, ordering effects of the flag vs. the table updates.
Lei Zhang40056702015-09-11 14:31:27 -0400228 if (!opcodeTableInitialized) spvOpcodeTableInitialize();
David Neto78c3b432015-08-27 13:03:52 -0400229
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100230 *pInstTable = &table;
231
232 return SPV_SUCCESS;
233}
234
235spv_result_t spvOpcodeTableNameLookup(const spv_opcode_table table,
236 const char *name,
237 spv_opcode_desc *pEntry) {
Lei Zhang40056702015-09-11 14:31:27 -0400238 if (!name || !pEntry) return SPV_ERROR_INVALID_POINTER;
239 if (!table) return SPV_ERROR_INVALID_TABLE;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100240
241 // TODO: This lookup of the Opcode table is suboptimal! Binary sort would be
242 // preferable but the table requires sorting on the Opcode name, but it's
243 // static
244 // const initialized and matches the order of the spec.
245 const size_t nameLength = strlen(name);
246 for (uint64_t opcodeIndex = 0; opcodeIndex < table->count; ++opcodeIndex) {
247 if (nameLength == strlen(table->entries[opcodeIndex].name) &&
248 !strncmp(name, table->entries[opcodeIndex].name, nameLength)) {
249 // NOTE: Found out Opcode!
250 *pEntry = &table->entries[opcodeIndex];
251 return SPV_SUCCESS;
252 }
253 }
254
255 return SPV_ERROR_INVALID_LOOKUP;
256}
257
258spv_result_t spvOpcodeTableValueLookup(const spv_opcode_table table,
259 const Op opcode,
260 spv_opcode_desc *pEntry) {
Lei Zhang40056702015-09-11 14:31:27 -0400261 if (!table) return SPV_ERROR_INVALID_TABLE;
262 if (!pEntry) return SPV_ERROR_INVALID_POINTER;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100263
264 // TODO: As above this lookup is not optimal.
265 for (uint64_t opcodeIndex = 0; opcodeIndex < table->count; ++opcodeIndex) {
266 if (opcode == table->entries[opcodeIndex].opcode) {
267 // NOTE: Found the Opcode!
268 *pEntry = &table->entries[opcodeIndex];
269 return SPV_SUCCESS;
270 }
271 }
272
273 return SPV_ERROR_INVALID_LOOKUP;
274}
275
Lei Zhangdfc50082015-08-21 11:50:55 -0400276int16_t spvOpcodeResultIdIndex(spv_opcode_desc entry) {
David Neto78c3b432015-08-27 13:03:52 -0400277 for (int16_t i = 0; i < entry->numTypes; ++i) {
Lei Zhangdfc50082015-08-21 11:50:55 -0400278 if (SPV_OPERAND_TYPE_RESULT_ID == entry->operandTypes[i]) return i;
279 }
280 return SPV_OPERAND_INVALID_RESULT_ID_INDEX;
281}
282
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100283int32_t spvOpcodeRequiresCapabilities(spv_opcode_desc entry) {
284 return SPV_OPCODE_FLAGS_CAPABILITIES ==
285 (SPV_OPCODE_FLAGS_CAPABILITIES & entry->flags);
286}
287
288void spvInstructionCopy(const uint32_t *words, const Op opcode,
289 const uint16_t wordCount, const spv_endianness_t endian,
290 spv_instruction_t *pInst) {
291 pInst->opcode = opcode;
292 pInst->wordCount = wordCount;
293 for (uint16_t wordIndex = 0; wordIndex < wordCount; ++wordIndex) {
294 pInst->words[wordIndex] = spvFixWord(words[wordIndex], endian);
295 if (!wordIndex) {
296 uint16_t thisWordCount;
297 Op thisOpcode;
298 spvOpcodeSplit(pInst->words[wordIndex], &thisWordCount, &thisOpcode);
299 assert(opcode == thisOpcode && wordCount == thisWordCount &&
300 "Endianness failed!");
301 }
302 }
303}
304
305const char *spvOpcodeString(const Op opcode) {
306#define CASE(OPCODE) \
307 case OPCODE: \
308 return #OPCODE;
309 switch (opcode) {
310 CASE(OpNop)
311 CASE(OpSource)
312 CASE(OpSourceExtension)
313 CASE(OpExtension)
314 CASE(OpExtInstImport)
315 CASE(OpMemoryModel)
316 CASE(OpEntryPoint)
317 CASE(OpExecutionMode)
318 CASE(OpTypeVoid)
319 CASE(OpTypeBool)
320 CASE(OpTypeInt)
321 CASE(OpTypeFloat)
322 CASE(OpTypeVector)
323 CASE(OpTypeMatrix)
324 CASE(OpTypeSampler)
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100325 CASE(OpTypeArray)
326 CASE(OpTypeRuntimeArray)
327 CASE(OpTypeStruct)
328 CASE(OpTypeOpaque)
329 CASE(OpTypePointer)
330 CASE(OpTypeFunction)
331 CASE(OpTypeEvent)
332 CASE(OpTypeDeviceEvent)
333 CASE(OpTypeReserveId)
334 CASE(OpTypeQueue)
335 CASE(OpTypePipe)
336 CASE(OpConstantTrue)
337 CASE(OpConstantFalse)
338 CASE(OpConstant)
339 CASE(OpConstantComposite)
340 CASE(OpConstantSampler)
341 CASE(OpConstantNull)
342 CASE(OpSpecConstantTrue)
343 CASE(OpSpecConstantFalse)
344 CASE(OpSpecConstant)
345 CASE(OpSpecConstantComposite)
346 CASE(OpVariable)
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100347 CASE(OpFunction)
348 CASE(OpFunctionParameter)
349 CASE(OpFunctionEnd)
350 CASE(OpFunctionCall)
351 CASE(OpExtInst)
352 CASE(OpUndef)
353 CASE(OpLoad)
354 CASE(OpStore)
355 CASE(OpPhi)
356 CASE(OpDecorationGroup)
357 CASE(OpDecorate)
358 CASE(OpMemberDecorate)
359 CASE(OpGroupDecorate)
360 CASE(OpGroupMemberDecorate)
361 CASE(OpName)
362 CASE(OpMemberName)
363 CASE(OpString)
364 CASE(OpLine)
365 CASE(OpVectorExtractDynamic)
366 CASE(OpVectorInsertDynamic)
367 CASE(OpVectorShuffle)
368 CASE(OpCompositeConstruct)
369 CASE(OpCompositeExtract)
370 CASE(OpCompositeInsert)
371 CASE(OpCopyObject)
372 CASE(OpCopyMemory)
373 CASE(OpCopyMemorySized)
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100374 CASE(OpAccessChain)
375 CASE(OpInBoundsAccessChain)
376 CASE(OpSNegate)
377 CASE(OpFNegate)
378 CASE(OpNot)
379 CASE(OpAny)
380 CASE(OpAll)
381 CASE(OpConvertFToU)
382 CASE(OpConvertFToS)
383 CASE(OpConvertSToF)
384 CASE(OpConvertUToF)
385 CASE(OpUConvert)
386 CASE(OpSConvert)
387 CASE(OpFConvert)
388 CASE(OpConvertPtrToU)
389 CASE(OpConvertUToPtr)
390 CASE(OpPtrCastToGeneric)
391 CASE(OpGenericCastToPtr)
392 CASE(OpBitcast)
393 CASE(OpTranspose)
394 CASE(OpIsNan)
395 CASE(OpIsInf)
396 CASE(OpIsFinite)
397 CASE(OpIsNormal)
398 CASE(OpSignBitSet)
399 CASE(OpLessOrGreater)
400 CASE(OpOrdered)
401 CASE(OpUnordered)
402 CASE(OpArrayLength)
403 CASE(OpIAdd)
404 CASE(OpFAdd)
405 CASE(OpISub)
406 CASE(OpFSub)
407 CASE(OpIMul)
408 CASE(OpFMul)
409 CASE(OpUDiv)
410 CASE(OpSDiv)
411 CASE(OpFDiv)
412 CASE(OpUMod)
413 CASE(OpSRem)
414 CASE(OpSMod)
415 CASE(OpFRem)
416 CASE(OpFMod)
417 CASE(OpVectorTimesScalar)
418 CASE(OpMatrixTimesScalar)
419 CASE(OpVectorTimesMatrix)
420 CASE(OpMatrixTimesVector)
421 CASE(OpMatrixTimesMatrix)
422 CASE(OpOuterProduct)
423 CASE(OpDot)
424 CASE(OpShiftRightLogical)
425 CASE(OpShiftRightArithmetic)
426 CASE(OpShiftLeftLogical)
427 CASE(OpLogicalOr)
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100428 CASE(OpLogicalAnd)
429 CASE(OpBitwiseOr)
430 CASE(OpBitwiseXor)
431 CASE(OpBitwiseAnd)
432 CASE(OpSelect)
433 CASE(OpIEqual)
434 CASE(OpFOrdEqual)
435 CASE(OpFUnordEqual)
436 CASE(OpINotEqual)
437 CASE(OpFOrdNotEqual)
438 CASE(OpFUnordNotEqual)
439 CASE(OpULessThan)
440 CASE(OpSLessThan)
441 CASE(OpFOrdLessThan)
442 CASE(OpFUnordLessThan)
443 CASE(OpUGreaterThan)
444 CASE(OpSGreaterThan)
445 CASE(OpFOrdGreaterThan)
446 CASE(OpFUnordGreaterThan)
447 CASE(OpULessThanEqual)
448 CASE(OpSLessThanEqual)
449 CASE(OpFOrdLessThanEqual)
450 CASE(OpFUnordLessThanEqual)
451 CASE(OpUGreaterThanEqual)
452 CASE(OpSGreaterThanEqual)
453 CASE(OpFOrdGreaterThanEqual)
454 CASE(OpFUnordGreaterThanEqual)
455 CASE(OpDPdx)
456 CASE(OpDPdy)
457 CASE(OpFwidth)
458 CASE(OpDPdxFine)
459 CASE(OpDPdyFine)
460 CASE(OpFwidthFine)
461 CASE(OpDPdxCoarse)
462 CASE(OpDPdyCoarse)
463 CASE(OpFwidthCoarse)
464 CASE(OpEmitVertex)
465 CASE(OpEndPrimitive)
466 CASE(OpEmitStreamVertex)
467 CASE(OpEndStreamPrimitive)
468 CASE(OpControlBarrier)
469 CASE(OpMemoryBarrier)
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100470 CASE(OpAtomicLoad)
471 CASE(OpAtomicStore)
472 CASE(OpAtomicExchange)
473 CASE(OpAtomicCompareExchange)
474 CASE(OpAtomicCompareExchangeWeak)
475 CASE(OpAtomicIIncrement)
476 CASE(OpAtomicIDecrement)
477 CASE(OpAtomicIAdd)
478 CASE(OpAtomicISub)
479 CASE(OpAtomicUMin)
480 CASE(OpAtomicUMax)
481 CASE(OpAtomicAnd)
482 CASE(OpAtomicOr)
483 CASE(OpAtomicXor)
484 CASE(OpLoopMerge)
485 CASE(OpSelectionMerge)
486 CASE(OpLabel)
487 CASE(OpBranch)
488 CASE(OpBranchConditional)
489 CASE(OpSwitch)
490 CASE(OpKill)
491 CASE(OpReturn)
492 CASE(OpReturnValue)
493 CASE(OpUnreachable)
494 CASE(OpLifetimeStart)
495 CASE(OpLifetimeStop)
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100496 CASE(OpAsyncGroupCopy)
497 CASE(OpWaitGroupEvents)
498 CASE(OpGroupAll)
499 CASE(OpGroupAny)
500 CASE(OpGroupBroadcast)
501 CASE(OpGroupIAdd)
502 CASE(OpGroupFAdd)
503 CASE(OpGroupFMin)
504 CASE(OpGroupUMin)
505 CASE(OpGroupSMin)
506 CASE(OpGroupFMax)
507 CASE(OpGroupUMax)
508 CASE(OpGroupSMax)
509 CASE(OpGenericCastToPtrExplicit)
510 CASE(OpGenericPtrMemSemantics)
511 CASE(OpReadPipe)
512 CASE(OpWritePipe)
513 CASE(OpReservedReadPipe)
514 CASE(OpReservedWritePipe)
515 CASE(OpReserveReadPipePackets)
516 CASE(OpReserveWritePipePackets)
517 CASE(OpCommitReadPipe)
518 CASE(OpCommitWritePipe)
519 CASE(OpIsValidReserveId)
520 CASE(OpGetNumPipePackets)
521 CASE(OpGetMaxPipePackets)
522 CASE(OpGroupReserveReadPipePackets)
523 CASE(OpGroupReserveWritePipePackets)
524 CASE(OpGroupCommitReadPipe)
525 CASE(OpGroupCommitWritePipe)
526 CASE(OpEnqueueMarker)
527 CASE(OpEnqueueKernel)
528 CASE(OpGetKernelNDrangeSubGroupCount)
529 CASE(OpGetKernelNDrangeMaxSubGroupSize)
530 CASE(OpGetKernelWorkGroupSize)
531 CASE(OpGetKernelPreferredWorkGroupSizeMultiple)
532 CASE(OpRetainEvent)
533 CASE(OpReleaseEvent)
534 CASE(OpCreateUserEvent)
535 CASE(OpIsValidEvent)
536 CASE(OpSetUserEventStatus)
537 CASE(OpCaptureEventProfilingInfo)
538 CASE(OpGetDefaultQueue)
539 CASE(OpBuildNDRange)
540 default:
541 assert(0 && "Unreachable!");
542 }
543#undef CASE
544 return "unknown";
545}
546
547int32_t spvOpcodeIsType(const Op opcode) {
548 switch (opcode) {
549 case OpTypeVoid:
550 case OpTypeBool:
551 case OpTypeInt:
552 case OpTypeFloat:
553 case OpTypeVector:
554 case OpTypeMatrix:
555 case OpTypeSampler:
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100556 case OpTypeArray:
557 case OpTypeRuntimeArray:
558 case OpTypeStruct:
559 case OpTypeOpaque:
560 case OpTypePointer:
561 case OpTypeFunction:
562 case OpTypeEvent:
563 case OpTypeDeviceEvent:
564 case OpTypeReserveId:
565 case OpTypeQueue:
566 case OpTypePipe:
567 return true;
568 default:
569 return false;
570 }
571}
572
573int32_t spvOpcodeIsScalarType(const Op opcode) {
574 switch (opcode) {
575 case OpTypeInt:
576 case OpTypeFloat:
577 return true;
578 default:
579 return false;
580 }
581}
582
583int32_t spvOpcodeIsConstant(const Op opcode) {
584 switch (opcode) {
585 case OpConstantTrue:
586 case OpConstantFalse:
587 case OpConstant:
588 case OpConstantComposite:
589 case OpConstantSampler:
590 // case OpConstantNull:
591 case OpConstantNull:
592 case OpSpecConstantTrue:
593 case OpSpecConstantFalse:
594 case OpSpecConstant:
595 case OpSpecConstantComposite:
596 // case OpSpecConstantOp:
597 return true;
598 default:
599 return false;
600 }
601}
602
603int32_t spvOpcodeIsComposite(const Op opcode) {
604 switch (opcode) {
605 case OpTypeVector:
606 case OpTypeMatrix:
607 case OpTypeArray:
608 case OpTypeStruct:
609 return true;
610 default:
611 return false;
612 }
613}
614
615int32_t spvOpcodeAreTypesEqual(const spv_instruction_t *pTypeInst0,
616 const spv_instruction_t *pTypeInst1) {
Lei Zhang40056702015-09-11 14:31:27 -0400617 if (pTypeInst0->opcode != pTypeInst1->opcode) return false;
618 if (pTypeInst0->words[1] != pTypeInst1->words[1]) return false;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100619 return true;
620}
621
622int32_t spvOpcodeIsPointer(const Op opcode) {
623 switch (opcode) {
624 case OpVariable:
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100625 case OpAccessChain:
626 case OpInBoundsAccessChain:
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100627 case OpFunctionParameter:
628 return true;
629 default:
630 return false;
631 }
632}
633
634int32_t spvOpcodeIsObject(const Op opcode) {
635 switch (opcode) {
636 case OpConstantTrue:
637 case OpConstantFalse:
638 case OpConstant:
639 case OpConstantComposite:
640 // TODO: case OpConstantSampler:
641 case OpConstantNull:
642 case OpSpecConstantTrue:
643 case OpSpecConstantFalse:
644 case OpSpecConstant:
645 case OpSpecConstantComposite:
646 // TODO: case OpSpecConstantOp:
647 case OpVariable:
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100648 case OpAccessChain:
649 case OpInBoundsAccessChain:
650 case OpConvertFToU:
651 case OpConvertFToS:
652 case OpConvertSToF:
653 case OpConvertUToF:
654 case OpUConvert:
655 case OpSConvert:
656 case OpFConvert:
657 case OpConvertPtrToU:
658 // TODO: case OpConvertUToPtr:
659 case OpPtrCastToGeneric:
660 // TODO: case OpGenericCastToPtr:
661 case OpBitcast:
662 // TODO: case OpGenericCastToPtrExplicit:
663 case OpSatConvertSToU:
664 case OpSatConvertUToS:
665 case OpVectorExtractDynamic:
666 case OpCompositeConstruct:
667 case OpCompositeExtract:
668 case OpCopyObject:
669 case OpTranspose:
670 case OpSNegate:
671 case OpFNegate:
672 case OpNot:
673 case OpIAdd:
674 case OpFAdd:
675 case OpISub:
676 case OpFSub:
677 case OpIMul:
678 case OpFMul:
679 case OpUDiv:
680 case OpSDiv:
681 case OpFDiv:
682 case OpUMod:
683 case OpSRem:
684 case OpSMod:
685 case OpVectorTimesScalar:
686 case OpMatrixTimesScalar:
687 case OpVectorTimesMatrix:
688 case OpMatrixTimesVector:
689 case OpMatrixTimesMatrix:
690 case OpOuterProduct:
691 case OpDot:
692 case OpShiftRightLogical:
693 case OpShiftRightArithmetic:
694 case OpShiftLeftLogical:
695 case OpBitwiseOr:
696 case OpBitwiseXor:
697 case OpBitwiseAnd:
698 case OpAny:
699 case OpAll:
700 case OpIsNan:
701 case OpIsInf:
702 case OpIsFinite:
703 case OpIsNormal:
704 case OpSignBitSet:
705 case OpLessOrGreater:
706 case OpOrdered:
707 case OpUnordered:
708 case OpLogicalOr:
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100709 case OpLogicalAnd:
710 case OpSelect:
711 case OpIEqual:
712 case OpFOrdEqual:
713 case OpFUnordEqual:
714 case OpINotEqual:
715 case OpFOrdNotEqual:
716 case OpFUnordNotEqual:
717 case OpULessThan:
718 case OpSLessThan:
719 case OpFOrdLessThan:
720 case OpFUnordLessThan:
721 case OpUGreaterThan:
722 case OpSGreaterThan:
723 case OpFOrdGreaterThan:
724 case OpFUnordGreaterThan:
725 case OpULessThanEqual:
726 case OpSLessThanEqual:
727 case OpFOrdLessThanEqual:
728 case OpFUnordLessThanEqual:
729 case OpUGreaterThanEqual:
730 case OpSGreaterThanEqual:
731 case OpFOrdGreaterThanEqual:
732 case OpFUnordGreaterThanEqual:
733 case OpDPdx:
734 case OpDPdy:
735 case OpFwidth:
736 case OpDPdxFine:
737 case OpDPdyFine:
738 case OpFwidthFine:
739 case OpDPdxCoarse:
740 case OpDPdyCoarse:
741 case OpFwidthCoarse:
742 case OpReturnValue:
743 return true;
744 default:
745 return false;
746 }
747}
748
749int32_t spvOpcodeIsBasicTypeNullable(Op opcode) {
750 switch (opcode) {
751 case OpTypeBool:
752 case OpTypeInt:
753 case OpTypeFloat:
754 case OpTypePointer:
755 case OpTypeEvent:
756 case OpTypeDeviceEvent:
757 case OpTypeReserveId:
758 case OpTypeQueue:
759 return true;
760 default:
761 return false;
762 }
763}
764
765int32_t spvInstructionIsInBasicBlock(const spv_instruction_t *pFirstInst,
766 const spv_instruction_t *pInst) {
767 while (pFirstInst != pInst) {
Lei Zhang40056702015-09-11 14:31:27 -0400768 if (OpFunction == pInst->opcode) break;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100769 pInst--;
770 }
Lei Zhang40056702015-09-11 14:31:27 -0400771 if (OpFunction != pInst->opcode) return false;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100772 return true;
773}
774
775int32_t spvOpcodeIsValue(Op opcode) {
Lei Zhang40056702015-09-11 14:31:27 -0400776 if (spvOpcodeIsPointer(opcode)) return true;
777 if (spvOpcodeIsConstant(opcode)) return true;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100778 switch (opcode) {
779 case OpLoad:
780 // TODO: Other Opcode's resulting in a value
781 return true;
782 default:
783 return false;
784 }
785}