blob: 407452fe65b190e3480d236c04e15096503dae21 [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
David Neto9f79d782015-10-27 16:27:05 -040027#ifndef LIBSPIRV_OPCODE_H_
28#define LIBSPIRV_OPCODE_H_
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010029
David Netob5dc8fc2015-10-06 16:22:00 -040030#include "instruction.h"
Lei Zhang923f6c12015-11-11 12:45:23 -050031#include "libspirv/libspirv.h"
Lei Zhang7a222e42015-11-11 12:40:25 -050032#include "table.h"
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010033
34// Functions
35
David Neto1780fc42015-10-26 15:43:12 -040036/// @brief Get the name of the SPIR-V generator.
37///
38/// See the registry at
39/// https://www.khronos.org/registry/spir-v/api/spir-v.xml
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010040///
41/// @param[in] generator Khronos SPIR-V generator ID
42///
43/// @return string name
Lei Zhang1a0334e2015-11-02 09:41:20 -050044const char* spvGeneratorStr(uint32_t generator);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010045
46/// @brief Combine word count and Opcode enumerant in single word
47///
48/// @param[in] wordCount Opcode consumes
49/// @param[in] opcode enumerant value
50///
51/// @return Opcode word
Lei Zhangb36e7042015-10-28 13:40:52 -040052uint32_t spvOpcodeMake(uint16_t wordCount, SpvOp opcode);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010053
54/// @brief Split the binary opcode into its constituent parts
55///
56/// @param[in] word binary opcode to split
57/// @param[out] wordCount the returned number of words (optional)
58/// @param[out] opcode the returned opcode enumerant (optional)
Lei Zhang1a0334e2015-11-02 09:41:20 -050059void spvOpcodeSplit(const uint32_t word, uint16_t* wordCount, SpvOp* opcode);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010060
61/// @brief Find the named Opcode in the table
62///
63/// @param[in] table to lookup
64/// @param[in] name name of Opcode to find
65/// @param[out] pEntry returned Opcode table entry
66///
67/// @return result code
68spv_result_t spvOpcodeTableNameLookup(const spv_opcode_table table,
Lei Zhang1a0334e2015-11-02 09:41:20 -050069 const char* name,
70 spv_opcode_desc* pEntry);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010071
72/// @brief Find the opcode ID in the table
73///
74/// @param[out] table to lookup
75/// @param[in] opcode value of Opcode to fine
76/// @param[out] pEntry return Opcode table entry
77///
78/// @return result code
79spv_result_t spvOpcodeTableValueLookup(const spv_opcode_table table,
Lei Zhangb36e7042015-10-28 13:40:52 -040080 const SpvOp opcode,
Lei Zhang1a0334e2015-11-02 09:41:20 -050081 spv_opcode_desc* pEntry);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010082
Lei Zhangdfc50082015-08-21 11:50:55 -040083/// @brief Get the argument index for the <result-id> operand, if any.
84///
85/// @param[in] entry the Opcode entry
86///
87/// @return index for the <result-id> operand, or
88/// SPV_OPERAND_INVALID_RESULT_ID_INDEX if the given opcode
89/// doesn't have a <result-id> operand.
90//
91/// For example, 0 means <result-id> is the first argument, i.e. right after
92/// the wordcount/opcode word.
93int16_t spvOpcodeResultIdIndex(spv_opcode_desc entry);
94
David Neto9db3a532015-10-07 16:58:38 -040095/// @brief Determine if the Opcode has capability requirements.
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010096///
97/// This function does not check if @a entry is valid.
98///
99/// @param[in] entry the Opcode entry
100///
101/// @return zero if false, non-zero otherwise
102int32_t spvOpcodeRequiresCapabilities(spv_opcode_desc entry);
103
104/// @brief Copy an instructions word and fix the endianness
105///
106/// @param[in] words the input instruction stream
107/// @param[in] opcode the instructions Opcode
108/// @param[in] wordCount the number of words to copy
109/// @param[in] endian the endianness of the stream
110/// @param[out] pInst the returned instruction
Lei Zhang1a0334e2015-11-02 09:41:20 -0500111void spvInstructionCopy(const uint32_t* words, const SpvOp opcode,
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100112 const uint16_t wordCount, const spv_endianness_t endian,
Lei Zhang1a0334e2015-11-02 09:41:20 -0500113 spv_instruction_t* pInst);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100114
David Neto1bcd3d12015-11-02 16:03:12 -0500115/// @brief Get the name of an instruction, without the "Op" prefix.
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100116///
117/// @param[in] opcode the opcode
118///
119/// @return the opcode string
Lei Zhang1a0334e2015-11-02 09:41:20 -0500120const char* spvOpcodeString(const SpvOp opcode);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100121
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100122/// @brief Determine if the OpCode is a scalar type
123///
124/// @param[in] opcode the opcode
125///
126/// @return zero if false, non-zero otherwise
Lei Zhangb36e7042015-10-28 13:40:52 -0400127int32_t spvOpcodeIsScalarType(const SpvOp opcode);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100128
129/// @brief Determine if the Opcode is a constant
130///
131/// @param[in] opcode the opcode
132///
133/// @return zero if false, non-zero otherwise
Lei Zhangb36e7042015-10-28 13:40:52 -0400134int32_t spvOpcodeIsConstant(const SpvOp opcode);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100135
136/// @brief Determine if the Opcode is a composite type
137///
138/// @param[in] opcode the opcode
139///
140/// @return zero if false, non-zero otherwise
Lei Zhangb36e7042015-10-28 13:40:52 -0400141int32_t spvOpcodeIsComposite(const SpvOp opcode);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100142
143/// @brief Deep comparison of type declaration instructions
144///
145/// @param[in] pTypeInst0 type definition zero
146/// @param[in] pTypeInst1 type definition one
147///
148/// @return zero if false, non-zero otherwise
Lei Zhang1a0334e2015-11-02 09:41:20 -0500149int32_t spvOpcodeAreTypesEqual(const spv_instruction_t* pTypeInst0,
150 const spv_instruction_t* pTypeInst1);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100151
152/// @brief Determine if the Opcode results in a pointer
153///
154/// @param[in] opcode the opcode
155///
156/// @return zero if false, non-zero otherwise
Lei Zhangb36e7042015-10-28 13:40:52 -0400157int32_t spvOpcodeIsPointer(const SpvOp opcode);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100158
159/// @brief Determine if the Opcode results in a instantation of a non-void type
160///
161/// @param[in] opcode the opcode
162///
163/// @return zero if false, non-zero otherwise
Lei Zhangb36e7042015-10-28 13:40:52 -0400164int32_t spvOpcodeIsObject(const SpvOp opcode);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100165
166/// @brief Determine if the scalar type Opcode is nullable
167///
168/// @param[in] opcode the opcode
169///
170/// @return zero if false, non-zero otherwise
Lei Zhangb36e7042015-10-28 13:40:52 -0400171int32_t spvOpcodeIsBasicTypeNullable(SpvOp opcode);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100172
173/// @brief Determine if instruction is in a basic block
174///
175/// @param[in] pFirstInst first instruction in the stream
176/// @param[in] pInst current instruction
177///
178/// @return zero if false, non-zero otherwise
Lei Zhang1a0334e2015-11-02 09:41:20 -0500179int32_t spvInstructionIsInBasicBlock(const spv_instruction_t* pFirstInst,
180 const spv_instruction_t* pInst);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100181
182/// @brief Determine if the Opcode contains a value
183///
184/// @param[in] opcode the opcode
185///
186/// @return zero if false, non-zero otherwise
Lei Zhangb36e7042015-10-28 13:40:52 -0400187int32_t spvOpcodeIsValue(SpvOp opcode);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100188
Andrew Woloszyn537e7762015-09-29 11:28:34 -0400189/// @brief Determine if the Opcode generates a type
190///
191/// @param[in] opcode the opcode
192///
193/// @return zero if false, non-zero otherwise
Lei Zhangb36e7042015-10-28 13:40:52 -0400194int32_t spvOpcodeGeneratesType(SpvOp op);
Andrew Woloszyn537e7762015-09-29 11:28:34 -0400195
David Neto9f79d782015-10-27 16:27:05 -0400196#endif // LIBSPIRV_OPCODE_H_