blob: a725436827e715c43f5969268853e44d172c2af8 [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_OPCODE_H_
28#define LIBSPIRV_OPCODE_H_
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010029
David Netob5dc8fc2015-10-06 16:22:00 -040030#include "instruction.h"
Lei Zhang923f6c12015-11-11 12:45:23 -050031#include "libspirv/libspirv.h"
Lei Zhang7a222e42015-11-11 12:40:25 -050032#include "table.h"
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010033
Lei Zhangaf9906e2015-11-16 10:48:43 -050034// Returns the name of a registered SPIR-V generator as a null-terminated
35// string. If the generator is not known, then returns the string "Unknown".
36// The generator parameter should be most significant 16-bits of the generator
37// word in the SPIR-V module header.
38//
39// See the registry at https://www.khronos.org/registry/spir-v/api/spir-v.xml.
Lei Zhang1a0334e2015-11-02 09:41:20 -050040const char* spvGeneratorStr(uint32_t generator);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010041
Lei Zhangaf9906e2015-11-16 10:48:43 -050042// Combines word_count and opcode enumerant in single word.
43uint32_t spvOpcodeMake(uint16_t word_count, SpvOp opcode);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010044
Lei Zhangaf9906e2015-11-16 10:48:43 -050045// Splits word into into two constituent parts: word_count and opcode.
46void spvOpcodeSplit(const uint32_t word, uint16_t* word_count, SpvOp* opcode);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010047
Lei Zhangaf9906e2015-11-16 10:48:43 -050048// Finds the named opcode in the given opcode table. On success, returns
49// SPV_SUCCESS and writes a handle of the table entry into *entry.
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010050spv_result_t spvOpcodeTableNameLookup(const spv_opcode_table table,
Lei Zhangaf9906e2015-11-16 10:48:43 -050051 const char* name, spv_opcode_desc* entry);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010052
Lei Zhangaf9906e2015-11-16 10:48:43 -050053// Finds the opcode by enumerant in the given opcode table. On success, returns
54// SPV_SUCCESS and writes a handle of the table entry into *entry.
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010055spv_result_t spvOpcodeTableValueLookup(const spv_opcode_table table,
Lei Zhangb36e7042015-10-28 13:40:52 -040056 const SpvOp opcode,
Lei Zhangaf9906e2015-11-16 10:48:43 -050057 spv_opcode_desc* entry);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010058
Lei Zhangaf9906e2015-11-16 10:48:43 -050059// Determines if the opcode has capability requirements. Returns zero if false,
60// non-zero otherwise. This function does not check if the given entry is valid.
61int32_t spvOpcodeRequiresCapabilities(spv_opcode_desc opcode);
Lei Zhangdfc50082015-08-21 11:50:55 -040062
Lei Zhangaf9906e2015-11-16 10:48:43 -050063// Copies an instruction's word and fixes the endianness to host native. The
64// source instruction's stream/opcode/endianness is in the words/opcode/endian
65// parameter. The word_count parameter specifies the number of words to copy.
66// Writes copied instruction into *inst.
Lei Zhang1a0334e2015-11-02 09:41:20 -050067void spvInstructionCopy(const uint32_t* words, const SpvOp opcode,
Lei Zhangaf9906e2015-11-16 10:48:43 -050068 const uint16_t word_count,
69 const spv_endianness_t endian, spv_instruction_t* inst);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010070
Lei Zhangaf9906e2015-11-16 10:48:43 -050071// Gets the name of an instruction, without the "Op" prefix.
Lei Zhang1a0334e2015-11-02 09:41:20 -050072const char* spvOpcodeString(const SpvOp opcode);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010073
Lei Zhangaf9906e2015-11-16 10:48:43 -050074// Determine if the given opcode is a scalar type. Returns zero if false,
75// non-zero otherwise.
Lei Zhangb36e7042015-10-28 13:40:52 -040076int32_t spvOpcodeIsScalarType(const SpvOp opcode);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010077
Lei Zhangaf9906e2015-11-16 10:48:43 -050078// Determines if the given opcode is a constant. Returns zero if false, non-zero
79// otherwise.
Lei Zhangb36e7042015-10-28 13:40:52 -040080int32_t spvOpcodeIsConstant(const SpvOp opcode);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010081
Lei Zhangaf9906e2015-11-16 10:48:43 -050082// Determines if the given opcode is a composite type. Returns zero if false,
83// non-zero otherwise.
Lei Zhangb36e7042015-10-28 13:40:52 -040084int32_t spvOpcodeIsComposite(const SpvOp opcode);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010085
Lei Zhangaf9906e2015-11-16 10:48:43 -050086// Deep equal comparison of type declaration instructions. Returns zero if
87// false, non-zero otherwise.
88int32_t spvOpcodeAreTypesEqual(const spv_instruction_t* type_inst0,
89 const spv_instruction_t* type_inst1);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010090
Lei Zhangaf9906e2015-11-16 10:48:43 -050091// Determines if the given opcode results in a pointer. Returns zero if false,
92// non-zero otherwise.
Lei Zhangb36e7042015-10-28 13:40:52 -040093int32_t spvOpcodeIsPointer(const SpvOp opcode);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010094
Lei Zhangaf9906e2015-11-16 10:48:43 -050095// Determines if the given opcode results in an instantation of a non-void type.
96// Returns zero if false, non-zero otherwise.
Lei Zhangb36e7042015-10-28 13:40:52 -040097int32_t spvOpcodeIsObject(const SpvOp opcode);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010098
Lei Zhangaf9906e2015-11-16 10:48:43 -050099// Determines if the scalar type opcode is nullable. Returns zero if false,
100// non-zero otherwise.
Lei Zhangb36e7042015-10-28 13:40:52 -0400101int32_t spvOpcodeIsBasicTypeNullable(SpvOp opcode);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100102
Lei Zhangaf9906e2015-11-16 10:48:43 -0500103// Determines if an instruction is in a basic block. The first_inst parameter
104// specifies the first instruction in the stream, while the inst parameter
105// specifies the current instruction. Returns zero if false, non-zero otherwise.
106int32_t spvInstructionIsInBasicBlock(const spv_instruction_t* first_inst,
107 const spv_instruction_t* inst);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100108
Lei Zhangaf9906e2015-11-16 10:48:43 -0500109// Determines if the given opcode contains a value. Returns zero if false,
110// non-zero otherwise.
Lei Zhangb36e7042015-10-28 13:40:52 -0400111int32_t spvOpcodeIsValue(SpvOp opcode);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100112
Lei Zhangaf9906e2015-11-16 10:48:43 -0500113// Determines if the given opcode generates a type. Returns zero if false,
114// non-zero otherwise.
115int32_t spvOpcodeGeneratesType(SpvOp opcode);
Andrew Woloszyn537e7762015-09-29 11:28:34 -0400116
David Neto9f79d782015-10-27 16:27:05 -0400117#endif // LIBSPIRV_OPCODE_H_