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