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