blob: 19972e95194e0d77a792f7a63e3d3e5357fcc322 [file] [log] [blame]
Dejan Mircevskib6fe02f2016-01-07 13:44:22 -05001// Copyright (c) 2015-2016 The Khronos Group Inc.
Andrew Woloszyn71fc0552015-09-24 10:26:51 -04002//
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
Andrew Woloszyn71fc0552015-09-24 10:26:51 -04006//
David Neto9fc86582016-09-01 15:33:59 -04007// http://www.apache.org/licenses/LICENSE-2.0
Andrew Woloszyn71fc0552015-09-24 10:26:51 -04008//
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.
Andrew Woloszyn71fc0552015-09-24 10:26:51 -040014
dan sinclair58a68762018-08-03 08:05:33 -040015#ifndef SOURCE_TEXT_HANDLER_H_
16#define SOURCE_TEXT_HANDLER_H_
Andrew Woloszyn71fc0552015-09-24 10:26:51 -040017
David Neto62741202015-10-13 15:51:12 -040018#include <iomanip>
dan sinclaireda2cfb2018-08-03 15:06:09 -040019#include <set>
David Neto62741202015-10-13 15:51:12 -040020#include <sstream>
dan sinclaireda2cfb2018-08-03 15:06:09 -040021#include <string>
David Neto62741202015-10-13 15:51:12 -040022#include <type_traits>
Andrew Woloszyn71fc0552015-09-24 10:26:51 -040023#include <unordered_map>
dan sinclaireda2cfb2018-08-03 15:06:09 -040024#include <utility>
Andrew Woloszyn71fc0552015-09-24 10:26:51 -040025
dan sinclaireda2cfb2018-08-03 15:06:09 -040026#include "source/diagnostic.h"
27#include "source/instruction.h"
dan sinclaireda2cfb2018-08-03 15:06:09 -040028#include "source/text.h"
David Neto5a703352016-02-17 14:44:00 -050029#include "spirv-tools/libspirv.h"
Andrew Woloszyn71fc0552015-09-24 10:26:51 -040030
dan sinclair3dad1cd2018-07-07 09:38:00 -040031namespace spvtools {
32
Andrew Woloszyn71fc0552015-09-24 10:26:51 -040033// Structures
34
Andrew Woloszyn71fc0552015-09-24 10:26:51 -040035// This is a lattice for tracking types.
36enum class IdTypeClass {
Lei Zhang1a0334e2015-11-02 09:41:20 -050037 kBottom = 0, // We have no information yet.
Andrew Woloszyn71fc0552015-09-24 10:26:51 -040038 kScalarIntegerType,
39 kScalarFloatType,
40 kOtherType
41};
42
Andrew Woloszyn71fc0552015-09-24 10:26:51 -040043// Contains ID type information that needs to be tracked across all Ids.
44// Bitwidth is only valid when type_class is kScalarIntegerType or
45// kScalarFloatType.
46struct IdType {
47 uint32_t bitwidth; // Safe to assume that we will not have > 2^32 bits.
Lei Zhang1a0334e2015-11-02 09:41:20 -050048 bool isSigned; // This is only significant if type_class is integral.
Andrew Woloszyn71fc0552015-09-24 10:26:51 -040049 IdTypeClass type_class;
50};
51
Andrew Woloszyn157e41b2015-10-16 15:11:00 -040052// Default equality operator for IdType. Tests if all members are the same.
Lei Zhang1a0334e2015-11-02 09:41:20 -050053inline bool operator==(const IdType& first, const IdType& second) {
Andrew Woloszyn157e41b2015-10-16 15:11:00 -040054 return (first.bitwidth == second.bitwidth) &&
55 (first.isSigned == second.isSigned) &&
56 (first.type_class == second.type_class);
57}
58
59// Tests whether any member of the IdTypes do not match.
Lei Zhang1a0334e2015-11-02 09:41:20 -050060inline bool operator!=(const IdType& first, const IdType& second) {
Andrew Woloszyn157e41b2015-10-16 15:11:00 -040061 return !(first == second);
62}
63
David Neto78e677b2015-10-05 13:28:46 -040064// A value representing an unknown type.
65extern const IdType kUnknownType;
66
Andrew Woloszyn537e7762015-09-29 11:28:34 -040067// Returns true if the type is a scalar integer type.
68inline bool isScalarIntegral(const IdType& type) {
69 return type.type_class == IdTypeClass::kScalarIntegerType;
70}
71
72// Returns true if the type is a scalar floating point type.
73inline bool isScalarFloating(const IdType& type) {
74 return type.type_class == IdTypeClass::kScalarFloatType;
75}
76
David Neto78e677b2015-10-05 13:28:46 -040077// Returns the number of bits in the type.
78// This is only valid for bottom, scalar integer, and scalar floating
79// classes. For bottom, assume 32 bits.
80inline int assumedBitWidth(const IdType& type) {
Lei Zhang1a0334e2015-11-02 09:41:20 -050081 switch (type.type_class) {
David Neto78e677b2015-10-05 13:28:46 -040082 case IdTypeClass::kBottom:
83 return 32;
84 case IdTypeClass::kScalarIntegerType:
85 case IdTypeClass::kScalarFloatType:
86 return type.bitwidth;
87 default:
88 break;
89 }
90 // We don't care about this case.
91 return 0;
92}
93
David Neto9e545d72015-11-06 18:08:49 -050094// A templated class with a static member function Clamp, where Clamp
95// sets a referenced value of type T to 0 if T is an unsigned
96// integer type, and returns true if it modified the referenced
97// value.
98template <typename T, typename = void>
99class ClampToZeroIfUnsignedType {
100 public:
101 // The default specialization does not clamp the value.
102 static bool Clamp(T*) { return false; }
103};
104
105// The specialization of ClampToZeroIfUnsignedType for unsigned integer
106// types.
107template <typename T>
108class ClampToZeroIfUnsignedType<
109 T, typename std::enable_if<std::is_unsigned<T>::value>::type> {
110 public:
111 static bool Clamp(T* value_pointer) {
112 if (*value_pointer) {
113 *value_pointer = 0;
114 return true;
115 }
116 return false;
117 }
118};
119
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400120// Encapsulates the data used during the assembly of a SPIR-V module.
121class AssemblyContext {
122 public:
dan sinclair3dad1cd2018-07-07 09:38:00 -0400123 AssemblyContext(spv_text text, const MessageConsumer& consumer,
Andrey Tuganovb173d1c2017-04-11 19:46:15 -0400124 std::set<uint32_t>&& ids_to_preserve = std::set<uint32_t>())
Diego Novillod2938e42017-11-08 12:40:02 -0500125 : current_position_({}),
126 consumer_(consumer),
127 text_(text),
128 bound_(1),
129 next_id_(1),
130 ids_to_preserve_(std::move(ids_to_preserve)) {}
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400131
132 // Assigns a new integer value to the given text ID, or returns the previously
133 // assigned integer value if the ID has been seen before.
Lei Zhang1a0334e2015-11-02 09:41:20 -0500134 uint32_t spvNamedIdAssignOrGet(const char* textValue);
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400135
136 // Returns the largest largest numeric ID that has been assigned.
137 uint32_t getBound() const;
138
139 // Advances position to point to the next word in the input stream.
140 // Returns SPV_SUCCESS on success.
141 spv_result_t advance();
142
Lei Zhang6032b982016-03-15 16:52:40 -0400143 // Sets word to the next word in the input text. Fills next_position with
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400144 // the next location past the end of the word.
Lei Zhang6032b982016-03-15 16:52:40 -0400145 spv_result_t getWord(std::string* word, spv_position next_position);
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400146
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400147 // Returns true if the next word in the input is the start of a new Opcode.
148 bool startsWithOp();
149
150 // Returns true if the next word in the input is the start of a new
151 // instruction.
152 bool isStartOfNewInst();
153
154 // Returns a diagnostic object initialized with current position in the input
David Netoac508b02015-10-09 15:48:09 -0400155 // stream, and for the given error code. Any data written to this object will
156 // show up in pDiagnsotic on destruction.
157 DiagnosticStream diagnostic(spv_result_t error) {
dan sinclairf80696e2018-06-19 16:02:44 -0400158 return DiagnosticStream(current_position_, consumer_, "", error);
David Netoac508b02015-10-09 15:48:09 -0400159 }
160
161 // Returns a diagnostic object with the default assembly error code.
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400162 DiagnosticStream diagnostic() {
David Netoac508b02015-10-09 15:48:09 -0400163 // The default failure for assembly is invalid text.
164 return diagnostic(SPV_ERROR_INVALID_TEXT);
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400165 }
166
Lei Zhangacf72872015-11-13 11:00:10 -0500167 // Returns then next character in the input stream.
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400168 char peek() const;
169
170 // Returns true if there is more text in the input stream.
171 bool hasText() const;
172
173 // Seeks the input stream forward by 'size' characters.
174 void seekForward(uint32_t size);
175
176 // Sets the current position in the input stream to the given position.
Lei Zhang1a0334e2015-11-02 09:41:20 -0500177 void setPosition(const spv_position_t& newPosition) {
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400178 current_position_ = newPosition;
179 }
180
181 // Returns the current position in the input stream.
Lei Zhang1a0334e2015-11-02 09:41:20 -0500182 const spv_position_t& position() const { return current_position_; }
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400183
184 // Appends the given 32-bit value to the given instruction.
David Neto78e677b2015-10-05 13:28:46 -0400185 // Returns SPV_SUCCESS if the value could be correctly inserted in the
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400186 // instruction.
Lei Zhang1a0334e2015-11-02 09:41:20 -0500187 spv_result_t binaryEncodeU32(const uint32_t value, spv_instruction_t* pInst);
David Neto78e677b2015-10-05 13:28:46 -0400188
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400189 // Appends the given string to the given instruction.
David Neto78e677b2015-10-05 13:28:46 -0400190 // Returns SPV_SUCCESS if the value could be correctly inserted in the
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400191 // instruction.
Lei Zhang1a0334e2015-11-02 09:41:20 -0500192 spv_result_t binaryEncodeString(const char* value, spv_instruction_t* pInst);
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400193
David Neto78e677b2015-10-05 13:28:46 -0400194 // Appends the given numeric literal to the given instruction.
195 // Validates and respects the bitwidth supplied in the IdType argument.
196 // If the type is of class kBottom the value will be encoded as a
197 // 32-bit integer.
198 // Returns SPV_SUCCESS if the value could be correctly added to the
David Neto51013d12015-10-14 11:31:51 -0400199 // instruction. Returns the given error code on failure, and emits
Lei Zhangacf72872015-11-13 11:00:10 -0500200 // a diagnostic if that error code is not SPV_FAILED_MATCH.
Lei Zhang1a0334e2015-11-02 09:41:20 -0500201 spv_result_t binaryEncodeNumericLiteral(const char* numeric_literal,
David Neto51013d12015-10-14 11:31:51 -0400202 spv_result_t error_code,
Lei Zhang1a0334e2015-11-02 09:41:20 -0500203 const IdType& type,
204 spv_instruction_t* pInst);
David Neto78e677b2015-10-05 13:28:46 -0400205
Andrew Woloszyn537e7762015-09-29 11:28:34 -0400206 // Returns the IdType associated with this type-generating value.
207 // If the type has not been previously recorded with recordTypeDefinition,
David Neto78e677b2015-10-05 13:28:46 -0400208 // kUnknownType will be returned.
Andrew Woloszyn537e7762015-09-29 11:28:34 -0400209 IdType getTypeOfTypeGeneratingValue(uint32_t value) const;
210
211 // Returns the IdType that represents the return value of this Value
212 // generating instruction.
213 // If the value has not been recorded with recordTypeIdForValue, or the type
David Neto78e677b2015-10-05 13:28:46 -0400214 // could not be determined kUnknownType will be returned.
Andrew Woloszyn537e7762015-09-29 11:28:34 -0400215 IdType getTypeOfValueInstruction(uint32_t value) const;
216
217 // Tracks the type-defining instruction. The result of the tracking can
218 // later be queried using getValueType.
219 // pInst is expected to be completely filled in by the time this instruction
220 // is called.
221 // Returns SPV_SUCCESS on success, or SPV_ERROR_INVALID_VALUE on error.
222 spv_result_t recordTypeDefinition(const spv_instruction_t* pInst);
223
224 // Tracks the relationship between the value and its type.
225 spv_result_t recordTypeIdForValue(uint32_t value, uint32_t type);
226
David Neto2ae4a682015-11-09 18:55:42 -0500227 // Records the given Id as being the import of the given extended instruction
228 // type.
229 spv_result_t recordIdAsExtInstImport(uint32_t id, spv_ext_inst_type_t type);
230
231 // Returns the extended instruction type corresponding to the import with
232 // the given Id, if it exists. Returns SPV_EXT_INST_TYPE_NONE if the
233 // id is not the id for an extended instruction type.
234 spv_ext_inst_type_t getExtInstTypeForId(uint32_t id) const;
235
Andrey Tuganovb173d1c2017-04-11 19:46:15 -0400236 // Returns a set consisting of each ID generated by spvNamedIdAssignOrGet from
237 // a numeric ID text representation. For example, generated from "%12" but not
238 // from "%foo".
239 std::set<uint32_t> GetNumericIds() const;
240
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400241 private:
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400242 // Maps ID names to their corresponding numerical ids.
243 using spv_named_id_table = std::unordered_map<std::string, uint32_t>;
244 // Maps type-defining IDs to their IdType.
245 using spv_id_to_type_map = std::unordered_map<uint32_t, IdType>;
246 // Maps Ids to the id of their type.
247 using spv_id_to_type_id = std::unordered_map<uint32_t, uint32_t>;
248
249 spv_named_id_table named_ids_;
250 spv_id_to_type_map types_;
251 spv_id_to_type_id value_types_;
David Neto2ae4a682015-11-09 18:55:42 -0500252 // Maps an extended instruction import Id to the extended instruction type.
253 std::unordered_map<uint32_t, spv_ext_inst_type_t> import_id_to_ext_inst_type_;
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400254 spv_position_t current_position_;
dan sinclair3dad1cd2018-07-07 09:38:00 -0400255 MessageConsumer consumer_;
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400256 spv_text text_;
257 uint32_t bound_;
Andrey Tuganovb173d1c2017-04-11 19:46:15 -0400258 uint32_t next_id_;
259 std::set<uint32_t> ids_to_preserve_;
Andrew Woloszyn71fc0552015-09-24 10:26:51 -0400260};
dan sinclair3dad1cd2018-07-07 09:38:00 -0400261
262} // namespace spvtools
263
dan sinclair58a68762018-08-03 08:05:33 -0400264#endif // SOURCE_TEXT_HANDLER_H_