blob: c13e4ef1316105e6e5c35d1814536863e690d96d [file] [log] [blame]
Dejan Mircevskib6fe02f2016-01-07 13:44:22 -05001// Copyright (c) 2015-2016 The Khronos Group Inc.
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +01002//
David Neto9fc86582016-09-01 15:33:59 -04003// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +01006//
David Neto9fc86582016-09-01 15:33:59 -04007// http://www.apache.org/licenses/LICENSE-2.0
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +01008//
David Neto9fc86582016-09-01 15:33:59 -04009// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010014
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010015#include "opcode.h"
16
17#include <assert.h>
18#include <string.h>
19
David Neto5a0b5ca2016-12-09 14:01:43 -050020#include <algorithm>
Lei Zhang972788b2015-11-12 13:48:30 -050021#include <cstdlib>
22
Lei Zhang923f6c12015-11-11 12:45:23 -050023#include "instruction.h"
Lei Zhangca1bf942016-04-27 16:47:13 -040024#include "macro.h"
David Neto5a703352016-02-17 14:44:00 -050025#include "spirv-tools/libspirv.h"
Lei Zhangaa056cd2015-11-11 14:24:04 -050026#include "spirv_constant.h"
David Neto4c215712015-12-22 15:08:41 -050027#include "spirv_endian.h"
Lei Zhang923f6c12015-11-11 12:45:23 -050028
David Neto78c3b432015-08-27 13:03:52 -040029namespace {
30
31// Descriptions of each opcode. Each entry describes the format of the
32// instruction that follows a particular opcode.
Dejan Mircevskicb3c49e2016-04-07 14:41:34 -040033const spv_opcode_desc_t opcodeTableEntries_1_0[] = {
Lei Zhang10dba912016-04-14 14:05:53 -040034#include "core.insts-1.0.inc"
David Neto78c3b432015-08-27 13:03:52 -040035};
Dejan Mircevskicb3c49e2016-04-07 14:41:34 -040036const spv_opcode_desc_t opcodeTableEntries_1_1[] = {
Lei Zhang10dba912016-04-14 14:05:53 -040037#include "core.insts-1.1.inc"
Dejan Mircevskicb3c49e2016-04-07 14:41:34 -040038};
David Netodbc20492017-03-14 12:43:41 -040039const spv_opcode_desc_t opcodeTableEntries_1_2[] = {
40#include "core.insts-1.2.inc"
41};
David Neto78c3b432015-08-27 13:03:52 -040042
David Neto5a0b5ca2016-12-09 14:01:43 -050043// Represents a vendor tool entry in the SPIR-V XML Regsitry.
44struct VendorTool {
45 uint32_t value;
46 const char* vendor;
47 const char* tool; // Might be empty string.
48 const char* vendor_tool; // Combiantion of vendor and tool.
49};
50
51const VendorTool vendor_tools[] = {
52#include "generators.inc"
53};
54
Lei Zhanga94701d2015-09-14 10:05:37 -040055} // anonymous namespace
David Neto78c3b432015-08-27 13:03:52 -040056
David Neto5a0b5ca2016-12-09 14:01:43 -050057// TODO(dneto): Move this to another file. It doesn't belong with opcode
58// processing.
Lei Zhang1a0334e2015-11-02 09:41:20 -050059const char* spvGeneratorStr(uint32_t generator) {
David Neto5a0b5ca2016-12-09 14:01:43 -050060 auto where = std::find_if(
61 std::begin(vendor_tools), std::end(vendor_tools),
62 [generator](const VendorTool& vt) { return generator == vt.value; });
63 if (where != std::end(vendor_tools)) return where->vendor_tool;
64 return "Unknown";
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010065}
66
Lei Zhangb36e7042015-10-28 13:40:52 -040067uint32_t spvOpcodeMake(uint16_t wordCount, SpvOp opcode) {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010068 return ((uint32_t)opcode) | (((uint32_t)wordCount) << 16);
69}
70
Lei Zhang6fa3f8a2016-03-31 17:26:31 -040071void spvOpcodeSplit(const uint32_t word, uint16_t* pWordCount,
72 uint16_t* pOpcode) {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010073 if (pWordCount) {
74 *pWordCount = (uint16_t)((0xffff0000 & word) >> 16);
75 }
76 if (pOpcode) {
Lei Zhang6fa3f8a2016-03-31 17:26:31 -040077 *pOpcode = 0x0000ffff & word;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010078 }
79}
80
Dejan Mircevskicb3c49e2016-04-07 14:41:34 -040081spv_result_t spvOpcodeTableGet(spv_opcode_table* pInstTable,
82 spv_target_env env) {
Lei Zhang40056702015-09-11 14:31:27 -040083 if (!pInstTable) return SPV_ERROR_INVALID_POINTER;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010084
Dejan Mircevskicb3c49e2016-04-07 14:41:34 -040085 static const spv_opcode_table_t table_1_0 = {
86 ARRAY_SIZE(opcodeTableEntries_1_0), opcodeTableEntries_1_0};
87 static const spv_opcode_table_t table_1_1 = {
88 ARRAY_SIZE(opcodeTableEntries_1_1), opcodeTableEntries_1_1};
David Netodbc20492017-03-14 12:43:41 -040089 static const spv_opcode_table_t table_1_2 = {
90 ARRAY_SIZE(opcodeTableEntries_1_2), opcodeTableEntries_1_2};
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010091
Dejan Mircevskicb3c49e2016-04-07 14:41:34 -040092 switch (env) {
93 case SPV_ENV_UNIVERSAL_1_0:
94 case SPV_ENV_VULKAN_1_0:
David Netoc2967012016-08-05 18:19:30 -040095 case SPV_ENV_OPENCL_2_1:
96 case SPV_ENV_OPENGL_4_0:
97 case SPV_ENV_OPENGL_4_1:
98 case SPV_ENV_OPENGL_4_2:
99 case SPV_ENV_OPENGL_4_3:
100 case SPV_ENV_OPENGL_4_5:
Dejan Mircevskicb3c49e2016-04-07 14:41:34 -0400101 *pInstTable = &table_1_0;
102 return SPV_SUCCESS;
103 case SPV_ENV_UNIVERSAL_1_1:
104 *pInstTable = &table_1_1;
105 return SPV_SUCCESS;
David Netodbc20492017-03-14 12:43:41 -0400106 case SPV_ENV_UNIVERSAL_1_2:
107 case SPV_ENV_OPENCL_2_2:
108 *pInstTable = &table_1_2;
109 return SPV_SUCCESS;
Dejan Mircevskicb3c49e2016-04-07 14:41:34 -0400110 }
111 assert(0 && "Unknown spv_target_env in spvOpcodeTableGet()");
112 return SPV_ERROR_INVALID_TABLE;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100113}
114
115spv_result_t spvOpcodeTableNameLookup(const spv_opcode_table table,
Lei Zhang1a0334e2015-11-02 09:41:20 -0500116 const char* name,
117 spv_opcode_desc* pEntry) {
Lei Zhang40056702015-09-11 14:31:27 -0400118 if (!name || !pEntry) return SPV_ERROR_INVALID_POINTER;
119 if (!table) return SPV_ERROR_INVALID_TABLE;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100120
121 // TODO: This lookup of the Opcode table is suboptimal! Binary sort would be
122 // preferable but the table requires sorting on the Opcode name, but it's
123 // static
124 // const initialized and matches the order of the spec.
125 const size_t nameLength = strlen(name);
126 for (uint64_t opcodeIndex = 0; opcodeIndex < table->count; ++opcodeIndex) {
127 if (nameLength == strlen(table->entries[opcodeIndex].name) &&
128 !strncmp(name, table->entries[opcodeIndex].name, nameLength)) {
129 // NOTE: Found out Opcode!
130 *pEntry = &table->entries[opcodeIndex];
131 return SPV_SUCCESS;
132 }
133 }
134
135 return SPV_ERROR_INVALID_LOOKUP;
136}
137
138spv_result_t spvOpcodeTableValueLookup(const spv_opcode_table table,
Lei Zhangb36e7042015-10-28 13:40:52 -0400139 const SpvOp opcode,
Lei Zhang1a0334e2015-11-02 09:41:20 -0500140 spv_opcode_desc* pEntry) {
Lei Zhang40056702015-09-11 14:31:27 -0400141 if (!table) return SPV_ERROR_INVALID_TABLE;
142 if (!pEntry) return SPV_ERROR_INVALID_POINTER;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100143
144 // TODO: As above this lookup is not optimal.
145 for (uint64_t opcodeIndex = 0; opcodeIndex < table->count; ++opcodeIndex) {
146 if (opcode == table->entries[opcodeIndex].opcode) {
147 // NOTE: Found the Opcode!
148 *pEntry = &table->entries[opcodeIndex];
149 return SPV_SUCCESS;
150 }
151 }
152
153 return SPV_ERROR_INVALID_LOOKUP;
154}
155
Lei Zhang1a0334e2015-11-02 09:41:20 -0500156void spvInstructionCopy(const uint32_t* words, const SpvOp opcode,
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100157 const uint16_t wordCount, const spv_endianness_t endian,
Lei Zhang1a0334e2015-11-02 09:41:20 -0500158 spv_instruction_t* pInst) {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100159 pInst->opcode = opcode;
David Netob5dc8fc2015-10-06 16:22:00 -0400160 pInst->words.resize(wordCount);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100161 for (uint16_t wordIndex = 0; wordIndex < wordCount; ++wordIndex) {
162 pInst->words[wordIndex] = spvFixWord(words[wordIndex], endian);
163 if (!wordIndex) {
164 uint16_t thisWordCount;
Lei Zhang6fa3f8a2016-03-31 17:26:31 -0400165 uint16_t thisOpcode;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100166 spvOpcodeSplit(pInst->words[wordIndex], &thisWordCount, &thisOpcode);
Lei Zhang6fa3f8a2016-03-31 17:26:31 -0400167 assert(opcode == static_cast<SpvOp>(thisOpcode) &&
168 wordCount == thisWordCount && "Endianness failed!");
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100169 }
170 }
171}
172
Lei Zhang1a0334e2015-11-02 09:41:20 -0500173const char* spvOpcodeString(const SpvOp opcode) {
Dejan Mircevskicb3c49e2016-04-07 14:41:34 -0400174 // Use the latest SPIR-V version, which should be backward-compatible with all
175 // previous ones.
David Netodbc20492017-03-14 12:43:41 -0400176 for (uint32_t i = 0; i < ARRAY_SIZE(opcodeTableEntries_1_2); ++i) {
177 if (opcodeTableEntries_1_2[i].opcode == opcode)
178 return opcodeTableEntries_1_2[i].name;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100179 }
Lei Zhang4f293b72016-03-21 16:36:14 -0400180 assert(0 && "Unreachable!");
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100181 return "unknown";
182}
183
Lei Zhangb36e7042015-10-28 13:40:52 -0400184int32_t spvOpcodeIsScalarType(const SpvOp opcode) {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100185 switch (opcode) {
Lei Zhangb36e7042015-10-28 13:40:52 -0400186 case SpvOpTypeInt:
187 case SpvOpTypeFloat:
Dejan Mircevski276a7242016-01-21 15:55:43 -0500188 case SpvOpTypeBool:
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100189 return true;
190 default:
191 return false;
192 }
193}
194
Lei Zhangb36e7042015-10-28 13:40:52 -0400195int32_t spvOpcodeIsConstant(const SpvOp opcode) {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100196 switch (opcode) {
Lei Zhangb36e7042015-10-28 13:40:52 -0400197 case SpvOpConstantTrue:
198 case SpvOpConstantFalse:
199 case SpvOpConstant:
200 case SpvOpConstantComposite:
201 case SpvOpConstantSampler:
Lei Zhangb36e7042015-10-28 13:40:52 -0400202 case SpvOpConstantNull:
203 case SpvOpSpecConstantTrue:
204 case SpvOpSpecConstantFalse:
205 case SpvOpSpecConstant:
206 case SpvOpSpecConstantComposite:
Dejan Mircevski3fb26762016-04-04 15:55:05 -0400207 case SpvOpSpecConstantOp:
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100208 return true;
209 default:
210 return false;
211 }
212}
213
David Neto1f3fb502016-09-14 11:57:20 -0400214bool spvOpcodeIsConstantOrUndef(const SpvOp opcode) {
215 return opcode == SpvOpUndef || spvOpcodeIsConstant(opcode);
216}
217
Aliya Pazylbekovaedb52642017-02-24 20:43:28 -0500218bool spvOpcodeIsScalarSpecConstant(const SpvOp opcode) {
219 switch (opcode) {
220 case SpvOpSpecConstantTrue:
221 case SpvOpSpecConstantFalse:
222 case SpvOpSpecConstant:
223 return true;
224 default:
225 return false;
226 }
227}
228
Lei Zhangb36e7042015-10-28 13:40:52 -0400229int32_t spvOpcodeIsComposite(const SpvOp opcode) {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100230 switch (opcode) {
Lei Zhangb36e7042015-10-28 13:40:52 -0400231 case SpvOpTypeVector:
232 case SpvOpTypeMatrix:
233 case SpvOpTypeArray:
234 case SpvOpTypeStruct:
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100235 return true;
236 default:
237 return false;
238 }
239}
240
Ehsan Nasiri23af06c2017-02-01 15:37:39 -0500241bool spvOpcodeReturnsLogicalVariablePointer(const SpvOp opcode) {
242 switch (opcode) {
243 case SpvOpVariable:
244 case SpvOpAccessChain:
245 case SpvOpInBoundsAccessChain:
246 case SpvOpFunctionParameter:
247 case SpvOpImageTexelPointer:
248 case SpvOpCopyObject:
249 case SpvOpSelect:
250 case SpvOpPhi:
251 case SpvOpFunctionCall:
252 case SpvOpPtrAccessChain:
253 case SpvOpLoad:
254 case SpvOpConstantNull:
255 return true;
256 default:
257 return false;
258 }
259}
260
Florian Ziesche66fcb452016-03-02 22:17:54 +0100261int32_t spvOpcodeReturnsLogicalPointer(const SpvOp opcode) {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100262 switch (opcode) {
Lei Zhangb36e7042015-10-28 13:40:52 -0400263 case SpvOpVariable:
264 case SpvOpAccessChain:
265 case SpvOpInBoundsAccessChain:
266 case SpvOpFunctionParameter:
Florian Ziesche66fcb452016-03-02 22:17:54 +0100267 case SpvOpImageTexelPointer:
268 case SpvOpCopyObject:
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100269 return true;
270 default:
271 return false;
272 }
273}
274
Lei Zhangb36e7042015-10-28 13:40:52 -0400275int32_t spvOpcodeGeneratesType(SpvOp op) {
Lei Zhang1a0334e2015-11-02 09:41:20 -0500276 switch (op) {
Lei Zhangb36e7042015-10-28 13:40:52 -0400277 case SpvOpTypeVoid:
278 case SpvOpTypeBool:
279 case SpvOpTypeInt:
280 case SpvOpTypeFloat:
281 case SpvOpTypeVector:
282 case SpvOpTypeMatrix:
283 case SpvOpTypeImage:
284 case SpvOpTypeSampler:
285 case SpvOpTypeSampledImage:
286 case SpvOpTypeArray:
287 case SpvOpTypeRuntimeArray:
288 case SpvOpTypeStruct:
289 case SpvOpTypeOpaque:
290 case SpvOpTypePointer:
291 case SpvOpTypeFunction:
292 case SpvOpTypeEvent:
293 case SpvOpTypeDeviceEvent:
294 case SpvOpTypeReserveId:
295 case SpvOpTypeQueue:
296 case SpvOpTypePipe:
Andrey Tuganov4ef3b3e2017-02-28 11:53:47 -0500297 case SpvOpTypePipeStorage:
298 case SpvOpTypeNamedBarrier:
Andrew Woloszyn537e7762015-09-29 11:28:34 -0400299 return true;
David Netoaef608c2015-11-02 14:59:02 -0500300 default:
301 // In particular, OpTypeForwardPointer does not generate a type,
302 // but declares a storage class for a pointer type generated
303 // by a different instruction.
304 break;
Andrew Woloszyn537e7762015-09-29 11:28:34 -0400305 }
306 return 0;
307}