blob: 2ab5c66ca6ce3a880e3628563e1ad850303b69b0 [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_BINARY_H_
28#define _LIBSPIRV_UTIL_BINARY_H_
29
30#include <libspirv/libspirv.h>
31#include "print.h"
32
33// Functions
34
35/// @brief Fix the endianness of a word
36///
37/// @param[in] word whos endianness should be fixed
38/// @param[in] endian the desired endianness
39///
40/// @return word with host endianness correction
41uint32_t spvFixWord(const uint32_t word, const spv_endianness_t endian);
42
43/// @brief Determine the endianness of the SPV binary
44///
45/// Gets the endianness of the SPV source. Returns SPV_ENDIANNESS_UNKNOWN if
46/// the
47/// SPV magic number is invalid, otherwise the determined endianness.
48///
49/// @param[in] binary the binary module
50/// @param[out] pEndian return the endianness of the SPV module
51///
52/// @return result code
53spv_result_t spvBinaryEndianness(const spv_binary binary,
54 spv_endianness_t *pEndian);
55
56/// @brief Grab the header from the SPV module
57///
58/// @param[in] binary the binary module
59/// @param[in] endian the endianness of the module
60/// @param[out] pHeader the returned header
61///
62/// @return result code
63spv_result_t spvBinaryHeaderGet(const spv_binary binary,
64 const spv_endianness_t endian,
65 spv_header_t *pHeader);
66
67/// @brief Populate a binary stream with this generators header
68///
69/// @param[in,out] binary the binary stream
70/// @param[in] bound the upper ID bound
71///
72/// @return result code
73spv_result_t spvBinaryHeaderSet(spv_binary binary, const uint32_t bound);
74
75/// @brief Append a single word into a binary stream
76///
77/// @param[in] value the word to encode
78/// @param[in] pInst the stream to append to
79/// @param[in,out] position position in the binary
80/// @param[out] pDiagnostic contains diagnostic on failure
81///
82/// @return result code
83spv_result_t spvBinaryEncodeU32(const uint32_t value, spv_instruction_t *pInst,
84 const spv_position position,
85 spv_diagnostic *pDiagnostic);
86
87/// @brief Append two related words into the binary stream
88///
89/// @param[in] value the two words to encode
90/// @param[in] pInst the stream to append to
91/// @param[in,out] position position in the binary
92/// @param[out] pDiagnostic contains diagnostic on failure
93///
94/// @return result code
95spv_result_t spvBinaryEncodeU64(const uint64_t value, spv_instruction_t *pInst,
96 const spv_position position,
97 spv_diagnostic *pDiagnostic);
98
99/// @brief Append a string literal in the binary stream
100///
101/// @param[in] str the string to encode
102/// @param[in] pInst the stream to append to
103/// @param[in,out] position position in the binary
104/// @param[out] pDiagnostic contains diagnostic on failure
105///
106/// @return result code
107spv_result_t spvBinaryEncodeString(const char *str, spv_instruction_t *pInst,
108 const spv_position position,
109 spv_diagnostic *pDiagnostic);
110
111/// @brief Determine the type of the desired operand
112///
113/// @param[in] word the operand value
114/// @param[in] index the word index in the instruction
115/// @param[in] opcodeEntry table of specified Opcodes
116/// @param[in] operandTable table of specified operands
117/// @param[in,out] pOperandEntry the entry in the operand table
118///
119/// @return type returned
120spv_operand_type_t spvBinaryOperandInfo(const uint32_t word,
121 const uint16_t index,
122 const spv_opcode_desc opcodeEntry,
123 const spv_operand_table operandTable,
124 spv_operand_desc *pOperandEntry);
125
126/// @brief Translate a binary operand to the textual form
127///
128/// @param[in] opcode of the current instruction
129/// @param[in] type type of the operand to decode
130/// @param[in] words the binary stream of words
131/// @param[in] endian the endianness of the stream
132/// @param[in] options bitfield of spv_binary_to_text_options_t values
133/// @param[in] operandTable table of specified operands
134/// @param[in,out] pExtInstType type of extended instruction library
135/// @param[in,out] stream the text output stream
136/// @param[in,out] position position in the binary stream
137/// @param[out] pDiag return diagnostic on error
138///
139/// @return result code
140spv_result_t spvBinaryDecodeOperand(
141 const Op opcode, const spv_operand_type_t type, const uint32_t *words,
142 const spv_endianness_t endian, const uint32_t options,
143 const spv_operand_table operandTable, const spv_ext_inst_table extInstTable,
144 spv_ext_inst_type_t *pExtInstType, out_stream &stream,
145 spv_position position, spv_diagnostic *pDiag);
146
147/// @brief Translate binary Opcode stream to textual form
148///
149/// @param[in] pInst the Opcode instruction stream
150/// @param[in] endian the endianness of the stream
151/// @param[in] options bitfield of spv_binary_to_text_options_t values
152/// @param[in] opcodeTable table of specified Opcodes
153/// @param[in] operandTable table of specified operands
154/// @param[out] stream output text stream
155/// @param[in,out] position position in the stream
156/// @param[out] pDiag return diagnostic on error
157///
158/// @return result code
159spv_result_t spvBinaryDecodeOpcode(
160 spv_instruction_t *pInst, const spv_endianness_t endian,
161 const uint32_t options, const spv_opcode_table opcodeTable,
162 const spv_operand_table operandTable, const spv_ext_inst_table extInstTable,
163 out_stream &stream, spv_position position, spv_diagnostic *pDiag);
164
165#endif
166