blob: 08f06994b0b621f1d98e038b3381396f2360401c [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_BINARY_H_
28#define LIBSPIRV_BINARY_H_
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010029
David Neto0ca6b592015-10-30 16:06:15 -040030#include "libspirv/libspirv.h"
31
32
33// TODO(dneto): Move spvBinaryParse and related type definitions to libspirv.h
34extern "C" {
35
36// The parser interface.
37// This is a C interface because we expect to expose this to clients of the
38// SPIR-V Tools C API.
39
40// A pointer to a function that accepts a parsed SPIR-V header.
41// The integer arguments are the 32-bit words from the header, as specified
42// in SPIR-V 1.0 Section 2.3 Table 1.
43// The function should return SPV_SUCCESS if parsing should continue.
44typedef spv_result_t (*spv_parsed_header_fn_t)(
45 void* user_data, spv_endianness_t endian, uint32_t magic, uint32_t version,
46 uint32_t generator, uint32_t id_bound, uint32_t reserved);
47
48// Number kind. This determines the format at a high level, but not
49// the bit width.
50// In principle, these could probably be folded into new entries in
51// spv_operand_type_t. But then we'd have some special case differences
52// between the assembler and disassembler.
53typedef enum spv_number_kind_t {
54 SPV_NUMBER_NONE = 0, // The default for value initialization.
55 SPV_NUMBER_UNSIGNED_INT,
56 SPV_NUMBER_SIGNED_INT,
57 SPV_NUMBER_FLOATING,
58} spv_number_kind_t;
59
60// Information about a parsed operand. Note that the values are not
61// included. You still need access to the binary to extract the values.
62typedef struct spv_parsed_operand_t {
63 // Location of the operand, in words from the start of the instruction.
64 uint16_t offset;
65 // Number of words occupied by this operand.
66 uint16_t num_words;
67 // The "concrete" operand type. See the definition of spv_operand_type_t
68 // for details.
69 spv_operand_type_t type;
70 // If type is a literal number type, then number_kind says whether it's
71 // a signed integer, an unsigned integer, or a floating point number.
72 spv_number_kind_t number_kind;
73 // The number of bits for a literal number type.
74 uint32_t number_bit_width;
75} spv_parsed_operand_info_t;
76
77// A parsed instruction.
78typedef struct spv_parsed_instruction_t {
79 // Location of the instruction, in words from the start of the SPIR-V binary.
80 size_t offset;
81 SpvOp opcode;
82 // The extended instruction type, if opcode is OpExtInst. Otherwise
83 // this is the "none" value.
84 spv_ext_inst_type_t ext_inst_type;
85 // The type id, or 0 if this instruction doesn't have one.
86 uint32_t type_id;
87 // The result id, or 0 if this instruction doesn't have one.
88 uint32_t result_id;
89 // The array of parsed operands.
90 const spv_parsed_operand_t* operands;
91 uint16_t num_operands;
92} spv_parsed_instruction_t;
93
94// A pointer to a function that accepts a parsed SPIR-V instruction.
95// The parsed_instruction value is transient: it may be overwritten
96// or released immediately after the function has returned. The function
97// should return SPV_SUCCESS if and only if parsing should continue.
98typedef spv_result_t (*spv_parsed_instruction_fn_t)(
99 void* user_data, const spv_parsed_instruction_t* parsed_instruction);
100
101// Parses a SPIR-V binary, specified as counted sequence of 32-bit words.
102// Parsing feedback is provided via two callbacks. In a valid parse, the
103// parsed-header callback is called once, and then the parsed-instruction
104// callback once for each instruction in the stream. The user_data parameter
105// is supplied as context to the callbacks. Returns SPV_SUCCESS on successful
106// parse where the callbacks always return SPV_SUCCESS. For an invalid parse,
107// returns SPV_ERROR_INVALID_BINARY and emits a diagnostic. If a callback
108// returns anything other than SPV_SUCCESS, then that error code is returned
109// and parsing terminates early.
110spv_result_t spvBinaryParse(void* user_data, const uint32_t* const words,
111 const size_t num_words,
112 spv_parsed_header_fn_t parse_header,
113 spv_parsed_instruction_fn_t parse_instruction,
114 spv_diagnostic* diagnostic);
115
116} // extern "C"
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100117
118// Functions
119
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100120/// @brief Grab the header from the SPV module
121///
122/// @param[in] binary the binary module
123/// @param[in] endian the endianness of the module
124/// @param[out] pHeader the returned header
125///
126/// @return result code
127spv_result_t spvBinaryHeaderGet(const spv_binary binary,
128 const spv_endianness_t endian,
Lei Zhang1a0334e2015-11-02 09:41:20 -0500129 spv_header_t* pHeader);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100130
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100131/// @brief Determine the type of the desired operand
132///
133/// @param[in] word the operand value
134/// @param[in] index the word index in the instruction
135/// @param[in] opcodeEntry table of specified Opcodes
136/// @param[in] operandTable table of specified operands
137/// @param[in,out] pOperandEntry the entry in the operand table
138///
139/// @return type returned
140spv_operand_type_t spvBinaryOperandInfo(const uint32_t word,
141 const uint16_t index,
142 const spv_opcode_desc opcodeEntry,
143 const spv_operand_table operandTable,
Lei Zhang1a0334e2015-11-02 09:41:20 -0500144 spv_operand_desc* pOperandEntry);
David Neto0ca6b592015-10-30 16:06:15 -0400145
Lei Zhang1a0334e2015-11-02 09:41:20 -0500146#endif // LIBSPIRV_BINARY_H_