blob: 6103844817d8f37d84f222344957913012a9511d [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>
David Netob5dc8fc2015-10-06 16:22:00 -040031#include "instruction.h"
David Neto78c3b432015-08-27 13:03:52 -040032#include "operand.h"
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010033#include "print.h"
34
35// Functions
36
37/// @brief Fix the endianness of a word
38///
39/// @param[in] word whos endianness should be fixed
40/// @param[in] endian the desired endianness
41///
42/// @return word with host endianness correction
43uint32_t spvFixWord(const uint32_t word, const spv_endianness_t endian);
44
Lei Zhangb41d1502015-09-14 15:22:23 -040045/// @brief Fix the endianness of a double word
46///
47/// @param[in] low the lower 32-bit of the double word
48/// @param[in] high the higher 32-bit of the double word
49/// @param[in] endian the desired endianness
50///
51/// @return word with host endianness correction
52uint64_t spvFixDoubleWord(const uint32_t low, const uint32_t high,
53 const spv_endianness_t endian);
54
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010055/// @brief Determine the endianness of the SPV binary
56///
57/// Gets the endianness of the SPV source. Returns SPV_ENDIANNESS_UNKNOWN if
58/// the
59/// SPV magic number is invalid, otherwise the determined endianness.
60///
61/// @param[in] binary the binary module
62/// @param[out] pEndian return the endianness of the SPV module
63///
64/// @return result code
65spv_result_t spvBinaryEndianness(const spv_binary binary,
66 spv_endianness_t *pEndian);
67
68/// @brief Grab the header from the SPV module
69///
70/// @param[in] binary the binary module
71/// @param[in] endian the endianness of the module
72/// @param[out] pHeader the returned header
73///
74/// @return result code
75spv_result_t spvBinaryHeaderGet(const spv_binary binary,
76 const spv_endianness_t endian,
77 spv_header_t *pHeader);
78
79/// @brief Populate a binary stream with this generators header
80///
81/// @param[in,out] binary the binary stream
82/// @param[in] bound the upper ID bound
83///
84/// @return result code
85spv_result_t spvBinaryHeaderSet(spv_binary binary, const uint32_t bound);
86
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010087/// @brief Determine the type of the desired operand
88///
89/// @param[in] word the operand value
90/// @param[in] index the word index in the instruction
91/// @param[in] opcodeEntry table of specified Opcodes
92/// @param[in] operandTable table of specified operands
93/// @param[in,out] pOperandEntry the entry in the operand table
94///
95/// @return type returned
96spv_operand_type_t spvBinaryOperandInfo(const uint32_t word,
97 const uint16_t index,
98 const spv_opcode_desc opcodeEntry,
99 const spv_operand_table operandTable,
100 spv_operand_desc *pOperandEntry);
101
102/// @brief Translate a binary operand to the textual form
103///
104/// @param[in] opcode of the current instruction
105/// @param[in] type type of the operand to decode
106/// @param[in] words the binary stream of words
107/// @param[in] endian the endianness of the stream
108/// @param[in] options bitfield of spv_binary_to_text_options_t values
109/// @param[in] operandTable table of specified operands
David Neto78c3b432015-08-27 13:03:52 -0400110/// @param[in,out] pExpectedOperands the expected operand types
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100111/// @param[in,out] pExtInstType type of extended instruction library
112/// @param[in,out] stream the text output stream
113/// @param[in,out] position position in the binary stream
114/// @param[out] pDiag return diagnostic on error
115///
116/// @return result code
117spv_result_t spvBinaryDecodeOperand(
118 const Op opcode, const spv_operand_type_t type, const uint32_t *words,
Lei Zhangb41d1502015-09-14 15:22:23 -0400119 uint16_t numWords, const spv_endianness_t endian, const uint32_t options,
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100120 const spv_operand_table operandTable, const spv_ext_inst_table extInstTable,
David Neto78c3b432015-08-27 13:03:52 -0400121 spv_operand_pattern_t *pExpectedOperands, spv_ext_inst_type_t *pExtInstType,
122 out_stream &stream, spv_position position, spv_diagnostic *pDiagnostic);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100123
124/// @brief Translate binary Opcode stream to textual form
125///
126/// @param[in] pInst the Opcode instruction stream
127/// @param[in] endian the endianness of the stream
128/// @param[in] options bitfield of spv_binary_to_text_options_t values
129/// @param[in] opcodeTable table of specified Opcodes
130/// @param[in] operandTable table of specified operands
Lei Zhang29e667e2015-09-11 11:01:59 -0400131/// @param[in] format the assembly syntax format to decode into
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100132/// @param[out] stream output text stream
133/// @param[in,out] position position in the stream
134/// @param[out] pDiag return diagnostic on error
135///
136/// @return result code
137spv_result_t spvBinaryDecodeOpcode(
138 spv_instruction_t *pInst, const spv_endianness_t endian,
139 const uint32_t options, const spv_opcode_table opcodeTable,
140 const spv_operand_table operandTable, const spv_ext_inst_table extInstTable,
Lei Zhang29e667e2015-09-11 11:01:59 -0400141 spv_assembly_syntax_format_t format, out_stream &stream,
142 spv_position position, spv_diagnostic *pDiag);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100143
144#endif