blob: 9dc9013b4b6b65b779e2c576073c13a024b5ddfd [file] [log] [blame]
Dejan Mircevskib6fe02f2016-01-07 13:44:22 -05001// Copyright (c) 2015-2016 The Khronos Group Inc.
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +01002//
David Neto9fc86582016-09-01 15:33:59 -04003// 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
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +01006//
David Neto9fc86582016-09-01 15:33:59 -04007// http://www.apache.org/licenses/LICENSE-2.0
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +01008//
David Neto9fc86582016-09-01 15:33:59 -04009// 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.
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010014
David Neto9f79d782015-10-27 16:27:05 -040015#ifndef LIBSPIRV_OPCODE_H_
16#define LIBSPIRV_OPCODE_H_
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010017
David Netob5dc8fc2015-10-06 16:22:00 -040018#include "instruction.h"
Andrey Tuganovaf7d5792017-12-13 17:55:00 -050019#include "latest_version_spirv_header.h"
David Neto5a703352016-02-17 14:44:00 -050020#include "spirv-tools/libspirv.h"
Lei Zhang7a222e42015-11-11 12:40:25 -050021#include "table.h"
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010022
Lei Zhangaf9906e2015-11-16 10:48:43 -050023// Returns the name of a registered SPIR-V generator as a null-terminated
24// string. If the generator is not known, then returns the string "Unknown".
25// The generator parameter should be most significant 16-bits of the generator
26// word in the SPIR-V module header.
27//
28// See the registry at https://www.khronos.org/registry/spir-v/api/spir-v.xml.
Lei Zhang1a0334e2015-11-02 09:41:20 -050029const char* spvGeneratorStr(uint32_t generator);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010030
Lei Zhangaf9906e2015-11-16 10:48:43 -050031// Combines word_count and opcode enumerant in single word.
32uint32_t spvOpcodeMake(uint16_t word_count, SpvOp opcode);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010033
Lei Zhangaf9906e2015-11-16 10:48:43 -050034// Splits word into into two constituent parts: word_count and opcode.
Lei Zhang6fa3f8a2016-03-31 17:26:31 -040035void spvOpcodeSplit(const uint32_t word, uint16_t* word_count,
36 uint16_t* opcode);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010037
Lei Zhangaf9906e2015-11-16 10:48:43 -050038// Finds the named opcode in the given opcode table. On success, returns
39// SPV_SUCCESS and writes a handle of the table entry into *entry.
Lei Zhang1ef6b192018-03-14 13:06:18 -040040spv_result_t spvOpcodeTableNameLookup(spv_target_env,
41 const spv_opcode_table table,
Lei Zhangaf9906e2015-11-16 10:48:43 -050042 const char* name, spv_opcode_desc* entry);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010043
Lei Zhangaf9906e2015-11-16 10:48:43 -050044// Finds the opcode by enumerant in the given opcode table. On success, returns
45// SPV_SUCCESS and writes a handle of the table entry into *entry.
Lei Zhang1ef6b192018-03-14 13:06:18 -040046spv_result_t spvOpcodeTableValueLookup(spv_target_env,
47 const spv_opcode_table table,
Lei Zhangb36e7042015-10-28 13:40:52 -040048 const SpvOp opcode,
Lei Zhangaf9906e2015-11-16 10:48:43 -050049 spv_opcode_desc* entry);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010050
Lei Zhangaf9906e2015-11-16 10:48:43 -050051// Copies an instruction's word and fixes the endianness to host native. The
52// source instruction's stream/opcode/endianness is in the words/opcode/endian
53// parameter. The word_count parameter specifies the number of words to copy.
54// Writes copied instruction into *inst.
Lei Zhang1a0334e2015-11-02 09:41:20 -050055void spvInstructionCopy(const uint32_t* words, const SpvOp opcode,
Lei Zhangaf9906e2015-11-16 10:48:43 -050056 const uint16_t word_count,
57 const spv_endianness_t endian, spv_instruction_t* inst);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010058
Lei Zhangaf9906e2015-11-16 10:48:43 -050059// Gets the name of an instruction, without the "Op" prefix.
Lei Zhang1a0334e2015-11-02 09:41:20 -050060const char* spvOpcodeString(const SpvOp opcode);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010061
Lei Zhangaf9906e2015-11-16 10:48:43 -050062// Determine if the given opcode is a scalar type. Returns zero if false,
63// non-zero otherwise.
Lei Zhangb36e7042015-10-28 13:40:52 -040064int32_t spvOpcodeIsScalarType(const SpvOp opcode);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010065
Lei Zhangaf9906e2015-11-16 10:48:43 -050066// Determines if the given opcode is a constant. Returns zero if false, non-zero
67// otherwise.
Lei Zhangb36e7042015-10-28 13:40:52 -040068int32_t spvOpcodeIsConstant(const SpvOp opcode);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010069
David Neto1f3fb502016-09-14 11:57:20 -040070// Returns true if the given opcode is a constant or undef.
71bool spvOpcodeIsConstantOrUndef(const SpvOp opcode);
72
Aliya Pazylbekovaedb52642017-02-24 20:43:28 -050073// Returns true if the given opcode is a scalar specialization constant.
74bool spvOpcodeIsScalarSpecConstant(const SpvOp opcode);
75
Lei Zhangaf9906e2015-11-16 10:48:43 -050076// Determines if the given opcode is a composite type. Returns zero if false,
77// non-zero otherwise.
Lei Zhangb36e7042015-10-28 13:40:52 -040078int32_t spvOpcodeIsComposite(const SpvOp opcode);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010079
Florian Ziesche66fcb452016-03-02 22:17:54 +010080// Determines if the given opcode results in a pointer when using the logical
81// addressing model. Returns zero if false, non-zero otherwise.
82int32_t spvOpcodeReturnsLogicalPointer(const SpvOp opcode);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010083
Ehsan Nasiri23af06c2017-02-01 15:37:39 -050084// Returns whether the given opcode could result in a pointer or a variable
85// pointer when using the logical addressing model.
86bool spvOpcodeReturnsLogicalVariablePointer(const SpvOp opcode);
87
Lei Zhangaf9906e2015-11-16 10:48:43 -050088// Determines if the given opcode generates a type. Returns zero if false,
89// non-zero otherwise.
90int32_t spvOpcodeGeneratesType(SpvOp opcode);
Andrew Woloszyn537e7762015-09-29 11:28:34 -040091
Steven Perron28c41552017-11-10 20:26:55 -050092// Returns true if the opcode adds a decoration to an id.
Steven Perroneb4653a2017-11-13 15:31:43 -050093bool spvOpcodeIsDecoration(const SpvOp opcode);
94
Steven Perron28c41552017-11-10 20:26:55 -050095// Returns true if the opcode is a load from memory into a result id. This
96// function only considers core instructions.
97bool spvOpcodeIsLoad(const SpvOp opcode);
98
99// Returns true if the opcode is an atomic operation.
100bool spvOpcodeIsAtomicOp(const SpvOp opcode);
101
Diego Novillo74327842017-11-17 08:59:25 -0500102// Returns true if the given opcode is a branch instruction.
103bool spvOpcodeIsBranch(SpvOp opcode);
104
105// Returns true if the given opcode is a return instruction.
106bool spvOpcodeIsReturn(SpvOp opcode);
107
Diego Novillo5f100782018-01-03 15:25:03 -0500108// Returns true if the given opcode is a return instruction or it aborts
109// execution.
110bool spvOpcodeIsReturnOrAbort(SpvOp opcode);
111
Diego Novillo74327842017-11-17 08:59:25 -0500112// Returns true if the given opcode is a basic block terminator.
113bool spvOpcodeIsBlockTerminator(SpvOp opcode);
114
Steven Perron79a00642017-12-11 13:10:24 -0500115// Returns true if the given opcode always defines an opaque type.
116bool spvOpcodeIsBaseOpaqueType(SpvOp opcode);
David Neto9f79d782015-10-27 16:27:05 -0400117#endif // LIBSPIRV_OPCODE_H_