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 SOURCE_TABLE_H_
16 #define SOURCE_TABLE_H_
17 
18 #include "source/extensions.h"
19 #include "source/latest_version_spirv_header.h"
20 #include "spirv-tools/libspirv.hpp"
21 
22 typedef struct spv_opcode_desc_t {
23   const char* name;
24   const SpvOp opcode;
25   const uint32_t numCapabilities;
26   const SpvCapability* capabilities;
27   // operandTypes[0..numTypes-1] describe logical operands for the instruction.
28   // The operand types include result id and result-type id, followed by
29   // the types of arguments.
30   const uint16_t numTypes;
31   spv_operand_type_t operandTypes[16];  // TODO: Smaller/larger?
32   const bool hasResult;  // Does the instruction have a result ID operand?
33   const bool hasType;    // Does the instruction have a type ID operand?
34   // A set of extensions that enable this feature. If empty then this operand
35   // value is in core and its availability is subject to minVersion. The
36   // assembler, binary parser, and disassembler ignore this rule, so you can
37   // freely process invalid modules.
38   const uint32_t numExtensions;
39   const spvtools::Extension* extensions;
40   // Minimal core SPIR-V version required for this feature, if without
41   // extensions. ~0u means reserved for future use. ~0u and non-empty extension
42   // lists means only available in extensions.
43   const uint32_t minVersion;
44   const uint32_t lastVersion;
45 } spv_opcode_desc_t;
46 
47 typedef struct spv_operand_desc_t {
48   const char* name;
49   const uint32_t value;
50   const uint32_t numCapabilities;
51   const SpvCapability* capabilities;
52   // A set of extensions that enable this feature. If empty then this operand
53   // value is in core and its availability is subject to minVersion. The
54   // assembler, binary parser, and disassembler ignore this rule, so you can
55   // freely process invalid modules.
56   const uint32_t numExtensions;
57   const spvtools::Extension* extensions;
58   const spv_operand_type_t operandTypes[16];  // TODO: Smaller/larger?
59   // Minimal core SPIR-V version required for this feature, if without
60   // extensions. ~0u means reserved for future use. ~0u and non-empty extension
61   // lists means only available in extensions.
62   const uint32_t minVersion;
63   const uint32_t lastVersion;
64 } spv_operand_desc_t;
65 
66 typedef struct spv_operand_desc_group_t {
67   const spv_operand_type_t type;
68   const uint32_t count;
69   const spv_operand_desc_t* entries;
70 } spv_operand_desc_group_t;
71 
72 typedef struct spv_ext_inst_desc_t {
73   const char* name;
74   const uint32_t ext_inst;
75   const uint32_t numCapabilities;
76   const SpvCapability* capabilities;
77   const spv_operand_type_t operandTypes[16];  // TODO: Smaller/larger?
78 } spv_ext_inst_desc_t;
79 
80 typedef struct spv_ext_inst_group_t {
81   const spv_ext_inst_type_t type;
82   const uint32_t count;
83   const spv_ext_inst_desc_t* entries;
84 } spv_ext_inst_group_t;
85 
86 typedef struct spv_opcode_table_t {
87   const uint32_t count;
88   const spv_opcode_desc_t* entries;
89 } spv_opcode_table_t;
90 
91 typedef struct spv_operand_table_t {
92   const uint32_t count;
93   const spv_operand_desc_group_t* types;
94 } spv_operand_table_t;
95 
96 typedef struct spv_ext_inst_table_t {
97   const uint32_t count;
98   const spv_ext_inst_group_t* groups;
99 } spv_ext_inst_table_t;
100 
101 typedef const spv_opcode_desc_t* spv_opcode_desc;
102 typedef const spv_operand_desc_t* spv_operand_desc;
103 typedef const spv_ext_inst_desc_t* spv_ext_inst_desc;
104 
105 typedef const spv_opcode_table_t* spv_opcode_table;
106 typedef const spv_operand_table_t* spv_operand_table;
107 typedef const spv_ext_inst_table_t* spv_ext_inst_table;
108 
109 struct spv_context_t {
110   const spv_target_env target_env;
111   const spv_opcode_table opcode_table;
112   const spv_operand_table operand_table;
113   const spv_ext_inst_table ext_inst_table;
114   spvtools::MessageConsumer consumer;
115 };
116 
117 namespace spvtools {
118 
119 // Sets the message consumer to |consumer| in the given |context|. The original
120 // message consumer will be overwritten.
121 void SetContextMessageConsumer(spv_context context, MessageConsumer consumer);
122 }  // namespace spvtools
123 
124 // Populates *table with entries for env.
125 spv_result_t spvOpcodeTableGet(spv_opcode_table* table, spv_target_env env);
126 
127 // Populates *table with entries for env.
128 spv_result_t spvOperandTableGet(spv_operand_table* table, spv_target_env env);
129 
130 // Populates *table with entries for env.
131 spv_result_t spvExtInstTableGet(spv_ext_inst_table* table, spv_target_env env);
132 
133 #endif  // SOURCE_TABLE_H_
134