• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2015-2016 The Khronos Group Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef SPIRV_TOOLS_LIBSPIRV_H_
16 #define SPIRV_TOOLS_LIBSPIRV_H_
17 
18 #ifdef __cplusplus
19 extern "C" {
20 #else
21 #include <stdbool.h>
22 #endif
23 
24 #include <stddef.h>
25 #include <stdint.h>
26 
27 // Helpers
28 
29 #define SPV_BIT(shift) (1 << (shift))
30 
31 #define SPV_FORCE_16_BIT_ENUM(name) _##name = 0x7fff
32 #define SPV_FORCE_32_BIT_ENUM(name) _##name = 0x7fffffff
33 
34 // Enumerations
35 
36 typedef enum spv_result_t {
37   SPV_SUCCESS = 0,
38   SPV_UNSUPPORTED = 1,
39   SPV_END_OF_STREAM = 2,
40   SPV_WARNING = 3,
41   SPV_FAILED_MATCH = 4,
42   SPV_REQUESTED_TERMINATION = 5,  // Success, but signals early termination.
43   SPV_ERROR_INTERNAL = -1,
44   SPV_ERROR_OUT_OF_MEMORY = -2,
45   SPV_ERROR_INVALID_POINTER = -3,
46   SPV_ERROR_INVALID_BINARY = -4,
47   SPV_ERROR_INVALID_TEXT = -5,
48   SPV_ERROR_INVALID_TABLE = -6,
49   SPV_ERROR_INVALID_VALUE = -7,
50   SPV_ERROR_INVALID_DIAGNOSTIC = -8,
51   SPV_ERROR_INVALID_LOOKUP = -9,
52   SPV_ERROR_INVALID_ID = -10,
53   SPV_ERROR_INVALID_CFG = -11,
54   SPV_ERROR_INVALID_LAYOUT = -12,
55   SPV_ERROR_INVALID_CAPABILITY = -13,
56   SPV_ERROR_INVALID_DATA = -14,  // Indicates data rules validation failure.
57   SPV_ERROR_MISSING_EXTENSION = -15,
58   SPV_FORCE_32_BIT_ENUM(spv_result_t)
59 } spv_result_t;
60 
61 // Severity levels of messages communicated to the consumer.
62 typedef enum spv_message_level_t {
63   SPV_MSG_FATAL,           // Unrecoverable error due to environment.
64                            // Will exit the program immediately. E.g.,
65                            // out of memory.
66   SPV_MSG_INTERNAL_ERROR,  // Unrecoverable error due to SPIRV-Tools
67                            // internals.
68                            // Will exit the program immediately. E.g.,
69                            // unimplemented feature.
70   SPV_MSG_ERROR,           // Normal error due to user input.
71   SPV_MSG_WARNING,         // Warning information.
72   SPV_MSG_INFO,            // General information.
73   SPV_MSG_DEBUG,           // Debug information.
74 } spv_message_level_t;
75 
76 typedef enum spv_endianness_t {
77   SPV_ENDIANNESS_LITTLE,
78   SPV_ENDIANNESS_BIG,
79   SPV_FORCE_32_BIT_ENUM(spv_endianness_t)
80 } spv_endianness_t;
81 
82 // The kinds of operands that an instruction may have.
83 //
84 // Some operand types are "concrete".  The binary parser uses a concrete
85 // operand type to describe an operand of a parsed instruction.
86 //
87 // The assembler uses all operand types.  In addition to determining what
88 // kind of value an operand may be, non-concrete operand types capture the
89 // fact that an operand might be optional (may be absent, or present exactly
90 // once), or might occur zero or more times.
91 //
92 // Sometimes we also need to be able to express the fact that an operand
93 // is a member of an optional tuple of values.  In that case the first member
94 // would be optional, and the subsequent members would be required.
95 typedef enum spv_operand_type_t {
96   // A sentinel value.
97   SPV_OPERAND_TYPE_NONE = 0,
98 
99 #define FIRST_CONCRETE(ENUM) ENUM, SPV_OPERAND_TYPE_FIRST_CONCRETE_TYPE = ENUM
100 #define LAST_CONCRETE(ENUM) ENUM, SPV_OPERAND_TYPE_LAST_CONCRETE_TYPE = ENUM
101 
102   // Set 1:  Operands that are IDs.
103   FIRST_CONCRETE(SPV_OPERAND_TYPE_ID),
104   SPV_OPERAND_TYPE_TYPE_ID,
105   SPV_OPERAND_TYPE_RESULT_ID,
106   SPV_OPERAND_TYPE_MEMORY_SEMANTICS_ID,  // SPIR-V Sec 3.25
107   SPV_OPERAND_TYPE_SCOPE_ID,             // SPIR-V Sec 3.27
108 
109   // Set 2:  Operands that are literal numbers.
110   SPV_OPERAND_TYPE_LITERAL_INTEGER,  // Always unsigned 32-bits.
111   // The Instruction argument to OpExtInst. It's an unsigned 32-bit literal
112   // number indicating which instruction to use from an extended instruction
113   // set.
114   SPV_OPERAND_TYPE_EXTENSION_INSTRUCTION_NUMBER,
115   // The Opcode argument to OpSpecConstantOp. It determines the operation
116   // to be performed on constant operands to compute a specialization constant
117   // result.
118   SPV_OPERAND_TYPE_SPEC_CONSTANT_OP_NUMBER,
119   // A literal number whose format and size are determined by a previous operand
120   // in the same instruction.  It's a signed integer, an unsigned integer, or a
121   // floating point number.  It also has a specified bit width.  The width
122   // may be larger than 32, which would require such a typed literal value to
123   // occupy multiple SPIR-V words.
124   SPV_OPERAND_TYPE_TYPED_LITERAL_NUMBER,
125 
126   // Set 3:  The literal string operand type.
127   SPV_OPERAND_TYPE_LITERAL_STRING,
128 
129   // Set 4:  Operands that are a single word enumerated value.
130   SPV_OPERAND_TYPE_SOURCE_LANGUAGE,               // SPIR-V Sec 3.2
131   SPV_OPERAND_TYPE_EXECUTION_MODEL,               // SPIR-V Sec 3.3
132   SPV_OPERAND_TYPE_ADDRESSING_MODEL,              // SPIR-V Sec 3.4
133   SPV_OPERAND_TYPE_MEMORY_MODEL,                  // SPIR-V Sec 3.5
134   SPV_OPERAND_TYPE_EXECUTION_MODE,                // SPIR-V Sec 3.6
135   SPV_OPERAND_TYPE_STORAGE_CLASS,                 // SPIR-V Sec 3.7
136   SPV_OPERAND_TYPE_DIMENSIONALITY,                // SPIR-V Sec 3.8
137   SPV_OPERAND_TYPE_SAMPLER_ADDRESSING_MODE,       // SPIR-V Sec 3.9
138   SPV_OPERAND_TYPE_SAMPLER_FILTER_MODE,           // SPIR-V Sec 3.10
139   SPV_OPERAND_TYPE_SAMPLER_IMAGE_FORMAT,          // SPIR-V Sec 3.11
140   SPV_OPERAND_TYPE_IMAGE_CHANNEL_ORDER,           // SPIR-V Sec 3.12
141   SPV_OPERAND_TYPE_IMAGE_CHANNEL_DATA_TYPE,       // SPIR-V Sec 3.13
142   SPV_OPERAND_TYPE_FP_ROUNDING_MODE,              // SPIR-V Sec 3.16
143   SPV_OPERAND_TYPE_LINKAGE_TYPE,                  // SPIR-V Sec 3.17
144   SPV_OPERAND_TYPE_ACCESS_QUALIFIER,              // SPIR-V Sec 3.18
145   SPV_OPERAND_TYPE_FUNCTION_PARAMETER_ATTRIBUTE,  // SPIR-V Sec 3.19
146   SPV_OPERAND_TYPE_DECORATION,                    // SPIR-V Sec 3.20
147   SPV_OPERAND_TYPE_BUILT_IN,                      // SPIR-V Sec 3.21
148   SPV_OPERAND_TYPE_GROUP_OPERATION,               // SPIR-V Sec 3.28
149   SPV_OPERAND_TYPE_KERNEL_ENQ_FLAGS,              // SPIR-V Sec 3.29
150   SPV_OPERAND_TYPE_KERNEL_PROFILING_INFO,         // SPIR-V Sec 3.30
151   SPV_OPERAND_TYPE_CAPABILITY,                    // SPIR-V Sec 3.31
152 
153 // Set 5:  Operands that are a single word bitmask.
154 // Sometimes a set bit indicates the instruction requires still more operands.
155 #define FIRST_CONCRETE_MASK(ENUM) \
156   ENUM, SPV_OPERAND_TYPE_FIRST_CONCRETE_MASK_TYPE = ENUM
157   FIRST_CONCRETE_MASK(SPV_OPERAND_TYPE_IMAGE),    // SPIR-V Sec 3.14
158   SPV_OPERAND_TYPE_FP_FAST_MATH_MODE,             // SPIR-V Sec 3.15
159   SPV_OPERAND_TYPE_SELECTION_CONTROL,             // SPIR-V Sec 3.22
160   SPV_OPERAND_TYPE_LOOP_CONTROL,                  // SPIR-V Sec 3.23
161   SPV_OPERAND_TYPE_FUNCTION_CONTROL,              // SPIR-V Sec 3.24
162   LAST_CONCRETE(SPV_OPERAND_TYPE_MEMORY_ACCESS),  // SPIR-V Sec 3.26
163   SPV_OPERAND_TYPE_LAST_CONCRETE_MASK_TYPE =
164       SPV_OPERAND_TYPE_LAST_CONCRETE_TYPE,
165 #undef FIRST_CONCRETE_MASK
166 #undef FIRST_CONCRETE
167 #undef LAST_CONCRETE
168 
169 // The remaining operand types are only used internally by the assembler.
170 // There are two categories:
171 //    Optional : expands to 0 or 1 operand, like ? in regular expressions.
172 //    Variable : expands to 0, 1 or many operands or pairs of operands.
173 //               This is similar to * in regular expressions.
174 
175 // Macros for defining bounds on optional and variable operand types.
176 // Any variable operand type is also optional.
177 #define FIRST_OPTIONAL(ENUM) ENUM, SPV_OPERAND_TYPE_FIRST_OPTIONAL_TYPE = ENUM
178 #define FIRST_VARIABLE(ENUM) ENUM, SPV_OPERAND_TYPE_FIRST_VARIABLE_TYPE = ENUM
179 #define LAST_VARIABLE(ENUM)                         \
180   ENUM, SPV_OPERAND_TYPE_LAST_VARIABLE_TYPE = ENUM, \
181         SPV_OPERAND_TYPE_LAST_OPTIONAL_TYPE = ENUM
182 
183   // An optional operand represents zero or one logical operands.
184   // In an instruction definition, this may only appear at the end of the
185   // operand types.
186   FIRST_OPTIONAL(SPV_OPERAND_TYPE_OPTIONAL_ID),
187   // An optional image operand type.
188   SPV_OPERAND_TYPE_OPTIONAL_IMAGE,
189   // An optional memory access type.
190   SPV_OPERAND_TYPE_OPTIONAL_MEMORY_ACCESS,
191   // An optional literal integer.
192   SPV_OPERAND_TYPE_OPTIONAL_LITERAL_INTEGER,
193   // An optional literal number, which may be either integer or floating point.
194   SPV_OPERAND_TYPE_OPTIONAL_LITERAL_NUMBER,
195   // Like SPV_OPERAND_TYPE_TYPED_LITERAL_NUMBER, but optional, and integral.
196   SPV_OPERAND_TYPE_OPTIONAL_TYPED_LITERAL_INTEGER,
197   // An optional literal string.
198   SPV_OPERAND_TYPE_OPTIONAL_LITERAL_STRING,
199   // An optional access qualifier
200   SPV_OPERAND_TYPE_OPTIONAL_ACCESS_QUALIFIER,
201   // An optional context-independent value, or CIV.  CIVs are tokens that we can
202   // assemble regardless of where they occur -- literals, IDs, immediate
203   // integers, etc.
204   SPV_OPERAND_TYPE_OPTIONAL_CIV,
205 
206   // A variable operand represents zero or more logical operands.
207   // In an instruction definition, this may only appear at the end of the
208   // operand types.
209   FIRST_VARIABLE(SPV_OPERAND_TYPE_VARIABLE_ID),
210   SPV_OPERAND_TYPE_VARIABLE_LITERAL_INTEGER,
211   // A sequence of zero or more pairs of (typed literal integer, Id).
212   // Expands to zero or more:
213   //  (SPV_OPERAND_TYPE_TYPED_LITERAL_INTEGER, SPV_OPERAND_TYPE_ID)
214   // where the literal number must always be an integer of some sort.
215   SPV_OPERAND_TYPE_VARIABLE_LITERAL_INTEGER_ID,
216   // A sequence of zero or more pairs of (Id, Literal integer)
217   LAST_VARIABLE(SPV_OPERAND_TYPE_VARIABLE_ID_LITERAL_INTEGER),
218 
219   // This is a sentinel value, and does not represent an operand type.
220   // It should come last.
221   SPV_OPERAND_TYPE_NUM_OPERAND_TYPES,
222 
223   SPV_FORCE_32_BIT_ENUM(spv_operand_type_t)
224 } spv_operand_type_t;
225 
226 typedef enum spv_ext_inst_type_t {
227   SPV_EXT_INST_TYPE_NONE = 0,
228   SPV_EXT_INST_TYPE_GLSL_STD_450,
229   SPV_EXT_INST_TYPE_OPENCL_STD,
230   SPV_EXT_INST_TYPE_SPV_AMD_SHADER_EXPLICIT_VERTEX_PARAMETER,
231   SPV_EXT_INST_TYPE_SPV_AMD_SHADER_TRINARY_MINMAX,
232   SPV_EXT_INST_TYPE_SPV_AMD_GCN_SHADER,
233   SPV_EXT_INST_TYPE_SPV_AMD_SHADER_BALLOT,
234 
235   SPV_FORCE_32_BIT_ENUM(spv_ext_inst_type_t)
236 } spv_ext_inst_type_t;
237 
238 // This determines at a high level the kind of a binary-encoded literal
239 // number, but not the bit width.
240 // In principle, these could probably be folded into new entries in
241 // spv_operand_type_t.  But then we'd have some special case differences
242 // between the assembler and disassembler.
243 typedef enum spv_number_kind_t {
244   SPV_NUMBER_NONE = 0,  // The default for value initialization.
245   SPV_NUMBER_UNSIGNED_INT,
246   SPV_NUMBER_SIGNED_INT,
247   SPV_NUMBER_FLOATING,
248 } spv_number_kind_t;
249 
250 typedef enum spv_text_to_binary_options_t {
251   SPV_TEXT_TO_BINARY_OPTION_NONE = SPV_BIT(0),
252   // Numeric IDs in the binary will have the same values as in the source.
253   // Non-numeric IDs are allocated by filling in the gaps, starting with 1
254   // and going up.
255   SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS = SPV_BIT(1),
256   SPV_FORCE_32_BIT_ENUM(spv_text_to_binary_options_t)
257 } spv_text_to_binary_options_t;
258 
259 typedef enum spv_binary_to_text_options_t {
260   SPV_BINARY_TO_TEXT_OPTION_NONE = SPV_BIT(0),
261   SPV_BINARY_TO_TEXT_OPTION_PRINT = SPV_BIT(1),
262   SPV_BINARY_TO_TEXT_OPTION_COLOR = SPV_BIT(2),
263   SPV_BINARY_TO_TEXT_OPTION_INDENT = SPV_BIT(3),
264   SPV_BINARY_TO_TEXT_OPTION_SHOW_BYTE_OFFSET = SPV_BIT(4),
265   // Do not output the module header as leading comments in the assembly.
266   SPV_BINARY_TO_TEXT_OPTION_NO_HEADER = SPV_BIT(5),
267   // Use friendly names where possible.  The heuristic may expand over
268   // time, but will use common names for scalar types, and debug names from
269   // OpName instructions.
270   SPV_BINARY_TO_TEXT_OPTION_FRIENDLY_NAMES = SPV_BIT(6),
271   SPV_FORCE_32_BIT_ENUM(spv_binary_to_text_options_t)
272 } spv_binary_to_text_options_t;
273 
274 // Structures
275 
276 // Information about an operand parsed from a binary SPIR-V module.
277 // Note that the values are not included.  You still need access to the binary
278 // to extract the values.
279 typedef struct spv_parsed_operand_t {
280   // Location of the operand, in words from the start of the instruction.
281   uint16_t offset;
282   // Number of words occupied by this operand.
283   uint16_t num_words;
284   // The "concrete" operand type.  See the definition of spv_operand_type_t
285   // for details.
286   spv_operand_type_t type;
287   // If type is a literal number type, then number_kind says whether it's
288   // a signed integer, an unsigned integer, or a floating point number.
289   spv_number_kind_t number_kind;
290   // The number of bits for a literal number type.
291   uint32_t number_bit_width;
292 } spv_parsed_operand_t;
293 
294 // An instruction parsed from a binary SPIR-V module.
295 typedef struct spv_parsed_instruction_t {
296   // An array of words for this instruction, in native endianness.
297   const uint32_t* words;
298   // The number of words in this instruction.
299   uint16_t num_words;
300   uint16_t opcode;
301   // The extended instruction type, if opcode is OpExtInst.  Otherwise
302   // this is the "none" value.
303   spv_ext_inst_type_t ext_inst_type;
304   // The type id, or 0 if this instruction doesn't have one.
305   uint32_t type_id;
306   // The result id, or 0 if this instruction doesn't have one.
307   uint32_t result_id;
308   // The array of parsed operands.
309   const spv_parsed_operand_t* operands;
310   uint16_t num_operands;
311 } spv_parsed_instruction_t;
312 
313 typedef struct spv_const_binary_t {
314   const uint32_t* code;
315   const size_t wordCount;
316 } spv_const_binary_t;
317 
318 typedef struct spv_binary_t {
319   uint32_t* code;
320   size_t wordCount;
321 } spv_binary_t;
322 
323 typedef struct spv_text_t {
324   const char* str;
325   size_t length;
326 } spv_text_t;
327 
328 typedef struct spv_position_t {
329   size_t line;
330   size_t column;
331   size_t index;
332 } spv_position_t;
333 
334 typedef struct spv_diagnostic_t {
335   spv_position_t position;
336   char* error;
337   bool isTextSource;
338 } spv_diagnostic_t;
339 
340 // Opaque struct containing the context used to operate on a SPIR-V module.
341 // Its object is used by various translation API functions.
342 typedef struct spv_context_t spv_context_t;
343 
344 typedef struct spv_validator_options_t spv_validator_options_t;
345 
346 // Type Definitions
347 
348 typedef spv_const_binary_t* spv_const_binary;
349 typedef spv_binary_t* spv_binary;
350 typedef spv_text_t* spv_text;
351 typedef spv_position_t* spv_position;
352 typedef spv_diagnostic_t* spv_diagnostic;
353 typedef const spv_context_t* spv_const_context;
354 typedef spv_context_t* spv_context;
355 typedef spv_validator_options_t* spv_validator_options;
356 typedef const spv_validator_options_t* spv_const_validator_options;
357 
358 // Platform API
359 
360 // Returns the SPIRV-Tools software version as a null-terminated string.
361 // The contents of the underlying storage is valid for the remainder of
362 // the process.
363 const char* spvSoftwareVersionString();
364 // Returns a null-terminated string containing the name of the project,
365 // the software version string, and commit details.
366 // The contents of the underlying storage is valid for the remainder of
367 // the process.
368 const char* spvSoftwareVersionDetailsString();
369 
370 // Certain target environments impose additional restrictions on SPIR-V, so it's
371 // often necessary to specify which one applies.  SPV_ENV_UNIVERSAL means
372 // environment-agnostic SPIR-V.
373 typedef enum {
374   SPV_ENV_UNIVERSAL_1_0,  // SPIR-V 1.0 latest revision, no other restrictions.
375   SPV_ENV_VULKAN_1_0,     // Vulkan 1.0 latest revision.
376   SPV_ENV_UNIVERSAL_1_1,  // SPIR-V 1.1 latest revision, no other restrictions.
377   SPV_ENV_OPENCL_2_1,     // OpenCL 2.1 latest revision.
378   SPV_ENV_OPENCL_2_2,     // OpenCL 2.2 latest revision.
379   SPV_ENV_OPENGL_4_0,     // OpenGL 4.0 plus GL_ARB_gl_spirv, latest revisions.
380   SPV_ENV_OPENGL_4_1,     // OpenGL 4.1 plus GL_ARB_gl_spirv, latest revisions.
381   SPV_ENV_OPENGL_4_2,     // OpenGL 4.2 plus GL_ARB_gl_spirv, latest revisions.
382   SPV_ENV_OPENGL_4_3,     // OpenGL 4.3 plus GL_ARB_gl_spirv, latest revisions.
383   // There is no variant for OpenGL 4.4.
384   SPV_ENV_OPENGL_4_5,     // OpenGL 4.5 plus GL_ARB_gl_spirv, latest revisions.
385   SPV_ENV_UNIVERSAL_1_2,  // SPIR-V 1.2, latest revision, no other restrictions.
386 } spv_target_env;
387 
388 // SPIR-V Validator can be parameterized with the following Universal Limits.
389 typedef enum {
390   spv_validator_limit_max_struct_members,
391   spv_validator_limit_max_struct_depth,
392   spv_validator_limit_max_local_variables,
393   spv_validator_limit_max_global_variables,
394   spv_validator_limit_max_switch_branches,
395   spv_validator_limit_max_function_args,
396   spv_validator_limit_max_control_flow_nesting_depth,
397   spv_validator_limit_max_access_chain_indexes,
398 } spv_validator_limit;
399 
400 // Returns a string describing the given SPIR-V target environment.
401 const char* spvTargetEnvDescription(spv_target_env env);
402 
403 // Creates a context object.  Returns null if env is invalid.
404 spv_context spvContextCreate(spv_target_env env);
405 
406 // Destroys the given context object.
407 void spvContextDestroy(spv_context context);
408 
409 // Creates a Validator options object with default options. Returns a valid
410 // options object. The object remains valid until it is passed into
411 // spvValidatorOptionsDestroy.
412 spv_validator_options spvValidatorOptionsCreate();
413 
414 // Destroys the given Validator options object.
415 void spvValidatorOptionsDestroy(spv_validator_options options);
416 
417 // Records the maximum Universal Limit that is considered valid in the given
418 // Validator options object. <options> argument must be a valid options object.
419 void spvValidatorOptionsSetUniversalLimit(spv_validator_options options,
420                                           spv_validator_limit limit_type,
421                                           uint32_t limit);
422 
423 // Encodes the given SPIR-V assembly text to its binary representation. The
424 // length parameter specifies the number of bytes for text. Encoded binary will
425 // be stored into *binary. Any error will be written into *diagnostic if
426 // diagnostic is non-null. The generated binary is independent of the context
427 // and may outlive it.
428 spv_result_t spvTextToBinary(const spv_const_context context, const char* text,
429                              const size_t length, spv_binary* binary,
430                              spv_diagnostic* diagnostic);
431 
432 // Encodes the given SPIR-V assembly text to its binary representation. Same as
433 // spvTextToBinary but with options. The options parameter is a bit field of
434 // spv_text_to_binary_options_t.
435 spv_result_t spvTextToBinaryWithOptions(
436     const spv_const_context context, const char* text, const size_t length,
437     const uint32_t options, spv_binary* binary, spv_diagnostic* diagnostic);
438 
439 // Frees an allocated text stream. This is a no-op if the text parameter
440 // is a null pointer.
441 void spvTextDestroy(spv_text text);
442 
443 // Decodes the given SPIR-V binary representation to its assembly text. The
444 // word_count parameter specifies the number of words for binary. The options
445 // parameter is a bit field of spv_binary_to_text_options_t. Decoded text will
446 // be stored into *text. Any error will be written into *diagnostic if
447 // diagnostic is non-null.
448 spv_result_t spvBinaryToText(const spv_const_context context,
449                              const uint32_t* binary, const size_t word_count,
450                              const uint32_t options, spv_text* text,
451                              spv_diagnostic* diagnostic);
452 
453 // Frees a binary stream from memory. This is a no-op if binary is a null
454 // pointer.
455 void spvBinaryDestroy(spv_binary binary);
456 
457 // Validates a SPIR-V binary for correctness. Any errors will be written into
458 // *diagnostic if diagnostic is non-null.
459 spv_result_t spvValidate(const spv_const_context context,
460                          const spv_const_binary binary,
461                          spv_diagnostic* diagnostic);
462 
463 // Validates a SPIR-V binary for correctness. Uses the provided Validator
464 // options. Any errors will be written into *diagnostic if diagnostic is
465 // non-null.
466 spv_result_t spvValidateWithOptions(const spv_const_context context,
467                                     const spv_const_validator_options options,
468                                     const spv_const_binary binary,
469                                     spv_diagnostic* diagnostic);
470 
471 // Validates a raw SPIR-V binary for correctness. Any errors will be written
472 // into *diagnostic if diagnostic is non-null.
473 spv_result_t spvValidateBinary(const spv_const_context context,
474                                const uint32_t* words, const size_t num_words,
475                                spv_diagnostic* diagnostic);
476 
477 // Creates a diagnostic object. The position parameter specifies the location in
478 // the text/binary stream. The message parameter, copied into the diagnostic
479 // object, contains the error message to display.
480 spv_diagnostic spvDiagnosticCreate(const spv_position position,
481                                    const char* message);
482 
483 // Destroys a diagnostic object.  This is a no-op if diagnostic is a null
484 // pointer.
485 void spvDiagnosticDestroy(spv_diagnostic diagnostic);
486 
487 // Prints the diagnostic to stderr.
488 spv_result_t spvDiagnosticPrint(const spv_diagnostic diagnostic);
489 
490 // The binary parser interface.
491 
492 // A pointer to a function that accepts a parsed SPIR-V header.
493 // The integer arguments are the 32-bit words from the header, as specified
494 // in SPIR-V 1.0 Section 2.3 Table 1.
495 // The function should return SPV_SUCCESS if parsing should continue.
496 typedef spv_result_t (*spv_parsed_header_fn_t)(
497     void* user_data, spv_endianness_t endian, uint32_t magic, uint32_t version,
498     uint32_t generator, uint32_t id_bound, uint32_t reserved);
499 
500 // A pointer to a function that accepts a parsed SPIR-V instruction.
501 // The parsed_instruction value is transient: it may be overwritten
502 // or released immediately after the function has returned.  That also
503 // applies to the words array member of the parsed instruction.  The
504 // function should return SPV_SUCCESS if and only if parsing should
505 // continue.
506 typedef spv_result_t (*spv_parsed_instruction_fn_t)(
507     void* user_data, const spv_parsed_instruction_t* parsed_instruction);
508 
509 // Parses a SPIR-V binary, specified as counted sequence of 32-bit words.
510 // Parsing feedback is provided via two callbacks provided as function
511 // pointers.  Each callback function pointer can be a null pointer, in
512 // which case it is never called.  Otherwise, in a valid parse the
513 // parsed-header callback is called once, and then the parsed-instruction
514 // callback once for each instruction in the stream.  The user_data parameter
515 // is supplied as context to the callbacks.  Returns SPV_SUCCESS on successful
516 // parse where the callbacks always return SPV_SUCCESS.  For an invalid parse,
517 // returns a status code other than SPV_SUCCESS, and if diagnostic is non-null
518 // also emits a diagnostic.  If a callback returns anything other than
519 // SPV_SUCCESS, then that status code is returned, no further callbacks are
520 // issued, and no additional diagnostics are emitted.
521 spv_result_t spvBinaryParse(const spv_const_context context, void* user_data,
522                             const uint32_t* words, const size_t num_words,
523                             spv_parsed_header_fn_t parse_header,
524                             spv_parsed_instruction_fn_t parse_instruction,
525                             spv_diagnostic* diagnostic);
526 
527 #ifdef __cplusplus
528 }
529 #endif
530 
531 #endif  // SPIRV_TOOLS_LIBSPIRV_H_
532