blob: 555bfb700b9c387a58e7d9155a72569f2567dda3 [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.
55#define Instruction(Name, HasResult, HasType, NumLogicalOperands, \
56 NumCapabilities, CapabilityRequired, IsVariable, \
57 LogicalArgsList) \
58 { \
59 #Name, SpvOp##Name, \
60 (NumCapabilities) ? (CapabilityRequired) : 0, 0, \
61 {}, /* Filled in later. Operand list, including \
62 result id and type id, if needed */ \
63 HasResult, HasType, LogicalArgsList \
64 } \
65 ,
David Neto78c3b432015-08-27 13:03:52 -040066#include "opcode.inc"
67#undef EmptyList
68#undef List
69#undef Capability
Dejan Mircevski205408b2015-09-30 16:42:34 -040070#undef Capability2
David Neto78c3b432015-08-27 13:03:52 -040071#undef CapabilityNone
72#undef Instruction
73};
74
75// Has the opcodeTableEntries table been fully elaborated?
76// That is, are the operandTypes fields initialized?
77bool opcodeTableInitialized = false;
78
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010079// Opcode API
80
David Neto78c3b432015-08-27 13:03:52 -040081// Converts the given operand class enum (from the SPIR-V document generation
82// logic) to the operand type required by the parser.
83// This only applies to logical operands.
Lei Zhangb36e7042015-10-28 13:40:52 -040084spv_operand_type_t convertOperandClassToType(SpvOp opcode,
85 OperandClass operandClass) {
David Neto78c3b432015-08-27 13:03:52 -040086 // The spec document generator uses OptionalOperandLiteral for several kinds
87 // of repeating values. Our parser needs more specific information about
88 // what is being repeated.
89 if (operandClass == OperandOptionalLiteral) {
Lei Zhang40056702015-09-11 14:31:27 -040090 switch (opcode) {
Lei Zhangb36e7042015-10-28 13:40:52 -040091 case SpvOpLoad:
92 case SpvOpStore:
93 case SpvOpCopyMemory:
94 case SpvOpCopyMemorySized:
David Neto3fca4cd2015-09-17 17:39:45 -040095 // Expect an optional mask. When the Aligned bit is set in the mask,
96 // we will later add the expectation of a literal number operand.
97 return SPV_OPERAND_TYPE_OPTIONAL_MEMORY_ACCESS;
Lei Zhangb36e7042015-10-28 13:40:52 -040098 case SpvOpExecutionMode:
David Neto78c3b432015-08-27 13:03:52 -040099 return SPV_OPERAND_TYPE_VARIABLE_EXECUTION_MODE;
100 default:
101 break;
102 }
Lei Zhangb41d1502015-09-14 15:22:23 -0400103 } else if (operandClass == OperandVariableLiterals) {
Lei Zhangb36e7042015-10-28 13:40:52 -0400104 if (opcode == SpvOpConstant || opcode == SpvOpSpecConstant)
Lei Zhangb41d1502015-09-14 15:22:23 -0400105 return SPV_OPERAND_TYPE_MULTIWORD_LITERAL_NUMBER;
David Neto78c3b432015-08-27 13:03:52 -0400106 }
107
Lei Zhang1a0334e2015-11-02 09:41:20 -0500108 switch (operandClass) {
109 case OperandNone:
110 return SPV_OPERAND_TYPE_NONE;
111 case OperandId:
112 return SPV_OPERAND_TYPE_ID;
113 case OperandOptionalId:
114 return SPV_OPERAND_TYPE_OPTIONAL_ID;
115 case OperandOptionalImage:
116 return SPV_OPERAND_TYPE_OPTIONAL_IMAGE;
117 case OperandVariableIds:
118 return SPV_OPERAND_TYPE_VARIABLE_ID;
David Neto561dc4e2015-09-25 14:23:29 -0400119 // The spec only uses OptionalLiteral for an optional literal number.
Lei Zhang1a0334e2015-11-02 09:41:20 -0500120 case OperandOptionalLiteral:
121 return SPV_OPERAND_TYPE_OPTIONAL_LITERAL_INTEGER;
122 case OperandOptionalLiteralString:
123 return SPV_OPERAND_TYPE_OPTIONAL_LITERAL_STRING;
David Neto561dc4e2015-09-25 14:23:29 -0400124 // This is only used for sequences of literal numbers.
Lei Zhang1a0334e2015-11-02 09:41:20 -0500125 case OperandVariableLiterals:
126 return SPV_OPERAND_TYPE_VARIABLE_LITERAL_INTEGER;
David Neto445ce442015-10-15 15:22:06 -0400127 case OperandLiteralNumber:
Lei Zhangb36e7042015-10-28 13:40:52 -0400128 if (opcode == SpvOpExtInst) {
David Neto445ce442015-10-15 15:22:06 -0400129 // We use a special operand type for the extension instruction number.
Lei Zhang1a0334e2015-11-02 09:41:20 -0500130 // For now, we assume there is only one LiteraNumber argument to
131 // OpExtInst, and it is the extension instruction argument.
David Neto445ce442015-10-15 15:22:06 -0400132 // See the ExtInst entry in opcode.inc
133 // TODO(dneto): Use a function to confirm the assumption, and to verify
134 // that the index into the operandClass is 1, as expected.
135 return SPV_OPERAND_TYPE_EXTENSION_INSTRUCTION_NUMBER;
136 }
137 return SPV_OPERAND_TYPE_LITERAL_INTEGER;
Lei Zhang1a0334e2015-11-02 09:41:20 -0500138 case OperandLiteralString:
139 return SPV_OPERAND_TYPE_LITERAL_STRING;
140 case OperandSource:
141 return SPV_OPERAND_TYPE_SOURCE_LANGUAGE;
142 case OperandExecutionModel:
143 return SPV_OPERAND_TYPE_EXECUTION_MODEL;
144 case OperandAddressing:
145 return SPV_OPERAND_TYPE_ADDRESSING_MODEL;
146 case OperandMemory:
147 return SPV_OPERAND_TYPE_MEMORY_MODEL;
148 case OperandExecutionMode:
149 return SPV_OPERAND_TYPE_EXECUTION_MODE;
150 case OperandStorage:
151 return SPV_OPERAND_TYPE_STORAGE_CLASS;
152 case OperandDimensionality:
153 return SPV_OPERAND_TYPE_DIMENSIONALITY;
154 case OperandSamplerAddressingMode:
155 return SPV_OPERAND_TYPE_SAMPLER_ADDRESSING_MODE;
156 case OperandSamplerFilterMode:
157 return SPV_OPERAND_TYPE_SAMPLER_FILTER_MODE;
158 case OperandSamplerImageFormat:
159 return SPV_OPERAND_TYPE_SAMPLER_IMAGE_FORMAT;
David Neto7cefb232015-09-28 15:33:49 -0400160 case OperandImageChannelOrder:
161 // This is only used to describe the value generated by OpImageQueryOrder.
162 // It is not used as an operand.
163 break;
164 case OperandImageChannelDataType:
Lei Zhang1a0334e2015-11-02 09:41:20 -0500165 // This is only used to describe the value generated by
166 // OpImageQueryFormat. It is not used as an operand.
David Neto7cefb232015-09-28 15:33:49 -0400167 break;
168 case OperandImageOperands:
Dejan Mircevskif8e091a2015-10-13 14:41:31 -0400169 // This is not used in opcode.inc. It only exists to generate the
170 // corresponding spec section. In parsing, image operands meld into the
171 // OperandOptionalImage case.
David Neto7cefb232015-09-28 15:33:49 -0400172 break;
Lei Zhang1a0334e2015-11-02 09:41:20 -0500173 case OperandFPFastMath:
174 return SPV_OPERAND_TYPE_FP_FAST_MATH_MODE;
175 case OperandFPRoundingMode:
176 return SPV_OPERAND_TYPE_FP_ROUNDING_MODE;
177 case OperandLinkageType:
178 return SPV_OPERAND_TYPE_LINKAGE_TYPE;
179 case OperandAccessQualifier:
180 return SPV_OPERAND_TYPE_ACCESS_QUALIFIER;
181 case OperandFuncParamAttr:
182 return SPV_OPERAND_TYPE_FUNCTION_PARAMETER_ATTRIBUTE;
183 case OperandDecoration:
184 return SPV_OPERAND_TYPE_DECORATION;
185 case OperandBuiltIn:
186 return SPV_OPERAND_TYPE_BUILT_IN;
187 case OperandSelect:
188 return SPV_OPERAND_TYPE_SELECTION_CONTROL;
189 case OperandLoop:
190 return SPV_OPERAND_TYPE_LOOP_CONTROL;
191 case OperandFunction:
192 return SPV_OPERAND_TYPE_FUNCTION_CONTROL;
193 case OperandMemorySemantics:
194 return SPV_OPERAND_TYPE_MEMORY_SEMANTICS;
David Neto78c3b432015-08-27 13:03:52 -0400195 case OperandMemoryAccess:
David Neto7cefb232015-09-28 15:33:49 -0400196 // This case does not occur in the table for SPIR-V 0.99 Rev 32.
David Neto3fca4cd2015-09-17 17:39:45 -0400197 // We expect that it will become SPV_OPERAND_TYPE_OPTIONAL_MEMORY_ACCESS,
David Neto78c3b432015-08-27 13:03:52 -0400198 // and we can remove the special casing above for memory operation
199 // instructions.
200 break;
Lei Zhang1a0334e2015-11-02 09:41:20 -0500201 case OperandScope:
202 return SPV_OPERAND_TYPE_EXECUTION_SCOPE;
203 case OperandGroupOperation:
204 return SPV_OPERAND_TYPE_GROUP_OPERATION;
205 case OperandKernelEnqueueFlags:
206 return SPV_OPERAND_TYPE_KERNEL_ENQ_FLAGS;
207 case OperandKernelProfilingInfo:
208 return SPV_OPERAND_TYPE_KERNEL_PROFILING_INFO;
209 case OperandCapability:
210 return SPV_OPERAND_TYPE_CAPABILITY;
David Neto78c3b432015-08-27 13:03:52 -0400211
212 // Used by GroupMemberDecorate
Lei Zhang1a0334e2015-11-02 09:41:20 -0500213 case OperandVariableIdLiteral:
214 return SPV_OPERAND_TYPE_VARIABLE_ID_LITERAL_INTEGER;
David Neto78c3b432015-08-27 13:03:52 -0400215
216 // Used by Switch
Lei Zhang1a0334e2015-11-02 09:41:20 -0500217 case OperandVariableLiteralId:
218 return SPV_OPERAND_TYPE_VARIABLE_LITERAL_INTEGER_ID;
David Neto78c3b432015-08-27 13:03:52 -0400219
220 // These exceptional cases shouldn't occur.
221 case OperandCount:
222 default:
223 break;
224 }
225 assert(0 && "Unexpected operand class");
226 return SPV_OPERAND_TYPE_NONE;
227}
228
Lei Zhanga94701d2015-09-14 10:05:37 -0400229} // anonymous namespace
David Neto78c3b432015-08-27 13:03:52 -0400230
231// Finish populating the opcodeTableEntries array.
232void spvOpcodeTableInitialize() {
233 // Compute the operandTypes field for each entry.
Lei Zhang1a0334e2015-11-02 09:41:20 -0500234 for (auto& opcode : opcodeTableEntries) {
David Neto78c3b432015-08-27 13:03:52 -0400235 opcode.numTypes = 0;
236 // Type ID always comes first, if present.
237 if (opcode.hasType)
Andrew Woloszyn537e7762015-09-29 11:28:34 -0400238 opcode.operandTypes[opcode.numTypes++] = SPV_OPERAND_TYPE_TYPE_ID;
David Neto78c3b432015-08-27 13:03:52 -0400239 // Result ID always comes next, if present
240 if (opcode.hasResult)
241 opcode.operandTypes[opcode.numTypes++] = SPV_OPERAND_TYPE_RESULT_ID;
Lei Zhanga94701d2015-09-14 10:05:37 -0400242 const uint16_t maxNumOperands =
243 sizeof(opcode.operandTypes) / sizeof(opcode.operandTypes[0]);
244 const uint16_t maxNumClasses =
245 sizeof(opcode.operandClass) / sizeof(opcode.operandClass[0]);
246 for (uint16_t classIndex = 0;
247 opcode.numTypes < maxNumOperands && classIndex < maxNumClasses;
248 classIndex++) {
David Neto78c3b432015-08-27 13:03:52 -0400249 const OperandClass operandClass = opcode.operandClass[classIndex];
Lei Zhanga94701d2015-09-14 10:05:37 -0400250 opcode.operandTypes[opcode.numTypes++] =
251 convertOperandClassToType(opcode.opcode, operandClass);
David Neto78c3b432015-08-27 13:03:52 -0400252 // The OperandNone value is not explicitly represented in the .inc file.
253 // However, it is the zero value, and is created via implicit value
254 // initialization.
255 if (operandClass == OperandNone) {
256 opcode.numTypes--;
257 break;
258 }
259 }
260 // We should have written the terminating SPV_OPERAND_TYPE_NONE entry, but
261 // also without overflowing.
Lei Zhanga94701d2015-09-14 10:05:37 -0400262 assert((opcode.numTypes < maxNumOperands) &&
263 "Operand class list is too long. Expand "
264 "spv_opcode_desc_t.operandClass");
David Neto78c3b432015-08-27 13:03:52 -0400265 }
266 opcodeTableInitialized = true;
267}
268
Lei Zhang1a0334e2015-11-02 09:41:20 -0500269const char* spvGeneratorStr(uint32_t generator) {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100270 switch (generator) {
271 case SPV_GENERATOR_KHRONOS:
272 return "Khronos";
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100273 case SPV_GENERATOR_LUNARG:
274 return "LunarG";
David Neto1780fc42015-10-26 15:43:12 -0400275 case SPV_GENERATOR_VALVE:
276 return "Valve";
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100277 case SPV_GENERATOR_CODEPLAY:
278 return "Codeplay Software Ltd.";
David Neto1780fc42015-10-26 15:43:12 -0400279 case SPV_GENERATOR_NVIDIA:
280 return "NVIDIA";
281 case SPV_GENERATOR_ARM:
282 return "ARM";
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100283 default:
284 return "Unknown";
285 }
286}
287
Lei Zhangb36e7042015-10-28 13:40:52 -0400288uint32_t spvOpcodeMake(uint16_t wordCount, SpvOp opcode) {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100289 return ((uint32_t)opcode) | (((uint32_t)wordCount) << 16);
290}
291
Lei Zhang1a0334e2015-11-02 09:41:20 -0500292void spvOpcodeSplit(const uint32_t word, uint16_t* pWordCount, SpvOp* pOpcode) {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100293 if (pWordCount) {
294 *pWordCount = (uint16_t)((0xffff0000 & word) >> 16);
295 }
296 if (pOpcode) {
Lei Zhangb36e7042015-10-28 13:40:52 -0400297 *pOpcode = (SpvOp)(0x0000ffff & word);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100298 }
299}
300
Lei Zhang1a0334e2015-11-02 09:41:20 -0500301spv_result_t spvOpcodeTableGet(spv_opcode_table* pInstTable) {
Lei Zhang40056702015-09-11 14:31:27 -0400302 if (!pInstTable) return SPV_ERROR_INVALID_POINTER;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100303
David Neto78c3b432015-08-27 13:03:52 -0400304 static spv_opcode_table_t table = {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100305 sizeof(opcodeTableEntries) / sizeof(spv_opcode_desc_t),
306 opcodeTableEntries};
307
David Neto78c3b432015-08-27 13:03:52 -0400308 // TODO(dneto): Consider thread safety of initialization.
309 // That is, ordering effects of the flag vs. the table updates.
Lei Zhang40056702015-09-11 14:31:27 -0400310 if (!opcodeTableInitialized) spvOpcodeTableInitialize();
David Neto78c3b432015-08-27 13:03:52 -0400311
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100312 *pInstTable = &table;
313
314 return SPV_SUCCESS;
315}
316
317spv_result_t spvOpcodeTableNameLookup(const spv_opcode_table table,
Lei Zhang1a0334e2015-11-02 09:41:20 -0500318 const char* name,
319 spv_opcode_desc* pEntry) {
Lei Zhang40056702015-09-11 14:31:27 -0400320 if (!name || !pEntry) return SPV_ERROR_INVALID_POINTER;
321 if (!table) return SPV_ERROR_INVALID_TABLE;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100322
323 // TODO: This lookup of the Opcode table is suboptimal! Binary sort would be
324 // preferable but the table requires sorting on the Opcode name, but it's
325 // static
326 // const initialized and matches the order of the spec.
327 const size_t nameLength = strlen(name);
328 for (uint64_t opcodeIndex = 0; opcodeIndex < table->count; ++opcodeIndex) {
329 if (nameLength == strlen(table->entries[opcodeIndex].name) &&
330 !strncmp(name, table->entries[opcodeIndex].name, nameLength)) {
331 // NOTE: Found out Opcode!
332 *pEntry = &table->entries[opcodeIndex];
333 return SPV_SUCCESS;
334 }
335 }
336
337 return SPV_ERROR_INVALID_LOOKUP;
338}
339
340spv_result_t spvOpcodeTableValueLookup(const spv_opcode_table table,
Lei Zhangb36e7042015-10-28 13:40:52 -0400341 const SpvOp opcode,
Lei Zhang1a0334e2015-11-02 09:41:20 -0500342 spv_opcode_desc* pEntry) {
Lei Zhang40056702015-09-11 14:31:27 -0400343 if (!table) return SPV_ERROR_INVALID_TABLE;
344 if (!pEntry) return SPV_ERROR_INVALID_POINTER;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100345
346 // TODO: As above this lookup is not optimal.
347 for (uint64_t opcodeIndex = 0; opcodeIndex < table->count; ++opcodeIndex) {
348 if (opcode == table->entries[opcodeIndex].opcode) {
349 // NOTE: Found the Opcode!
350 *pEntry = &table->entries[opcodeIndex];
351 return SPV_SUCCESS;
352 }
353 }
354
355 return SPV_ERROR_INVALID_LOOKUP;
356}
357
Lei Zhangdfc50082015-08-21 11:50:55 -0400358int16_t spvOpcodeResultIdIndex(spv_opcode_desc entry) {
David Neto78c3b432015-08-27 13:03:52 -0400359 for (int16_t i = 0; i < entry->numTypes; ++i) {
Lei Zhangdfc50082015-08-21 11:50:55 -0400360 if (SPV_OPERAND_TYPE_RESULT_ID == entry->operandTypes[i]) return i;
361 }
362 return SPV_OPERAND_INVALID_RESULT_ID_INDEX;
363}
364
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100365int32_t spvOpcodeRequiresCapabilities(spv_opcode_desc entry) {
David Neto9db3a532015-10-07 16:58:38 -0400366 return entry->capabilities != 0;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100367}
368
Lei Zhang1a0334e2015-11-02 09:41:20 -0500369void spvInstructionCopy(const uint32_t* words, const SpvOp opcode,
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100370 const uint16_t wordCount, const spv_endianness_t endian,
Lei Zhang1a0334e2015-11-02 09:41:20 -0500371 spv_instruction_t* pInst) {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100372 pInst->opcode = opcode;
David Netob5dc8fc2015-10-06 16:22:00 -0400373 pInst->words.resize(wordCount);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100374 for (uint16_t wordIndex = 0; wordIndex < wordCount; ++wordIndex) {
375 pInst->words[wordIndex] = spvFixWord(words[wordIndex], endian);
376 if (!wordIndex) {
377 uint16_t thisWordCount;
Lei Zhangb36e7042015-10-28 13:40:52 -0400378 SpvOp thisOpcode;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100379 spvOpcodeSplit(pInst->words[wordIndex], &thisWordCount, &thisOpcode);
380 assert(opcode == thisOpcode && wordCount == thisWordCount &&
381 "Endianness failed!");
382 }
383 }
384}
385
Lei Zhang1a0334e2015-11-02 09:41:20 -0500386const char* spvOpcodeString(const SpvOp opcode) {
David Neto1bcd3d12015-11-02 16:03:12 -0500387 // Use the syntax table so it's sure to be complete.
388#define Instruction(Name, ...) \
389 case SpvOp##Name: \
390 return #Name;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100391 switch (opcode) {
David Neto1bcd3d12015-11-02 16:03:12 -0500392#include "opcode.inc"
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100393 default:
394 assert(0 && "Unreachable!");
395 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100396 return "unknown";
David Neto1bcd3d12015-11-02 16:03:12 -0500397#undef Instruction
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100398}
399
Lei Zhangb36e7042015-10-28 13:40:52 -0400400int32_t spvOpcodeIsScalarType(const SpvOp opcode) {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100401 switch (opcode) {
Lei Zhangb36e7042015-10-28 13:40:52 -0400402 case SpvOpTypeInt:
403 case SpvOpTypeFloat:
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100404 return true;
405 default:
406 return false;
407 }
408}
409
Lei Zhangb36e7042015-10-28 13:40:52 -0400410int32_t spvOpcodeIsConstant(const SpvOp opcode) {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100411 switch (opcode) {
Lei Zhangb36e7042015-10-28 13:40:52 -0400412 case SpvOpConstantTrue:
413 case SpvOpConstantFalse:
414 case SpvOpConstant:
415 case SpvOpConstantComposite:
416 case SpvOpConstantSampler:
417 // case SpvOpConstantNull:
418 case SpvOpConstantNull:
419 case SpvOpSpecConstantTrue:
420 case SpvOpSpecConstantFalse:
421 case SpvOpSpecConstant:
422 case SpvOpSpecConstantComposite:
423 // case SpvOpSpecConstantOp:
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100424 return true;
425 default:
426 return false;
427 }
428}
429
Lei Zhangb36e7042015-10-28 13:40:52 -0400430int32_t spvOpcodeIsComposite(const SpvOp opcode) {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100431 switch (opcode) {
Lei Zhangb36e7042015-10-28 13:40:52 -0400432 case SpvOpTypeVector:
433 case SpvOpTypeMatrix:
434 case SpvOpTypeArray:
435 case SpvOpTypeStruct:
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100436 return true;
437 default:
438 return false;
439 }
440}
441
Lei Zhang1a0334e2015-11-02 09:41:20 -0500442int32_t spvOpcodeAreTypesEqual(const spv_instruction_t* pTypeInst0,
443 const spv_instruction_t* pTypeInst1) {
Lei Zhang40056702015-09-11 14:31:27 -0400444 if (pTypeInst0->opcode != pTypeInst1->opcode) return false;
445 if (pTypeInst0->words[1] != pTypeInst1->words[1]) return false;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100446 return true;
447}
448
Lei Zhangb36e7042015-10-28 13:40:52 -0400449int32_t spvOpcodeIsPointer(const SpvOp opcode) {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100450 switch (opcode) {
Lei Zhangb36e7042015-10-28 13:40:52 -0400451 case SpvOpVariable:
452 case SpvOpAccessChain:
453 case SpvOpInBoundsAccessChain:
454 case SpvOpFunctionParameter:
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100455 return true;
456 default:
457 return false;
458 }
459}
460
Lei Zhangb36e7042015-10-28 13:40:52 -0400461int32_t spvOpcodeIsObject(const SpvOp opcode) {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100462 switch (opcode) {
Lei Zhangb36e7042015-10-28 13:40:52 -0400463 case SpvOpConstantTrue:
464 case SpvOpConstantFalse:
465 case SpvOpConstant:
466 case SpvOpConstantComposite:
467 // TODO: case SpvOpConstantSampler:
468 case SpvOpConstantNull:
469 case SpvOpSpecConstantTrue:
470 case SpvOpSpecConstantFalse:
471 case SpvOpSpecConstant:
472 case SpvOpSpecConstantComposite:
473 // TODO: case SpvOpSpecConstantOp:
474 case SpvOpVariable:
475 case SpvOpAccessChain:
476 case SpvOpInBoundsAccessChain:
477 case SpvOpConvertFToU:
478 case SpvOpConvertFToS:
479 case SpvOpConvertSToF:
480 case SpvOpConvertUToF:
481 case SpvOpUConvert:
482 case SpvOpSConvert:
483 case SpvOpFConvert:
484 case SpvOpConvertPtrToU:
485 // TODO: case SpvOpConvertUToPtr:
486 case SpvOpPtrCastToGeneric:
487 // TODO: case SpvOpGenericCastToPtr:
488 case SpvOpBitcast:
489 // TODO: case SpvOpGenericCastToPtrExplicit:
490 case SpvOpSatConvertSToU:
491 case SpvOpSatConvertUToS:
492 case SpvOpVectorExtractDynamic:
493 case SpvOpCompositeConstruct:
494 case SpvOpCompositeExtract:
495 case SpvOpCopyObject:
496 case SpvOpTranspose:
497 case SpvOpSNegate:
498 case SpvOpFNegate:
499 case SpvOpNot:
500 case SpvOpIAdd:
501 case SpvOpFAdd:
502 case SpvOpISub:
503 case SpvOpFSub:
504 case SpvOpIMul:
505 case SpvOpFMul:
506 case SpvOpUDiv:
507 case SpvOpSDiv:
508 case SpvOpFDiv:
509 case SpvOpUMod:
510 case SpvOpSRem:
511 case SpvOpSMod:
512 case SpvOpVectorTimesScalar:
513 case SpvOpMatrixTimesScalar:
514 case SpvOpVectorTimesMatrix:
515 case SpvOpMatrixTimesVector:
516 case SpvOpMatrixTimesMatrix:
517 case SpvOpOuterProduct:
518 case SpvOpDot:
519 case SpvOpShiftRightLogical:
520 case SpvOpShiftRightArithmetic:
521 case SpvOpShiftLeftLogical:
522 case SpvOpBitwiseOr:
523 case SpvOpBitwiseXor:
524 case SpvOpBitwiseAnd:
525 case SpvOpAny:
526 case SpvOpAll:
527 case SpvOpIsNan:
528 case SpvOpIsInf:
529 case SpvOpIsFinite:
530 case SpvOpIsNormal:
531 case SpvOpSignBitSet:
532 case SpvOpLessOrGreater:
533 case SpvOpOrdered:
534 case SpvOpUnordered:
535 case SpvOpLogicalOr:
536 case SpvOpLogicalAnd:
537 case SpvOpSelect:
538 case SpvOpIEqual:
539 case SpvOpFOrdEqual:
540 case SpvOpFUnordEqual:
541 case SpvOpINotEqual:
542 case SpvOpFOrdNotEqual:
543 case SpvOpFUnordNotEqual:
544 case SpvOpULessThan:
545 case SpvOpSLessThan:
546 case SpvOpFOrdLessThan:
547 case SpvOpFUnordLessThan:
548 case SpvOpUGreaterThan:
549 case SpvOpSGreaterThan:
550 case SpvOpFOrdGreaterThan:
551 case SpvOpFUnordGreaterThan:
552 case SpvOpULessThanEqual:
553 case SpvOpSLessThanEqual:
554 case SpvOpFOrdLessThanEqual:
555 case SpvOpFUnordLessThanEqual:
556 case SpvOpUGreaterThanEqual:
557 case SpvOpSGreaterThanEqual:
558 case SpvOpFOrdGreaterThanEqual:
559 case SpvOpFUnordGreaterThanEqual:
560 case SpvOpDPdx:
561 case SpvOpDPdy:
562 case SpvOpFwidth:
563 case SpvOpDPdxFine:
564 case SpvOpDPdyFine:
565 case SpvOpFwidthFine:
566 case SpvOpDPdxCoarse:
567 case SpvOpDPdyCoarse:
568 case SpvOpFwidthCoarse:
569 case SpvOpReturnValue:
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100570 return true;
571 default:
572 return false;
573 }
574}
575
Lei Zhangb36e7042015-10-28 13:40:52 -0400576int32_t spvOpcodeIsBasicTypeNullable(SpvOp opcode) {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100577 switch (opcode) {
Lei Zhangb36e7042015-10-28 13:40:52 -0400578 case SpvOpTypeBool:
579 case SpvOpTypeInt:
580 case SpvOpTypeFloat:
581 case SpvOpTypePointer:
582 case SpvOpTypeEvent:
583 case SpvOpTypeDeviceEvent:
584 case SpvOpTypeReserveId:
585 case SpvOpTypeQueue:
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100586 return true;
587 default:
588 return false;
589 }
590}
591
Lei Zhang1a0334e2015-11-02 09:41:20 -0500592int32_t spvInstructionIsInBasicBlock(const spv_instruction_t* pFirstInst,
593 const spv_instruction_t* pInst) {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100594 while (pFirstInst != pInst) {
Lei Zhangb36e7042015-10-28 13:40:52 -0400595 if (SpvOpFunction == pInst->opcode) break;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100596 pInst--;
597 }
Lei Zhangb36e7042015-10-28 13:40:52 -0400598 if (SpvOpFunction != pInst->opcode) return false;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100599 return true;
600}
601
Lei Zhangb36e7042015-10-28 13:40:52 -0400602int32_t spvOpcodeIsValue(SpvOp opcode) {
Lei Zhang40056702015-09-11 14:31:27 -0400603 if (spvOpcodeIsPointer(opcode)) return true;
604 if (spvOpcodeIsConstant(opcode)) return true;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100605 switch (opcode) {
Lei Zhangb36e7042015-10-28 13:40:52 -0400606 case SpvOpLoad:
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100607 // TODO: Other Opcode's resulting in a value
608 return true;
609 default:
610 return false;
611 }
612}
Andrew Woloszyn537e7762015-09-29 11:28:34 -0400613
Lei Zhangb36e7042015-10-28 13:40:52 -0400614int32_t spvOpcodeGeneratesType(SpvOp op) {
Lei Zhang1a0334e2015-11-02 09:41:20 -0500615 switch (op) {
Lei Zhangb36e7042015-10-28 13:40:52 -0400616 case SpvOpTypeVoid:
617 case SpvOpTypeBool:
618 case SpvOpTypeInt:
619 case SpvOpTypeFloat:
620 case SpvOpTypeVector:
621 case SpvOpTypeMatrix:
622 case SpvOpTypeImage:
623 case SpvOpTypeSampler:
624 case SpvOpTypeSampledImage:
625 case SpvOpTypeArray:
626 case SpvOpTypeRuntimeArray:
627 case SpvOpTypeStruct:
628 case SpvOpTypeOpaque:
629 case SpvOpTypePointer:
630 case SpvOpTypeFunction:
631 case SpvOpTypeEvent:
632 case SpvOpTypeDeviceEvent:
633 case SpvOpTypeReserveId:
634 case SpvOpTypeQueue:
635 case SpvOpTypePipe:
Andrew Woloszyn537e7762015-09-29 11:28:34 -0400636 return true;
David Netoaef608c2015-11-02 14:59:02 -0500637 default:
638 // In particular, OpTypeForwardPointer does not generate a type,
639 // but declares a storage class for a pointer type generated
640 // by a different instruction.
641 break;
Andrew Woloszyn537e7762015-09-29 11:28:34 -0400642 }
643 return 0;
644}