blob: 27273330e1154008108f76b193c7f415fd07cafb [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#ifndef _LIBSPIRV_UTIL_OPCODE_H_
28#define _LIBSPIRV_UTIL_OPCODE_H_
29
30#include <libspirv/libspirv.h>
31
32// Functions
33
34/// @brief Get the name of the SPIR-V generator
35///
36/// @param[in] generator Khronos SPIR-V generator ID
37///
38/// @return string name
39const char *spvGeneratorStr(uint32_t generator);
40
41/// @brief Combine word count and Opcode enumerant in single word
42///
43/// @param[in] wordCount Opcode consumes
44/// @param[in] opcode enumerant value
45///
46/// @return Opcode word
47uint32_t spvOpcodeMake(uint16_t wordCount, Op opcode);
48
49/// @brief Split the binary opcode into its constituent parts
50///
51/// @param[in] word binary opcode to split
52/// @param[out] wordCount the returned number of words (optional)
53/// @param[out] opcode the returned opcode enumerant (optional)
54void spvOpcodeSplit(const uint32_t word, uint16_t *wordCount, Op *opcode);
55
56/// @brief Find the named Opcode in the table
57///
58/// @param[in] table to lookup
59/// @param[in] name name of Opcode to find
60/// @param[out] pEntry returned Opcode table entry
61///
62/// @return result code
63spv_result_t spvOpcodeTableNameLookup(const spv_opcode_table table,
Lei Zhanga94701d2015-09-14 10:05:37 -040064 const char *name,
65 spv_opcode_desc *pEntry);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010066
67/// @brief Find the opcode ID in the table
68///
69/// @param[out] table to lookup
70/// @param[in] opcode value of Opcode to fine
71/// @param[out] pEntry return Opcode table entry
72///
73/// @return result code
74spv_result_t spvOpcodeTableValueLookup(const spv_opcode_table table,
Lei Zhanga94701d2015-09-14 10:05:37 -040075 const Op opcode,
76 spv_opcode_desc *pEntry);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010077
Lei Zhangdfc50082015-08-21 11:50:55 -040078/// @brief Get the argument index for the <result-id> operand, if any.
79///
80/// @param[in] entry the Opcode entry
81///
82/// @return index for the <result-id> operand, or
83/// SPV_OPERAND_INVALID_RESULT_ID_INDEX if the given opcode
84/// doesn't have a <result-id> operand.
85//
86/// For example, 0 means <result-id> is the first argument, i.e. right after
87/// the wordcount/opcode word.
88int16_t spvOpcodeResultIdIndex(spv_opcode_desc entry);
89
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010090/// @brief Determine if the Opcode has capaspvity requirements
91///
92/// This function does not check if @a entry is valid.
93///
94/// @param[in] entry the Opcode entry
95///
96/// @return zero if false, non-zero otherwise
97int32_t spvOpcodeRequiresCapabilities(spv_opcode_desc entry);
98
99/// @brief Copy an instructions word and fix the endianness
100///
101/// @param[in] words the input instruction stream
102/// @param[in] opcode the instructions Opcode
103/// @param[in] wordCount the number of words to copy
104/// @param[in] endian the endianness of the stream
105/// @param[out] pInst the returned instruction
106void spvInstructionCopy(const uint32_t *words, const Op opcode,
107 const uint16_t wordCount, const spv_endianness_t endian,
108 spv_instruction_t *pInst);
109
110/// @brief Get the string of an OpCode
111///
112/// @param[in] opcode the opcode
113///
114/// @return the opcode string
115const char *spvOpcodeString(const Op opcode);
116
117/// @brief Determine if the Opcode is a type
118///
119/// @param[in] opcode the opcode
120///
121/// @return zero if false, non-zero otherwise
122int32_t spvOpcodeIsType(const Op opcode);
123
124/// @brief Determine if the OpCode is a scalar type
125///
126/// @param[in] opcode the opcode
127///
128/// @return zero if false, non-zero otherwise
129int32_t spvOpcodeIsScalarType(const Op opcode);
130
131/// @brief Determine if the Opcode is a constant
132///
133/// @param[in] opcode the opcode
134///
135/// @return zero if false, non-zero otherwise
136int32_t spvOpcodeIsConstant(const Op opcode);
137
138/// @brief Determine if the Opcode is a composite type
139///
140/// @param[in] opcode the opcode
141///
142/// @return zero if false, non-zero otherwise
143int32_t spvOpcodeIsComposite(const Op opcode);
144
145/// @brief Deep comparison of type declaration instructions
146///
147/// @param[in] pTypeInst0 type definition zero
148/// @param[in] pTypeInst1 type definition one
149///
150/// @return zero if false, non-zero otherwise
151int32_t spvOpcodeAreTypesEqual(const spv_instruction_t *pTypeInst0,
152 const spv_instruction_t *pTypeInst1);
153
154/// @brief Determine if the Opcode results in a pointer
155///
156/// @param[in] opcode the opcode
157///
158/// @return zero if false, non-zero otherwise
159int32_t spvOpcodeIsPointer(const Op opcode);
160
161/// @brief Determine if the Opcode results in a instantation of a non-void type
162///
163/// @param[in] opcode the opcode
164///
165/// @return zero if false, non-zero otherwise
166int32_t spvOpcodeIsObject(const Op opcode);
167
168/// @brief Determine if the scalar type Opcode is nullable
169///
170/// @param[in] opcode the opcode
171///
172/// @return zero if false, non-zero otherwise
173int32_t spvOpcodeIsBasicTypeNullable(Op opcode);
174
175/// @brief Determine if instruction is in a basic block
176///
177/// @param[in] pFirstInst first instruction in the stream
178/// @param[in] pInst current instruction
179///
180/// @return zero if false, non-zero otherwise
181int32_t spvInstructionIsInBasicBlock(const spv_instruction_t *pFirstInst,
182 const spv_instruction_t *pInst);
183
184/// @brief Determine if the Opcode contains a value
185///
186/// @param[in] opcode the opcode
187///
188/// @return zero if false, non-zero otherwise
189int32_t spvOpcodeIsValue(Op opcode);
190
191#endif