blob: c56b0cf599c7a5b187152483960078f9c13155de [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.
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010040spv_result_t spvOpcodeTableNameLookup(const spv_opcode_table table,
Lei Zhangaf9906e2015-11-16 10:48:43 -050041 const char* name, spv_opcode_desc* entry);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010042
Lei Zhangaf9906e2015-11-16 10:48:43 -050043// Finds the opcode by enumerant in the given opcode table. On success, returns
44// SPV_SUCCESS and writes a handle of the table entry into *entry.
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010045spv_result_t spvOpcodeTableValueLookup(const spv_opcode_table table,
Lei Zhangb36e7042015-10-28 13:40:52 -040046 const SpvOp opcode,
Lei Zhangaf9906e2015-11-16 10:48:43 -050047 spv_opcode_desc* entry);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010048
Lei Zhangaf9906e2015-11-16 10:48:43 -050049// Copies an instruction's word and fixes the endianness to host native. The
50// source instruction's stream/opcode/endianness is in the words/opcode/endian
51// parameter. The word_count parameter specifies the number of words to copy.
52// Writes copied instruction into *inst.
Lei Zhang1a0334e2015-11-02 09:41:20 -050053void spvInstructionCopy(const uint32_t* words, const SpvOp opcode,
Lei Zhangaf9906e2015-11-16 10:48:43 -050054 const uint16_t word_count,
55 const spv_endianness_t endian, spv_instruction_t* inst);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010056
Lei Zhangaf9906e2015-11-16 10:48:43 -050057// Gets the name of an instruction, without the "Op" prefix.
Lei Zhang1a0334e2015-11-02 09:41:20 -050058const char* spvOpcodeString(const SpvOp opcode);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010059
Lei Zhangaf9906e2015-11-16 10:48:43 -050060// Determine if the given opcode is a scalar type. Returns zero if false,
61// non-zero otherwise.
Lei Zhangb36e7042015-10-28 13:40:52 -040062int32_t spvOpcodeIsScalarType(const SpvOp opcode);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010063
Lei Zhangaf9906e2015-11-16 10:48:43 -050064// Determines if the given opcode is a constant. Returns zero if false, non-zero
65// otherwise.
Lei Zhangb36e7042015-10-28 13:40:52 -040066int32_t spvOpcodeIsConstant(const SpvOp opcode);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010067
David Neto1f3fb502016-09-14 11:57:20 -040068// Returns true if the given opcode is a constant or undef.
69bool spvOpcodeIsConstantOrUndef(const SpvOp opcode);
70
Aliya Pazylbekovaedb52642017-02-24 20:43:28 -050071// Returns true if the given opcode is a scalar specialization constant.
72bool spvOpcodeIsScalarSpecConstant(const SpvOp opcode);
73
Lei Zhangaf9906e2015-11-16 10:48:43 -050074// Determines if the given opcode is a composite type. Returns zero if false,
75// non-zero otherwise.
Lei Zhangb36e7042015-10-28 13:40:52 -040076int32_t spvOpcodeIsComposite(const SpvOp opcode);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010077
Florian Ziesche66fcb452016-03-02 22:17:54 +010078// Determines if the given opcode results in a pointer when using the logical
79// addressing model. Returns zero if false, non-zero otherwise.
80int32_t spvOpcodeReturnsLogicalPointer(const SpvOp opcode);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010081
Ehsan Nasiri23af06c2017-02-01 15:37:39 -050082// Returns whether the given opcode could result in a pointer or a variable
83// pointer when using the logical addressing model.
84bool spvOpcodeReturnsLogicalVariablePointer(const SpvOp opcode);
85
Lei Zhangaf9906e2015-11-16 10:48:43 -050086// Determines if the given opcode generates a type. Returns zero if false,
87// non-zero otherwise.
88int32_t spvOpcodeGeneratesType(SpvOp opcode);
Andrew Woloszyn537e7762015-09-29 11:28:34 -040089
Steven Perron28c41552017-11-10 20:26:55 -050090// Returns true if the opcode adds a decoration to an id.
Steven Perroneb4653a2017-11-13 15:31:43 -050091bool spvOpcodeIsDecoration(const SpvOp opcode);
92
Steven Perron28c41552017-11-10 20:26:55 -050093// Returns true if the opcode is a load from memory into a result id. This
94// function only considers core instructions.
95bool spvOpcodeIsLoad(const SpvOp opcode);
96
97// Returns true if the opcode is an atomic operation.
98bool spvOpcodeIsAtomicOp(const SpvOp opcode);
99
Diego Novillo74327842017-11-17 08:59:25 -0500100// Returns true if the given opcode is a branch instruction.
101bool spvOpcodeIsBranch(SpvOp opcode);
102
103// Returns true if the given opcode is a return instruction.
104bool spvOpcodeIsReturn(SpvOp opcode);
105
Diego Novillo5f100782018-01-03 15:25:03 -0500106// Returns true if the given opcode is a return instruction or it aborts
107// execution.
108bool spvOpcodeIsReturnOrAbort(SpvOp opcode);
109
Diego Novillo74327842017-11-17 08:59:25 -0500110// Returns true if the given opcode is a basic block terminator.
111bool spvOpcodeIsBlockTerminator(SpvOp opcode);
112
Steven Perron79a00642017-12-11 13:10:24 -0500113// Returns true if the given opcode always defines an opaque type.
114bool spvOpcodeIsBaseOpaqueType(SpvOp opcode);
David Neto9f79d782015-10-27 16:27:05 -0400115#endif // LIBSPIRV_OPCODE_H_