blob: 588e2ad0ed9d83ad71c82002eacd14226fcda1d2 [file] [log] [blame]
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +01001// Copyright (c) 2015 The Khronos Group Inc.
2//
3// Permission is hereby granted, free of charge, to any person obtaining a
4// copy of this software and/or associated documentation files (the
5// "Materials"), to deal in the Materials without restriction, including
6// without limitation the rights to use, copy, modify, merge, publish,
7// distribute, sublicense, and/or sell copies of the Materials, and to
8// permit persons to whom the Materials are furnished to do so, subject to
9// the following conditions:
10//
11// The above copyright notice and this permission notice shall be included
12// in all copies or substantial portions of the Materials.
13//
14// MODIFICATIONS TO THIS FILE MAY MEAN IT NO LONGER ACCURATELY REFLECTS
15// KHRONOS STANDARDS. THE UNMODIFIED, NORMATIVE VERSIONS OF KHRONOS
16// SPECIFICATIONS AND HEADER INFORMATION ARE LOCATED AT
17// https://www.khronos.org/registry/
18//
19// THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
22// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
23// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
24// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
25// MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS.
26
27#include <libspirv/libspirv.h>
David Netodb901b62015-10-27 16:14:40 -040028#include "endian.h"
David Netob5dc8fc2015-10-06 16:22:00 -040029#include "instruction.h"
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010030#include "opcode.h"
31
32#include <assert.h>
33#include <string.h>
34
David Neto78c3b432015-08-27 13:03:52 -040035namespace {
36
37// Descriptions of each opcode. Each entry describes the format of the
38// instruction that follows a particular opcode.
39//
40// Most fields are initialized statically by including an automatically
41// generated file.
42// The operandTypes fields are initialized during spvOpcodeInitialize().
43//
44// TODO(dneto): Some of the macros are quite unreadable. We could make
45// good use of constexpr functions, but some compilers don't support that yet.
46spv_opcode_desc_t opcodeTableEntries[] = {
Lei Zhang1a0334e2015-11-02 09:41:20 -050047#define EmptyList \
48 {}
49#define List(...) \
50 { __VA_ARGS__ }
Lei Zhangb36e7042015-10-28 13:40:52 -040051#define Capability(X) SPV_CAPABILITY_AS_MASK(SpvCapability##X)
Lei Zhang1a0334e2015-11-02 09:41:20 -050052#define Capability2(X, Y) Capability(X) | Capability(Y)
53#define SpvCapabilityNone \
54 0 // Needed so Capability(None) still expands to valid syntax.
David Neto201caf72015-11-04 17:38:17 -050055#define Instruction(Name, HasResult, HasType, NumLogicalOperands, \
56 NumCapabilities, CapabilityRequired, IsVariable, \
57 LogicalArgsList) \
58 {#Name, SpvOp##Name, (NumCapabilities) ? (CapabilityRequired) : 0, \
59 0, {}, /* Filled in later. Operand list, including \
60 result id and type id, if needed */ \
61 HasResult, HasType, LogicalArgsList},
David Neto78c3b432015-08-27 13:03:52 -040062#include "opcode.inc"
63#undef EmptyList
64#undef List
65#undef Capability
Dejan Mircevski205408b2015-09-30 16:42:34 -040066#undef Capability2
David Neto78c3b432015-08-27 13:03:52 -040067#undef CapabilityNone
68#undef Instruction
69};
70
71// Has the opcodeTableEntries table been fully elaborated?
72// That is, are the operandTypes fields initialized?
73bool opcodeTableInitialized = false;
74
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010075// Opcode API
76
David Neto78c3b432015-08-27 13:03:52 -040077// Converts the given operand class enum (from the SPIR-V document generation
78// logic) to the operand type required by the parser.
79// This only applies to logical operands.
Lei Zhangb36e7042015-10-28 13:40:52 -040080spv_operand_type_t convertOperandClassToType(SpvOp opcode,
81 OperandClass operandClass) {
David Neto78c3b432015-08-27 13:03:52 -040082 // The spec document generator uses OptionalOperandLiteral for several kinds
83 // of repeating values. Our parser needs more specific information about
84 // what is being repeated.
85 if (operandClass == OperandOptionalLiteral) {
Lei Zhang40056702015-09-11 14:31:27 -040086 switch (opcode) {
Lei Zhangb36e7042015-10-28 13:40:52 -040087 case SpvOpLoad:
88 case SpvOpStore:
89 case SpvOpCopyMemory:
90 case SpvOpCopyMemorySized:
David Neto3fca4cd2015-09-17 17:39:45 -040091 // Expect an optional mask. When the Aligned bit is set in the mask,
92 // we will later add the expectation of a literal number operand.
93 return SPV_OPERAND_TYPE_OPTIONAL_MEMORY_ACCESS;
Lei Zhangb36e7042015-10-28 13:40:52 -040094 case SpvOpExecutionMode:
David Neto78c3b432015-08-27 13:03:52 -040095 return SPV_OPERAND_TYPE_VARIABLE_EXECUTION_MODE;
96 default:
97 break;
98 }
Lei Zhangb41d1502015-09-14 15:22:23 -040099 } else if (operandClass == OperandVariableLiterals) {
David Neto201caf72015-11-04 17:38:17 -0500100 if (opcode == SpvOpConstant || opcode == SpvOpSpecConstant) {
101 // The number type is determined by the type Id operand.
102 return SPV_OPERAND_TYPE_TYPED_LITERAL_NUMBER;
103 }
David Neto78c3b432015-08-27 13:03:52 -0400104 }
105
Lei Zhang1a0334e2015-11-02 09:41:20 -0500106 switch (operandClass) {
107 case OperandNone:
108 return SPV_OPERAND_TYPE_NONE;
109 case OperandId:
110 return SPV_OPERAND_TYPE_ID;
111 case OperandOptionalId:
112 return SPV_OPERAND_TYPE_OPTIONAL_ID;
113 case OperandOptionalImage:
114 return SPV_OPERAND_TYPE_OPTIONAL_IMAGE;
115 case OperandVariableIds:
116 return SPV_OPERAND_TYPE_VARIABLE_ID;
David Neto561dc4e2015-09-25 14:23:29 -0400117 // The spec only uses OptionalLiteral for an optional literal number.
Lei Zhang1a0334e2015-11-02 09:41:20 -0500118 case OperandOptionalLiteral:
119 return SPV_OPERAND_TYPE_OPTIONAL_LITERAL_INTEGER;
120 case OperandOptionalLiteralString:
121 return SPV_OPERAND_TYPE_OPTIONAL_LITERAL_STRING;
David Neto561dc4e2015-09-25 14:23:29 -0400122 // This is only used for sequences of literal numbers.
Lei Zhang1a0334e2015-11-02 09:41:20 -0500123 case OperandVariableLiterals:
124 return SPV_OPERAND_TYPE_VARIABLE_LITERAL_INTEGER;
David Neto445ce442015-10-15 15:22:06 -0400125 case OperandLiteralNumber:
Lei Zhangb36e7042015-10-28 13:40:52 -0400126 if (opcode == SpvOpExtInst) {
David Neto445ce442015-10-15 15:22:06 -0400127 // We use a special operand type for the extension instruction number.
Lei Zhang1a0334e2015-11-02 09:41:20 -0500128 // For now, we assume there is only one LiteraNumber argument to
129 // OpExtInst, and it is the extension instruction argument.
David Neto445ce442015-10-15 15:22:06 -0400130 // See the ExtInst entry in opcode.inc
131 // TODO(dneto): Use a function to confirm the assumption, and to verify
132 // that the index into the operandClass is 1, as expected.
133 return SPV_OPERAND_TYPE_EXTENSION_INSTRUCTION_NUMBER;
134 }
135 return SPV_OPERAND_TYPE_LITERAL_INTEGER;
Lei Zhang1a0334e2015-11-02 09:41:20 -0500136 case OperandLiteralString:
137 return SPV_OPERAND_TYPE_LITERAL_STRING;
138 case OperandSource:
139 return SPV_OPERAND_TYPE_SOURCE_LANGUAGE;
140 case OperandExecutionModel:
141 return SPV_OPERAND_TYPE_EXECUTION_MODEL;
142 case OperandAddressing:
143 return SPV_OPERAND_TYPE_ADDRESSING_MODEL;
144 case OperandMemory:
145 return SPV_OPERAND_TYPE_MEMORY_MODEL;
146 case OperandExecutionMode:
147 return SPV_OPERAND_TYPE_EXECUTION_MODE;
148 case OperandStorage:
149 return SPV_OPERAND_TYPE_STORAGE_CLASS;
150 case OperandDimensionality:
151 return SPV_OPERAND_TYPE_DIMENSIONALITY;
152 case OperandSamplerAddressingMode:
153 return SPV_OPERAND_TYPE_SAMPLER_ADDRESSING_MODE;
154 case OperandSamplerFilterMode:
155 return SPV_OPERAND_TYPE_SAMPLER_FILTER_MODE;
156 case OperandSamplerImageFormat:
157 return SPV_OPERAND_TYPE_SAMPLER_IMAGE_FORMAT;
David Neto7cefb232015-09-28 15:33:49 -0400158 case OperandImageChannelOrder:
159 // This is only used to describe the value generated by OpImageQueryOrder.
160 // It is not used as an operand.
161 break;
162 case OperandImageChannelDataType:
Lei Zhang1a0334e2015-11-02 09:41:20 -0500163 // This is only used to describe the value generated by
164 // OpImageQueryFormat. It is not used as an operand.
David Neto7cefb232015-09-28 15:33:49 -0400165 break;
166 case OperandImageOperands:
Dejan Mircevskif8e091a2015-10-13 14:41:31 -0400167 // This is not used in opcode.inc. It only exists to generate the
168 // corresponding spec section. In parsing, image operands meld into the
169 // OperandOptionalImage case.
David Neto7cefb232015-09-28 15:33:49 -0400170 break;
Lei Zhang1a0334e2015-11-02 09:41:20 -0500171 case OperandFPFastMath:
172 return SPV_OPERAND_TYPE_FP_FAST_MATH_MODE;
173 case OperandFPRoundingMode:
174 return SPV_OPERAND_TYPE_FP_ROUNDING_MODE;
175 case OperandLinkageType:
176 return SPV_OPERAND_TYPE_LINKAGE_TYPE;
177 case OperandAccessQualifier:
178 return SPV_OPERAND_TYPE_ACCESS_QUALIFIER;
179 case OperandFuncParamAttr:
180 return SPV_OPERAND_TYPE_FUNCTION_PARAMETER_ATTRIBUTE;
181 case OperandDecoration:
182 return SPV_OPERAND_TYPE_DECORATION;
183 case OperandBuiltIn:
184 return SPV_OPERAND_TYPE_BUILT_IN;
185 case OperandSelect:
186 return SPV_OPERAND_TYPE_SELECTION_CONTROL;
187 case OperandLoop:
188 return SPV_OPERAND_TYPE_LOOP_CONTROL;
189 case OperandFunction:
190 return SPV_OPERAND_TYPE_FUNCTION_CONTROL;
191 case OperandMemorySemantics:
192 return SPV_OPERAND_TYPE_MEMORY_SEMANTICS;
David Neto78c3b432015-08-27 13:03:52 -0400193 case OperandMemoryAccess:
David Neto7cefb232015-09-28 15:33:49 -0400194 // This case does not occur in the table for SPIR-V 0.99 Rev 32.
David Neto3fca4cd2015-09-17 17:39:45 -0400195 // We expect that it will become SPV_OPERAND_TYPE_OPTIONAL_MEMORY_ACCESS,
David Neto78c3b432015-08-27 13:03:52 -0400196 // and we can remove the special casing above for memory operation
197 // instructions.
198 break;
Lei Zhang1a0334e2015-11-02 09:41:20 -0500199 case OperandScope:
200 return SPV_OPERAND_TYPE_EXECUTION_SCOPE;
201 case OperandGroupOperation:
202 return SPV_OPERAND_TYPE_GROUP_OPERATION;
203 case OperandKernelEnqueueFlags:
204 return SPV_OPERAND_TYPE_KERNEL_ENQ_FLAGS;
205 case OperandKernelProfilingInfo:
206 return SPV_OPERAND_TYPE_KERNEL_PROFILING_INFO;
207 case OperandCapability:
208 return SPV_OPERAND_TYPE_CAPABILITY;
David Neto78c3b432015-08-27 13:03:52 -0400209
210 // Used by GroupMemberDecorate
Lei Zhang1a0334e2015-11-02 09:41:20 -0500211 case OperandVariableIdLiteral:
212 return SPV_OPERAND_TYPE_VARIABLE_ID_LITERAL_INTEGER;
David Neto78c3b432015-08-27 13:03:52 -0400213
214 // Used by Switch
Lei Zhang1a0334e2015-11-02 09:41:20 -0500215 case OperandVariableLiteralId:
216 return SPV_OPERAND_TYPE_VARIABLE_LITERAL_INTEGER_ID;
David Neto78c3b432015-08-27 13:03:52 -0400217
218 // These exceptional cases shouldn't occur.
219 case OperandCount:
220 default:
221 break;
222 }
223 assert(0 && "Unexpected operand class");
224 return SPV_OPERAND_TYPE_NONE;
225}
226
Lei Zhanga94701d2015-09-14 10:05:37 -0400227} // anonymous namespace
David Neto78c3b432015-08-27 13:03:52 -0400228
229// Finish populating the opcodeTableEntries array.
230void spvOpcodeTableInitialize() {
231 // Compute the operandTypes field for each entry.
Lei Zhang1a0334e2015-11-02 09:41:20 -0500232 for (auto& opcode : opcodeTableEntries) {
David Neto78c3b432015-08-27 13:03:52 -0400233 opcode.numTypes = 0;
234 // Type ID always comes first, if present.
235 if (opcode.hasType)
Andrew Woloszyn537e7762015-09-29 11:28:34 -0400236 opcode.operandTypes[opcode.numTypes++] = SPV_OPERAND_TYPE_TYPE_ID;
David Neto78c3b432015-08-27 13:03:52 -0400237 // Result ID always comes next, if present
238 if (opcode.hasResult)
239 opcode.operandTypes[opcode.numTypes++] = SPV_OPERAND_TYPE_RESULT_ID;
Lei Zhanga94701d2015-09-14 10:05:37 -0400240 const uint16_t maxNumOperands =
241 sizeof(opcode.operandTypes) / sizeof(opcode.operandTypes[0]);
242 const uint16_t maxNumClasses =
243 sizeof(opcode.operandClass) / sizeof(opcode.operandClass[0]);
244 for (uint16_t classIndex = 0;
245 opcode.numTypes < maxNumOperands && classIndex < maxNumClasses;
246 classIndex++) {
David Neto78c3b432015-08-27 13:03:52 -0400247 const OperandClass operandClass = opcode.operandClass[classIndex];
Lei Zhanga94701d2015-09-14 10:05:37 -0400248 opcode.operandTypes[opcode.numTypes++] =
249 convertOperandClassToType(opcode.opcode, operandClass);
David Neto78c3b432015-08-27 13:03:52 -0400250 // The OperandNone value is not explicitly represented in the .inc file.
251 // However, it is the zero value, and is created via implicit value
252 // initialization.
253 if (operandClass == OperandNone) {
254 opcode.numTypes--;
255 break;
256 }
257 }
258 // We should have written the terminating SPV_OPERAND_TYPE_NONE entry, but
259 // also without overflowing.
Lei Zhanga94701d2015-09-14 10:05:37 -0400260 assert((opcode.numTypes < maxNumOperands) &&
261 "Operand class list is too long. Expand "
262 "spv_opcode_desc_t.operandClass");
David Neto78c3b432015-08-27 13:03:52 -0400263 }
264 opcodeTableInitialized = true;
265}
266
Lei Zhang1a0334e2015-11-02 09:41:20 -0500267const char* spvGeneratorStr(uint32_t generator) {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100268 switch (generator) {
269 case SPV_GENERATOR_KHRONOS:
270 return "Khronos";
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100271 case SPV_GENERATOR_LUNARG:
272 return "LunarG";
David Neto1780fc42015-10-26 15:43:12 -0400273 case SPV_GENERATOR_VALVE:
274 return "Valve";
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100275 case SPV_GENERATOR_CODEPLAY:
276 return "Codeplay Software Ltd.";
David Neto1780fc42015-10-26 15:43:12 -0400277 case SPV_GENERATOR_NVIDIA:
278 return "NVIDIA";
279 case SPV_GENERATOR_ARM:
280 return "ARM";
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100281 default:
282 return "Unknown";
283 }
284}
285
Lei Zhangb36e7042015-10-28 13:40:52 -0400286uint32_t spvOpcodeMake(uint16_t wordCount, SpvOp opcode) {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100287 return ((uint32_t)opcode) | (((uint32_t)wordCount) << 16);
288}
289
Lei Zhang1a0334e2015-11-02 09:41:20 -0500290void spvOpcodeSplit(const uint32_t word, uint16_t* pWordCount, SpvOp* pOpcode) {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100291 if (pWordCount) {
292 *pWordCount = (uint16_t)((0xffff0000 & word) >> 16);
293 }
294 if (pOpcode) {
Lei Zhangb36e7042015-10-28 13:40:52 -0400295 *pOpcode = (SpvOp)(0x0000ffff & word);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100296 }
297}
298
Lei Zhang1a0334e2015-11-02 09:41:20 -0500299spv_result_t spvOpcodeTableGet(spv_opcode_table* pInstTable) {
Lei Zhang40056702015-09-11 14:31:27 -0400300 if (!pInstTable) return SPV_ERROR_INVALID_POINTER;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100301
David Neto78c3b432015-08-27 13:03:52 -0400302 static spv_opcode_table_t table = {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100303 sizeof(opcodeTableEntries) / sizeof(spv_opcode_desc_t),
304 opcodeTableEntries};
305
David Neto78c3b432015-08-27 13:03:52 -0400306 // TODO(dneto): Consider thread safety of initialization.
307 // That is, ordering effects of the flag vs. the table updates.
Lei Zhang40056702015-09-11 14:31:27 -0400308 if (!opcodeTableInitialized) spvOpcodeTableInitialize();
David Neto78c3b432015-08-27 13:03:52 -0400309
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100310 *pInstTable = &table;
311
312 return SPV_SUCCESS;
313}
314
315spv_result_t spvOpcodeTableNameLookup(const spv_opcode_table table,
Lei Zhang1a0334e2015-11-02 09:41:20 -0500316 const char* name,
317 spv_opcode_desc* pEntry) {
Lei Zhang40056702015-09-11 14:31:27 -0400318 if (!name || !pEntry) return SPV_ERROR_INVALID_POINTER;
319 if (!table) return SPV_ERROR_INVALID_TABLE;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100320
321 // TODO: This lookup of the Opcode table is suboptimal! Binary sort would be
322 // preferable but the table requires sorting on the Opcode name, but it's
323 // static
324 // const initialized and matches the order of the spec.
325 const size_t nameLength = strlen(name);
326 for (uint64_t opcodeIndex = 0; opcodeIndex < table->count; ++opcodeIndex) {
327 if (nameLength == strlen(table->entries[opcodeIndex].name) &&
328 !strncmp(name, table->entries[opcodeIndex].name, nameLength)) {
329 // NOTE: Found out Opcode!
330 *pEntry = &table->entries[opcodeIndex];
331 return SPV_SUCCESS;
332 }
333 }
334
335 return SPV_ERROR_INVALID_LOOKUP;
336}
337
338spv_result_t spvOpcodeTableValueLookup(const spv_opcode_table table,
Lei Zhangb36e7042015-10-28 13:40:52 -0400339 const SpvOp opcode,
Lei Zhang1a0334e2015-11-02 09:41:20 -0500340 spv_opcode_desc* pEntry) {
Lei Zhang40056702015-09-11 14:31:27 -0400341 if (!table) return SPV_ERROR_INVALID_TABLE;
342 if (!pEntry) return SPV_ERROR_INVALID_POINTER;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100343
344 // TODO: As above this lookup is not optimal.
345 for (uint64_t opcodeIndex = 0; opcodeIndex < table->count; ++opcodeIndex) {
346 if (opcode == table->entries[opcodeIndex].opcode) {
347 // NOTE: Found the Opcode!
348 *pEntry = &table->entries[opcodeIndex];
349 return SPV_SUCCESS;
350 }
351 }
352
353 return SPV_ERROR_INVALID_LOOKUP;
354}
355
Lei Zhangdfc50082015-08-21 11:50:55 -0400356int16_t spvOpcodeResultIdIndex(spv_opcode_desc entry) {
David Neto78c3b432015-08-27 13:03:52 -0400357 for (int16_t i = 0; i < entry->numTypes; ++i) {
Lei Zhangdfc50082015-08-21 11:50:55 -0400358 if (SPV_OPERAND_TYPE_RESULT_ID == entry->operandTypes[i]) return i;
359 }
360 return SPV_OPERAND_INVALID_RESULT_ID_INDEX;
361}
362
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100363int32_t spvOpcodeRequiresCapabilities(spv_opcode_desc entry) {
David Neto9db3a532015-10-07 16:58:38 -0400364 return entry->capabilities != 0;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100365}
366
Lei Zhang1a0334e2015-11-02 09:41:20 -0500367void spvInstructionCopy(const uint32_t* words, const SpvOp opcode,
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100368 const uint16_t wordCount, const spv_endianness_t endian,
Lei Zhang1a0334e2015-11-02 09:41:20 -0500369 spv_instruction_t* pInst) {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100370 pInst->opcode = opcode;
David Netob5dc8fc2015-10-06 16:22:00 -0400371 pInst->words.resize(wordCount);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100372 for (uint16_t wordIndex = 0; wordIndex < wordCount; ++wordIndex) {
373 pInst->words[wordIndex] = spvFixWord(words[wordIndex], endian);
374 if (!wordIndex) {
375 uint16_t thisWordCount;
Lei Zhangb36e7042015-10-28 13:40:52 -0400376 SpvOp thisOpcode;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100377 spvOpcodeSplit(pInst->words[wordIndex], &thisWordCount, &thisOpcode);
378 assert(opcode == thisOpcode && wordCount == thisWordCount &&
379 "Endianness failed!");
380 }
381 }
382}
383
Lei Zhang1a0334e2015-11-02 09:41:20 -0500384const char* spvOpcodeString(const SpvOp opcode) {
David Neto201caf72015-11-04 17:38:17 -0500385// Use the syntax table so it's sure to be complete.
David Neto1bcd3d12015-11-02 16:03:12 -0500386#define Instruction(Name, ...) \
387 case SpvOp##Name: \
388 return #Name;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100389 switch (opcode) {
David Neto1bcd3d12015-11-02 16:03:12 -0500390#include "opcode.inc"
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100391 default:
392 assert(0 && "Unreachable!");
393 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100394 return "unknown";
David Neto1bcd3d12015-11-02 16:03:12 -0500395#undef Instruction
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100396}
397
Lei Zhangb36e7042015-10-28 13:40:52 -0400398int32_t spvOpcodeIsScalarType(const SpvOp opcode) {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100399 switch (opcode) {
Lei Zhangb36e7042015-10-28 13:40:52 -0400400 case SpvOpTypeInt:
401 case SpvOpTypeFloat:
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100402 return true;
403 default:
404 return false;
405 }
406}
407
Lei Zhangb36e7042015-10-28 13:40:52 -0400408int32_t spvOpcodeIsConstant(const SpvOp opcode) {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100409 switch (opcode) {
Lei Zhangb36e7042015-10-28 13:40:52 -0400410 case SpvOpConstantTrue:
411 case SpvOpConstantFalse:
412 case SpvOpConstant:
413 case SpvOpConstantComposite:
414 case SpvOpConstantSampler:
415 // case SpvOpConstantNull:
416 case SpvOpConstantNull:
417 case SpvOpSpecConstantTrue:
418 case SpvOpSpecConstantFalse:
419 case SpvOpSpecConstant:
420 case SpvOpSpecConstantComposite:
421 // case SpvOpSpecConstantOp:
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100422 return true;
423 default:
424 return false;
425 }
426}
427
Lei Zhangb36e7042015-10-28 13:40:52 -0400428int32_t spvOpcodeIsComposite(const SpvOp opcode) {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100429 switch (opcode) {
Lei Zhangb36e7042015-10-28 13:40:52 -0400430 case SpvOpTypeVector:
431 case SpvOpTypeMatrix:
432 case SpvOpTypeArray:
433 case SpvOpTypeStruct:
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100434 return true;
435 default:
436 return false;
437 }
438}
439
Lei Zhang1a0334e2015-11-02 09:41:20 -0500440int32_t spvOpcodeAreTypesEqual(const spv_instruction_t* pTypeInst0,
441 const spv_instruction_t* pTypeInst1) {
Lei Zhang40056702015-09-11 14:31:27 -0400442 if (pTypeInst0->opcode != pTypeInst1->opcode) return false;
443 if (pTypeInst0->words[1] != pTypeInst1->words[1]) return false;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100444 return true;
445}
446
Lei Zhangb36e7042015-10-28 13:40:52 -0400447int32_t spvOpcodeIsPointer(const SpvOp opcode) {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100448 switch (opcode) {
Lei Zhangb36e7042015-10-28 13:40:52 -0400449 case SpvOpVariable:
450 case SpvOpAccessChain:
451 case SpvOpInBoundsAccessChain:
452 case SpvOpFunctionParameter:
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100453 return true;
454 default:
455 return false;
456 }
457}
458
Lei Zhangb36e7042015-10-28 13:40:52 -0400459int32_t spvOpcodeIsObject(const SpvOp opcode) {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100460 switch (opcode) {
Lei Zhangb36e7042015-10-28 13:40:52 -0400461 case SpvOpConstantTrue:
462 case SpvOpConstantFalse:
463 case SpvOpConstant:
464 case SpvOpConstantComposite:
465 // TODO: case SpvOpConstantSampler:
466 case SpvOpConstantNull:
467 case SpvOpSpecConstantTrue:
468 case SpvOpSpecConstantFalse:
469 case SpvOpSpecConstant:
470 case SpvOpSpecConstantComposite:
471 // TODO: case SpvOpSpecConstantOp:
472 case SpvOpVariable:
473 case SpvOpAccessChain:
474 case SpvOpInBoundsAccessChain:
475 case SpvOpConvertFToU:
476 case SpvOpConvertFToS:
477 case SpvOpConvertSToF:
478 case SpvOpConvertUToF:
479 case SpvOpUConvert:
480 case SpvOpSConvert:
481 case SpvOpFConvert:
482 case SpvOpConvertPtrToU:
483 // TODO: case SpvOpConvertUToPtr:
484 case SpvOpPtrCastToGeneric:
485 // TODO: case SpvOpGenericCastToPtr:
486 case SpvOpBitcast:
487 // TODO: case SpvOpGenericCastToPtrExplicit:
488 case SpvOpSatConvertSToU:
489 case SpvOpSatConvertUToS:
490 case SpvOpVectorExtractDynamic:
491 case SpvOpCompositeConstruct:
492 case SpvOpCompositeExtract:
493 case SpvOpCopyObject:
494 case SpvOpTranspose:
495 case SpvOpSNegate:
496 case SpvOpFNegate:
497 case SpvOpNot:
498 case SpvOpIAdd:
499 case SpvOpFAdd:
500 case SpvOpISub:
501 case SpvOpFSub:
502 case SpvOpIMul:
503 case SpvOpFMul:
504 case SpvOpUDiv:
505 case SpvOpSDiv:
506 case SpvOpFDiv:
507 case SpvOpUMod:
508 case SpvOpSRem:
509 case SpvOpSMod:
510 case SpvOpVectorTimesScalar:
511 case SpvOpMatrixTimesScalar:
512 case SpvOpVectorTimesMatrix:
513 case SpvOpMatrixTimesVector:
514 case SpvOpMatrixTimesMatrix:
515 case SpvOpOuterProduct:
516 case SpvOpDot:
517 case SpvOpShiftRightLogical:
518 case SpvOpShiftRightArithmetic:
519 case SpvOpShiftLeftLogical:
520 case SpvOpBitwiseOr:
521 case SpvOpBitwiseXor:
522 case SpvOpBitwiseAnd:
523 case SpvOpAny:
524 case SpvOpAll:
525 case SpvOpIsNan:
526 case SpvOpIsInf:
527 case SpvOpIsFinite:
528 case SpvOpIsNormal:
529 case SpvOpSignBitSet:
530 case SpvOpLessOrGreater:
531 case SpvOpOrdered:
532 case SpvOpUnordered:
533 case SpvOpLogicalOr:
534 case SpvOpLogicalAnd:
535 case SpvOpSelect:
536 case SpvOpIEqual:
537 case SpvOpFOrdEqual:
538 case SpvOpFUnordEqual:
539 case SpvOpINotEqual:
540 case SpvOpFOrdNotEqual:
541 case SpvOpFUnordNotEqual:
542 case SpvOpULessThan:
543 case SpvOpSLessThan:
544 case SpvOpFOrdLessThan:
545 case SpvOpFUnordLessThan:
546 case SpvOpUGreaterThan:
547 case SpvOpSGreaterThan:
548 case SpvOpFOrdGreaterThan:
549 case SpvOpFUnordGreaterThan:
550 case SpvOpULessThanEqual:
551 case SpvOpSLessThanEqual:
552 case SpvOpFOrdLessThanEqual:
553 case SpvOpFUnordLessThanEqual:
554 case SpvOpUGreaterThanEqual:
555 case SpvOpSGreaterThanEqual:
556 case SpvOpFOrdGreaterThanEqual:
557 case SpvOpFUnordGreaterThanEqual:
558 case SpvOpDPdx:
559 case SpvOpDPdy:
560 case SpvOpFwidth:
561 case SpvOpDPdxFine:
562 case SpvOpDPdyFine:
563 case SpvOpFwidthFine:
564 case SpvOpDPdxCoarse:
565 case SpvOpDPdyCoarse:
566 case SpvOpFwidthCoarse:
567 case SpvOpReturnValue:
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100568 return true;
569 default:
570 return false;
571 }
572}
573
Lei Zhangb36e7042015-10-28 13:40:52 -0400574int32_t spvOpcodeIsBasicTypeNullable(SpvOp opcode) {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100575 switch (opcode) {
Lei Zhangb36e7042015-10-28 13:40:52 -0400576 case SpvOpTypeBool:
577 case SpvOpTypeInt:
578 case SpvOpTypeFloat:
579 case SpvOpTypePointer:
580 case SpvOpTypeEvent:
581 case SpvOpTypeDeviceEvent:
582 case SpvOpTypeReserveId:
583 case SpvOpTypeQueue:
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100584 return true;
585 default:
586 return false;
587 }
588}
589
Lei Zhang1a0334e2015-11-02 09:41:20 -0500590int32_t spvInstructionIsInBasicBlock(const spv_instruction_t* pFirstInst,
591 const spv_instruction_t* pInst) {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100592 while (pFirstInst != pInst) {
Lei Zhangb36e7042015-10-28 13:40:52 -0400593 if (SpvOpFunction == pInst->opcode) break;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100594 pInst--;
595 }
Lei Zhangb36e7042015-10-28 13:40:52 -0400596 if (SpvOpFunction != pInst->opcode) return false;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100597 return true;
598}
599
Lei Zhangb36e7042015-10-28 13:40:52 -0400600int32_t spvOpcodeIsValue(SpvOp opcode) {
Lei Zhang40056702015-09-11 14:31:27 -0400601 if (spvOpcodeIsPointer(opcode)) return true;
602 if (spvOpcodeIsConstant(opcode)) return true;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100603 switch (opcode) {
Lei Zhangb36e7042015-10-28 13:40:52 -0400604 case SpvOpLoad:
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100605 // TODO: Other Opcode's resulting in a value
606 return true;
607 default:
608 return false;
609 }
610}
Andrew Woloszyn537e7762015-09-29 11:28:34 -0400611
Lei Zhangb36e7042015-10-28 13:40:52 -0400612int32_t spvOpcodeGeneratesType(SpvOp op) {
Lei Zhang1a0334e2015-11-02 09:41:20 -0500613 switch (op) {
Lei Zhangb36e7042015-10-28 13:40:52 -0400614 case SpvOpTypeVoid:
615 case SpvOpTypeBool:
616 case SpvOpTypeInt:
617 case SpvOpTypeFloat:
618 case SpvOpTypeVector:
619 case SpvOpTypeMatrix:
620 case SpvOpTypeImage:
621 case SpvOpTypeSampler:
622 case SpvOpTypeSampledImage:
623 case SpvOpTypeArray:
624 case SpvOpTypeRuntimeArray:
625 case SpvOpTypeStruct:
626 case SpvOpTypeOpaque:
627 case SpvOpTypePointer:
628 case SpvOpTypeFunction:
629 case SpvOpTypeEvent:
630 case SpvOpTypeDeviceEvent:
631 case SpvOpTypeReserveId:
632 case SpvOpTypeQueue:
633 case SpvOpTypePipe:
Andrew Woloszyn537e7762015-09-29 11:28:34 -0400634 return true;
David Netoaef608c2015-11-02 14:59:02 -0500635 default:
636 // In particular, OpTypeForwardPointer does not generate a type,
637 // but declares a storage class for a pointer type generated
638 // by a different instruction.
639 break;
Andrew Woloszyn537e7762015-09-29 11:28:34 -0400640 }
641 return 0;
642}