File: | clang/utils/TableGen/MveEmitter.cpp |
Warning: | line 1405, column 23 Division by zero |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
1 | //===- MveEmitter.cpp - Generate arm_mve.h for use with clang -*- C++ -*-=====// | ||||||
2 | // | ||||||
3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||||||
4 | // See https://llvm.org/LICENSE.txt for license information. | ||||||
5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||||||
6 | // | ||||||
7 | //===----------------------------------------------------------------------===// | ||||||
8 | // | ||||||
9 | // This set of linked tablegen backends is responsible for emitting the bits | ||||||
10 | // and pieces that implement <arm_mve.h>, which is defined by the ACLE standard | ||||||
11 | // and provides a set of types and functions for (more or less) direct access | ||||||
12 | // to the MVE instruction set, including the scalar shifts as well as the | ||||||
13 | // vector instructions. | ||||||
14 | // | ||||||
15 | // MVE's standard intrinsic functions are unusual in that they have a system of | ||||||
16 | // polymorphism. For example, the function vaddq() can behave like vaddq_u16(), | ||||||
17 | // vaddq_f32(), vaddq_s8(), etc., depending on the types of the vector | ||||||
18 | // arguments you give it. | ||||||
19 | // | ||||||
20 | // This constrains the implementation strategies. The usual approach to making | ||||||
21 | // the user-facing functions polymorphic would be to either use | ||||||
22 | // __attribute__((overloadable)) to make a set of vaddq() functions that are | ||||||
23 | // all inline wrappers on the underlying clang builtins, or to define a single | ||||||
24 | // vaddq() macro which expands to an instance of _Generic. | ||||||
25 | // | ||||||
26 | // The inline-wrappers approach would work fine for most intrinsics, except for | ||||||
27 | // the ones that take an argument required to be a compile-time constant, | ||||||
28 | // because if you wrap an inline function around a call to a builtin, the | ||||||
29 | // constant nature of the argument is not passed through. | ||||||
30 | // | ||||||
31 | // The _Generic approach can be made to work with enough effort, but it takes a | ||||||
32 | // lot of machinery, because of the design feature of _Generic that even the | ||||||
33 | // untaken branches are required to pass all front-end validity checks such as | ||||||
34 | // type-correctness. You can work around that by nesting further _Generics all | ||||||
35 | // over the place to coerce things to the right type in untaken branches, but | ||||||
36 | // what you get out is complicated, hard to guarantee its correctness, and | ||||||
37 | // worst of all, gives _completely unreadable_ error messages if the user gets | ||||||
38 | // the types wrong for an intrinsic call. | ||||||
39 | // | ||||||
40 | // Therefore, my strategy is to introduce a new __attribute__ that allows a | ||||||
41 | // function to be mapped to a clang builtin even though it doesn't have the | ||||||
42 | // same name, and then declare all the user-facing MVE function names with that | ||||||
43 | // attribute, mapping each one directly to the clang builtin. And the | ||||||
44 | // polymorphic ones have __attribute__((overloadable)) as well. So once the | ||||||
45 | // compiler has resolved the overload, it knows the internal builtin ID of the | ||||||
46 | // selected function, and can check the immediate arguments against that; and | ||||||
47 | // if the user gets the types wrong in a call to a polymorphic intrinsic, they | ||||||
48 | // get a completely clear error message showing all the declarations of that | ||||||
49 | // function in the header file and explaining why each one doesn't fit their | ||||||
50 | // call. | ||||||
51 | // | ||||||
52 | // The downside of this is that if every clang builtin has to correspond | ||||||
53 | // exactly to a user-facing ACLE intrinsic, then you can't save work in the | ||||||
54 | // frontend by doing it in the header file: CGBuiltin.cpp has to do the entire | ||||||
55 | // job of converting an ACLE intrinsic call into LLVM IR. So the Tablegen | ||||||
56 | // description for an MVE intrinsic has to contain a full description of the | ||||||
57 | // sequence of IRBuilder calls that clang will need to make. | ||||||
58 | // | ||||||
59 | //===----------------------------------------------------------------------===// | ||||||
60 | |||||||
61 | #include "llvm/ADT/APInt.h" | ||||||
62 | #include "llvm/ADT/StringRef.h" | ||||||
63 | #include "llvm/ADT/StringSwitch.h" | ||||||
64 | #include "llvm/Support/Casting.h" | ||||||
65 | #include "llvm/Support/raw_ostream.h" | ||||||
66 | #include "llvm/TableGen/Error.h" | ||||||
67 | #include "llvm/TableGen/Record.h" | ||||||
68 | #include "llvm/TableGen/StringToOffsetTable.h" | ||||||
69 | #include <cassert> | ||||||
70 | #include <cstddef> | ||||||
71 | #include <cstdint> | ||||||
72 | #include <list> | ||||||
73 | #include <map> | ||||||
74 | #include <memory> | ||||||
75 | #include <set> | ||||||
76 | #include <string> | ||||||
77 | #include <vector> | ||||||
78 | |||||||
79 | using namespace llvm; | ||||||
80 | |||||||
81 | namespace { | ||||||
82 | |||||||
83 | class EmitterBase; | ||||||
84 | class Result; | ||||||
85 | |||||||
86 | // ----------------------------------------------------------------------------- | ||||||
87 | // A system of classes to represent all the types we'll need to deal with in | ||||||
88 | // the prototypes of intrinsics. | ||||||
89 | // | ||||||
90 | // Query methods include finding out the C name of a type; the "LLVM name" in | ||||||
91 | // the sense of a C++ code snippet that can be used in the codegen function; | ||||||
92 | // the suffix that represents the type in the ACLE intrinsic naming scheme | ||||||
93 | // (e.g. 's32' represents int32_t in intrinsics such as vaddq_s32); whether the | ||||||
94 | // type is floating-point related (hence should be under #ifdef in the MVE | ||||||
95 | // header so that it isn't included in integer-only MVE mode); and the type's | ||||||
96 | // size in bits. Not all subtypes support all these queries. | ||||||
97 | |||||||
98 | class Type { | ||||||
99 | public: | ||||||
100 | enum class TypeKind { | ||||||
101 | // Void appears as a return type (for store intrinsics, which are pure | ||||||
102 | // side-effect). It's also used as the parameter type in the Tablegen | ||||||
103 | // when an intrinsic doesn't need to come in various suffixed forms like | ||||||
104 | // vfooq_s8,vfooq_u16,vfooq_f32. | ||||||
105 | Void, | ||||||
106 | |||||||
107 | // Scalar is used for ordinary int and float types of all sizes. | ||||||
108 | Scalar, | ||||||
109 | |||||||
110 | // Vector is used for anything that occupies exactly one MVE vector | ||||||
111 | // register, i.e. {uint,int,float}NxM_t. | ||||||
112 | Vector, | ||||||
113 | |||||||
114 | // MultiVector is used for the {uint,int,float}NxMxK_t types used by the | ||||||
115 | // interleaving load/store intrinsics v{ld,st}{2,4}q. | ||||||
116 | MultiVector, | ||||||
117 | |||||||
118 | // Predicate is used by all the predicated intrinsics. Its C | ||||||
119 | // representation is mve_pred16_t (which is just an alias for uint16_t). | ||||||
120 | // But we give more detail here, by indicating that a given predicate | ||||||
121 | // instruction is logically regarded as a vector of i1 containing the | ||||||
122 | // same number of lanes as the input vector type. So our Predicate type | ||||||
123 | // comes with a lane count, which we use to decide which kind of <n x i1> | ||||||
124 | // we'll invoke the pred_i2v IR intrinsic to translate it into. | ||||||
125 | Predicate, | ||||||
126 | |||||||
127 | // Pointer is used for pointer types (obviously), and comes with a flag | ||||||
128 | // indicating whether it's a pointer to a const or mutable instance of | ||||||
129 | // the pointee type. | ||||||
130 | Pointer, | ||||||
131 | }; | ||||||
132 | |||||||
133 | private: | ||||||
134 | const TypeKind TKind; | ||||||
135 | |||||||
136 | protected: | ||||||
137 | Type(TypeKind K) : TKind(K) {} | ||||||
138 | |||||||
139 | public: | ||||||
140 | TypeKind typeKind() const { return TKind; } | ||||||
141 | virtual ~Type() = default; | ||||||
142 | virtual bool requiresFloat() const = 0; | ||||||
143 | virtual bool requiresMVE() const = 0; | ||||||
144 | virtual unsigned sizeInBits() const = 0; | ||||||
145 | virtual std::string cName() const = 0; | ||||||
146 | virtual std::string llvmName() const { | ||||||
147 | PrintFatalError("no LLVM type name available for type " + cName()); | ||||||
148 | } | ||||||
149 | virtual std::string acleSuffix(std::string) const { | ||||||
150 | PrintFatalError("no ACLE suffix available for this type"); | ||||||
151 | } | ||||||
152 | }; | ||||||
153 | |||||||
154 | enum class ScalarTypeKind { SignedInt, UnsignedInt, Float }; | ||||||
155 | inline std::string toLetter(ScalarTypeKind kind) { | ||||||
156 | switch (kind) { | ||||||
157 | case ScalarTypeKind::SignedInt: | ||||||
158 | return "s"; | ||||||
159 | case ScalarTypeKind::UnsignedInt: | ||||||
160 | return "u"; | ||||||
161 | case ScalarTypeKind::Float: | ||||||
162 | return "f"; | ||||||
163 | } | ||||||
164 | llvm_unreachable("Unhandled ScalarTypeKind enum")::llvm::llvm_unreachable_internal("Unhandled ScalarTypeKind enum" , "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/clang/utils/TableGen/MveEmitter.cpp" , 164); | ||||||
165 | } | ||||||
166 | inline std::string toCPrefix(ScalarTypeKind kind) { | ||||||
167 | switch (kind) { | ||||||
168 | case ScalarTypeKind::SignedInt: | ||||||
169 | return "int"; | ||||||
170 | case ScalarTypeKind::UnsignedInt: | ||||||
171 | return "uint"; | ||||||
172 | case ScalarTypeKind::Float: | ||||||
173 | return "float"; | ||||||
174 | } | ||||||
175 | llvm_unreachable("Unhandled ScalarTypeKind enum")::llvm::llvm_unreachable_internal("Unhandled ScalarTypeKind enum" , "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/clang/utils/TableGen/MveEmitter.cpp" , 175); | ||||||
176 | } | ||||||
177 | |||||||
178 | class VoidType : public Type { | ||||||
179 | public: | ||||||
180 | VoidType() : Type(TypeKind::Void) {} | ||||||
181 | unsigned sizeInBits() const override { return 0; } | ||||||
182 | bool requiresFloat() const override { return false; } | ||||||
183 | bool requiresMVE() const override { return false; } | ||||||
184 | std::string cName() const override { return "void"; } | ||||||
185 | |||||||
186 | static bool classof(const Type *T) { return T->typeKind() == TypeKind::Void; } | ||||||
187 | std::string acleSuffix(std::string) const override { return ""; } | ||||||
188 | }; | ||||||
189 | |||||||
190 | class PointerType : public Type { | ||||||
191 | const Type *Pointee; | ||||||
192 | bool Const; | ||||||
193 | |||||||
194 | public: | ||||||
195 | PointerType(const Type *Pointee, bool Const) | ||||||
196 | : Type(TypeKind::Pointer), Pointee(Pointee), Const(Const) {} | ||||||
197 | unsigned sizeInBits() const override { return 32; } | ||||||
198 | bool requiresFloat() const override { return Pointee->requiresFloat(); } | ||||||
199 | bool requiresMVE() const override { return Pointee->requiresMVE(); } | ||||||
200 | std::string cName() const override { | ||||||
201 | std::string Name = Pointee->cName(); | ||||||
202 | |||||||
203 | // The syntax for a pointer in C is different when the pointee is | ||||||
204 | // itself a pointer. The MVE intrinsics don't contain any double | ||||||
205 | // pointers, so we don't need to worry about that wrinkle. | ||||||
206 | assert(!isa<PointerType>(Pointee) && "Pointer to pointer not supported")(static_cast <bool> (!isa<PointerType>(Pointee) && "Pointer to pointer not supported") ? void (0) : __assert_fail ("!isa<PointerType>(Pointee) && \"Pointer to pointer not supported\"" , "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/clang/utils/TableGen/MveEmitter.cpp" , 206, __extension__ __PRETTY_FUNCTION__)); | ||||||
207 | |||||||
208 | if (Const) | ||||||
209 | Name = "const " + Name; | ||||||
210 | return Name + " *"; | ||||||
211 | } | ||||||
212 | std::string llvmName() const override { | ||||||
213 | return "llvm::PointerType::getUnqual(" + Pointee->llvmName() + ")"; | ||||||
214 | } | ||||||
215 | |||||||
216 | static bool classof(const Type *T) { | ||||||
217 | return T->typeKind() == TypeKind::Pointer; | ||||||
218 | } | ||||||
219 | }; | ||||||
220 | |||||||
221 | // Base class for all the types that have a name of the form | ||||||
222 | // [prefix][numbers]_t, like int32_t, uint16x8_t, float32x4x2_t. | ||||||
223 | // | ||||||
224 | // For this sub-hierarchy we invent a cNameBase() method which returns the | ||||||
225 | // whole name except for the trailing "_t", so that Vector and MultiVector can | ||||||
226 | // append an extra "x2" or whatever to their element type's cNameBase(). Then | ||||||
227 | // the main cName() query method puts "_t" on the end for the final type name. | ||||||
228 | |||||||
229 | class CRegularNamedType : public Type { | ||||||
230 | using Type::Type; | ||||||
231 | virtual std::string cNameBase() const = 0; | ||||||
232 | |||||||
233 | public: | ||||||
234 | std::string cName() const override { return cNameBase() + "_t"; } | ||||||
235 | }; | ||||||
236 | |||||||
237 | class ScalarType : public CRegularNamedType { | ||||||
238 | ScalarTypeKind Kind; | ||||||
239 | unsigned Bits; | ||||||
240 | std::string NameOverride; | ||||||
241 | |||||||
242 | public: | ||||||
243 | ScalarType(const Record *Record) : CRegularNamedType(TypeKind::Scalar) { | ||||||
244 | Kind = StringSwitch<ScalarTypeKind>(Record->getValueAsString("kind")) | ||||||
245 | .Case("s", ScalarTypeKind::SignedInt) | ||||||
246 | .Case("u", ScalarTypeKind::UnsignedInt) | ||||||
247 | .Case("f", ScalarTypeKind::Float); | ||||||
248 | Bits = Record->getValueAsInt("size"); | ||||||
249 | NameOverride = std::string(Record->getValueAsString("nameOverride")); | ||||||
250 | } | ||||||
251 | unsigned sizeInBits() const override { return Bits; } | ||||||
252 | ScalarTypeKind kind() const { return Kind; } | ||||||
253 | std::string suffix() const { return toLetter(Kind) + utostr(Bits); } | ||||||
254 | std::string cNameBase() const override { | ||||||
255 | return toCPrefix(Kind) + utostr(Bits); | ||||||
256 | } | ||||||
257 | std::string cName() const override { | ||||||
258 | if (NameOverride.empty()) | ||||||
259 | return CRegularNamedType::cName(); | ||||||
260 | return NameOverride; | ||||||
261 | } | ||||||
262 | std::string llvmName() const override { | ||||||
263 | if (Kind == ScalarTypeKind::Float) { | ||||||
264 | if (Bits == 16) | ||||||
265 | return "HalfTy"; | ||||||
266 | if (Bits == 32) | ||||||
267 | return "FloatTy"; | ||||||
268 | if (Bits == 64) | ||||||
269 | return "DoubleTy"; | ||||||
270 | PrintFatalError("bad size for floating type"); | ||||||
271 | } | ||||||
272 | return "Int" + utostr(Bits) + "Ty"; | ||||||
273 | } | ||||||
274 | std::string acleSuffix(std::string overrideLetter) const override { | ||||||
275 | return "_" + (overrideLetter.size() ? overrideLetter : toLetter(Kind)) | ||||||
276 | + utostr(Bits); | ||||||
277 | } | ||||||
278 | bool isInteger() const { return Kind != ScalarTypeKind::Float; } | ||||||
279 | bool requiresFloat() const override { return !isInteger(); } | ||||||
280 | bool requiresMVE() const override { return false; } | ||||||
281 | bool hasNonstandardName() const { return !NameOverride.empty(); } | ||||||
282 | |||||||
283 | static bool classof(const Type *T) { | ||||||
284 | return T->typeKind() == TypeKind::Scalar; | ||||||
285 | } | ||||||
286 | }; | ||||||
287 | |||||||
288 | class VectorType : public CRegularNamedType { | ||||||
289 | const ScalarType *Element; | ||||||
290 | unsigned Lanes; | ||||||
291 | |||||||
292 | public: | ||||||
293 | VectorType(const ScalarType *Element, unsigned Lanes) | ||||||
294 | : CRegularNamedType(TypeKind::Vector), Element(Element), Lanes(Lanes) {} | ||||||
295 | unsigned sizeInBits() const override { return Lanes * Element->sizeInBits(); } | ||||||
296 | unsigned lanes() const { return Lanes; } | ||||||
297 | bool requiresFloat() const override { return Element->requiresFloat(); } | ||||||
298 | bool requiresMVE() const override { return true; } | ||||||
299 | std::string cNameBase() const override { | ||||||
300 | return Element->cNameBase() + "x" + utostr(Lanes); | ||||||
301 | } | ||||||
302 | std::string llvmName() const override { | ||||||
303 | return "llvm::FixedVectorType::get(" + Element->llvmName() + ", " + | ||||||
304 | utostr(Lanes) + ")"; | ||||||
305 | } | ||||||
306 | |||||||
307 | static bool classof(const Type *T) { | ||||||
308 | return T->typeKind() == TypeKind::Vector; | ||||||
309 | } | ||||||
310 | }; | ||||||
311 | |||||||
312 | class MultiVectorType : public CRegularNamedType { | ||||||
313 | const VectorType *Element; | ||||||
314 | unsigned Registers; | ||||||
315 | |||||||
316 | public: | ||||||
317 | MultiVectorType(unsigned Registers, const VectorType *Element) | ||||||
318 | : CRegularNamedType(TypeKind::MultiVector), Element(Element), | ||||||
319 | Registers(Registers) {} | ||||||
320 | unsigned sizeInBits() const override { | ||||||
321 | return Registers * Element->sizeInBits(); | ||||||
322 | } | ||||||
323 | unsigned registers() const { return Registers; } | ||||||
324 | bool requiresFloat() const override { return Element->requiresFloat(); } | ||||||
325 | bool requiresMVE() const override { return true; } | ||||||
326 | std::string cNameBase() const override { | ||||||
327 | return Element->cNameBase() + "x" + utostr(Registers); | ||||||
328 | } | ||||||
329 | |||||||
330 | // MultiVectorType doesn't override llvmName, because we don't expect to do | ||||||
331 | // automatic code generation for the MVE intrinsics that use it: the {vld2, | ||||||
332 | // vld4, vst2, vst4} family are the only ones that use these types, so it was | ||||||
333 | // easier to hand-write the codegen for dealing with these structs than to | ||||||
334 | // build in lots of extra automatic machinery that would only be used once. | ||||||
335 | |||||||
336 | static bool classof(const Type *T) { | ||||||
337 | return T->typeKind() == TypeKind::MultiVector; | ||||||
338 | } | ||||||
339 | }; | ||||||
340 | |||||||
341 | class PredicateType : public CRegularNamedType { | ||||||
342 | unsigned Lanes; | ||||||
343 | |||||||
344 | public: | ||||||
345 | PredicateType(unsigned Lanes) | ||||||
346 | : CRegularNamedType(TypeKind::Predicate), Lanes(Lanes) {} | ||||||
347 | unsigned sizeInBits() const override { return 16; } | ||||||
348 | std::string cNameBase() const override { return "mve_pred16"; } | ||||||
349 | bool requiresFloat() const override { return false; }; | ||||||
350 | bool requiresMVE() const override { return true; } | ||||||
351 | std::string llvmName() const override { | ||||||
352 | // Use <4 x i1> instead of <2 x i1> for two-lane vector types. See | ||||||
353 | // the comment in llvm/lib/Target/ARM/ARMInstrMVE.td for further | ||||||
354 | // explanation. | ||||||
355 | unsigned ModifiedLanes = (Lanes == 2 ? 4 : Lanes); | ||||||
356 | |||||||
357 | return "llvm::FixedVectorType::get(Builder.getInt1Ty(), " + | ||||||
358 | utostr(ModifiedLanes) + ")"; | ||||||
359 | } | ||||||
360 | |||||||
361 | static bool classof(const Type *T) { | ||||||
362 | return T->typeKind() == TypeKind::Predicate; | ||||||
363 | } | ||||||
364 | }; | ||||||
365 | |||||||
366 | // ----------------------------------------------------------------------------- | ||||||
367 | // Class to facilitate merging together the code generation for many intrinsics | ||||||
368 | // by means of varying a few constant or type parameters. | ||||||
369 | // | ||||||
370 | // Most obviously, the intrinsics in a single parametrised family will have | ||||||
371 | // code generation sequences that only differ in a type or two, e.g. vaddq_s8 | ||||||
372 | // and vaddq_u16 will look the same apart from putting a different vector type | ||||||
373 | // in the call to CGM.getIntrinsic(). But also, completely different intrinsics | ||||||
374 | // will often code-generate in the same way, with only a different choice of | ||||||
375 | // _which_ IR intrinsic they lower to (e.g. vaddq_m_s8 and vmulq_m_s8), but | ||||||
376 | // marshalling the arguments and return values of the IR intrinsic in exactly | ||||||
377 | // the same way. And others might differ only in some other kind of constant, | ||||||
378 | // such as a lane index. | ||||||
379 | // | ||||||
380 | // So, when we generate the IR-building code for all these intrinsics, we keep | ||||||
381 | // track of every value that could possibly be pulled out of the code and | ||||||
382 | // stored ahead of time in a local variable. Then we group together intrinsics | ||||||
383 | // by textual equivalence of the code that would result if _all_ those | ||||||
384 | // parameters were stored in local variables. That gives us maximal sets that | ||||||
385 | // can be implemented by a single piece of IR-building code by changing | ||||||
386 | // parameter values ahead of time. | ||||||
387 | // | ||||||
388 | // After we've done that, we do a second pass in which we only allocate _some_ | ||||||
389 | // of the parameters into local variables, by tracking which ones have the same | ||||||
390 | // values as each other (so that a single variable can be reused) and which | ||||||
391 | // ones are the same across the whole set (so that no variable is needed at | ||||||
392 | // all). | ||||||
393 | // | ||||||
394 | // Hence the class below. Its allocParam method is invoked during code | ||||||
395 | // generation by every method of a Result subclass (see below) that wants to | ||||||
396 | // give it the opportunity to pull something out into a switchable parameter. | ||||||
397 | // It returns a variable name for the parameter, or (if it's being used in the | ||||||
398 | // second pass once we've decided that some parameters don't need to be stored | ||||||
399 | // in variables after all) it might just return the input expression unchanged. | ||||||
400 | |||||||
401 | struct CodeGenParamAllocator { | ||||||
402 | // Accumulated during code generation | ||||||
403 | std::vector<std::string> *ParamTypes = nullptr; | ||||||
404 | std::vector<std::string> *ParamValues = nullptr; | ||||||
405 | |||||||
406 | // Provided ahead of time in pass 2, to indicate which parameters are being | ||||||
407 | // assigned to what. This vector contains an entry for each call to | ||||||
408 | // allocParam expected during code gen (which we counted up in pass 1), and | ||||||
409 | // indicates the number of the parameter variable that should be returned, or | ||||||
410 | // -1 if this call shouldn't allocate a parameter variable at all. | ||||||
411 | // | ||||||
412 | // We rely on the recursive code generation working identically in passes 1 | ||||||
413 | // and 2, so that the same list of calls to allocParam happen in the same | ||||||
414 | // order. That guarantees that the parameter numbers recorded in pass 1 will | ||||||
415 | // match the entries in this vector that store what EmitterBase::EmitBuiltinCG | ||||||
416 | // decided to do about each one in pass 2. | ||||||
417 | std::vector<int> *ParamNumberMap = nullptr; | ||||||
418 | |||||||
419 | // Internally track how many things we've allocated | ||||||
420 | unsigned nparams = 0; | ||||||
421 | |||||||
422 | std::string allocParam(StringRef Type, StringRef Value) { | ||||||
423 | unsigned ParamNumber; | ||||||
424 | |||||||
425 | if (!ParamNumberMap) { | ||||||
426 | // In pass 1, unconditionally assign a new parameter variable to every | ||||||
427 | // value we're asked to process. | ||||||
428 | ParamNumber = nparams++; | ||||||
429 | } else { | ||||||
430 | // In pass 2, consult the map provided by the caller to find out which | ||||||
431 | // variable we should be keeping things in. | ||||||
432 | int MapValue = (*ParamNumberMap)[nparams++]; | ||||||
433 | if (MapValue < 0) | ||||||
434 | return std::string(Value); | ||||||
435 | ParamNumber = MapValue; | ||||||
436 | } | ||||||
437 | |||||||
438 | // If we've allocated a new parameter variable for the first time, store | ||||||
439 | // its type and value to be retrieved after codegen. | ||||||
440 | if (ParamTypes && ParamTypes->size() == ParamNumber) | ||||||
441 | ParamTypes->push_back(std::string(Type)); | ||||||
442 | if (ParamValues && ParamValues->size() == ParamNumber) | ||||||
443 | ParamValues->push_back(std::string(Value)); | ||||||
444 | |||||||
445 | // Unimaginative naming scheme for parameter variables. | ||||||
446 | return "Param" + utostr(ParamNumber); | ||||||
447 | } | ||||||
448 | }; | ||||||
449 | |||||||
450 | // ----------------------------------------------------------------------------- | ||||||
451 | // System of classes that represent all the intermediate values used during | ||||||
452 | // code-generation for an intrinsic. | ||||||
453 | // | ||||||
454 | // The base class 'Result' can represent a value of the LLVM type 'Value', or | ||||||
455 | // sometimes 'Address' (for loads/stores, including an alignment requirement). | ||||||
456 | // | ||||||
457 | // In the case where the Tablegen provides a value in the codegen dag as a | ||||||
458 | // plain integer literal, the Result object we construct here will be one that | ||||||
459 | // returns true from hasIntegerConstantValue(). This allows the generated C++ | ||||||
460 | // code to use the constant directly in contexts which can take a literal | ||||||
461 | // integer, such as Builder.CreateExtractValue(thing, 1), without going to the | ||||||
462 | // effort of calling llvm::ConstantInt::get() and then pulling the constant | ||||||
463 | // back out of the resulting llvm:Value later. | ||||||
464 | |||||||
465 | class Result { | ||||||
466 | public: | ||||||
467 | // Convenient shorthand for the pointer type we'll be using everywhere. | ||||||
468 | using Ptr = std::shared_ptr<Result>; | ||||||
469 | |||||||
470 | private: | ||||||
471 | Ptr Predecessor; | ||||||
472 | std::string VarName; | ||||||
473 | bool VarNameUsed = false; | ||||||
474 | unsigned Visited = 0; | ||||||
475 | |||||||
476 | public: | ||||||
477 | virtual ~Result() = default; | ||||||
478 | using Scope = std::map<std::string, Ptr>; | ||||||
479 | virtual void genCode(raw_ostream &OS, CodeGenParamAllocator &) const = 0; | ||||||
480 | virtual bool hasIntegerConstantValue() const { return false; } | ||||||
481 | virtual uint32_t integerConstantValue() const { return 0; } | ||||||
482 | virtual bool hasIntegerValue() const { return false; } | ||||||
483 | virtual std::string getIntegerValue(const std::string &) { | ||||||
484 | llvm_unreachable("non-working Result::getIntegerValue called")::llvm::llvm_unreachable_internal("non-working Result::getIntegerValue called" , "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/clang/utils/TableGen/MveEmitter.cpp" , 484); | ||||||
485 | } | ||||||
486 | virtual std::string typeName() const { return "Value *"; } | ||||||
487 | |||||||
488 | // Mostly, when a code-generation operation has a dependency on prior | ||||||
489 | // operations, it's because it uses the output values of those operations as | ||||||
490 | // inputs. But there's one exception, which is the use of 'seq' in Tablegen | ||||||
491 | // to indicate that operations have to be performed in sequence regardless of | ||||||
492 | // whether they use each others' output values. | ||||||
493 | // | ||||||
494 | // So, the actual generation of code is done by depth-first search, using the | ||||||
495 | // prerequisites() method to get a list of all the other Results that have to | ||||||
496 | // be computed before this one. That method divides into the 'predecessor', | ||||||
497 | // set by setPredecessor() while processing a 'seq' dag node, and the list | ||||||
498 | // returned by 'morePrerequisites', which each subclass implements to return | ||||||
499 | // a list of the Results it uses as input to whatever its own computation is | ||||||
500 | // doing. | ||||||
501 | |||||||
502 | virtual void morePrerequisites(std::vector<Ptr> &output) const {} | ||||||
503 | std::vector<Ptr> prerequisites() const { | ||||||
504 | std::vector<Ptr> ToRet; | ||||||
505 | if (Predecessor) | ||||||
506 | ToRet.push_back(Predecessor); | ||||||
507 | morePrerequisites(ToRet); | ||||||
508 | return ToRet; | ||||||
509 | } | ||||||
510 | |||||||
511 | void setPredecessor(Ptr p) { | ||||||
512 | // If the user has nested one 'seq' node inside another, and this | ||||||
513 | // method is called on the return value of the inner 'seq' (i.e. | ||||||
514 | // the final item inside it), then we can't link _this_ node to p, | ||||||
515 | // because it already has a predecessor. Instead, walk the chain | ||||||
516 | // until we find the first item in the inner seq, and link that to | ||||||
517 | // p, so that nesting seqs has the obvious effect of linking | ||||||
518 | // everything together into one long sequential chain. | ||||||
519 | Result *r = this; | ||||||
520 | while (r->Predecessor) | ||||||
521 | r = r->Predecessor.get(); | ||||||
522 | r->Predecessor = p; | ||||||
523 | } | ||||||
524 | |||||||
525 | // Each Result will be assigned a variable name in the output code, but not | ||||||
526 | // all those variable names will actually be used (e.g. the return value of | ||||||
527 | // Builder.CreateStore has void type, so nobody will want to refer to it). To | ||||||
528 | // prevent annoying compiler warnings, we track whether each Result's | ||||||
529 | // variable name was ever actually mentioned in subsequent statements, so | ||||||
530 | // that it can be left out of the final generated code. | ||||||
531 | std::string varname() { | ||||||
532 | VarNameUsed = true; | ||||||
533 | return VarName; | ||||||
534 | } | ||||||
535 | void setVarname(const StringRef s) { VarName = std::string(s); } | ||||||
536 | bool varnameUsed() const { return VarNameUsed; } | ||||||
537 | |||||||
538 | // Emit code to generate this result as a Value *. | ||||||
539 | virtual std::string asValue() { | ||||||
540 | return varname(); | ||||||
541 | } | ||||||
542 | |||||||
543 | // Code generation happens in multiple passes. This method tracks whether a | ||||||
544 | // Result has yet been visited in a given pass, without the need for a | ||||||
545 | // tedious loop in between passes that goes through and resets a 'visited' | ||||||
546 | // flag back to false: you just set Pass=1 the first time round, and Pass=2 | ||||||
547 | // the second time. | ||||||
548 | bool needsVisiting(unsigned Pass) { | ||||||
549 | bool ToRet = Visited < Pass; | ||||||
550 | Visited = Pass; | ||||||
551 | return ToRet; | ||||||
552 | } | ||||||
553 | }; | ||||||
554 | |||||||
555 | // Result subclass that retrieves one of the arguments to the clang builtin | ||||||
556 | // function. In cases where the argument has pointer type, we call | ||||||
557 | // EmitPointerWithAlignment and store the result in a variable of type Address, | ||||||
558 | // so that load and store IR nodes can know the right alignment. Otherwise, we | ||||||
559 | // call EmitScalarExpr. | ||||||
560 | // | ||||||
561 | // There are aggregate parameters in the MVE intrinsics API, but we don't deal | ||||||
562 | // with them in this Tablegen back end: they only arise in the vld2q/vld4q and | ||||||
563 | // vst2q/vst4q family, which is few enough that we just write the code by hand | ||||||
564 | // for those in CGBuiltin.cpp. | ||||||
565 | class BuiltinArgResult : public Result { | ||||||
566 | public: | ||||||
567 | unsigned ArgNum; | ||||||
568 | bool AddressType; | ||||||
569 | bool Immediate; | ||||||
570 | BuiltinArgResult(unsigned ArgNum, bool AddressType, bool Immediate) | ||||||
571 | : ArgNum(ArgNum), AddressType(AddressType), Immediate(Immediate) {} | ||||||
572 | void genCode(raw_ostream &OS, CodeGenParamAllocator &) const override { | ||||||
573 | OS << (AddressType ? "EmitPointerWithAlignment" : "EmitScalarExpr") | ||||||
574 | << "(E->getArg(" << ArgNum << "))"; | ||||||
575 | } | ||||||
576 | std::string typeName() const override { | ||||||
577 | return AddressType ? "Address" : Result::typeName(); | ||||||
578 | } | ||||||
579 | // Emit code to generate this result as a Value *. | ||||||
580 | std::string asValue() override { | ||||||
581 | if (AddressType) | ||||||
582 | return "(" + varname() + ".getPointer())"; | ||||||
583 | return Result::asValue(); | ||||||
584 | } | ||||||
585 | bool hasIntegerValue() const override { return Immediate; } | ||||||
586 | std::string getIntegerValue(const std::string &IntType) override { | ||||||
587 | return "GetIntegerConstantValue<" + IntType + ">(E->getArg(" + | ||||||
588 | utostr(ArgNum) + "), getContext())"; | ||||||
589 | } | ||||||
590 | }; | ||||||
591 | |||||||
592 | // Result subclass for an integer literal appearing in Tablegen. This may need | ||||||
593 | // to be turned into an llvm::Result by means of llvm::ConstantInt::get(), or | ||||||
594 | // it may be used directly as an integer, depending on which IRBuilder method | ||||||
595 | // it's being passed to. | ||||||
596 | class IntLiteralResult : public Result { | ||||||
597 | public: | ||||||
598 | const ScalarType *IntegerType; | ||||||
599 | uint32_t IntegerValue; | ||||||
600 | IntLiteralResult(const ScalarType *IntegerType, uint32_t IntegerValue) | ||||||
601 | : IntegerType(IntegerType), IntegerValue(IntegerValue) {} | ||||||
602 | void genCode(raw_ostream &OS, | ||||||
603 | CodeGenParamAllocator &ParamAlloc) const override { | ||||||
604 | OS << "llvm::ConstantInt::get(" | ||||||
605 | << ParamAlloc.allocParam("llvm::Type *", IntegerType->llvmName()) | ||||||
606 | << ", "; | ||||||
607 | OS << ParamAlloc.allocParam(IntegerType->cName(), utostr(IntegerValue)) | ||||||
608 | << ")"; | ||||||
609 | } | ||||||
610 | bool hasIntegerConstantValue() const override { return true; } | ||||||
611 | uint32_t integerConstantValue() const override { return IntegerValue; } | ||||||
612 | }; | ||||||
613 | |||||||
614 | // Result subclass representing a cast between different integer types. We use | ||||||
615 | // our own ScalarType abstraction as the representation of the target type, | ||||||
616 | // which gives both size and signedness. | ||||||
617 | class IntCastResult : public Result { | ||||||
618 | public: | ||||||
619 | const ScalarType *IntegerType; | ||||||
620 | Ptr V; | ||||||
621 | IntCastResult(const ScalarType *IntegerType, Ptr V) | ||||||
622 | : IntegerType(IntegerType), V(V) {} | ||||||
623 | void genCode(raw_ostream &OS, | ||||||
624 | CodeGenParamAllocator &ParamAlloc) const override { | ||||||
625 | OS << "Builder.CreateIntCast(" << V->varname() << ", " | ||||||
626 | << ParamAlloc.allocParam("llvm::Type *", IntegerType->llvmName()) << ", " | ||||||
627 | << ParamAlloc.allocParam("bool", | ||||||
628 | IntegerType->kind() == ScalarTypeKind::SignedInt | ||||||
629 | ? "true" | ||||||
630 | : "false") | ||||||
631 | << ")"; | ||||||
632 | } | ||||||
633 | void morePrerequisites(std::vector<Ptr> &output) const override { | ||||||
634 | output.push_back(V); | ||||||
635 | } | ||||||
636 | }; | ||||||
637 | |||||||
638 | // Result subclass representing a cast between different pointer types. | ||||||
639 | class PointerCastResult : public Result { | ||||||
640 | public: | ||||||
641 | const PointerType *PtrType; | ||||||
642 | Ptr V; | ||||||
643 | PointerCastResult(const PointerType *PtrType, Ptr V) | ||||||
644 | : PtrType(PtrType), V(V) {} | ||||||
645 | void genCode(raw_ostream &OS, | ||||||
646 | CodeGenParamAllocator &ParamAlloc) const override { | ||||||
647 | OS << "Builder.CreatePointerCast(" << V->asValue() << ", " | ||||||
648 | << ParamAlloc.allocParam("llvm::Type *", PtrType->llvmName()) << ")"; | ||||||
649 | } | ||||||
650 | void morePrerequisites(std::vector<Ptr> &output) const override { | ||||||
651 | output.push_back(V); | ||||||
652 | } | ||||||
653 | }; | ||||||
654 | |||||||
655 | // Result subclass representing a call to an IRBuilder method. Each IRBuilder | ||||||
656 | // method we want to use will have a Tablegen record giving the method name and | ||||||
657 | // describing any important details of how to call it, such as whether a | ||||||
658 | // particular argument should be an integer constant instead of an llvm::Value. | ||||||
659 | class IRBuilderResult : public Result { | ||||||
660 | public: | ||||||
661 | StringRef CallPrefix; | ||||||
662 | std::vector<Ptr> Args; | ||||||
663 | std::set<unsigned> AddressArgs; | ||||||
664 | std::map<unsigned, std::string> IntegerArgs; | ||||||
665 | IRBuilderResult(StringRef CallPrefix, std::vector<Ptr> Args, | ||||||
666 | std::set<unsigned> AddressArgs, | ||||||
667 | std::map<unsigned, std::string> IntegerArgs) | ||||||
668 | : CallPrefix(CallPrefix), Args(Args), AddressArgs(AddressArgs), | ||||||
669 | IntegerArgs(IntegerArgs) {} | ||||||
670 | void genCode(raw_ostream &OS, | ||||||
671 | CodeGenParamAllocator &ParamAlloc) const override { | ||||||
672 | OS << CallPrefix; | ||||||
673 | const char *Sep = ""; | ||||||
674 | for (unsigned i = 0, e = Args.size(); i < e; ++i) { | ||||||
675 | Ptr Arg = Args[i]; | ||||||
676 | auto it = IntegerArgs.find(i); | ||||||
677 | |||||||
678 | OS << Sep; | ||||||
679 | Sep = ", "; | ||||||
680 | |||||||
681 | if (it != IntegerArgs.end()) { | ||||||
682 | if (Arg->hasIntegerConstantValue()) | ||||||
683 | OS << "static_cast<" << it->second << ">(" | ||||||
684 | << ParamAlloc.allocParam(it->second, | ||||||
685 | utostr(Arg->integerConstantValue())) | ||||||
686 | << ")"; | ||||||
687 | else if (Arg->hasIntegerValue()) | ||||||
688 | OS << ParamAlloc.allocParam(it->second, | ||||||
689 | Arg->getIntegerValue(it->second)); | ||||||
690 | } else { | ||||||
691 | OS << Arg->varname(); | ||||||
692 | } | ||||||
693 | } | ||||||
694 | OS << ")"; | ||||||
695 | } | ||||||
696 | void morePrerequisites(std::vector<Ptr> &output) const override { | ||||||
697 | for (unsigned i = 0, e = Args.size(); i < e; ++i) { | ||||||
698 | Ptr Arg = Args[i]; | ||||||
699 | if (IntegerArgs.find(i) != IntegerArgs.end()) | ||||||
700 | continue; | ||||||
701 | output.push_back(Arg); | ||||||
702 | } | ||||||
703 | } | ||||||
704 | }; | ||||||
705 | |||||||
706 | // Result subclass representing making an Address out of a Value. | ||||||
707 | class AddressResult : public Result { | ||||||
708 | public: | ||||||
709 | Ptr Arg; | ||||||
710 | unsigned Align; | ||||||
711 | AddressResult(Ptr Arg, unsigned Align) : Arg(Arg), Align(Align) {} | ||||||
712 | void genCode(raw_ostream &OS, | ||||||
713 | CodeGenParamAllocator &ParamAlloc) const override { | ||||||
714 | OS << "Address(" << Arg->varname() << ", CharUnits::fromQuantity(" | ||||||
715 | << Align << "))"; | ||||||
716 | } | ||||||
717 | std::string typeName() const override { | ||||||
718 | return "Address"; | ||||||
719 | } | ||||||
720 | void morePrerequisites(std::vector<Ptr> &output) const override { | ||||||
721 | output.push_back(Arg); | ||||||
722 | } | ||||||
723 | }; | ||||||
724 | |||||||
725 | // Result subclass representing a call to an IR intrinsic, which we first have | ||||||
726 | // to look up using an Intrinsic::ID constant and an array of types. | ||||||
727 | class IRIntrinsicResult : public Result { | ||||||
728 | public: | ||||||
729 | std::string IntrinsicID; | ||||||
730 | std::vector<const Type *> ParamTypes; | ||||||
731 | std::vector<Ptr> Args; | ||||||
732 | IRIntrinsicResult(StringRef IntrinsicID, std::vector<const Type *> ParamTypes, | ||||||
733 | std::vector<Ptr> Args) | ||||||
734 | : IntrinsicID(std::string(IntrinsicID)), ParamTypes(ParamTypes), | ||||||
735 | Args(Args) {} | ||||||
736 | void genCode(raw_ostream &OS, | ||||||
737 | CodeGenParamAllocator &ParamAlloc) const override { | ||||||
738 | std::string IntNo = ParamAlloc.allocParam( | ||||||
739 | "Intrinsic::ID", "Intrinsic::" + IntrinsicID); | ||||||
740 | OS << "Builder.CreateCall(CGM.getIntrinsic(" << IntNo; | ||||||
741 | if (!ParamTypes.empty()) { | ||||||
742 | OS << ", {"; | ||||||
743 | const char *Sep = ""; | ||||||
744 | for (auto T : ParamTypes) { | ||||||
745 | OS << Sep << ParamAlloc.allocParam("llvm::Type *", T->llvmName()); | ||||||
746 | Sep = ", "; | ||||||
747 | } | ||||||
748 | OS << "}"; | ||||||
749 | } | ||||||
750 | OS << "), {"; | ||||||
751 | const char *Sep = ""; | ||||||
752 | for (auto Arg : Args) { | ||||||
753 | OS << Sep << Arg->asValue(); | ||||||
754 | Sep = ", "; | ||||||
755 | } | ||||||
756 | OS << "})"; | ||||||
757 | } | ||||||
758 | void morePrerequisites(std::vector<Ptr> &output) const override { | ||||||
759 | output.insert(output.end(), Args.begin(), Args.end()); | ||||||
760 | } | ||||||
761 | }; | ||||||
762 | |||||||
763 | // Result subclass that specifies a type, for use in IRBuilder operations such | ||||||
764 | // as CreateBitCast that take a type argument. | ||||||
765 | class TypeResult : public Result { | ||||||
766 | public: | ||||||
767 | const Type *T; | ||||||
768 | TypeResult(const Type *T) : T(T) {} | ||||||
769 | void genCode(raw_ostream &OS, CodeGenParamAllocator &) const override { | ||||||
770 | OS << T->llvmName(); | ||||||
771 | } | ||||||
772 | std::string typeName() const override { | ||||||
773 | return "llvm::Type *"; | ||||||
774 | } | ||||||
775 | }; | ||||||
776 | |||||||
777 | // ----------------------------------------------------------------------------- | ||||||
778 | // Class that describes a single ACLE intrinsic. | ||||||
779 | // | ||||||
780 | // A Tablegen record will typically describe more than one ACLE intrinsic, by | ||||||
781 | // means of setting the 'list<Type> Params' field to a list of multiple | ||||||
782 | // parameter types, so as to define vaddq_{s8,u8,...,f16,f32} all in one go. | ||||||
783 | // We'll end up with one instance of ACLEIntrinsic for *each* parameter type, | ||||||
784 | // rather than a single one for all of them. Hence, the constructor takes both | ||||||
785 | // a Tablegen record and the current value of the parameter type. | ||||||
786 | |||||||
787 | class ACLEIntrinsic { | ||||||
788 | // Structure documenting that one of the intrinsic's arguments is required to | ||||||
789 | // be a compile-time constant integer, and what constraints there are on its | ||||||
790 | // value. Used when generating Sema checking code. | ||||||
791 | struct ImmediateArg { | ||||||
792 | enum class BoundsType { ExplicitRange, UInt }; | ||||||
793 | BoundsType boundsType; | ||||||
794 | int64_t i1, i2; | ||||||
795 | StringRef ExtraCheckType, ExtraCheckArgs; | ||||||
796 | const Type *ArgType; | ||||||
797 | }; | ||||||
798 | |||||||
799 | // For polymorphic intrinsics, FullName is the explicit name that uniquely | ||||||
800 | // identifies this variant of the intrinsic, and ShortName is the name it | ||||||
801 | // shares with at least one other intrinsic. | ||||||
802 | std::string ShortName, FullName; | ||||||
803 | |||||||
804 | // Name of the architecture extension, used in the Clang builtin name | ||||||
805 | StringRef BuiltinExtension; | ||||||
806 | |||||||
807 | // A very small number of intrinsics _only_ have a polymorphic | ||||||
808 | // variant (vuninitializedq taking an unevaluated argument). | ||||||
809 | bool PolymorphicOnly; | ||||||
810 | |||||||
811 | // Another rarely-used flag indicating that the builtin doesn't | ||||||
812 | // evaluate its argument(s) at all. | ||||||
813 | bool NonEvaluating; | ||||||
814 | |||||||
815 | // True if the intrinsic needs only the C header part (no codegen, semantic | ||||||
816 | // checks, etc). Used for redeclaring MVE intrinsics in the arm_cde.h header. | ||||||
817 | bool HeaderOnly; | ||||||
818 | |||||||
819 | const Type *ReturnType; | ||||||
820 | std::vector<const Type *> ArgTypes; | ||||||
821 | std::map<unsigned, ImmediateArg> ImmediateArgs; | ||||||
822 | Result::Ptr Code; | ||||||
823 | |||||||
824 | std::map<std::string, std::string> CustomCodeGenArgs; | ||||||
825 | |||||||
826 | // Recursive function that does the internals of code generation. | ||||||
827 | void genCodeDfs(Result::Ptr V, std::list<Result::Ptr> &Used, | ||||||
828 | unsigned Pass) const { | ||||||
829 | if (!V->needsVisiting(Pass)) | ||||||
830 | return; | ||||||
831 | |||||||
832 | for (Result::Ptr W : V->prerequisites()) | ||||||
833 | genCodeDfs(W, Used, Pass); | ||||||
834 | |||||||
835 | Used.push_back(V); | ||||||
836 | } | ||||||
837 | |||||||
838 | public: | ||||||
839 | const std::string &shortName() const { return ShortName; } | ||||||
840 | const std::string &fullName() const { return FullName; } | ||||||
841 | StringRef builtinExtension() const { return BuiltinExtension; } | ||||||
842 | const Type *returnType() const { return ReturnType; } | ||||||
843 | const std::vector<const Type *> &argTypes() const { return ArgTypes; } | ||||||
844 | bool requiresFloat() const { | ||||||
845 | if (ReturnType->requiresFloat()) | ||||||
846 | return true; | ||||||
847 | for (const Type *T : ArgTypes) | ||||||
848 | if (T->requiresFloat()) | ||||||
849 | return true; | ||||||
850 | return false; | ||||||
851 | } | ||||||
852 | bool requiresMVE() const { | ||||||
853 | return ReturnType->requiresMVE() || | ||||||
854 | any_of(ArgTypes, [](const Type *T) { return T->requiresMVE(); }); | ||||||
855 | } | ||||||
856 | bool polymorphic() const { return ShortName != FullName; } | ||||||
857 | bool polymorphicOnly() const { return PolymorphicOnly; } | ||||||
858 | bool nonEvaluating() const { return NonEvaluating; } | ||||||
859 | bool headerOnly() const { return HeaderOnly; } | ||||||
860 | |||||||
861 | // External entry point for code generation, called from EmitterBase. | ||||||
862 | void genCode(raw_ostream &OS, CodeGenParamAllocator &ParamAlloc, | ||||||
863 | unsigned Pass) const { | ||||||
864 | assert(!headerOnly() && "Called genCode for header-only intrinsic")(static_cast <bool> (!headerOnly() && "Called genCode for header-only intrinsic" ) ? void (0) : __assert_fail ("!headerOnly() && \"Called genCode for header-only intrinsic\"" , "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/clang/utils/TableGen/MveEmitter.cpp" , 864, __extension__ __PRETTY_FUNCTION__)); | ||||||
865 | if (!hasCode()) { | ||||||
866 | for (auto kv : CustomCodeGenArgs) | ||||||
867 | OS << " " << kv.first << " = " << kv.second << ";\n"; | ||||||
868 | OS << " break; // custom code gen\n"; | ||||||
869 | return; | ||||||
870 | } | ||||||
871 | std::list<Result::Ptr> Used; | ||||||
872 | genCodeDfs(Code, Used, Pass); | ||||||
873 | |||||||
874 | unsigned varindex = 0; | ||||||
875 | for (Result::Ptr V : Used) | ||||||
876 | if (V->varnameUsed()) | ||||||
877 | V->setVarname("Val" + utostr(varindex++)); | ||||||
878 | |||||||
879 | for (Result::Ptr V : Used) { | ||||||
880 | OS << " "; | ||||||
881 | if (V == Used.back()) { | ||||||
882 | assert(!V->varnameUsed())(static_cast <bool> (!V->varnameUsed()) ? void (0) : __assert_fail ("!V->varnameUsed()", "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/clang/utils/TableGen/MveEmitter.cpp" , 882, __extension__ __PRETTY_FUNCTION__)); | ||||||
883 | OS << "return "; // FIXME: what if the top-level thing is void? | ||||||
884 | } else if (V->varnameUsed()) { | ||||||
885 | std::string Type = V->typeName(); | ||||||
886 | OS << V->typeName(); | ||||||
887 | if (!StringRef(Type).endswith("*")) | ||||||
888 | OS << " "; | ||||||
889 | OS << V->varname() << " = "; | ||||||
890 | } | ||||||
891 | V->genCode(OS, ParamAlloc); | ||||||
892 | OS << ";\n"; | ||||||
893 | } | ||||||
894 | } | ||||||
895 | bool hasCode() const { return Code != nullptr; } | ||||||
896 | |||||||
897 | static std::string signedHexLiteral(const llvm::APInt &iOrig) { | ||||||
898 | llvm::APInt i = iOrig.trunc(64); | ||||||
899 | SmallString<40> s; | ||||||
900 | i.toString(s, 16, true, true); | ||||||
901 | return std::string(s.str()); | ||||||
902 | } | ||||||
903 | |||||||
904 | std::string genSema() const { | ||||||
905 | assert(!headerOnly() && "Called genSema for header-only intrinsic")(static_cast <bool> (!headerOnly() && "Called genSema for header-only intrinsic" ) ? void (0) : __assert_fail ("!headerOnly() && \"Called genSema for header-only intrinsic\"" , "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/clang/utils/TableGen/MveEmitter.cpp" , 905, __extension__ __PRETTY_FUNCTION__)); | ||||||
906 | std::vector<std::string> SemaChecks; | ||||||
907 | |||||||
908 | for (const auto &kv : ImmediateArgs) { | ||||||
909 | const ImmediateArg &IA = kv.second; | ||||||
910 | |||||||
911 | llvm::APInt lo(128, 0), hi(128, 0); | ||||||
912 | switch (IA.boundsType) { | ||||||
913 | case ImmediateArg::BoundsType::ExplicitRange: | ||||||
914 | lo = IA.i1; | ||||||
915 | hi = IA.i2; | ||||||
916 | break; | ||||||
917 | case ImmediateArg::BoundsType::UInt: | ||||||
918 | lo = 0; | ||||||
919 | hi = llvm::APInt::getMaxValue(IA.i1).zext(128); | ||||||
920 | break; | ||||||
921 | } | ||||||
922 | |||||||
923 | std::string Index = utostr(kv.first); | ||||||
924 | |||||||
925 | // Emit a range check if the legal range of values for the | ||||||
926 | // immediate is smaller than the _possible_ range of values for | ||||||
927 | // its type. | ||||||
928 | unsigned ArgTypeBits = IA.ArgType->sizeInBits(); | ||||||
929 | llvm::APInt ArgTypeRange = llvm::APInt::getMaxValue(ArgTypeBits).zext(128); | ||||||
930 | llvm::APInt ActualRange = (hi-lo).trunc(64).sext(128); | ||||||
931 | if (ActualRange.ult(ArgTypeRange)) | ||||||
932 | SemaChecks.push_back("SemaBuiltinConstantArgRange(TheCall, " + Index + | ||||||
933 | ", " + signedHexLiteral(lo) + ", " + | ||||||
934 | signedHexLiteral(hi) + ")"); | ||||||
935 | |||||||
936 | if (!IA.ExtraCheckType.empty()) { | ||||||
937 | std::string Suffix; | ||||||
938 | if (!IA.ExtraCheckArgs.empty()) { | ||||||
939 | std::string tmp; | ||||||
940 | StringRef Arg = IA.ExtraCheckArgs; | ||||||
941 | if (Arg == "!lanesize") { | ||||||
942 | tmp = utostr(IA.ArgType->sizeInBits()); | ||||||
943 | Arg = tmp; | ||||||
944 | } | ||||||
945 | Suffix = (Twine(", ") + Arg).str(); | ||||||
946 | } | ||||||
947 | SemaChecks.push_back((Twine("SemaBuiltinConstantArg") + | ||||||
948 | IA.ExtraCheckType + "(TheCall, " + Index + | ||||||
949 | Suffix + ")") | ||||||
950 | .str()); | ||||||
951 | } | ||||||
952 | |||||||
953 | assert(!SemaChecks.empty())(static_cast <bool> (!SemaChecks.empty()) ? void (0) : __assert_fail ("!SemaChecks.empty()", "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/clang/utils/TableGen/MveEmitter.cpp" , 953, __extension__ __PRETTY_FUNCTION__)); | ||||||
954 | } | ||||||
955 | if (SemaChecks.empty()) | ||||||
956 | return ""; | ||||||
957 | return join(std::begin(SemaChecks), std::end(SemaChecks), | ||||||
958 | " ||\n ") + | ||||||
959 | ";\n"; | ||||||
960 | } | ||||||
961 | |||||||
962 | ACLEIntrinsic(EmitterBase &ME, Record *R, const Type *Param); | ||||||
963 | }; | ||||||
964 | |||||||
965 | // ----------------------------------------------------------------------------- | ||||||
966 | // The top-level class that holds all the state from analyzing the entire | ||||||
967 | // Tablegen input. | ||||||
968 | |||||||
969 | class EmitterBase { | ||||||
970 | protected: | ||||||
971 | // EmitterBase holds a collection of all the types we've instantiated. | ||||||
972 | VoidType Void; | ||||||
973 | std::map<std::string, std::unique_ptr<ScalarType>> ScalarTypes; | ||||||
974 | std::map<std::tuple<ScalarTypeKind, unsigned, unsigned>, | ||||||
975 | std::unique_ptr<VectorType>> | ||||||
976 | VectorTypes; | ||||||
977 | std::map<std::pair<std::string, unsigned>, std::unique_ptr<MultiVectorType>> | ||||||
978 | MultiVectorTypes; | ||||||
979 | std::map<unsigned, std::unique_ptr<PredicateType>> PredicateTypes; | ||||||
980 | std::map<std::string, std::unique_ptr<PointerType>> PointerTypes; | ||||||
981 | |||||||
982 | // And all the ACLEIntrinsic instances we've created. | ||||||
983 | std::map<std::string, std::unique_ptr<ACLEIntrinsic>> ACLEIntrinsics; | ||||||
984 | |||||||
985 | public: | ||||||
986 | // Methods to create a Type object, or return the right existing one from the | ||||||
987 | // maps stored in this object. | ||||||
988 | const VoidType *getVoidType() { return &Void; } | ||||||
989 | const ScalarType *getScalarType(StringRef Name) { | ||||||
990 | return ScalarTypes[std::string(Name)].get(); | ||||||
991 | } | ||||||
992 | const ScalarType *getScalarType(Record *R) { | ||||||
993 | return getScalarType(R->getName()); | ||||||
994 | } | ||||||
995 | const VectorType *getVectorType(const ScalarType *ST, unsigned Lanes) { | ||||||
996 | std::tuple<ScalarTypeKind, unsigned, unsigned> key(ST->kind(), | ||||||
997 | ST->sizeInBits(), Lanes); | ||||||
998 | if (VectorTypes.find(key) == VectorTypes.end()) | ||||||
999 | VectorTypes[key] = std::make_unique<VectorType>(ST, Lanes); | ||||||
1000 | return VectorTypes[key].get(); | ||||||
1001 | } | ||||||
1002 | const VectorType *getVectorType(const ScalarType *ST) { | ||||||
1003 | return getVectorType(ST, 128 / ST->sizeInBits()); | ||||||
1004 | } | ||||||
1005 | const MultiVectorType *getMultiVectorType(unsigned Registers, | ||||||
1006 | const VectorType *VT) { | ||||||
1007 | std::pair<std::string, unsigned> key(VT->cNameBase(), Registers); | ||||||
1008 | if (MultiVectorTypes.find(key) == MultiVectorTypes.end()) | ||||||
1009 | MultiVectorTypes[key] = std::make_unique<MultiVectorType>(Registers, VT); | ||||||
1010 | return MultiVectorTypes[key].get(); | ||||||
1011 | } | ||||||
1012 | const PredicateType *getPredicateType(unsigned Lanes) { | ||||||
1013 | unsigned key = Lanes; | ||||||
1014 | if (PredicateTypes.find(key) == PredicateTypes.end()) | ||||||
1015 | PredicateTypes[key] = std::make_unique<PredicateType>(Lanes); | ||||||
1016 | return PredicateTypes[key].get(); | ||||||
1017 | } | ||||||
1018 | const PointerType *getPointerType(const Type *T, bool Const) { | ||||||
1019 | PointerType PT(T, Const); | ||||||
1020 | std::string key = PT.cName(); | ||||||
1021 | if (PointerTypes.find(key) == PointerTypes.end()) | ||||||
1022 | PointerTypes[key] = std::make_unique<PointerType>(PT); | ||||||
1023 | return PointerTypes[key].get(); | ||||||
1024 | } | ||||||
1025 | |||||||
1026 | // Methods to construct a type from various pieces of Tablegen. These are | ||||||
1027 | // always called in the context of setting up a particular ACLEIntrinsic, so | ||||||
1028 | // there's always an ambient parameter type (because we're iterating through | ||||||
1029 | // the Params list in the Tablegen record for the intrinsic), which is used | ||||||
1030 | // to expand Tablegen classes like 'Vector' which mean something different in | ||||||
1031 | // each member of a parametric family. | ||||||
1032 | const Type *getType(Record *R, const Type *Param); | ||||||
1033 | const Type *getType(DagInit *D, const Type *Param); | ||||||
1034 | const Type *getType(Init *I, const Type *Param); | ||||||
1035 | |||||||
1036 | // Functions that translate the Tablegen representation of an intrinsic's | ||||||
1037 | // code generation into a collection of Value objects (which will then be | ||||||
1038 | // reprocessed to read out the actual C++ code included by CGBuiltin.cpp). | ||||||
1039 | Result::Ptr getCodeForDag(DagInit *D, const Result::Scope &Scope, | ||||||
1040 | const Type *Param); | ||||||
1041 | Result::Ptr getCodeForDagArg(DagInit *D, unsigned ArgNum, | ||||||
1042 | const Result::Scope &Scope, const Type *Param); | ||||||
1043 | Result::Ptr getCodeForArg(unsigned ArgNum, const Type *ArgType, bool Promote, | ||||||
1044 | bool Immediate); | ||||||
1045 | |||||||
1046 | void GroupSemaChecks(std::map<std::string, std::set<std::string>> &Checks); | ||||||
1047 | |||||||
1048 | // Constructor and top-level functions. | ||||||
1049 | |||||||
1050 | EmitterBase(RecordKeeper &Records); | ||||||
1051 | virtual ~EmitterBase() = default; | ||||||
1052 | |||||||
1053 | virtual void EmitHeader(raw_ostream &OS) = 0; | ||||||
1054 | virtual void EmitBuiltinDef(raw_ostream &OS) = 0; | ||||||
1055 | virtual void EmitBuiltinSema(raw_ostream &OS) = 0; | ||||||
1056 | void EmitBuiltinCG(raw_ostream &OS); | ||||||
1057 | void EmitBuiltinAliases(raw_ostream &OS); | ||||||
1058 | }; | ||||||
1059 | |||||||
1060 | const Type *EmitterBase::getType(Init *I, const Type *Param) { | ||||||
1061 | if (auto Dag = dyn_cast<DagInit>(I)) | ||||||
1062 | return getType(Dag, Param); | ||||||
1063 | if (auto Def = dyn_cast<DefInit>(I)) | ||||||
1064 | return getType(Def->getDef(), Param); | ||||||
1065 | |||||||
1066 | PrintFatalError("Could not convert this value into a type"); | ||||||
1067 | } | ||||||
1068 | |||||||
1069 | const Type *EmitterBase::getType(Record *R, const Type *Param) { | ||||||
1070 | // Pass to a subfield of any wrapper records. We don't expect more than one | ||||||
1071 | // of these: immediate operands are used as plain numbers rather than as | ||||||
1072 | // llvm::Value, so it's meaningless to promote their type anyway. | ||||||
1073 | if (R->isSubClassOf("Immediate")) | ||||||
1074 | R = R->getValueAsDef("type"); | ||||||
1075 | else if (R->isSubClassOf("unpromoted")) | ||||||
1076 | R = R->getValueAsDef("underlying_type"); | ||||||
1077 | |||||||
1078 | if (R->getName() == "Void") | ||||||
1079 | return getVoidType(); | ||||||
1080 | if (R->isSubClassOf("PrimitiveType")) | ||||||
1081 | return getScalarType(R); | ||||||
1082 | if (R->isSubClassOf("ComplexType")) | ||||||
1083 | return getType(R->getValueAsDag("spec"), Param); | ||||||
1084 | |||||||
1085 | PrintFatalError(R->getLoc(), "Could not convert this record into a type"); | ||||||
1086 | } | ||||||
1087 | |||||||
1088 | const Type *EmitterBase::getType(DagInit *D, const Type *Param) { | ||||||
1089 | // The meat of the getType system: types in the Tablegen are represented by a | ||||||
1090 | // dag whose operators select sub-cases of this function. | ||||||
1091 | |||||||
1092 | Record *Op = cast<DefInit>(D->getOperator())->getDef(); | ||||||
1093 | if (!Op->isSubClassOf("ComplexTypeOp")) | ||||||
1094 | PrintFatalError( | ||||||
1095 | "Expected ComplexTypeOp as dag operator in type expression"); | ||||||
1096 | |||||||
1097 | if (Op->getName() == "CTO_Parameter") { | ||||||
1098 | if (isa<VoidType>(Param)) | ||||||
1099 | PrintFatalError("Parametric type in unparametrised context"); | ||||||
1100 | return Param; | ||||||
1101 | } | ||||||
1102 | |||||||
1103 | if (Op->getName() == "CTO_Vec") { | ||||||
1104 | const Type *Element = getType(D->getArg(0), Param); | ||||||
1105 | if (D->getNumArgs() == 1) { | ||||||
1106 | return getVectorType(cast<ScalarType>(Element)); | ||||||
1107 | } else { | ||||||
1108 | const Type *ExistingVector = getType(D->getArg(1), Param); | ||||||
1109 | return getVectorType(cast<ScalarType>(Element), | ||||||
1110 | cast<VectorType>(ExistingVector)->lanes()); | ||||||
1111 | } | ||||||
1112 | } | ||||||
1113 | |||||||
1114 | if (Op->getName() == "CTO_Pred") { | ||||||
1115 | const Type *Element = getType(D->getArg(0), Param); | ||||||
1116 | return getPredicateType(128 / Element->sizeInBits()); | ||||||
1117 | } | ||||||
1118 | |||||||
1119 | if (Op->isSubClassOf("CTO_Tuple")) { | ||||||
1120 | unsigned Registers = Op->getValueAsInt("n"); | ||||||
1121 | const Type *Element = getType(D->getArg(0), Param); | ||||||
1122 | return getMultiVectorType(Registers, cast<VectorType>(Element)); | ||||||
1123 | } | ||||||
1124 | |||||||
1125 | if (Op->isSubClassOf("CTO_Pointer")) { | ||||||
1126 | const Type *Pointee = getType(D->getArg(0), Param); | ||||||
1127 | return getPointerType(Pointee, Op->getValueAsBit("const")); | ||||||
1128 | } | ||||||
1129 | |||||||
1130 | if (Op->getName() == "CTO_CopyKind") { | ||||||
1131 | const ScalarType *STSize = cast<ScalarType>(getType(D->getArg(0), Param)); | ||||||
1132 | const ScalarType *STKind = cast<ScalarType>(getType(D->getArg(1), Param)); | ||||||
1133 | for (const auto &kv : ScalarTypes) { | ||||||
1134 | const ScalarType *RT = kv.second.get(); | ||||||
1135 | if (RT->kind() == STKind->kind() && RT->sizeInBits() == STSize->sizeInBits()) | ||||||
1136 | return RT; | ||||||
1137 | } | ||||||
1138 | PrintFatalError("Cannot find a type to satisfy CopyKind"); | ||||||
1139 | } | ||||||
1140 | |||||||
1141 | if (Op->isSubClassOf("CTO_ScaleSize")) { | ||||||
1142 | const ScalarType *STKind = cast<ScalarType>(getType(D->getArg(0), Param)); | ||||||
1143 | int Num = Op->getValueAsInt("num"), Denom = Op->getValueAsInt("denom"); | ||||||
1144 | unsigned DesiredSize = STKind->sizeInBits() * Num / Denom; | ||||||
1145 | for (const auto &kv : ScalarTypes) { | ||||||
1146 | const ScalarType *RT = kv.second.get(); | ||||||
1147 | if (RT->kind() == STKind->kind() && RT->sizeInBits() == DesiredSize) | ||||||
1148 | return RT; | ||||||
1149 | } | ||||||
1150 | PrintFatalError("Cannot find a type to satisfy ScaleSize"); | ||||||
1151 | } | ||||||
1152 | |||||||
1153 | PrintFatalError("Bad operator in type dag expression"); | ||||||
1154 | } | ||||||
1155 | |||||||
1156 | Result::Ptr EmitterBase::getCodeForDag(DagInit *D, const Result::Scope &Scope, | ||||||
1157 | const Type *Param) { | ||||||
1158 | Record *Op = cast<DefInit>(D->getOperator())->getDef(); | ||||||
1159 | |||||||
1160 | if (Op->getName() == "seq") { | ||||||
1161 | Result::Scope SubScope = Scope; | ||||||
1162 | Result::Ptr PrevV = nullptr; | ||||||
1163 | for (unsigned i = 0, e = D->getNumArgs(); i < e; ++i) { | ||||||
1164 | // We don't use getCodeForDagArg here, because the argument name | ||||||
1165 | // has different semantics in a seq | ||||||
1166 | Result::Ptr V = | ||||||
1167 | getCodeForDag(cast<DagInit>(D->getArg(i)), SubScope, Param); | ||||||
1168 | StringRef ArgName = D->getArgNameStr(i); | ||||||
1169 | if (!ArgName.empty()) | ||||||
1170 | SubScope[std::string(ArgName)] = V; | ||||||
1171 | if (PrevV) | ||||||
1172 | V->setPredecessor(PrevV); | ||||||
1173 | PrevV = V; | ||||||
1174 | } | ||||||
1175 | return PrevV; | ||||||
1176 | } else if (Op->isSubClassOf("Type")) { | ||||||
1177 | if (D->getNumArgs() != 1) | ||||||
1178 | PrintFatalError("Type casts should have exactly one argument"); | ||||||
1179 | const Type *CastType = getType(Op, Param); | ||||||
1180 | Result::Ptr Arg = getCodeForDagArg(D, 0, Scope, Param); | ||||||
1181 | if (const auto *ST = dyn_cast<ScalarType>(CastType)) { | ||||||
1182 | if (!ST->requiresFloat()) { | ||||||
1183 | if (Arg->hasIntegerConstantValue()) | ||||||
1184 | return std::make_shared<IntLiteralResult>( | ||||||
1185 | ST, Arg->integerConstantValue()); | ||||||
1186 | else | ||||||
1187 | return std::make_shared<IntCastResult>(ST, Arg); | ||||||
1188 | } | ||||||
1189 | } else if (const auto *PT = dyn_cast<PointerType>(CastType)) { | ||||||
1190 | return std::make_shared<PointerCastResult>(PT, Arg); | ||||||
1191 | } | ||||||
1192 | PrintFatalError("Unsupported type cast"); | ||||||
1193 | } else if (Op->getName() == "address") { | ||||||
1194 | if (D->getNumArgs() != 2) | ||||||
1195 | PrintFatalError("'address' should have two arguments"); | ||||||
1196 | Result::Ptr Arg = getCodeForDagArg(D, 0, Scope, Param); | ||||||
1197 | unsigned Alignment; | ||||||
1198 | if (auto *II = dyn_cast<IntInit>(D->getArg(1))) { | ||||||
1199 | Alignment = II->getValue(); | ||||||
1200 | } else { | ||||||
1201 | PrintFatalError("'address' alignment argument should be an integer"); | ||||||
1202 | } | ||||||
1203 | return std::make_shared<AddressResult>(Arg, Alignment); | ||||||
1204 | } else if (Op->getName() == "unsignedflag") { | ||||||
1205 | if (D->getNumArgs() != 1) | ||||||
1206 | PrintFatalError("unsignedflag should have exactly one argument"); | ||||||
1207 | Record *TypeRec = cast<DefInit>(D->getArg(0))->getDef(); | ||||||
1208 | if (!TypeRec->isSubClassOf("Type")) | ||||||
1209 | PrintFatalError("unsignedflag's argument should be a type"); | ||||||
1210 | if (const auto *ST = dyn_cast<ScalarType>(getType(TypeRec, Param))) { | ||||||
1211 | return std::make_shared<IntLiteralResult>( | ||||||
1212 | getScalarType("u32"), ST->kind() == ScalarTypeKind::UnsignedInt); | ||||||
1213 | } else { | ||||||
1214 | PrintFatalError("unsignedflag's argument should be a scalar type"); | ||||||
1215 | } | ||||||
1216 | } else if (Op->getName() == "bitsize") { | ||||||
1217 | if (D->getNumArgs() != 1) | ||||||
1218 | PrintFatalError("bitsize should have exactly one argument"); | ||||||
1219 | Record *TypeRec = cast<DefInit>(D->getArg(0))->getDef(); | ||||||
1220 | if (!TypeRec->isSubClassOf("Type")) | ||||||
1221 | PrintFatalError("bitsize's argument should be a type"); | ||||||
1222 | if (const auto *ST = dyn_cast<ScalarType>(getType(TypeRec, Param))) { | ||||||
1223 | return std::make_shared<IntLiteralResult>(getScalarType("u32"), | ||||||
1224 | ST->sizeInBits()); | ||||||
1225 | } else { | ||||||
1226 | PrintFatalError("bitsize's argument should be a scalar type"); | ||||||
1227 | } | ||||||
1228 | } else { | ||||||
1229 | std::vector<Result::Ptr> Args; | ||||||
1230 | for (unsigned i = 0, e = D->getNumArgs(); i < e; ++i) | ||||||
1231 | Args.push_back(getCodeForDagArg(D, i, Scope, Param)); | ||||||
1232 | if (Op->isSubClassOf("IRBuilderBase")) { | ||||||
1233 | std::set<unsigned> AddressArgs; | ||||||
1234 | std::map<unsigned, std::string> IntegerArgs; | ||||||
1235 | for (Record *sp : Op->getValueAsListOfDefs("special_params")) { | ||||||
1236 | unsigned Index = sp->getValueAsInt("index"); | ||||||
1237 | if (sp->isSubClassOf("IRBuilderAddrParam")) { | ||||||
1238 | AddressArgs.insert(Index); | ||||||
1239 | } else if (sp->isSubClassOf("IRBuilderIntParam")) { | ||||||
1240 | IntegerArgs[Index] = std::string(sp->getValueAsString("type")); | ||||||
1241 | } | ||||||
1242 | } | ||||||
1243 | return std::make_shared<IRBuilderResult>(Op->getValueAsString("prefix"), | ||||||
1244 | Args, AddressArgs, IntegerArgs); | ||||||
1245 | } else if (Op->isSubClassOf("IRIntBase")) { | ||||||
1246 | std::vector<const Type *> ParamTypes; | ||||||
1247 | for (Record *RParam : Op->getValueAsListOfDefs("params")) | ||||||
1248 | ParamTypes.push_back(getType(RParam, Param)); | ||||||
1249 | std::string IntName = std::string(Op->getValueAsString("intname")); | ||||||
1250 | if (Op->getValueAsBit("appendKind")) | ||||||
1251 | IntName += "_" + toLetter(cast<ScalarType>(Param)->kind()); | ||||||
1252 | return std::make_shared<IRIntrinsicResult>(IntName, ParamTypes, Args); | ||||||
1253 | } else { | ||||||
1254 | PrintFatalError("Unsupported dag node " + Op->getName()); | ||||||
1255 | } | ||||||
1256 | } | ||||||
1257 | } | ||||||
1258 | |||||||
1259 | Result::Ptr EmitterBase::getCodeForDagArg(DagInit *D, unsigned ArgNum, | ||||||
1260 | const Result::Scope &Scope, | ||||||
1261 | const Type *Param) { | ||||||
1262 | Init *Arg = D->getArg(ArgNum); | ||||||
1263 | StringRef Name = D->getArgNameStr(ArgNum); | ||||||
1264 | |||||||
1265 | if (!Name.empty()) { | ||||||
1266 | if (!isa<UnsetInit>(Arg)) | ||||||
1267 | PrintFatalError( | ||||||
1268 | "dag operator argument should not have both a value and a name"); | ||||||
1269 | auto it = Scope.find(std::string(Name)); | ||||||
1270 | if (it == Scope.end()) | ||||||
1271 | PrintFatalError("unrecognized variable name '" + Name + "'"); | ||||||
1272 | return it->second; | ||||||
1273 | } | ||||||
1274 | |||||||
1275 | // Sometimes the Arg is a bit. Prior to multiclass template argument | ||||||
1276 | // checking, integers would sneak through the bit declaration, | ||||||
1277 | // but now they really are bits. | ||||||
1278 | if (auto *BI = dyn_cast<BitInit>(Arg)) | ||||||
1279 | return std::make_shared<IntLiteralResult>(getScalarType("u32"), | ||||||
1280 | BI->getValue()); | ||||||
1281 | |||||||
1282 | if (auto *II = dyn_cast<IntInit>(Arg)) | ||||||
1283 | return std::make_shared<IntLiteralResult>(getScalarType("u32"), | ||||||
1284 | II->getValue()); | ||||||
1285 | |||||||
1286 | if (auto *DI = dyn_cast<DagInit>(Arg)) | ||||||
1287 | return getCodeForDag(DI, Scope, Param); | ||||||
1288 | |||||||
1289 | if (auto *DI = dyn_cast<DefInit>(Arg)) { | ||||||
1290 | Record *Rec = DI->getDef(); | ||||||
1291 | if (Rec->isSubClassOf("Type")) { | ||||||
1292 | const Type *T = getType(Rec, Param); | ||||||
1293 | return std::make_shared<TypeResult>(T); | ||||||
1294 | } | ||||||
1295 | } | ||||||
1296 | |||||||
1297 | PrintError("bad DAG argument type for code generation"); | ||||||
1298 | PrintNote("DAG: " + D->getAsString()); | ||||||
1299 | if (TypedInit *Typed = dyn_cast<TypedInit>(Arg)) | ||||||
1300 | PrintNote("argument type: " + Typed->getType()->getAsString()); | ||||||
1301 | PrintFatalNote("argument number " + Twine(ArgNum) + ": " + Arg->getAsString()); | ||||||
1302 | } | ||||||
1303 | |||||||
1304 | Result::Ptr EmitterBase::getCodeForArg(unsigned ArgNum, const Type *ArgType, | ||||||
1305 | bool Promote, bool Immediate) { | ||||||
1306 | Result::Ptr V = std::make_shared<BuiltinArgResult>( | ||||||
1307 | ArgNum, isa<PointerType>(ArgType), Immediate); | ||||||
1308 | |||||||
1309 | if (Promote) { | ||||||
1310 | if (const auto *ST = dyn_cast<ScalarType>(ArgType)) { | ||||||
1311 | if (ST->isInteger() && ST->sizeInBits() < 32) | ||||||
1312 | V = std::make_shared<IntCastResult>(getScalarType("u32"), V); | ||||||
1313 | } else if (const auto *PT = dyn_cast<PredicateType>(ArgType)) { | ||||||
1314 | V = std::make_shared<IntCastResult>(getScalarType("u32"), V); | ||||||
1315 | V = std::make_shared<IRIntrinsicResult>("arm_mve_pred_i2v", | ||||||
1316 | std::vector<const Type *>{PT}, | ||||||
1317 | std::vector<Result::Ptr>{V}); | ||||||
1318 | } | ||||||
1319 | } | ||||||
1320 | |||||||
1321 | return V; | ||||||
1322 | } | ||||||
1323 | |||||||
1324 | ACLEIntrinsic::ACLEIntrinsic(EmitterBase &ME, Record *R, const Type *Param) | ||||||
1325 | : ReturnType(ME.getType(R->getValueAsDef("ret"), Param)) { | ||||||
1326 | // Derive the intrinsic's full name, by taking the name of the | ||||||
1327 | // Tablegen record (or override) and appending the suffix from its | ||||||
1328 | // parameter type. (If the intrinsic is unparametrised, its | ||||||
1329 | // parameter type will be given as Void, which returns the empty | ||||||
1330 | // string for acleSuffix.) | ||||||
1331 | StringRef BaseName = | ||||||
1332 | (R->isSubClassOf("NameOverride") ? R->getValueAsString("basename") | ||||||
1333 | : R->getName()); | ||||||
1334 | StringRef overrideLetter = R->getValueAsString("overrideKindLetter"); | ||||||
1335 | FullName = | ||||||
1336 | (Twine(BaseName) + Param->acleSuffix(std::string(overrideLetter))).str(); | ||||||
1337 | |||||||
1338 | // Derive the intrinsic's polymorphic name, by removing components from the | ||||||
1339 | // full name as specified by its 'pnt' member ('polymorphic name type'), | ||||||
1340 | // which indicates how many type suffixes to remove, and any other piece of | ||||||
1341 | // the name that should be removed. | ||||||
1342 | Record *PolymorphicNameType = R->getValueAsDef("pnt"); | ||||||
1343 | SmallVector<StringRef, 8> NameParts; | ||||||
1344 | StringRef(FullName).split(NameParts, '_'); | ||||||
1345 | for (unsigned i = 0, e = PolymorphicNameType->getValueAsInt( | ||||||
1346 | "NumTypeSuffixesToDiscard"); | ||||||
1347 | i < e; ++i) | ||||||
1348 | NameParts.pop_back(); | ||||||
1349 | if (!PolymorphicNameType->isValueUnset("ExtraSuffixToDiscard")) { | ||||||
1350 | StringRef ExtraSuffix = | ||||||
1351 | PolymorphicNameType->getValueAsString("ExtraSuffixToDiscard"); | ||||||
1352 | auto it = NameParts.end(); | ||||||
1353 | while (it != NameParts.begin()) { | ||||||
1354 | --it; | ||||||
1355 | if (*it == ExtraSuffix) { | ||||||
1356 | NameParts.erase(it); | ||||||
1357 | break; | ||||||
1358 | } | ||||||
1359 | } | ||||||
1360 | } | ||||||
1361 | ShortName = join(std::begin(NameParts), std::end(NameParts), "_"); | ||||||
1362 | |||||||
1363 | BuiltinExtension = R->getValueAsString("builtinExtension"); | ||||||
1364 | |||||||
1365 | PolymorphicOnly = R->getValueAsBit("polymorphicOnly"); | ||||||
1366 | NonEvaluating = R->getValueAsBit("nonEvaluating"); | ||||||
1367 | HeaderOnly = R->getValueAsBit("headerOnly"); | ||||||
1368 | |||||||
1369 | // Process the intrinsic's argument list. | ||||||
1370 | DagInit *ArgsDag = R->getValueAsDag("args"); | ||||||
1371 | Result::Scope Scope; | ||||||
1372 | for (unsigned i = 0, e = ArgsDag->getNumArgs(); i < e; ++i) { | ||||||
1373 | Init *TypeInit = ArgsDag->getArg(i); | ||||||
1374 | |||||||
1375 | bool Promote = true; | ||||||
1376 | if (auto TypeDI
| ||||||
1377 | if (TypeDI->getDef()->isSubClassOf("unpromoted")) | ||||||
1378 | Promote = false; | ||||||
1379 | |||||||
1380 | // Work out the type of the argument, for use in the function prototype in | ||||||
1381 | // the header file. | ||||||
1382 | const Type *ArgType = ME.getType(TypeInit, Param); | ||||||
1383 | ArgTypes.push_back(ArgType); | ||||||
1384 | |||||||
1385 | // If the argument is a subclass of Immediate, record the details about | ||||||
1386 | // what values it can take, for Sema checking. | ||||||
1387 | bool Immediate = false; | ||||||
1388 | if (auto TypeDI
| ||||||
1389 | Record *TypeRec = TypeDI->getDef(); | ||||||
1390 | if (TypeRec->isSubClassOf("Immediate")) { | ||||||
1391 | Immediate = true; | ||||||
1392 | |||||||
1393 | Record *Bounds = TypeRec->getValueAsDef("bounds"); | ||||||
1394 | ImmediateArg &IA = ImmediateArgs[i]; | ||||||
1395 | if (Bounds->isSubClassOf("IB_ConstRange")) { | ||||||
1396 | IA.boundsType = ImmediateArg::BoundsType::ExplicitRange; | ||||||
1397 | IA.i1 = Bounds->getValueAsInt("lo"); | ||||||
1398 | IA.i2 = Bounds->getValueAsInt("hi"); | ||||||
1399 | } else if (Bounds->getName() == "IB_UEltValue") { | ||||||
1400 | IA.boundsType = ImmediateArg::BoundsType::UInt; | ||||||
1401 | IA.i1 = Param->sizeInBits(); | ||||||
1402 | } else if (Bounds->getName() == "IB_LaneIndex") { | ||||||
1403 | IA.boundsType = ImmediateArg::BoundsType::ExplicitRange; | ||||||
1404 | IA.i1 = 0; | ||||||
1405 | IA.i2 = 128 / Param->sizeInBits() - 1; | ||||||
| |||||||
1406 | } else if (Bounds->isSubClassOf("IB_EltBit")) { | ||||||
1407 | IA.boundsType = ImmediateArg::BoundsType::ExplicitRange; | ||||||
1408 | IA.i1 = Bounds->getValueAsInt("base"); | ||||||
1409 | const Type *T = ME.getType(Bounds->getValueAsDef("type"), Param); | ||||||
1410 | IA.i2 = IA.i1 + T->sizeInBits() - 1; | ||||||
1411 | } else { | ||||||
1412 | PrintFatalError("unrecognised ImmediateBounds subclass"); | ||||||
1413 | } | ||||||
1414 | |||||||
1415 | IA.ArgType = ArgType; | ||||||
1416 | |||||||
1417 | if (!TypeRec->isValueUnset("extra")) { | ||||||
1418 | IA.ExtraCheckType = TypeRec->getValueAsString("extra"); | ||||||
1419 | if (!TypeRec->isValueUnset("extraarg")) | ||||||
1420 | IA.ExtraCheckArgs = TypeRec->getValueAsString("extraarg"); | ||||||
1421 | } | ||||||
1422 | } | ||||||
1423 | } | ||||||
1424 | |||||||
1425 | // The argument will usually have a name in the arguments dag, which goes | ||||||
1426 | // into the variable-name scope that the code gen will refer to. | ||||||
1427 | StringRef ArgName = ArgsDag->getArgNameStr(i); | ||||||
1428 | if (!ArgName.empty()) | ||||||
1429 | Scope[std::string(ArgName)] = | ||||||
1430 | ME.getCodeForArg(i, ArgType, Promote, Immediate); | ||||||
1431 | } | ||||||
1432 | |||||||
1433 | // Finally, go through the codegen dag and translate it into a Result object | ||||||
1434 | // (with an arbitrary DAG of depended-on Results hanging off it). | ||||||
1435 | DagInit *CodeDag = R->getValueAsDag("codegen"); | ||||||
1436 | Record *MainOp = cast<DefInit>(CodeDag->getOperator())->getDef(); | ||||||
1437 | if (MainOp->isSubClassOf("CustomCodegen")) { | ||||||
1438 | // Or, if it's the special case of CustomCodegen, just accumulate | ||||||
1439 | // a list of parameters we're going to assign to variables before | ||||||
1440 | // breaking from the loop. | ||||||
1441 | CustomCodeGenArgs["CustomCodeGenType"] = | ||||||
1442 | (Twine("CustomCodeGen::") + MainOp->getValueAsString("type")).str(); | ||||||
1443 | for (unsigned i = 0, e = CodeDag->getNumArgs(); i < e; ++i) { | ||||||
1444 | StringRef Name = CodeDag->getArgNameStr(i); | ||||||
1445 | if (Name.empty()) { | ||||||
1446 | PrintFatalError("Operands to CustomCodegen should have names"); | ||||||
1447 | } else if (auto *II = dyn_cast<IntInit>(CodeDag->getArg(i))) { | ||||||
1448 | CustomCodeGenArgs[std::string(Name)] = itostr(II->getValue()); | ||||||
1449 | } else if (auto *SI = dyn_cast<StringInit>(CodeDag->getArg(i))) { | ||||||
1450 | CustomCodeGenArgs[std::string(Name)] = std::string(SI->getValue()); | ||||||
1451 | } else { | ||||||
1452 | PrintFatalError("Operands to CustomCodegen should be integers"); | ||||||
1453 | } | ||||||
1454 | } | ||||||
1455 | } else { | ||||||
1456 | Code = ME.getCodeForDag(CodeDag, Scope, Param); | ||||||
1457 | } | ||||||
1458 | } | ||||||
1459 | |||||||
1460 | EmitterBase::EmitterBase(RecordKeeper &Records) { | ||||||
1461 | // Construct the whole EmitterBase. | ||||||
1462 | |||||||
1463 | // First, look up all the instances of PrimitiveType. This gives us the list | ||||||
1464 | // of vector typedefs we have to put in arm_mve.h, and also allows us to | ||||||
1465 | // collect all the useful ScalarType instances into a big list so that we can | ||||||
1466 | // use it for operations such as 'find the unsigned version of this signed | ||||||
1467 | // integer type'. | ||||||
1468 | for (Record *R : Records.getAllDerivedDefinitions("PrimitiveType")) | ||||||
1469 | ScalarTypes[std::string(R->getName())] = std::make_unique<ScalarType>(R); | ||||||
1470 | |||||||
1471 | // Now go through the instances of Intrinsic, and for each one, iterate | ||||||
1472 | // through its list of type parameters making an ACLEIntrinsic for each one. | ||||||
1473 | for (Record *R : Records.getAllDerivedDefinitions("Intrinsic")) { | ||||||
1474 | for (Record *RParam : R->getValueAsListOfDefs("params")) { | ||||||
1475 | const Type *Param = getType(RParam, getVoidType()); | ||||||
1476 | auto Intrinsic = std::make_unique<ACLEIntrinsic>(*this, R, Param); | ||||||
1477 | ACLEIntrinsics[Intrinsic->fullName()] = std::move(Intrinsic); | ||||||
1478 | } | ||||||
1479 | } | ||||||
1480 | } | ||||||
1481 | |||||||
1482 | /// A wrapper on raw_string_ostream that contains its own buffer rather than | ||||||
1483 | /// having to point it at one elsewhere. (In other words, it works just like | ||||||
1484 | /// std::ostringstream; also, this makes it convenient to declare a whole array | ||||||
1485 | /// of them at once.) | ||||||
1486 | /// | ||||||
1487 | /// We have to set this up using multiple inheritance, to ensure that the | ||||||
1488 | /// string member has been constructed before raw_string_ostream's constructor | ||||||
1489 | /// is given a pointer to it. | ||||||
1490 | class string_holder { | ||||||
1491 | protected: | ||||||
1492 | std::string S; | ||||||
1493 | }; | ||||||
1494 | class raw_self_contained_string_ostream : private string_holder, | ||||||
1495 | public raw_string_ostream { | ||||||
1496 | public: | ||||||
1497 | raw_self_contained_string_ostream() | ||||||
1498 | : string_holder(), raw_string_ostream(S) {} | ||||||
1499 | }; | ||||||
1500 | |||||||
1501 | const char LLVMLicenseHeader[] = | ||||||
1502 | " *\n" | ||||||
1503 | " *\n" | ||||||
1504 | " * Part of the LLVM Project, under the Apache License v2.0 with LLVM" | ||||||
1505 | " Exceptions.\n" | ||||||
1506 | " * See https://llvm.org/LICENSE.txt for license information.\n" | ||||||
1507 | " * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception\n" | ||||||
1508 | " *\n" | ||||||
1509 | " *===-----------------------------------------------------------------" | ||||||
1510 | "------===\n" | ||||||
1511 | " */\n" | ||||||
1512 | "\n"; | ||||||
1513 | |||||||
1514 | // Machinery for the grouping of intrinsics by similar codegen. | ||||||
1515 | // | ||||||
1516 | // The general setup is that 'MergeableGroup' stores the things that a set of | ||||||
1517 | // similarly shaped intrinsics have in common: the text of their code | ||||||
1518 | // generation, and the number and type of their parameter variables. | ||||||
1519 | // MergeableGroup is the key in a std::map whose value is a set of | ||||||
1520 | // OutputIntrinsic, which stores the ways in which a particular intrinsic | ||||||
1521 | // specializes the MergeableGroup's generic description: the function name and | ||||||
1522 | // the _values_ of the parameter variables. | ||||||
1523 | |||||||
1524 | struct ComparableStringVector : std::vector<std::string> { | ||||||
1525 | // Infrastructure: a derived class of vector<string> which comes with an | ||||||
1526 | // ordering, so that it can be used as a key in maps and an element in sets. | ||||||
1527 | // There's no requirement on the ordering beyond being deterministic. | ||||||
1528 | bool operator<(const ComparableStringVector &rhs) const { | ||||||
1529 | if (size() != rhs.size()) | ||||||
1530 | return size() < rhs.size(); | ||||||
1531 | for (size_t i = 0, e = size(); i < e; ++i) | ||||||
1532 | if ((*this)[i] != rhs[i]) | ||||||
1533 | return (*this)[i] < rhs[i]; | ||||||
1534 | return false; | ||||||
1535 | } | ||||||
1536 | }; | ||||||
1537 | |||||||
1538 | struct OutputIntrinsic { | ||||||
1539 | const ACLEIntrinsic *Int; | ||||||
1540 | std::string Name; | ||||||
1541 | ComparableStringVector ParamValues; | ||||||
1542 | bool operator<(const OutputIntrinsic &rhs) const { | ||||||
1543 | if (Name != rhs.Name) | ||||||
1544 | return Name < rhs.Name; | ||||||
1545 | return ParamValues < rhs.ParamValues; | ||||||
1546 | } | ||||||
1547 | }; | ||||||
1548 | struct MergeableGroup { | ||||||
1549 | std::string Code; | ||||||
1550 | ComparableStringVector ParamTypes; | ||||||
1551 | bool operator<(const MergeableGroup &rhs) const { | ||||||
1552 | if (Code != rhs.Code) | ||||||
1553 | return Code < rhs.Code; | ||||||
1554 | return ParamTypes < rhs.ParamTypes; | ||||||
1555 | } | ||||||
1556 | }; | ||||||
1557 | |||||||
1558 | void EmitterBase::EmitBuiltinCG(raw_ostream &OS) { | ||||||
1559 | // Pass 1: generate code for all the intrinsics as if every type or constant | ||||||
1560 | // that can possibly be abstracted out into a parameter variable will be. | ||||||
1561 | // This identifies the sets of intrinsics we'll group together into a single | ||||||
1562 | // piece of code generation. | ||||||
1563 | |||||||
1564 | std::map<MergeableGroup, std::set<OutputIntrinsic>> MergeableGroupsPrelim; | ||||||
1565 | |||||||
1566 | for (const auto &kv : ACLEIntrinsics) { | ||||||
1567 | const ACLEIntrinsic &Int = *kv.second; | ||||||
1568 | if (Int.headerOnly()) | ||||||
1569 | continue; | ||||||
1570 | |||||||
1571 | MergeableGroup MG; | ||||||
1572 | OutputIntrinsic OI; | ||||||
1573 | |||||||
1574 | OI.Int = ∬ | ||||||
1575 | OI.Name = Int.fullName(); | ||||||
1576 | CodeGenParamAllocator ParamAllocPrelim{&MG.ParamTypes, &OI.ParamValues}; | ||||||
1577 | raw_string_ostream OS(MG.Code); | ||||||
1578 | Int.genCode(OS, ParamAllocPrelim, 1); | ||||||
1579 | OS.flush(); | ||||||
1580 | |||||||
1581 | MergeableGroupsPrelim[MG].insert(OI); | ||||||
1582 | } | ||||||
1583 | |||||||
1584 | // Pass 2: for each of those groups, optimize the parameter variable set by | ||||||
1585 | // eliminating 'parameters' that are the same for all intrinsics in the | ||||||
1586 | // group, and merging together pairs of parameter variables that take the | ||||||
1587 | // same values as each other for all intrinsics in the group. | ||||||
1588 | |||||||
1589 | std::map<MergeableGroup, std::set<OutputIntrinsic>> MergeableGroups; | ||||||
1590 | |||||||
1591 | for (const auto &kv : MergeableGroupsPrelim) { | ||||||
1592 | const MergeableGroup &MG = kv.first; | ||||||
1593 | std::vector<int> ParamNumbers; | ||||||
1594 | std::map<ComparableStringVector, int> ParamNumberMap; | ||||||
1595 | |||||||
1596 | // Loop over the parameters for this group. | ||||||
1597 | for (size_t i = 0, e = MG.ParamTypes.size(); i < e; ++i) { | ||||||
1598 | // Is this parameter the same for all intrinsics in the group? | ||||||
1599 | const OutputIntrinsic &OI_first = *kv.second.begin(); | ||||||
1600 | bool Constant = all_of(kv.second, [&](const OutputIntrinsic &OI) { | ||||||
1601 | return OI.ParamValues[i] == OI_first.ParamValues[i]; | ||||||
1602 | }); | ||||||
1603 | |||||||
1604 | // If so, record it as -1, meaning 'no parameter variable needed'. Then | ||||||
1605 | // the corresponding call to allocParam in pass 2 will not generate a | ||||||
1606 | // variable at all, and just use the value inline. | ||||||
1607 | if (Constant) { | ||||||
1608 | ParamNumbers.push_back(-1); | ||||||
1609 | continue; | ||||||
1610 | } | ||||||
1611 | |||||||
1612 | // Otherwise, make a list of the values this parameter takes for each | ||||||
1613 | // intrinsic, and see if that value vector matches anything we already | ||||||
1614 | // have. We also record the parameter type, so that we don't accidentally | ||||||
1615 | // match up two parameter variables with different types. (Not that | ||||||
1616 | // there's much chance of them having textually equivalent values, but in | ||||||
1617 | // _principle_ it could happen.) | ||||||
1618 | ComparableStringVector key; | ||||||
1619 | key.push_back(MG.ParamTypes[i]); | ||||||
1620 | for (const auto &OI : kv.second) | ||||||
1621 | key.push_back(OI.ParamValues[i]); | ||||||
1622 | |||||||
1623 | auto Found = ParamNumberMap.find(key); | ||||||
1624 | if (Found != ParamNumberMap.end()) { | ||||||
1625 | // Yes, an existing parameter variable can be reused for this. | ||||||
1626 | ParamNumbers.push_back(Found->second); | ||||||
1627 | continue; | ||||||
1628 | } | ||||||
1629 | |||||||
1630 | // No, we need a new parameter variable. | ||||||
1631 | int ExistingIndex = ParamNumberMap.size(); | ||||||
1632 | ParamNumberMap[key] = ExistingIndex; | ||||||
1633 | ParamNumbers.push_back(ExistingIndex); | ||||||
1634 | } | ||||||
1635 | |||||||
1636 | // Now we're ready to do the pass 2 code generation, which will emit the | ||||||
1637 | // reduced set of parameter variables we've just worked out. | ||||||
1638 | |||||||
1639 | for (const auto &OI_prelim : kv.second) { | ||||||
1640 | const ACLEIntrinsic *Int = OI_prelim.Int; | ||||||
1641 | |||||||
1642 | MergeableGroup MG; | ||||||
1643 | OutputIntrinsic OI; | ||||||
1644 | |||||||
1645 | OI.Int = OI_prelim.Int; | ||||||
1646 | OI.Name = OI_prelim.Name; | ||||||
1647 | CodeGenParamAllocator ParamAlloc{&MG.ParamTypes, &OI.ParamValues, | ||||||
1648 | &ParamNumbers}; | ||||||
1649 | raw_string_ostream OS(MG.Code); | ||||||
1650 | Int->genCode(OS, ParamAlloc, 2); | ||||||
1651 | OS.flush(); | ||||||
1652 | |||||||
1653 | MergeableGroups[MG].insert(OI); | ||||||
1654 | } | ||||||
1655 | } | ||||||
1656 | |||||||
1657 | // Output the actual C++ code. | ||||||
1658 | |||||||
1659 | for (const auto &kv : MergeableGroups) { | ||||||
1660 | const MergeableGroup &MG = kv.first; | ||||||
1661 | |||||||
1662 | // List of case statements in the main switch on BuiltinID, and an open | ||||||
1663 | // brace. | ||||||
1664 | const char *prefix = ""; | ||||||
1665 | for (const auto &OI : kv.second) { | ||||||
1666 | OS << prefix << "case ARM::BI__builtin_arm_" << OI.Int->builtinExtension() | ||||||
1667 | << "_" << OI.Name << ":"; | ||||||
1668 | |||||||
1669 | prefix = "\n"; | ||||||
1670 | } | ||||||
1671 | OS << " {\n"; | ||||||
1672 | |||||||
1673 | if (!MG.ParamTypes.empty()) { | ||||||
1674 | // If we've got some parameter variables, then emit their declarations... | ||||||
1675 | for (size_t i = 0, e = MG.ParamTypes.size(); i < e; ++i) { | ||||||
1676 | StringRef Type = MG.ParamTypes[i]; | ||||||
1677 | OS << " " << Type; | ||||||
1678 | if (!Type.endswith("*")) | ||||||
1679 | OS << " "; | ||||||
1680 | OS << " Param" << utostr(i) << ";\n"; | ||||||
1681 | } | ||||||
1682 | |||||||
1683 | // ... and an inner switch on BuiltinID that will fill them in with each | ||||||
1684 | // individual intrinsic's values. | ||||||
1685 | OS << " switch (BuiltinID) {\n"; | ||||||
1686 | for (const auto &OI : kv.second) { | ||||||
1687 | OS << " case ARM::BI__builtin_arm_" << OI.Int->builtinExtension() | ||||||
1688 | << "_" << OI.Name << ":\n"; | ||||||
1689 | for (size_t i = 0, e = MG.ParamTypes.size(); i < e; ++i) | ||||||
1690 | OS << " Param" << utostr(i) << " = " << OI.ParamValues[i] << ";\n"; | ||||||
1691 | OS << " break;\n"; | ||||||
1692 | } | ||||||
1693 | OS << " }\n"; | ||||||
1694 | } | ||||||
1695 | |||||||
1696 | // And finally, output the code, and close the outer pair of braces. (The | ||||||
1697 | // code will always end with a 'return' statement, so we need not insert a | ||||||
1698 | // 'break' here.) | ||||||
1699 | OS << MG.Code << "}\n"; | ||||||
1700 | } | ||||||
1701 | } | ||||||
1702 | |||||||
1703 | void EmitterBase::EmitBuiltinAliases(raw_ostream &OS) { | ||||||
1704 | // Build a sorted table of: | ||||||
1705 | // - intrinsic id number | ||||||
1706 | // - full name | ||||||
1707 | // - polymorphic name or -1 | ||||||
1708 | StringToOffsetTable StringTable; | ||||||
1709 | OS << "static const IntrinToName MapData[] = {\n"; | ||||||
1710 | for (const auto &kv : ACLEIntrinsics) { | ||||||
1711 | const ACLEIntrinsic &Int = *kv.second; | ||||||
1712 | if (Int.headerOnly()) | ||||||
1713 | continue; | ||||||
1714 | int32_t ShortNameOffset = | ||||||
1715 | Int.polymorphic() ? StringTable.GetOrAddStringOffset(Int.shortName()) | ||||||
1716 | : -1; | ||||||
1717 | OS << " { ARM::BI__builtin_arm_" << Int.builtinExtension() << "_" | ||||||
1718 | << Int.fullName() << ", " | ||||||
1719 | << StringTable.GetOrAddStringOffset(Int.fullName()) << ", " | ||||||
1720 | << ShortNameOffset << "},\n"; | ||||||
1721 | } | ||||||
1722 | OS << "};\n\n"; | ||||||
1723 | |||||||
1724 | OS << "ArrayRef<IntrinToName> Map(MapData);\n\n"; | ||||||
1725 | |||||||
1726 | OS << "static const char IntrinNames[] = {\n"; | ||||||
1727 | StringTable.EmitString(OS); | ||||||
1728 | OS << "};\n\n"; | ||||||
1729 | } | ||||||
1730 | |||||||
1731 | void EmitterBase::GroupSemaChecks( | ||||||
1732 | std::map<std::string, std::set<std::string>> &Checks) { | ||||||
1733 | for (const auto &kv : ACLEIntrinsics) { | ||||||
1734 | const ACLEIntrinsic &Int = *kv.second; | ||||||
1735 | if (Int.headerOnly()) | ||||||
1736 | continue; | ||||||
1737 | std::string Check = Int.genSema(); | ||||||
1738 | if (!Check.empty()) | ||||||
1739 | Checks[Check].insert(Int.fullName()); | ||||||
1740 | } | ||||||
1741 | } | ||||||
1742 | |||||||
1743 | // ----------------------------------------------------------------------------- | ||||||
1744 | // The class used for generating arm_mve.h and related Clang bits | ||||||
1745 | // | ||||||
1746 | |||||||
1747 | class MveEmitter : public EmitterBase { | ||||||
1748 | public: | ||||||
1749 | MveEmitter(RecordKeeper &Records) : EmitterBase(Records){}; | ||||||
1750 | void EmitHeader(raw_ostream &OS) override; | ||||||
1751 | void EmitBuiltinDef(raw_ostream &OS) override; | ||||||
1752 | void EmitBuiltinSema(raw_ostream &OS) override; | ||||||
1753 | }; | ||||||
1754 | |||||||
1755 | void MveEmitter::EmitHeader(raw_ostream &OS) { | ||||||
1756 | // Accumulate pieces of the header file that will be enabled under various | ||||||
1757 | // different combinations of #ifdef. The index into parts[] is made up of | ||||||
1758 | // the following bit flags. | ||||||
1759 | constexpr unsigned Float = 1; | ||||||
1760 | constexpr unsigned UseUserNamespace = 2; | ||||||
1761 | |||||||
1762 | constexpr unsigned NumParts = 4; | ||||||
1763 | raw_self_contained_string_ostream parts[NumParts]; | ||||||
1764 | |||||||
1765 | // Write typedefs for all the required vector types, and a few scalar | ||||||
1766 | // types that don't already have the name we want them to have. | ||||||
1767 | |||||||
1768 | parts[0] << "typedef uint16_t mve_pred16_t;\n"; | ||||||
1769 | parts[Float] << "typedef __fp16 float16_t;\n" | ||||||
1770 | "typedef float float32_t;\n"; | ||||||
1771 | for (const auto &kv : ScalarTypes) { | ||||||
1772 | const ScalarType *ST = kv.second.get(); | ||||||
1773 | if (ST->hasNonstandardName()) | ||||||
1774 | continue; | ||||||
1775 | raw_ostream &OS = parts[ST->requiresFloat() ? Float : 0]; | ||||||
1776 | const VectorType *VT = getVectorType(ST); | ||||||
1777 | |||||||
1778 | OS << "typedef __attribute__((__neon_vector_type__(" << VT->lanes() | ||||||
1779 | << "), __clang_arm_mve_strict_polymorphism)) " << ST->cName() << " " | ||||||
1780 | << VT->cName() << ";\n"; | ||||||
1781 | |||||||
1782 | // Every vector type also comes with a pair of multi-vector types for | ||||||
1783 | // the VLD2 and VLD4 instructions. | ||||||
1784 | for (unsigned n = 2; n <= 4; n += 2) { | ||||||
1785 | const MultiVectorType *MT = getMultiVectorType(n, VT); | ||||||
1786 | OS << "typedef struct { " << VT->cName() << " val[" << n << "]; } " | ||||||
1787 | << MT->cName() << ";\n"; | ||||||
1788 | } | ||||||
1789 | } | ||||||
1790 | parts[0] << "\n"; | ||||||
1791 | parts[Float] << "\n"; | ||||||
1792 | |||||||
1793 | // Write declarations for all the intrinsics. | ||||||
1794 | |||||||
1795 | for (const auto &kv : ACLEIntrinsics) { | ||||||
1796 | const ACLEIntrinsic &Int = *kv.second; | ||||||
1797 | |||||||
1798 | // We generate each intrinsic twice, under its full unambiguous | ||||||
1799 | // name and its shorter polymorphic name (if the latter exists). | ||||||
1800 | for (bool Polymorphic : {false, true}) { | ||||||
1801 | if (Polymorphic && !Int.polymorphic()) | ||||||
1802 | continue; | ||||||
1803 | if (!Polymorphic && Int.polymorphicOnly()) | ||||||
1804 | continue; | ||||||
1805 | |||||||
1806 | // We also generate each intrinsic under a name like __arm_vfooq | ||||||
1807 | // (which is in C language implementation namespace, so it's | ||||||
1808 | // safe to define in any conforming user program) and a shorter | ||||||
1809 | // one like vfooq (which is in user namespace, so a user might | ||||||
1810 | // reasonably have used it for something already). If so, they | ||||||
1811 | // can #define __ARM_MVE_PRESERVE_USER_NAMESPACE before | ||||||
1812 | // including the header, which will suppress the shorter names | ||||||
1813 | // and leave only the implementation-namespace ones. Then they | ||||||
1814 | // have to write __arm_vfooq everywhere, of course. | ||||||
1815 | |||||||
1816 | for (bool UserNamespace : {false, true}) { | ||||||
1817 | raw_ostream &OS = parts[(Int.requiresFloat() ? Float : 0) | | ||||||
1818 | (UserNamespace ? UseUserNamespace : 0)]; | ||||||
1819 | |||||||
1820 | // Make the name of the function in this declaration. | ||||||
1821 | |||||||
1822 | std::string FunctionName = | ||||||
1823 | Polymorphic ? Int.shortName() : Int.fullName(); | ||||||
1824 | if (!UserNamespace) | ||||||
1825 | FunctionName = "__arm_" + FunctionName; | ||||||
1826 | |||||||
1827 | // Make strings for the types involved in the function's | ||||||
1828 | // prototype. | ||||||
1829 | |||||||
1830 | std::string RetTypeName = Int.returnType()->cName(); | ||||||
1831 | if (!StringRef(RetTypeName).endswith("*")) | ||||||
1832 | RetTypeName += " "; | ||||||
1833 | |||||||
1834 | std::vector<std::string> ArgTypeNames; | ||||||
1835 | for (const Type *ArgTypePtr : Int.argTypes()) | ||||||
1836 | ArgTypeNames.push_back(ArgTypePtr->cName()); | ||||||
1837 | std::string ArgTypesString = | ||||||
1838 | join(std::begin(ArgTypeNames), std::end(ArgTypeNames), ", "); | ||||||
1839 | |||||||
1840 | // Emit the actual declaration. All these functions are | ||||||
1841 | // declared 'static inline' without a body, which is fine | ||||||
1842 | // provided clang recognizes them as builtins, and has the | ||||||
1843 | // effect that this type signature is used in place of the one | ||||||
1844 | // that Builtins.def didn't provide. That's how we can get | ||||||
1845 | // structure types that weren't defined until this header was | ||||||
1846 | // included to be part of the type signature of a builtin that | ||||||
1847 | // was known to clang already. | ||||||
1848 | // | ||||||
1849 | // The declarations use __attribute__(__clang_arm_builtin_alias), | ||||||
1850 | // so that each function declared will be recognized as the | ||||||
1851 | // appropriate MVE builtin in spite of its user-facing name. | ||||||
1852 | // | ||||||
1853 | // (That's better than making them all wrapper functions, | ||||||
1854 | // partly because it avoids any compiler error message citing | ||||||
1855 | // the wrapper function definition instead of the user's code, | ||||||
1856 | // and mostly because some MVE intrinsics have arguments | ||||||
1857 | // required to be compile-time constants, and that property | ||||||
1858 | // can't be propagated through a wrapper function. It can be | ||||||
1859 | // propagated through a macro, but macros can't be overloaded | ||||||
1860 | // on argument types very easily - you have to use _Generic, | ||||||
1861 | // which makes error messages very confusing when the user | ||||||
1862 | // gets it wrong.) | ||||||
1863 | // | ||||||
1864 | // Finally, the polymorphic versions of the intrinsics are | ||||||
1865 | // also defined with __attribute__(overloadable), so that when | ||||||
1866 | // the same name is defined with several type signatures, the | ||||||
1867 | // right thing happens. Each one of the overloaded | ||||||
1868 | // declarations is given a different builtin id, which | ||||||
1869 | // has exactly the effect we want: first clang resolves the | ||||||
1870 | // overload to the right function, then it knows which builtin | ||||||
1871 | // it's referring to, and then the Sema checking for that | ||||||
1872 | // builtin can check further things like the constant | ||||||
1873 | // arguments. | ||||||
1874 | // | ||||||
1875 | // One more subtlety is the newline just before the return | ||||||
1876 | // type name. That's a cosmetic tweak to make the error | ||||||
1877 | // messages legible if the user gets the types wrong in a call | ||||||
1878 | // to a polymorphic function: this way, clang will print just | ||||||
1879 | // the _final_ line of each declaration in the header, to show | ||||||
1880 | // the type signatures that would have been legal. So all the | ||||||
1881 | // confusing machinery with __attribute__ is left out of the | ||||||
1882 | // error message, and the user sees something that's more or | ||||||
1883 | // less self-documenting: "here's a list of actually readable | ||||||
1884 | // type signatures for vfooq(), and here's why each one didn't | ||||||
1885 | // match your call". | ||||||
1886 | |||||||
1887 | OS << "static __inline__ __attribute__((" | ||||||
1888 | << (Polymorphic ? "__overloadable__, " : "") | ||||||
1889 | << "__clang_arm_builtin_alias(__builtin_arm_mve_" << Int.fullName() | ||||||
1890 | << ")))\n" | ||||||
1891 | << RetTypeName << FunctionName << "(" << ArgTypesString << ");\n"; | ||||||
1892 | } | ||||||
1893 | } | ||||||
1894 | } | ||||||
1895 | for (auto &part : parts) | ||||||
1896 | part << "\n"; | ||||||
1897 | |||||||
1898 | // Now we've finished accumulating bits and pieces into the parts[] array. | ||||||
1899 | // Put it all together to write the final output file. | ||||||
1900 | |||||||
1901 | OS << "/*===---- arm_mve.h - ARM MVE intrinsics " | ||||||
1902 | "-----------------------------------===\n" | ||||||
1903 | << LLVMLicenseHeader | ||||||
1904 | << "#ifndef __ARM_MVE_H\n" | ||||||
1905 | "#define __ARM_MVE_H\n" | ||||||
1906 | "\n" | ||||||
1907 | "#if !__ARM_FEATURE_MVE\n" | ||||||
1908 | "#error \"MVE support not enabled\"\n" | ||||||
1909 | "#endif\n" | ||||||
1910 | "\n" | ||||||
1911 | "#include <stdint.h>\n" | ||||||
1912 | "\n" | ||||||
1913 | "#ifdef __cplusplus\n" | ||||||
1914 | "extern \"C\" {\n" | ||||||
1915 | "#endif\n" | ||||||
1916 | "\n"; | ||||||
1917 | |||||||
1918 | for (size_t i = 0; i < NumParts; ++i) { | ||||||
1919 | std::vector<std::string> conditions; | ||||||
1920 | if (i & Float) | ||||||
1921 | conditions.push_back("(__ARM_FEATURE_MVE & 2)"); | ||||||
1922 | if (i & UseUserNamespace) | ||||||
1923 | conditions.push_back("(!defined __ARM_MVE_PRESERVE_USER_NAMESPACE)"); | ||||||
1924 | |||||||
1925 | std::string condition = | ||||||
1926 | join(std::begin(conditions), std::end(conditions), " && "); | ||||||
1927 | if (!condition.empty()) | ||||||
1928 | OS << "#if " << condition << "\n\n"; | ||||||
1929 | OS << parts[i].str(); | ||||||
1930 | if (!condition.empty()) | ||||||
1931 | OS << "#endif /* " << condition << " */\n\n"; | ||||||
1932 | } | ||||||
1933 | |||||||
1934 | OS << "#ifdef __cplusplus\n" | ||||||
1935 | "} /* extern \"C\" */\n" | ||||||
1936 | "#endif\n" | ||||||
1937 | "\n" | ||||||
1938 | "#endif /* __ARM_MVE_H */\n"; | ||||||
1939 | } | ||||||
1940 | |||||||
1941 | void MveEmitter::EmitBuiltinDef(raw_ostream &OS) { | ||||||
1942 | for (const auto &kv : ACLEIntrinsics) { | ||||||
1943 | const ACLEIntrinsic &Int = *kv.second; | ||||||
1944 | OS << "TARGET_HEADER_BUILTIN(__builtin_arm_mve_" << Int.fullName() | ||||||
1945 | << ", \"\", \"n\", \"arm_mve.h\", ALL_LANGUAGES, \"\")\n"; | ||||||
1946 | } | ||||||
1947 | |||||||
1948 | std::set<std::string> ShortNamesSeen; | ||||||
1949 | |||||||
1950 | for (const auto &kv : ACLEIntrinsics) { | ||||||
1951 | const ACLEIntrinsic &Int = *kv.second; | ||||||
1952 | if (Int.polymorphic()) { | ||||||
1953 | StringRef Name = Int.shortName(); | ||||||
1954 | if (ShortNamesSeen.find(std::string(Name)) == ShortNamesSeen.end()) { | ||||||
1955 | OS << "BUILTIN(__builtin_arm_mve_" << Name << ", \"vi.\", \"nt"; | ||||||
1956 | if (Int.nonEvaluating()) | ||||||
1957 | OS << "u"; // indicate that this builtin doesn't evaluate its args | ||||||
1958 | OS << "\")\n"; | ||||||
1959 | ShortNamesSeen.insert(std::string(Name)); | ||||||
1960 | } | ||||||
1961 | } | ||||||
1962 | } | ||||||
1963 | } | ||||||
1964 | |||||||
1965 | void MveEmitter::EmitBuiltinSema(raw_ostream &OS) { | ||||||
1966 | std::map<std::string, std::set<std::string>> Checks; | ||||||
1967 | GroupSemaChecks(Checks); | ||||||
1968 | |||||||
1969 | for (const auto &kv : Checks) { | ||||||
1970 | for (StringRef Name : kv.second) | ||||||
1971 | OS << "case ARM::BI__builtin_arm_mve_" << Name << ":\n"; | ||||||
1972 | OS << " return " << kv.first; | ||||||
1973 | } | ||||||
1974 | } | ||||||
1975 | |||||||
1976 | // ----------------------------------------------------------------------------- | ||||||
1977 | // Class that describes an ACLE intrinsic implemented as a macro. | ||||||
1978 | // | ||||||
1979 | // This class is used when the intrinsic is polymorphic in 2 or 3 types, but we | ||||||
1980 | // want to avoid a combinatorial explosion by reinterpreting the arguments to | ||||||
1981 | // fixed types. | ||||||
1982 | |||||||
1983 | class FunctionMacro { | ||||||
1984 | std::vector<StringRef> Params; | ||||||
1985 | StringRef Definition; | ||||||
1986 | |||||||
1987 | public: | ||||||
1988 | FunctionMacro(const Record &R); | ||||||
1989 | |||||||
1990 | const std::vector<StringRef> &getParams() const { return Params; } | ||||||
1991 | StringRef getDefinition() const { return Definition; } | ||||||
1992 | }; | ||||||
1993 | |||||||
1994 | FunctionMacro::FunctionMacro(const Record &R) { | ||||||
1995 | Params = R.getValueAsListOfStrings("params"); | ||||||
1996 | Definition = R.getValueAsString("definition"); | ||||||
1997 | } | ||||||
1998 | |||||||
1999 | // ----------------------------------------------------------------------------- | ||||||
2000 | // The class used for generating arm_cde.h and related Clang bits | ||||||
2001 | // | ||||||
2002 | |||||||
2003 | class CdeEmitter : public EmitterBase { | ||||||
2004 | std::map<StringRef, FunctionMacro> FunctionMacros; | ||||||
2005 | |||||||
2006 | public: | ||||||
2007 | CdeEmitter(RecordKeeper &Records); | ||||||
2008 | void EmitHeader(raw_ostream &OS) override; | ||||||
2009 | void EmitBuiltinDef(raw_ostream &OS) override; | ||||||
2010 | void EmitBuiltinSema(raw_ostream &OS) override; | ||||||
2011 | }; | ||||||
2012 | |||||||
2013 | CdeEmitter::CdeEmitter(RecordKeeper &Records) : EmitterBase(Records) { | ||||||
2014 | for (Record *R : Records.getAllDerivedDefinitions("FunctionMacro")) | ||||||
2015 | FunctionMacros.emplace(R->getName(), FunctionMacro(*R)); | ||||||
2016 | } | ||||||
2017 | |||||||
2018 | void CdeEmitter::EmitHeader(raw_ostream &OS) { | ||||||
2019 | // Accumulate pieces of the header file that will be enabled under various | ||||||
2020 | // different combinations of #ifdef. The index into parts[] is one of the | ||||||
2021 | // following: | ||||||
2022 | constexpr unsigned None = 0; | ||||||
2023 | constexpr unsigned MVE = 1; | ||||||
2024 | constexpr unsigned MVEFloat = 2; | ||||||
2025 | |||||||
2026 | constexpr unsigned NumParts = 3; | ||||||
2027 | raw_self_contained_string_ostream parts[NumParts]; | ||||||
2028 | |||||||
2029 | // Write typedefs for all the required vector types, and a few scalar | ||||||
2030 | // types that don't already have the name we want them to have. | ||||||
2031 | |||||||
2032 | parts[MVE] << "typedef uint16_t mve_pred16_t;\n"; | ||||||
2033 | parts[MVEFloat] << "typedef __fp16 float16_t;\n" | ||||||
2034 | "typedef float float32_t;\n"; | ||||||
2035 | for (const auto &kv : ScalarTypes) { | ||||||
2036 | const ScalarType *ST = kv.second.get(); | ||||||
2037 | if (ST->hasNonstandardName()) | ||||||
2038 | continue; | ||||||
2039 | // We don't have float64x2_t | ||||||
2040 | if (ST->kind() == ScalarTypeKind::Float && ST->sizeInBits() == 64) | ||||||
2041 | continue; | ||||||
2042 | raw_ostream &OS = parts[ST->requiresFloat() ? MVEFloat : MVE]; | ||||||
2043 | const VectorType *VT = getVectorType(ST); | ||||||
2044 | |||||||
2045 | OS << "typedef __attribute__((__neon_vector_type__(" << VT->lanes() | ||||||
2046 | << "), __clang_arm_mve_strict_polymorphism)) " << ST->cName() << " " | ||||||
2047 | << VT->cName() << ";\n"; | ||||||
2048 | } | ||||||
2049 | parts[MVE] << "\n"; | ||||||
2050 | parts[MVEFloat] << "\n"; | ||||||
2051 | |||||||
2052 | // Write declarations for all the intrinsics. | ||||||
2053 | |||||||
2054 | for (const auto &kv : ACLEIntrinsics) { | ||||||
2055 | const ACLEIntrinsic &Int = *kv.second; | ||||||
2056 | |||||||
2057 | // We generate each intrinsic twice, under its full unambiguous | ||||||
2058 | // name and its shorter polymorphic name (if the latter exists). | ||||||
2059 | for (bool Polymorphic : {false, true}) { | ||||||
2060 | if (Polymorphic && !Int.polymorphic()) | ||||||
2061 | continue; | ||||||
2062 | if (!Polymorphic && Int.polymorphicOnly()) | ||||||
2063 | continue; | ||||||
2064 | |||||||
2065 | raw_ostream &OS = | ||||||
2066 | parts[Int.requiresFloat() ? MVEFloat | ||||||
2067 | : Int.requiresMVE() ? MVE : None]; | ||||||
2068 | |||||||
2069 | // Make the name of the function in this declaration. | ||||||
2070 | std::string FunctionName = | ||||||
2071 | "__arm_" + (Polymorphic ? Int.shortName() : Int.fullName()); | ||||||
2072 | |||||||
2073 | // Make strings for the types involved in the function's | ||||||
2074 | // prototype. | ||||||
2075 | std::string RetTypeName = Int.returnType()->cName(); | ||||||
2076 | if (!StringRef(RetTypeName).endswith("*")) | ||||||
2077 | RetTypeName += " "; | ||||||
2078 | |||||||
2079 | std::vector<std::string> ArgTypeNames; | ||||||
2080 | for (const Type *ArgTypePtr : Int.argTypes()) | ||||||
2081 | ArgTypeNames.push_back(ArgTypePtr->cName()); | ||||||
2082 | std::string ArgTypesString = | ||||||
2083 | join(std::begin(ArgTypeNames), std::end(ArgTypeNames), ", "); | ||||||
2084 | |||||||
2085 | // Emit the actual declaration. See MveEmitter::EmitHeader for detailed | ||||||
2086 | // comments | ||||||
2087 | OS << "static __inline__ __attribute__((" | ||||||
2088 | << (Polymorphic ? "__overloadable__, " : "") | ||||||
2089 | << "__clang_arm_builtin_alias(__builtin_arm_" << Int.builtinExtension() | ||||||
2090 | << "_" << Int.fullName() << ")))\n" | ||||||
2091 | << RetTypeName << FunctionName << "(" << ArgTypesString << ");\n"; | ||||||
2092 | } | ||||||
2093 | } | ||||||
2094 | |||||||
2095 | for (const auto &kv : FunctionMacros) { | ||||||
2096 | StringRef Name = kv.first; | ||||||
2097 | const FunctionMacro &FM = kv.second; | ||||||
2098 | |||||||
2099 | raw_ostream &OS = parts[MVE]; | ||||||
2100 | OS << "#define " | ||||||
2101 | << "__arm_" << Name << "(" << join(FM.getParams(), ", ") << ") " | ||||||
2102 | << FM.getDefinition() << "\n"; | ||||||
2103 | } | ||||||
2104 | |||||||
2105 | for (auto &part : parts) | ||||||
2106 | part << "\n"; | ||||||
2107 | |||||||
2108 | // Now we've finished accumulating bits and pieces into the parts[] array. | ||||||
2109 | // Put it all together to write the final output file. | ||||||
2110 | |||||||
2111 | OS << "/*===---- arm_cde.h - ARM CDE intrinsics " | ||||||
2112 | "-----------------------------------===\n" | ||||||
2113 | << LLVMLicenseHeader | ||||||
2114 | << "#ifndef __ARM_CDE_H\n" | ||||||
2115 | "#define __ARM_CDE_H\n" | ||||||
2116 | "\n" | ||||||
2117 | "#if !__ARM_FEATURE_CDE\n" | ||||||
2118 | "#error \"CDE support not enabled\"\n" | ||||||
2119 | "#endif\n" | ||||||
2120 | "\n" | ||||||
2121 | "#include <stdint.h>\n" | ||||||
2122 | "\n" | ||||||
2123 | "#ifdef __cplusplus\n" | ||||||
2124 | "extern \"C\" {\n" | ||||||
2125 | "#endif\n" | ||||||
2126 | "\n"; | ||||||
2127 | |||||||
2128 | for (size_t i = 0; i < NumParts; ++i) { | ||||||
2129 | std::string condition; | ||||||
2130 | if (i == MVEFloat) | ||||||
2131 | condition = "__ARM_FEATURE_MVE & 2"; | ||||||
2132 | else if (i == MVE) | ||||||
2133 | condition = "__ARM_FEATURE_MVE"; | ||||||
2134 | |||||||
2135 | if (!condition.empty()) | ||||||
2136 | OS << "#if " << condition << "\n\n"; | ||||||
2137 | OS << parts[i].str(); | ||||||
2138 | if (!condition.empty()) | ||||||
2139 | OS << "#endif /* " << condition << " */\n\n"; | ||||||
2140 | } | ||||||
2141 | |||||||
2142 | OS << "#ifdef __cplusplus\n" | ||||||
2143 | "} /* extern \"C\" */\n" | ||||||
2144 | "#endif\n" | ||||||
2145 | "\n" | ||||||
2146 | "#endif /* __ARM_CDE_H */\n"; | ||||||
2147 | } | ||||||
2148 | |||||||
2149 | void CdeEmitter::EmitBuiltinDef(raw_ostream &OS) { | ||||||
2150 | for (const auto &kv : ACLEIntrinsics) { | ||||||
2151 | if (kv.second->headerOnly()) | ||||||
2152 | continue; | ||||||
2153 | const ACLEIntrinsic &Int = *kv.second; | ||||||
2154 | OS << "TARGET_HEADER_BUILTIN(__builtin_arm_cde_" << Int.fullName() | ||||||
2155 | << ", \"\", \"ncU\", \"arm_cde.h\", ALL_LANGUAGES, \"\")\n"; | ||||||
2156 | } | ||||||
2157 | } | ||||||
2158 | |||||||
2159 | void CdeEmitter::EmitBuiltinSema(raw_ostream &OS) { | ||||||
2160 | std::map<std::string, std::set<std::string>> Checks; | ||||||
2161 | GroupSemaChecks(Checks); | ||||||
2162 | |||||||
2163 | for (const auto &kv : Checks) { | ||||||
2164 | for (StringRef Name : kv.second) | ||||||
2165 | OS << "case ARM::BI__builtin_arm_cde_" << Name << ":\n"; | ||||||
2166 | OS << " Err = " << kv.first << " break;\n"; | ||||||
2167 | } | ||||||
2168 | } | ||||||
2169 | |||||||
2170 | } // namespace | ||||||
2171 | |||||||
2172 | namespace clang { | ||||||
2173 | |||||||
2174 | // MVE | ||||||
2175 | |||||||
2176 | void EmitMveHeader(RecordKeeper &Records, raw_ostream &OS) { | ||||||
2177 | MveEmitter(Records).EmitHeader(OS); | ||||||
2178 | } | ||||||
2179 | |||||||
2180 | void EmitMveBuiltinDef(RecordKeeper &Records, raw_ostream &OS) { | ||||||
2181 | MveEmitter(Records).EmitBuiltinDef(OS); | ||||||
2182 | } | ||||||
2183 | |||||||
2184 | void EmitMveBuiltinSema(RecordKeeper &Records, raw_ostream &OS) { | ||||||
2185 | MveEmitter(Records).EmitBuiltinSema(OS); | ||||||
2186 | } | ||||||
2187 | |||||||
2188 | void EmitMveBuiltinCG(RecordKeeper &Records, raw_ostream &OS) { | ||||||
2189 | MveEmitter(Records).EmitBuiltinCG(OS); | ||||||
2190 | } | ||||||
2191 | |||||||
2192 | void EmitMveBuiltinAliases(RecordKeeper &Records, raw_ostream &OS) { | ||||||
2193 | MveEmitter(Records).EmitBuiltinAliases(OS); | ||||||
2194 | } | ||||||
2195 | |||||||
2196 | // CDE | ||||||
2197 | |||||||
2198 | void EmitCdeHeader(RecordKeeper &Records, raw_ostream &OS) { | ||||||
2199 | CdeEmitter(Records).EmitHeader(OS); | ||||||
2200 | } | ||||||
2201 | |||||||
2202 | void EmitCdeBuiltinDef(RecordKeeper &Records, raw_ostream &OS) { | ||||||
2203 | CdeEmitter(Records).EmitBuiltinDef(OS); | ||||||
2204 | } | ||||||
2205 | |||||||
2206 | void EmitCdeBuiltinSema(RecordKeeper &Records, raw_ostream &OS) { | ||||||
2207 | CdeEmitter(Records).EmitBuiltinSema(OS); | ||||||
2208 | } | ||||||
2209 | |||||||
2210 | void EmitCdeBuiltinCG(RecordKeeper &Records, raw_ostream &OS) { | ||||||
2211 | CdeEmitter(Records).EmitBuiltinCG(OS); | ||||||
2212 | } | ||||||
2213 | |||||||
2214 | void EmitCdeBuiltinAliases(RecordKeeper &Records, raw_ostream &OS) { | ||||||
2215 | CdeEmitter(Records).EmitBuiltinAliases(OS); | ||||||
| |||||||
2216 | } | ||||||
2217 | |||||||
2218 | } // end namespace clang |
1 | // unique_ptr implementation -*- C++ -*- |
2 | |
3 | // Copyright (C) 2008-2020 Free Software Foundation, Inc. |
4 | // |
5 | // This file is part of the GNU ISO C++ Library. This library is free |
6 | // software; you can redistribute it and/or modify it under the |
7 | // terms of the GNU General Public License as published by the |
8 | // Free Software Foundation; either version 3, or (at your option) |
9 | // any later version. |
10 | |
11 | // This library is distributed in the hope that it will be useful, |
12 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | // GNU General Public License for more details. |
15 | |
16 | // Under Section 7 of GPL version 3, you are granted additional |
17 | // permissions described in the GCC Runtime Library Exception, version |
18 | // 3.1, as published by the Free Software Foundation. |
19 | |
20 | // You should have received a copy of the GNU General Public License and |
21 | // a copy of the GCC Runtime Library Exception along with this program; |
22 | // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see |
23 | // <http://www.gnu.org/licenses/>. |
24 | |
25 | /** @file bits/unique_ptr.h |
26 | * This is an internal header file, included by other library headers. |
27 | * Do not attempt to use it directly. @headername{memory} |
28 | */ |
29 | |
30 | #ifndef _UNIQUE_PTR_H1 |
31 | #define _UNIQUE_PTR_H1 1 |
32 | |
33 | #include <bits/c++config.h> |
34 | #include <debug/assertions.h> |
35 | #include <type_traits> |
36 | #include <utility> |
37 | #include <tuple> |
38 | #include <bits/stl_function.h> |
39 | #include <bits/functional_hash.h> |
40 | #if __cplusplus201402L > 201703L |
41 | # include <compare> |
42 | # include <ostream> |
43 | #endif |
44 | |
45 | namespace std _GLIBCXX_VISIBILITY(default)__attribute__ ((__visibility__ ("default"))) |
46 | { |
47 | _GLIBCXX_BEGIN_NAMESPACE_VERSION |
48 | |
49 | /** |
50 | * @addtogroup pointer_abstractions |
51 | * @{ |
52 | */ |
53 | |
54 | #if _GLIBCXX_USE_DEPRECATED1 |
55 | #pragma GCC diagnostic push |
56 | #pragma GCC diagnostic ignored "-Wdeprecated-declarations" |
57 | template<typename> class auto_ptr; |
58 | #pragma GCC diagnostic pop |
59 | #endif |
60 | |
61 | /// Primary template of default_delete, used by unique_ptr for single objects |
62 | template<typename _Tp> |
63 | struct default_delete |
64 | { |
65 | /// Default constructor |
66 | constexpr default_delete() noexcept = default; |
67 | |
68 | /** @brief Converting constructor. |
69 | * |
70 | * Allows conversion from a deleter for objects of another type, `_Up`, |
71 | * only if `_Up*` is convertible to `_Tp*`. |
72 | */ |
73 | template<typename _Up, |
74 | typename = _Require<is_convertible<_Up*, _Tp*>>> |
75 | default_delete(const default_delete<_Up>&) noexcept { } |
76 | |
77 | /// Calls `delete __ptr` |
78 | void |
79 | operator()(_Tp* __ptr) const |
80 | { |
81 | static_assert(!is_void<_Tp>::value, |
82 | "can't delete pointer to incomplete type"); |
83 | static_assert(sizeof(_Tp)>0, |
84 | "can't delete pointer to incomplete type"); |
85 | delete __ptr; |
86 | } |
87 | }; |
88 | |
89 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
90 | // DR 740 - omit specialization for array objects with a compile time length |
91 | |
92 | /// Specialization of default_delete for arrays, used by `unique_ptr<T[]>` |
93 | template<typename _Tp> |
94 | struct default_delete<_Tp[]> |
95 | { |
96 | public: |
97 | /// Default constructor |
98 | constexpr default_delete() noexcept = default; |
99 | |
100 | /** @brief Converting constructor. |
101 | * |
102 | * Allows conversion from a deleter for arrays of another type, such as |
103 | * a const-qualified version of `_Tp`. |
104 | * |
105 | * Conversions from types derived from `_Tp` are not allowed because |
106 | * it is undefined to `delete[]` an array of derived types through a |
107 | * pointer to the base type. |
108 | */ |
109 | template<typename _Up, |
110 | typename = _Require<is_convertible<_Up(*)[], _Tp(*)[]>>> |
111 | default_delete(const default_delete<_Up[]>&) noexcept { } |
112 | |
113 | /// Calls `delete[] __ptr` |
114 | template<typename _Up> |
115 | typename enable_if<is_convertible<_Up(*)[], _Tp(*)[]>::value>::type |
116 | operator()(_Up* __ptr) const |
117 | { |
118 | static_assert(sizeof(_Tp)>0, |
119 | "can't delete pointer to incomplete type"); |
120 | delete [] __ptr; |
121 | } |
122 | }; |
123 | |
124 | /// @cond undocumented |
125 | |
126 | // Manages the pointer and deleter of a unique_ptr |
127 | template <typename _Tp, typename _Dp> |
128 | class __uniq_ptr_impl |
129 | { |
130 | template <typename _Up, typename _Ep, typename = void> |
131 | struct _Ptr |
132 | { |
133 | using type = _Up*; |
134 | }; |
135 | |
136 | template <typename _Up, typename _Ep> |
137 | struct |
138 | _Ptr<_Up, _Ep, __void_t<typename remove_reference<_Ep>::type::pointer>> |
139 | { |
140 | using type = typename remove_reference<_Ep>::type::pointer; |
141 | }; |
142 | |
143 | public: |
144 | using _DeleterConstraint = enable_if< |
145 | __and_<__not_<is_pointer<_Dp>>, |
146 | is_default_constructible<_Dp>>::value>; |
147 | |
148 | using pointer = typename _Ptr<_Tp, _Dp>::type; |
149 | |
150 | static_assert( !is_rvalue_reference<_Dp>::value, |
151 | "unique_ptr's deleter type must be a function object type" |
152 | " or an lvalue reference type" ); |
153 | |
154 | __uniq_ptr_impl() = default; |
155 | __uniq_ptr_impl(pointer __p) : _M_t() { _M_ptr() = __p; } |
156 | |
157 | template<typename _Del> |
158 | __uniq_ptr_impl(pointer __p, _Del&& __d) |
159 | : _M_t(__p, std::forward<_Del>(__d)) { } |
160 | |
161 | __uniq_ptr_impl(__uniq_ptr_impl&& __u) noexcept |
162 | : _M_t(std::move(__u._M_t)) |
163 | { __u._M_ptr() = nullptr; } |
164 | |
165 | __uniq_ptr_impl& operator=(__uniq_ptr_impl&& __u) noexcept |
166 | { |
167 | reset(__u.release()); |
168 | _M_deleter() = std::forward<_Dp>(__u._M_deleter()); |
169 | return *this; |
170 | } |
171 | |
172 | pointer& _M_ptr() { return std::get<0>(_M_t); } |
173 | pointer _M_ptr() const { return std::get<0>(_M_t); } |
174 | _Dp& _M_deleter() { return std::get<1>(_M_t); } |
175 | const _Dp& _M_deleter() const { return std::get<1>(_M_t); } |
176 | |
177 | void reset(pointer __p) noexcept |
178 | { |
179 | const pointer __old_p = _M_ptr(); |
180 | _M_ptr() = __p; |
181 | if (__old_p) |
182 | _M_deleter()(__old_p); |
183 | } |
184 | |
185 | pointer release() noexcept |
186 | { |
187 | pointer __p = _M_ptr(); |
188 | _M_ptr() = nullptr; |
189 | return __p; |
190 | } |
191 | |
192 | void |
193 | swap(__uniq_ptr_impl& __rhs) noexcept |
194 | { |
195 | using std::swap; |
196 | swap(this->_M_ptr(), __rhs._M_ptr()); |
197 | swap(this->_M_deleter(), __rhs._M_deleter()); |
198 | } |
199 | |
200 | private: |
201 | tuple<pointer, _Dp> _M_t; |
202 | }; |
203 | |
204 | // Defines move construction + assignment as either defaulted or deleted. |
205 | template <typename _Tp, typename _Dp, |
206 | bool = is_move_constructible<_Dp>::value, |
207 | bool = is_move_assignable<_Dp>::value> |
208 | struct __uniq_ptr_data : __uniq_ptr_impl<_Tp, _Dp> |
209 | { |
210 | using __uniq_ptr_impl<_Tp, _Dp>::__uniq_ptr_impl; |
211 | __uniq_ptr_data(__uniq_ptr_data&&) = default; |
212 | __uniq_ptr_data& operator=(__uniq_ptr_data&&) = default; |
213 | }; |
214 | |
215 | template <typename _Tp, typename _Dp> |
216 | struct __uniq_ptr_data<_Tp, _Dp, true, false> : __uniq_ptr_impl<_Tp, _Dp> |
217 | { |
218 | using __uniq_ptr_impl<_Tp, _Dp>::__uniq_ptr_impl; |
219 | __uniq_ptr_data(__uniq_ptr_data&&) = default; |
220 | __uniq_ptr_data& operator=(__uniq_ptr_data&&) = delete; |
221 | }; |
222 | |
223 | template <typename _Tp, typename _Dp> |
224 | struct __uniq_ptr_data<_Tp, _Dp, false, true> : __uniq_ptr_impl<_Tp, _Dp> |
225 | { |
226 | using __uniq_ptr_impl<_Tp, _Dp>::__uniq_ptr_impl; |
227 | __uniq_ptr_data(__uniq_ptr_data&&) = delete; |
228 | __uniq_ptr_data& operator=(__uniq_ptr_data&&) = default; |
229 | }; |
230 | |
231 | template <typename _Tp, typename _Dp> |
232 | struct __uniq_ptr_data<_Tp, _Dp, false, false> : __uniq_ptr_impl<_Tp, _Dp> |
233 | { |
234 | using __uniq_ptr_impl<_Tp, _Dp>::__uniq_ptr_impl; |
235 | __uniq_ptr_data(__uniq_ptr_data&&) = delete; |
236 | __uniq_ptr_data& operator=(__uniq_ptr_data&&) = delete; |
237 | }; |
238 | /// @endcond |
239 | |
240 | /// 20.7.1.2 unique_ptr for single objects. |
241 | template <typename _Tp, typename _Dp = default_delete<_Tp>> |
242 | class unique_ptr |
243 | { |
244 | template <typename _Up> |
245 | using _DeleterConstraint = |
246 | typename __uniq_ptr_impl<_Tp, _Up>::_DeleterConstraint::type; |
247 | |
248 | __uniq_ptr_data<_Tp, _Dp> _M_t; |
249 | |
250 | public: |
251 | using pointer = typename __uniq_ptr_impl<_Tp, _Dp>::pointer; |
252 | using element_type = _Tp; |
253 | using deleter_type = _Dp; |
254 | |
255 | private: |
256 | // helper template for detecting a safe conversion from another |
257 | // unique_ptr |
258 | template<typename _Up, typename _Ep> |
259 | using __safe_conversion_up = __and_< |
260 | is_convertible<typename unique_ptr<_Up, _Ep>::pointer, pointer>, |
261 | __not_<is_array<_Up>> |
262 | >; |
263 | |
264 | public: |
265 | // Constructors. |
266 | |
267 | /// Default constructor, creates a unique_ptr that owns nothing. |
268 | template<typename _Del = _Dp, typename = _DeleterConstraint<_Del>> |
269 | constexpr unique_ptr() noexcept |
270 | : _M_t() |
271 | { } |
272 | |
273 | /** Takes ownership of a pointer. |
274 | * |
275 | * @param __p A pointer to an object of @c element_type |
276 | * |
277 | * The deleter will be value-initialized. |
278 | */ |
279 | template<typename _Del = _Dp, typename = _DeleterConstraint<_Del>> |
280 | explicit |
281 | unique_ptr(pointer __p) noexcept |
282 | : _M_t(__p) |
283 | { } |
284 | |
285 | /** Takes ownership of a pointer. |
286 | * |
287 | * @param __p A pointer to an object of @c element_type |
288 | * @param __d A reference to a deleter. |
289 | * |
290 | * The deleter will be initialized with @p __d |
291 | */ |
292 | template<typename _Del = deleter_type, |
293 | typename = _Require<is_copy_constructible<_Del>>> |
294 | unique_ptr(pointer __p, const deleter_type& __d) noexcept |
295 | : _M_t(__p, __d) { } |
296 | |
297 | /** Takes ownership of a pointer. |
298 | * |
299 | * @param __p A pointer to an object of @c element_type |
300 | * @param __d An rvalue reference to a (non-reference) deleter. |
301 | * |
302 | * The deleter will be initialized with @p std::move(__d) |
303 | */ |
304 | template<typename _Del = deleter_type, |
305 | typename = _Require<is_move_constructible<_Del>>> |
306 | unique_ptr(pointer __p, |
307 | __enable_if_t<!is_lvalue_reference<_Del>::value, |
308 | _Del&&> __d) noexcept |
309 | : _M_t(__p, std::move(__d)) |
310 | { } |
311 | |
312 | template<typename _Del = deleter_type, |
313 | typename _DelUnref = typename remove_reference<_Del>::type> |
314 | unique_ptr(pointer, |
315 | __enable_if_t<is_lvalue_reference<_Del>::value, |
316 | _DelUnref&&>) = delete; |
317 | |
318 | /// Creates a unique_ptr that owns nothing. |
319 | template<typename _Del = _Dp, typename = _DeleterConstraint<_Del>> |
320 | constexpr unique_ptr(nullptr_t) noexcept |
321 | : _M_t() |
322 | { } |
323 | |
324 | // Move constructors. |
325 | |
326 | /// Move constructor. |
327 | unique_ptr(unique_ptr&&) = default; |
328 | |
329 | /** @brief Converting constructor from another type |
330 | * |
331 | * Requires that the pointer owned by @p __u is convertible to the |
332 | * type of pointer owned by this object, @p __u does not own an array, |
333 | * and @p __u has a compatible deleter type. |
334 | */ |
335 | template<typename _Up, typename _Ep, typename = _Require< |
336 | __safe_conversion_up<_Up, _Ep>, |
337 | typename conditional<is_reference<_Dp>::value, |
338 | is_same<_Ep, _Dp>, |
339 | is_convertible<_Ep, _Dp>>::type>> |
340 | unique_ptr(unique_ptr<_Up, _Ep>&& __u) noexcept |
341 | : _M_t(__u.release(), std::forward<_Ep>(__u.get_deleter())) |
342 | { } |
343 | |
344 | #if _GLIBCXX_USE_DEPRECATED1 |
345 | #pragma GCC diagnostic push |
346 | #pragma GCC diagnostic ignored "-Wdeprecated-declarations" |
347 | /// Converting constructor from @c auto_ptr |
348 | template<typename _Up, typename = _Require< |
349 | is_convertible<_Up*, _Tp*>, is_same<_Dp, default_delete<_Tp>>>> |
350 | unique_ptr(auto_ptr<_Up>&& __u) noexcept; |
351 | #pragma GCC diagnostic pop |
352 | #endif |
353 | |
354 | /// Destructor, invokes the deleter if the stored pointer is not null. |
355 | ~unique_ptr() noexcept |
356 | { |
357 | static_assert(__is_invocable<deleter_type&, pointer>::value, |
358 | "unique_ptr's deleter must be invocable with a pointer"); |
359 | auto& __ptr = _M_t._M_ptr(); |
360 | if (__ptr != nullptr) |
361 | get_deleter()(std::move(__ptr)); |
362 | __ptr = pointer(); |
363 | } |
364 | |
365 | // Assignment. |
366 | |
367 | /** @brief Move assignment operator. |
368 | * |
369 | * Invokes the deleter if this object owns a pointer. |
370 | */ |
371 | unique_ptr& operator=(unique_ptr&&) = default; |
372 | |
373 | /** @brief Assignment from another type. |
374 | * |
375 | * @param __u The object to transfer ownership from, which owns a |
376 | * convertible pointer to a non-array object. |
377 | * |
378 | * Invokes the deleter if this object owns a pointer. |
379 | */ |
380 | template<typename _Up, typename _Ep> |
381 | typename enable_if< __and_< |
382 | __safe_conversion_up<_Up, _Ep>, |
383 | is_assignable<deleter_type&, _Ep&&> |
384 | >::value, |
385 | unique_ptr&>::type |
386 | operator=(unique_ptr<_Up, _Ep>&& __u) noexcept |
387 | { |
388 | reset(__u.release()); |
389 | get_deleter() = std::forward<_Ep>(__u.get_deleter()); |
390 | return *this; |
391 | } |
392 | |
393 | /// Reset the %unique_ptr to empty, invoking the deleter if necessary. |
394 | unique_ptr& |
395 | operator=(nullptr_t) noexcept |
396 | { |
397 | reset(); |
398 | return *this; |
399 | } |
400 | |
401 | // Observers. |
402 | |
403 | /// Dereference the stored pointer. |
404 | typename add_lvalue_reference<element_type>::type |
405 | operator*() const |
406 | { |
407 | __glibcxx_assert(get() != pointer()); |
408 | return *get(); |
409 | } |
410 | |
411 | /// Return the stored pointer. |
412 | pointer |
413 | operator->() const noexcept |
414 | { |
415 | _GLIBCXX_DEBUG_PEDASSERT(get() != pointer()); |
416 | return get(); |
417 | } |
418 | |
419 | /// Return the stored pointer. |
420 | pointer |
421 | get() const noexcept |
422 | { return _M_t._M_ptr(); } |
423 | |
424 | /// Return a reference to the stored deleter. |
425 | deleter_type& |
426 | get_deleter() noexcept |
427 | { return _M_t._M_deleter(); } |
428 | |
429 | /// Return a reference to the stored deleter. |
430 | const deleter_type& |
431 | get_deleter() const noexcept |
432 | { return _M_t._M_deleter(); } |
433 | |
434 | /// Return @c true if the stored pointer is not null. |
435 | explicit operator bool() const noexcept |
436 | { return get() == pointer() ? false : true; } |
437 | |
438 | // Modifiers. |
439 | |
440 | /// Release ownership of any stored pointer. |
441 | pointer |
442 | release() noexcept |
443 | { return _M_t.release(); } |
444 | |
445 | /** @brief Replace the stored pointer. |
446 | * |
447 | * @param __p The new pointer to store. |
448 | * |
449 | * The deleter will be invoked if a pointer is already owned. |
450 | */ |
451 | void |
452 | reset(pointer __p = pointer()) noexcept |
453 | { |
454 | static_assert(__is_invocable<deleter_type&, pointer>::value, |
455 | "unique_ptr's deleter must be invocable with a pointer"); |
456 | _M_t.reset(std::move(__p)); |
457 | } |
458 | |
459 | /// Exchange the pointer and deleter with another object. |
460 | void |
461 | swap(unique_ptr& __u) noexcept |
462 | { |
463 | static_assert(__is_swappable<_Dp>::value, "deleter must be swappable"); |
464 | _M_t.swap(__u._M_t); |
465 | } |
466 | |
467 | // Disable copy from lvalue. |
468 | unique_ptr(const unique_ptr&) = delete; |
469 | unique_ptr& operator=(const unique_ptr&) = delete; |
470 | }; |
471 | |
472 | /// 20.7.1.3 unique_ptr for array objects with a runtime length |
473 | // [unique.ptr.runtime] |
474 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
475 | // DR 740 - omit specialization for array objects with a compile time length |
476 | template<typename _Tp, typename _Dp> |
477 | class unique_ptr<_Tp[], _Dp> |
478 | { |
479 | template <typename _Up> |
480 | using _DeleterConstraint = |
481 | typename __uniq_ptr_impl<_Tp, _Up>::_DeleterConstraint::type; |
482 | |
483 | __uniq_ptr_data<_Tp, _Dp> _M_t; |
484 | |
485 | template<typename _Up> |
486 | using __remove_cv = typename remove_cv<_Up>::type; |
487 | |
488 | // like is_base_of<_Tp, _Up> but false if unqualified types are the same |
489 | template<typename _Up> |
490 | using __is_derived_Tp |
491 | = __and_< is_base_of<_Tp, _Up>, |
492 | __not_<is_same<__remove_cv<_Tp>, __remove_cv<_Up>>> >; |
493 | |
494 | public: |
495 | using pointer = typename __uniq_ptr_impl<_Tp, _Dp>::pointer; |
496 | using element_type = _Tp; |
497 | using deleter_type = _Dp; |
498 | |
499 | // helper template for detecting a safe conversion from another |
500 | // unique_ptr |
501 | template<typename _Up, typename _Ep, |
502 | typename _UPtr = unique_ptr<_Up, _Ep>, |
503 | typename _UP_pointer = typename _UPtr::pointer, |
504 | typename _UP_element_type = typename _UPtr::element_type> |
505 | using __safe_conversion_up = __and_< |
506 | is_array<_Up>, |
507 | is_same<pointer, element_type*>, |
508 | is_same<_UP_pointer, _UP_element_type*>, |
509 | is_convertible<_UP_element_type(*)[], element_type(*)[]> |
510 | >; |
511 | |
512 | // helper template for detecting a safe conversion from a raw pointer |
513 | template<typename _Up> |
514 | using __safe_conversion_raw = __and_< |
515 | __or_<__or_<is_same<_Up, pointer>, |
516 | is_same<_Up, nullptr_t>>, |
517 | __and_<is_pointer<_Up>, |
518 | is_same<pointer, element_type*>, |
519 | is_convertible< |
520 | typename remove_pointer<_Up>::type(*)[], |
521 | element_type(*)[]> |
522 | > |
523 | > |
524 | >; |
525 | |
526 | // Constructors. |
527 | |
528 | /// Default constructor, creates a unique_ptr that owns nothing. |
529 | template<typename _Del = _Dp, typename = _DeleterConstraint<_Del>> |
530 | constexpr unique_ptr() noexcept |
531 | : _M_t() |
532 | { } |
533 | |
534 | /** Takes ownership of a pointer. |
535 | * |
536 | * @param __p A pointer to an array of a type safely convertible |
537 | * to an array of @c element_type |
538 | * |
539 | * The deleter will be value-initialized. |
540 | */ |
541 | template<typename _Up, |
542 | typename _Vp = _Dp, |
543 | typename = _DeleterConstraint<_Vp>, |
544 | typename = typename enable_if< |
545 | __safe_conversion_raw<_Up>::value, bool>::type> |
546 | explicit |
547 | unique_ptr(_Up __p) noexcept |
548 | : _M_t(__p) |
549 | { } |
550 | |
551 | /** Takes ownership of a pointer. |
552 | * |
553 | * @param __p A pointer to an array of a type safely convertible |
554 | * to an array of @c element_type |
555 | * @param __d A reference to a deleter. |
556 | * |
557 | * The deleter will be initialized with @p __d |
558 | */ |
559 | template<typename _Up, typename _Del = deleter_type, |
560 | typename = _Require<__safe_conversion_raw<_Up>, |
561 | is_copy_constructible<_Del>>> |
562 | unique_ptr(_Up __p, const deleter_type& __d) noexcept |
563 | : _M_t(__p, __d) { } |
564 | |
565 | /** Takes ownership of a pointer. |
566 | * |
567 | * @param __p A pointer to an array of a type safely convertible |
568 | * to an array of @c element_type |
569 | * @param __d A reference to a deleter. |
570 | * |
571 | * The deleter will be initialized with @p std::move(__d) |
572 | */ |
573 | template<typename _Up, typename _Del = deleter_type, |
574 | typename = _Require<__safe_conversion_raw<_Up>, |
575 | is_move_constructible<_Del>>> |
576 | unique_ptr(_Up __p, |
577 | __enable_if_t<!is_lvalue_reference<_Del>::value, |
578 | _Del&&> __d) noexcept |
579 | : _M_t(std::move(__p), std::move(__d)) |
580 | { } |
581 | |
582 | template<typename _Up, typename _Del = deleter_type, |
583 | typename _DelUnref = typename remove_reference<_Del>::type, |
584 | typename = _Require<__safe_conversion_raw<_Up>>> |
585 | unique_ptr(_Up, |
586 | __enable_if_t<is_lvalue_reference<_Del>::value, |
587 | _DelUnref&&>) = delete; |
588 | |
589 | /// Move constructor. |
590 | unique_ptr(unique_ptr&&) = default; |
591 | |
592 | /// Creates a unique_ptr that owns nothing. |
593 | template<typename _Del = _Dp, typename = _DeleterConstraint<_Del>> |
594 | constexpr unique_ptr(nullptr_t) noexcept |
595 | : _M_t() |
596 | { } |
597 | |
598 | template<typename _Up, typename _Ep, typename = _Require< |
599 | __safe_conversion_up<_Up, _Ep>, |
600 | typename conditional<is_reference<_Dp>::value, |
601 | is_same<_Ep, _Dp>, |
602 | is_convertible<_Ep, _Dp>>::type>> |
603 | unique_ptr(unique_ptr<_Up, _Ep>&& __u) noexcept |
604 | : _M_t(__u.release(), std::forward<_Ep>(__u.get_deleter())) |
605 | { } |
606 | |
607 | /// Destructor, invokes the deleter if the stored pointer is not null. |
608 | ~unique_ptr() |
609 | { |
610 | auto& __ptr = _M_t._M_ptr(); |
611 | if (__ptr != nullptr) |
612 | get_deleter()(__ptr); |
613 | __ptr = pointer(); |
614 | } |
615 | |
616 | // Assignment. |
617 | |
618 | /** @brief Move assignment operator. |
619 | * |
620 | * Invokes the deleter if this object owns a pointer. |
621 | */ |
622 | unique_ptr& |
623 | operator=(unique_ptr&&) = default; |
624 | |
625 | /** @brief Assignment from another type. |
626 | * |
627 | * @param __u The object to transfer ownership from, which owns a |
628 | * convertible pointer to an array object. |
629 | * |
630 | * Invokes the deleter if this object owns a pointer. |
631 | */ |
632 | template<typename _Up, typename _Ep> |
633 | typename |
634 | enable_if<__and_<__safe_conversion_up<_Up, _Ep>, |
635 | is_assignable<deleter_type&, _Ep&&> |
636 | >::value, |
637 | unique_ptr&>::type |
638 | operator=(unique_ptr<_Up, _Ep>&& __u) noexcept |
639 | { |
640 | reset(__u.release()); |
641 | get_deleter() = std::forward<_Ep>(__u.get_deleter()); |
642 | return *this; |
643 | } |
644 | |
645 | /// Reset the %unique_ptr to empty, invoking the deleter if necessary. |
646 | unique_ptr& |
647 | operator=(nullptr_t) noexcept |
648 | { |
649 | reset(); |
650 | return *this; |
651 | } |
652 | |
653 | // Observers. |
654 | |
655 | /// Access an element of owned array. |
656 | typename std::add_lvalue_reference<element_type>::type |
657 | operator[](size_t __i) const |
658 | { |
659 | __glibcxx_assert(get() != pointer()); |
660 | return get()[__i]; |
661 | } |
662 | |
663 | /// Return the stored pointer. |
664 | pointer |
665 | get() const noexcept |
666 | { return _M_t._M_ptr(); } |
667 | |
668 | /// Return a reference to the stored deleter. |
669 | deleter_type& |
670 | get_deleter() noexcept |
671 | { return _M_t._M_deleter(); } |
672 | |
673 | /// Return a reference to the stored deleter. |
674 | const deleter_type& |
675 | get_deleter() const noexcept |
676 | { return _M_t._M_deleter(); } |
677 | |
678 | /// Return @c true if the stored pointer is not null. |
679 | explicit operator bool() const noexcept |
680 | { return get() == pointer() ? false : true; } |
681 | |
682 | // Modifiers. |
683 | |
684 | /// Release ownership of any stored pointer. |
685 | pointer |
686 | release() noexcept |
687 | { return _M_t.release(); } |
688 | |
689 | /** @brief Replace the stored pointer. |
690 | * |
691 | * @param __p The new pointer to store. |
692 | * |
693 | * The deleter will be invoked if a pointer is already owned. |
694 | */ |
695 | template <typename _Up, |
696 | typename = _Require< |
697 | __or_<is_same<_Up, pointer>, |
698 | __and_<is_same<pointer, element_type*>, |
699 | is_pointer<_Up>, |
700 | is_convertible< |
701 | typename remove_pointer<_Up>::type(*)[], |
702 | element_type(*)[] |
703 | > |
704 | > |
705 | > |
706 | >> |
707 | void |
708 | reset(_Up __p) noexcept |
709 | { _M_t.reset(std::move(__p)); } |
710 | |
711 | void reset(nullptr_t = nullptr) noexcept |
712 | { reset(pointer()); } |
713 | |
714 | /// Exchange the pointer and deleter with another object. |
715 | void |
716 | swap(unique_ptr& __u) noexcept |
717 | { |
718 | static_assert(__is_swappable<_Dp>::value, "deleter must be swappable"); |
719 | _M_t.swap(__u._M_t); |
720 | } |
721 | |
722 | // Disable copy from lvalue. |
723 | unique_ptr(const unique_ptr&) = delete; |
724 | unique_ptr& operator=(const unique_ptr&) = delete; |
725 | }; |
726 | |
727 | /// @relates unique_ptr @{ |
728 | |
729 | /// Swap overload for unique_ptr |
730 | template<typename _Tp, typename _Dp> |
731 | inline |
732 | #if __cplusplus201402L > 201402L || !defined(__STRICT_ANSI__1) // c++1z or gnu++11 |
733 | // Constrained free swap overload, see p0185r1 |
734 | typename enable_if<__is_swappable<_Dp>::value>::type |
735 | #else |
736 | void |
737 | #endif |
738 | swap(unique_ptr<_Tp, _Dp>& __x, |
739 | unique_ptr<_Tp, _Dp>& __y) noexcept |
740 | { __x.swap(__y); } |
741 | |
742 | #if __cplusplus201402L > 201402L || !defined(__STRICT_ANSI__1) // c++1z or gnu++11 |
743 | template<typename _Tp, typename _Dp> |
744 | typename enable_if<!__is_swappable<_Dp>::value>::type |
745 | swap(unique_ptr<_Tp, _Dp>&, |
746 | unique_ptr<_Tp, _Dp>&) = delete; |
747 | #endif |
748 | |
749 | /// Equality operator for unique_ptr objects, compares the owned pointers |
750 | template<typename _Tp, typename _Dp, |
751 | typename _Up, typename _Ep> |
752 | _GLIBCXX_NODISCARD inline bool |
753 | operator==(const unique_ptr<_Tp, _Dp>& __x, |
754 | const unique_ptr<_Up, _Ep>& __y) |
755 | { return __x.get() == __y.get(); } |
756 | |
757 | /// unique_ptr comparison with nullptr |
758 | template<typename _Tp, typename _Dp> |
759 | _GLIBCXX_NODISCARD inline bool |
760 | operator==(const unique_ptr<_Tp, _Dp>& __x, nullptr_t) noexcept |
761 | { return !__x; } |
762 | |
763 | #ifndef __cpp_lib_three_way_comparison |
764 | /// unique_ptr comparison with nullptr |
765 | template<typename _Tp, typename _Dp> |
766 | _GLIBCXX_NODISCARD inline bool |
767 | operator==(nullptr_t, const unique_ptr<_Tp, _Dp>& __x) noexcept |
768 | { return !__x; } |
769 | |
770 | /// Inequality operator for unique_ptr objects, compares the owned pointers |
771 | template<typename _Tp, typename _Dp, |
772 | typename _Up, typename _Ep> |
773 | _GLIBCXX_NODISCARD inline bool |
774 | operator!=(const unique_ptr<_Tp, _Dp>& __x, |
775 | const unique_ptr<_Up, _Ep>& __y) |
776 | { return __x.get() != __y.get(); } |
777 | |
778 | /// unique_ptr comparison with nullptr |
779 | template<typename _Tp, typename _Dp> |
780 | _GLIBCXX_NODISCARD inline bool |
781 | operator!=(const unique_ptr<_Tp, _Dp>& __x, nullptr_t) noexcept |
782 | { return (bool)__x; } |
783 | |
784 | /// unique_ptr comparison with nullptr |
785 | template<typename _Tp, typename _Dp> |
786 | _GLIBCXX_NODISCARD inline bool |
787 | operator!=(nullptr_t, const unique_ptr<_Tp, _Dp>& __x) noexcept |
788 | { return (bool)__x; } |
789 | #endif // three way comparison |
790 | |
791 | /// Relational operator for unique_ptr objects, compares the owned pointers |
792 | template<typename _Tp, typename _Dp, |
793 | typename _Up, typename _Ep> |
794 | _GLIBCXX_NODISCARD inline bool |
795 | operator<(const unique_ptr<_Tp, _Dp>& __x, |
796 | const unique_ptr<_Up, _Ep>& __y) |
797 | { |
798 | typedef typename |
799 | std::common_type<typename unique_ptr<_Tp, _Dp>::pointer, |
800 | typename unique_ptr<_Up, _Ep>::pointer>::type _CT; |
801 | return std::less<_CT>()(__x.get(), __y.get()); |
802 | } |
803 | |
804 | /// unique_ptr comparison with nullptr |
805 | template<typename _Tp, typename _Dp> |
806 | _GLIBCXX_NODISCARD inline bool |
807 | operator<(const unique_ptr<_Tp, _Dp>& __x, nullptr_t) |
808 | { |
809 | return std::less<typename unique_ptr<_Tp, _Dp>::pointer>()(__x.get(), |
810 | nullptr); |
811 | } |
812 | |
813 | /// unique_ptr comparison with nullptr |
814 | template<typename _Tp, typename _Dp> |
815 | _GLIBCXX_NODISCARD inline bool |
816 | operator<(nullptr_t, const unique_ptr<_Tp, _Dp>& __x) |
817 | { |
818 | return std::less<typename unique_ptr<_Tp, _Dp>::pointer>()(nullptr, |
819 | __x.get()); |
820 | } |
821 | |
822 | /// Relational operator for unique_ptr objects, compares the owned pointers |
823 | template<typename _Tp, typename _Dp, |
824 | typename _Up, typename _Ep> |
825 | _GLIBCXX_NODISCARD inline bool |
826 | operator<=(const unique_ptr<_Tp, _Dp>& __x, |
827 | const unique_ptr<_Up, _Ep>& __y) |
828 | { return !(__y < __x); } |
829 | |
830 | /// unique_ptr comparison with nullptr |
831 | template<typename _Tp, typename _Dp> |
832 | _GLIBCXX_NODISCARD inline bool |
833 | operator<=(const unique_ptr<_Tp, _Dp>& __x, nullptr_t) |
834 | { return !(nullptr < __x); } |
835 | |
836 | /// unique_ptr comparison with nullptr |
837 | template<typename _Tp, typename _Dp> |
838 | _GLIBCXX_NODISCARD inline bool |
839 | operator<=(nullptr_t, const unique_ptr<_Tp, _Dp>& __x) |
840 | { return !(__x < nullptr); } |
841 | |
842 | /// Relational operator for unique_ptr objects, compares the owned pointers |
843 | template<typename _Tp, typename _Dp, |
844 | typename _Up, typename _Ep> |
845 | _GLIBCXX_NODISCARD inline bool |
846 | operator>(const unique_ptr<_Tp, _Dp>& __x, |
847 | const unique_ptr<_Up, _Ep>& __y) |
848 | { return (__y < __x); } |
849 | |
850 | /// unique_ptr comparison with nullptr |
851 | template<typename _Tp, typename _Dp> |
852 | _GLIBCXX_NODISCARD inline bool |
853 | operator>(const unique_ptr<_Tp, _Dp>& __x, nullptr_t) |
854 | { |
855 | return std::less<typename unique_ptr<_Tp, _Dp>::pointer>()(nullptr, |
856 | __x.get()); |
857 | } |
858 | |
859 | /// unique_ptr comparison with nullptr |
860 | template<typename _Tp, typename _Dp> |
861 | _GLIBCXX_NODISCARD inline bool |
862 | operator>(nullptr_t, const unique_ptr<_Tp, _Dp>& __x) |
863 | { |
864 | return std::less<typename unique_ptr<_Tp, _Dp>::pointer>()(__x.get(), |
865 | nullptr); |
866 | } |
867 | |
868 | /// Relational operator for unique_ptr objects, compares the owned pointers |
869 | template<typename _Tp, typename _Dp, |
870 | typename _Up, typename _Ep> |
871 | _GLIBCXX_NODISCARD inline bool |
872 | operator>=(const unique_ptr<_Tp, _Dp>& __x, |
873 | const unique_ptr<_Up, _Ep>& __y) |
874 | { return !(__x < __y); } |
875 | |
876 | /// unique_ptr comparison with nullptr |
877 | template<typename _Tp, typename _Dp> |
878 | _GLIBCXX_NODISCARD inline bool |
879 | operator>=(const unique_ptr<_Tp, _Dp>& __x, nullptr_t) |
880 | { return !(__x < nullptr); } |
881 | |
882 | /// unique_ptr comparison with nullptr |
883 | template<typename _Tp, typename _Dp> |
884 | _GLIBCXX_NODISCARD inline bool |
885 | operator>=(nullptr_t, const unique_ptr<_Tp, _Dp>& __x) |
886 | { return !(nullptr < __x); } |
887 | |
888 | #ifdef __cpp_lib_three_way_comparison |
889 | template<typename _Tp, typename _Dp, typename _Up, typename _Ep> |
890 | requires three_way_comparable_with<typename unique_ptr<_Tp, _Dp>::pointer, |
891 | typename unique_ptr<_Up, _Ep>::pointer> |
892 | inline |
893 | compare_three_way_result_t<typename unique_ptr<_Tp, _Dp>::pointer, |
894 | typename unique_ptr<_Up, _Ep>::pointer> |
895 | operator<=>(const unique_ptr<_Tp, _Dp>& __x, |
896 | const unique_ptr<_Up, _Ep>& __y) |
897 | { return compare_three_way()(__x.get(), __y.get()); } |
898 | |
899 | template<typename _Tp, typename _Dp> |
900 | requires three_way_comparable<typename unique_ptr<_Tp, _Dp>::pointer> |
901 | inline |
902 | compare_three_way_result_t<typename unique_ptr<_Tp, _Dp>::pointer> |
903 | operator<=>(const unique_ptr<_Tp, _Dp>& __x, nullptr_t) |
904 | { |
905 | using pointer = typename unique_ptr<_Tp, _Dp>::pointer; |
906 | return compare_three_way()(__x.get(), static_cast<pointer>(nullptr)); |
907 | } |
908 | #endif |
909 | // @} relates unique_ptr |
910 | |
911 | /// @cond undocumented |
912 | template<typename _Up, typename _Ptr = typename _Up::pointer, |
913 | bool = __poison_hash<_Ptr>::__enable_hash_call> |
914 | struct __uniq_ptr_hash |
915 | #if ! _GLIBCXX_INLINE_VERSION0 |
916 | : private __poison_hash<_Ptr> |
917 | #endif |
918 | { |
919 | size_t |
920 | operator()(const _Up& __u) const |
921 | noexcept(noexcept(std::declval<hash<_Ptr>>()(std::declval<_Ptr>()))) |
922 | { return hash<_Ptr>()(__u.get()); } |
923 | }; |
924 | |
925 | template<typename _Up, typename _Ptr> |
926 | struct __uniq_ptr_hash<_Up, _Ptr, false> |
927 | : private __poison_hash<_Ptr> |
928 | { }; |
929 | /// @endcond |
930 | |
931 | /// std::hash specialization for unique_ptr. |
932 | template<typename _Tp, typename _Dp> |
933 | struct hash<unique_ptr<_Tp, _Dp>> |
934 | : public __hash_base<size_t, unique_ptr<_Tp, _Dp>>, |
935 | public __uniq_ptr_hash<unique_ptr<_Tp, _Dp>> |
936 | { }; |
937 | |
938 | #if __cplusplus201402L >= 201402L |
939 | /// @relates unique_ptr @{ |
940 | #define __cpp_lib_make_unique201304 201304 |
941 | |
942 | /// @cond undocumented |
943 | |
944 | template<typename _Tp> |
945 | struct _MakeUniq |
946 | { typedef unique_ptr<_Tp> __single_object; }; |
947 | |
948 | template<typename _Tp> |
949 | struct _MakeUniq<_Tp[]> |
950 | { typedef unique_ptr<_Tp[]> __array; }; |
951 | |
952 | template<typename _Tp, size_t _Bound> |
953 | struct _MakeUniq<_Tp[_Bound]> |
954 | { struct __invalid_type { }; }; |
955 | |
956 | /// @endcond |
957 | |
958 | /// std::make_unique for single objects |
959 | template<typename _Tp, typename... _Args> |
960 | inline typename _MakeUniq<_Tp>::__single_object |
961 | make_unique(_Args&&... __args) |
962 | { return unique_ptr<_Tp>(new _Tp(std::forward<_Args>(__args)...)); } |
963 | |
964 | /// std::make_unique for arrays of unknown bound |
965 | template<typename _Tp> |
966 | inline typename _MakeUniq<_Tp>::__array |
967 | make_unique(size_t __num) |
968 | { return unique_ptr<_Tp>(new remove_extent_t<_Tp>[__num]()); } |
969 | |
970 | /// Disable std::make_unique for arrays of known bound |
971 | template<typename _Tp, typename... _Args> |
972 | inline typename _MakeUniq<_Tp>::__invalid_type |
973 | make_unique(_Args&&...) = delete; |
974 | // @} relates unique_ptr |
975 | #endif // C++14 |
976 | |
977 | #if __cplusplus201402L > 201703L && __cpp_concepts |
978 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
979 | // 2948. unique_ptr does not define operator<< for stream output |
980 | /// Stream output operator for unique_ptr |
981 | template<typename _CharT, typename _Traits, typename _Tp, typename _Dp> |
982 | inline basic_ostream<_CharT, _Traits>& |
983 | operator<<(basic_ostream<_CharT, _Traits>& __os, |
984 | const unique_ptr<_Tp, _Dp>& __p) |
985 | requires requires { __os << __p.get(); } |
986 | { |
987 | __os << __p.get(); |
988 | return __os; |
989 | } |
990 | #endif // C++20 |
991 | |
992 | // @} group pointer_abstractions |
993 | |
994 | #if __cplusplus201402L >= 201703L |
995 | namespace __detail::__variant |
996 | { |
997 | template<typename> struct _Never_valueless_alt; // see <variant> |
998 | |
999 | // Provide the strong exception-safety guarantee when emplacing a |
1000 | // unique_ptr into a variant. |
1001 | template<typename _Tp, typename _Del> |
1002 | struct _Never_valueless_alt<std::unique_ptr<_Tp, _Del>> |
1003 | : std::true_type |
1004 | { }; |
1005 | } // namespace __detail::__variant |
1006 | #endif // C++17 |
1007 | |
1008 | _GLIBCXX_END_NAMESPACE_VERSION |
1009 | } // namespace |
1010 | |
1011 | #endif /* _UNIQUE_PTR_H */ |
1 | //===- llvm/TableGen/Record.h - Classes for Table Records -------*- C++ -*-===// | ||||||
2 | // | ||||||
3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||||||
4 | // See https://llvm.org/LICENSE.txt for license information. | ||||||
5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||||||
6 | // | ||||||
7 | //===----------------------------------------------------------------------===// | ||||||
8 | // | ||||||
9 | // This file defines the main TableGen data structures, including the TableGen | ||||||
10 | // types, values, and high-level data structures. | ||||||
11 | // | ||||||
12 | //===----------------------------------------------------------------------===// | ||||||
13 | |||||||
14 | #ifndef LLVM_TABLEGEN_RECORD_H | ||||||
15 | #define LLVM_TABLEGEN_RECORD_H | ||||||
16 | |||||||
17 | #include "llvm/ADT/ArrayRef.h" | ||||||
18 | #include "llvm/ADT/DenseMap.h" | ||||||
19 | #include "llvm/ADT/DenseSet.h" | ||||||
20 | #include "llvm/ADT/FoldingSet.h" | ||||||
21 | #include "llvm/ADT/PointerIntPair.h" | ||||||
22 | #include "llvm/ADT/SmallVector.h" | ||||||
23 | #include "llvm/ADT/StringExtras.h" | ||||||
24 | #include "llvm/ADT/StringRef.h" | ||||||
25 | #include "llvm/Support/Casting.h" | ||||||
26 | #include "llvm/Support/ErrorHandling.h" | ||||||
27 | #include "llvm/Support/SMLoc.h" | ||||||
28 | #include "llvm/Support/Timer.h" | ||||||
29 | #include "llvm/Support/TrailingObjects.h" | ||||||
30 | #include "llvm/Support/raw_ostream.h" | ||||||
31 | #include <algorithm> | ||||||
32 | #include <cassert> | ||||||
33 | #include <cstddef> | ||||||
34 | #include <cstdint> | ||||||
35 | #include <map> | ||||||
36 | #include <memory> | ||||||
37 | #include <string> | ||||||
38 | #include <utility> | ||||||
39 | #include <vector> | ||||||
40 | |||||||
41 | namespace llvm { | ||||||
42 | |||||||
43 | class ListRecTy; | ||||||
44 | struct MultiClass; | ||||||
45 | class Record; | ||||||
46 | class RecordKeeper; | ||||||
47 | class RecordVal; | ||||||
48 | class Resolver; | ||||||
49 | class StringInit; | ||||||
50 | class TypedInit; | ||||||
51 | |||||||
52 | //===----------------------------------------------------------------------===// | ||||||
53 | // Type Classes | ||||||
54 | //===----------------------------------------------------------------------===// | ||||||
55 | |||||||
56 | class RecTy { | ||||||
57 | public: | ||||||
58 | /// Subclass discriminator (for dyn_cast<> et al.) | ||||||
59 | enum RecTyKind { | ||||||
60 | BitRecTyKind, | ||||||
61 | BitsRecTyKind, | ||||||
62 | IntRecTyKind, | ||||||
63 | StringRecTyKind, | ||||||
64 | ListRecTyKind, | ||||||
65 | DagRecTyKind, | ||||||
66 | RecordRecTyKind | ||||||
67 | }; | ||||||
68 | |||||||
69 | private: | ||||||
70 | RecTyKind Kind; | ||||||
71 | /// ListRecTy of the list that has elements of this type. | ||||||
72 | ListRecTy *ListTy = nullptr; | ||||||
73 | |||||||
74 | public: | ||||||
75 | RecTy(RecTyKind K) : Kind(K) {} | ||||||
76 | virtual ~RecTy() = default; | ||||||
77 | |||||||
78 | RecTyKind getRecTyKind() const { return Kind; } | ||||||
79 | |||||||
80 | virtual std::string getAsString() const = 0; | ||||||
81 | void print(raw_ostream &OS) const { OS << getAsString(); } | ||||||
82 | void dump() const; | ||||||
83 | |||||||
84 | /// Return true if all values of 'this' type can be converted to the specified | ||||||
85 | /// type. | ||||||
86 | virtual bool typeIsConvertibleTo(const RecTy *RHS) const; | ||||||
87 | |||||||
88 | /// Return true if 'this' type is equal to or a subtype of RHS. For example, | ||||||
89 | /// a bit set is not an int, but they are convertible. | ||||||
90 | virtual bool typeIsA(const RecTy *RHS) const; | ||||||
91 | |||||||
92 | /// Returns the type representing list<thistype>. | ||||||
93 | ListRecTy *getListTy(); | ||||||
94 | }; | ||||||
95 | |||||||
96 | inline raw_ostream &operator<<(raw_ostream &OS, const RecTy &Ty) { | ||||||
97 | Ty.print(OS); | ||||||
98 | return OS; | ||||||
99 | } | ||||||
100 | |||||||
101 | /// 'bit' - Represent a single bit | ||||||
102 | class BitRecTy : public RecTy { | ||||||
103 | static BitRecTy Shared; | ||||||
104 | |||||||
105 | BitRecTy() : RecTy(BitRecTyKind) {} | ||||||
106 | |||||||
107 | public: | ||||||
108 | static bool classof(const RecTy *RT) { | ||||||
109 | return RT->getRecTyKind() == BitRecTyKind; | ||||||
110 | } | ||||||
111 | |||||||
112 | static BitRecTy *get() { return &Shared; } | ||||||
113 | |||||||
114 | std::string getAsString() const override { return "bit"; } | ||||||
115 | |||||||
116 | bool typeIsConvertibleTo(const RecTy *RHS) const override; | ||||||
117 | }; | ||||||
118 | |||||||
119 | /// 'bits<n>' - Represent a fixed number of bits | ||||||
120 | class BitsRecTy : public RecTy { | ||||||
121 | unsigned Size; | ||||||
122 | |||||||
123 | explicit BitsRecTy(unsigned Sz) : RecTy(BitsRecTyKind), Size(Sz) {} | ||||||
124 | |||||||
125 | public: | ||||||
126 | static bool classof(const RecTy *RT) { | ||||||
127 | return RT->getRecTyKind() == BitsRecTyKind; | ||||||
128 | } | ||||||
129 | |||||||
130 | static BitsRecTy *get(unsigned Sz); | ||||||
131 | |||||||
132 | unsigned getNumBits() const { return Size; } | ||||||
133 | |||||||
134 | std::string getAsString() const override; | ||||||
135 | |||||||
136 | bool typeIsConvertibleTo(const RecTy *RHS) const override; | ||||||
137 | |||||||
138 | bool typeIsA(const RecTy *RHS) const override; | ||||||
139 | }; | ||||||
140 | |||||||
141 | /// 'int' - Represent an integer value of no particular size | ||||||
142 | class IntRecTy : public RecTy { | ||||||
143 | static IntRecTy Shared; | ||||||
144 | |||||||
145 | IntRecTy() : RecTy(IntRecTyKind) {} | ||||||
146 | |||||||
147 | public: | ||||||
148 | static bool classof(const RecTy *RT) { | ||||||
149 | return RT->getRecTyKind() == IntRecTyKind; | ||||||
150 | } | ||||||
151 | |||||||
152 | static IntRecTy *get() { return &Shared; } | ||||||
153 | |||||||
154 | std::string getAsString() const override { return "int"; } | ||||||
155 | |||||||
156 | bool typeIsConvertibleTo(const RecTy *RHS) const override; | ||||||
157 | }; | ||||||
158 | |||||||
159 | /// 'string' - Represent an string value | ||||||
160 | class StringRecTy : public RecTy { | ||||||
161 | static StringRecTy Shared; | ||||||
162 | |||||||
163 | StringRecTy() : RecTy(StringRecTyKind) {} | ||||||
164 | |||||||
165 | public: | ||||||
166 | static bool classof(const RecTy *RT) { | ||||||
167 | return RT->getRecTyKind() == StringRecTyKind; | ||||||
168 | } | ||||||
169 | |||||||
170 | static StringRecTy *get() { return &Shared; } | ||||||
171 | |||||||
172 | std::string getAsString() const override; | ||||||
173 | |||||||
174 | bool typeIsConvertibleTo(const RecTy *RHS) const override; | ||||||
175 | }; | ||||||
176 | |||||||
177 | /// 'list<Ty>' - Represent a list of element values, all of which must be of | ||||||
178 | /// the specified type. The type is stored in ElementTy. | ||||||
179 | class ListRecTy : public RecTy { | ||||||
180 | friend ListRecTy *RecTy::getListTy(); | ||||||
181 | |||||||
182 | RecTy *ElementTy; | ||||||
183 | |||||||
184 | explicit ListRecTy(RecTy *T) : RecTy(ListRecTyKind), ElementTy(T) {} | ||||||
185 | |||||||
186 | public: | ||||||
187 | static bool classof(const RecTy *RT) { | ||||||
188 | return RT->getRecTyKind() == ListRecTyKind; | ||||||
189 | } | ||||||
190 | |||||||
191 | static ListRecTy *get(RecTy *T) { return T->getListTy(); } | ||||||
192 | RecTy *getElementType() const { return ElementTy; } | ||||||
193 | |||||||
194 | std::string getAsString() const override; | ||||||
195 | |||||||
196 | bool typeIsConvertibleTo(const RecTy *RHS) const override; | ||||||
197 | |||||||
198 | bool typeIsA(const RecTy *RHS) const override; | ||||||
199 | }; | ||||||
200 | |||||||
201 | /// 'dag' - Represent a dag fragment | ||||||
202 | class DagRecTy : public RecTy { | ||||||
203 | static DagRecTy Shared; | ||||||
204 | |||||||
205 | DagRecTy() : RecTy(DagRecTyKind) {} | ||||||
206 | |||||||
207 | public: | ||||||
208 | static bool classof(const RecTy *RT) { | ||||||
209 | return RT->getRecTyKind() == DagRecTyKind; | ||||||
210 | } | ||||||
211 | |||||||
212 | static DagRecTy *get() { return &Shared; } | ||||||
213 | |||||||
214 | std::string getAsString() const override; | ||||||
215 | }; | ||||||
216 | |||||||
217 | /// '[classname]' - Type of record values that have zero or more superclasses. | ||||||
218 | /// | ||||||
219 | /// The list of superclasses is non-redundant, i.e. only contains classes that | ||||||
220 | /// are not the superclass of some other listed class. | ||||||
221 | class RecordRecTy final : public RecTy, public FoldingSetNode, | ||||||
222 | public TrailingObjects<RecordRecTy, Record *> { | ||||||
223 | friend class Record; | ||||||
224 | |||||||
225 | unsigned NumClasses; | ||||||
226 | |||||||
227 | explicit RecordRecTy(unsigned Num) | ||||||
228 | : RecTy(RecordRecTyKind), NumClasses(Num) {} | ||||||
229 | |||||||
230 | public: | ||||||
231 | RecordRecTy(const RecordRecTy &) = delete; | ||||||
232 | RecordRecTy &operator=(const RecordRecTy &) = delete; | ||||||
233 | |||||||
234 | // Do not use sized deallocation due to trailing objects. | ||||||
235 | void operator delete(void *p) { ::operator delete(p); } | ||||||
236 | |||||||
237 | static bool classof(const RecTy *RT) { | ||||||
238 | return RT->getRecTyKind() == RecordRecTyKind; | ||||||
239 | } | ||||||
240 | |||||||
241 | /// Get the record type with the given non-redundant list of superclasses. | ||||||
242 | static RecordRecTy *get(ArrayRef<Record *> Classes); | ||||||
243 | |||||||
244 | void Profile(FoldingSetNodeID &ID) const; | ||||||
245 | |||||||
246 | ArrayRef<Record *> getClasses() const { | ||||||
247 | return makeArrayRef(getTrailingObjects<Record *>(), NumClasses); | ||||||
248 | } | ||||||
249 | |||||||
250 | using const_record_iterator = Record * const *; | ||||||
251 | |||||||
252 | const_record_iterator classes_begin() const { return getClasses().begin(); } | ||||||
253 | const_record_iterator classes_end() const { return getClasses().end(); } | ||||||
254 | |||||||
255 | std::string getAsString() const override; | ||||||
256 | |||||||
257 | bool isSubClassOf(Record *Class) const; | ||||||
258 | bool typeIsConvertibleTo(const RecTy *RHS) const override; | ||||||
259 | |||||||
260 | bool typeIsA(const RecTy *RHS) const override; | ||||||
261 | }; | ||||||
262 | |||||||
263 | /// Find a common type that T1 and T2 convert to. | ||||||
264 | /// Return 0 if no such type exists. | ||||||
265 | RecTy *resolveTypes(RecTy *T1, RecTy *T2); | ||||||
266 | |||||||
267 | //===----------------------------------------------------------------------===// | ||||||
268 | // Initializer Classes | ||||||
269 | //===----------------------------------------------------------------------===// | ||||||
270 | |||||||
271 | class Init { | ||||||
272 | protected: | ||||||
273 | /// Discriminator enum (for isa<>, dyn_cast<>, et al.) | ||||||
274 | /// | ||||||
275 | /// This enum is laid out by a preorder traversal of the inheritance | ||||||
276 | /// hierarchy, and does not contain an entry for abstract classes, as per | ||||||
277 | /// the recommendation in docs/HowToSetUpLLVMStyleRTTI.rst. | ||||||
278 | /// | ||||||
279 | /// We also explicitly include "first" and "last" values for each | ||||||
280 | /// interior node of the inheritance tree, to make it easier to read the | ||||||
281 | /// corresponding classof(). | ||||||
282 | /// | ||||||
283 | /// We could pack these a bit tighter by not having the IK_FirstXXXInit | ||||||
284 | /// and IK_LastXXXInit be their own values, but that would degrade | ||||||
285 | /// readability for really no benefit. | ||||||
286 | enum InitKind : uint8_t { | ||||||
287 | IK_First, // unused; silence a spurious warning | ||||||
288 | IK_FirstTypedInit, | ||||||
289 | IK_BitInit, | ||||||
290 | IK_BitsInit, | ||||||
291 | IK_DagInit, | ||||||
292 | IK_DefInit, | ||||||
293 | IK_FieldInit, | ||||||
294 | IK_IntInit, | ||||||
295 | IK_ListInit, | ||||||
296 | IK_FirstOpInit, | ||||||
297 | IK_BinOpInit, | ||||||
298 | IK_TernOpInit, | ||||||
299 | IK_UnOpInit, | ||||||
300 | IK_LastOpInit, | ||||||
301 | IK_CondOpInit, | ||||||
302 | IK_FoldOpInit, | ||||||
303 | IK_IsAOpInit, | ||||||
304 | IK_AnonymousNameInit, | ||||||
305 | IK_StringInit, | ||||||
306 | IK_VarInit, | ||||||
307 | IK_VarListElementInit, | ||||||
308 | IK_VarBitInit, | ||||||
309 | IK_VarDefInit, | ||||||
310 | IK_LastTypedInit, | ||||||
311 | IK_UnsetInit | ||||||
312 | }; | ||||||
313 | |||||||
314 | private: | ||||||
315 | const InitKind Kind; | ||||||
316 | |||||||
317 | protected: | ||||||
318 | uint8_t Opc; // Used by UnOpInit, BinOpInit, and TernOpInit | ||||||
319 | |||||||
320 | private: | ||||||
321 | virtual void anchor(); | ||||||
322 | |||||||
323 | public: | ||||||
324 | /// Get the kind (type) of the value. | ||||||
325 | InitKind getKind() const { return Kind; } | ||||||
326 | |||||||
327 | protected: | ||||||
328 | explicit Init(InitKind K, uint8_t Opc = 0) : Kind(K), Opc(Opc) {} | ||||||
329 | |||||||
330 | public: | ||||||
331 | Init(const Init &) = delete; | ||||||
332 | Init &operator=(const Init &) = delete; | ||||||
333 | virtual ~Init() = default; | ||||||
334 | |||||||
335 | /// Is this a complete value with no unset (uninitialized) subvalues? | ||||||
336 | virtual bool isComplete() const { return true; } | ||||||
337 | |||||||
338 | /// Is this a concrete and fully resolved value without any references or | ||||||
339 | /// stuck operations? Unset values are concrete. | ||||||
340 | virtual bool isConcrete() const { return false; } | ||||||
341 | |||||||
342 | /// Print this value. | ||||||
343 | void print(raw_ostream &OS) const { OS << getAsString(); } | ||||||
344 | |||||||
345 | /// Convert this value to a literal form. | ||||||
346 | virtual std::string getAsString() const = 0; | ||||||
347 | |||||||
348 | /// Convert this value to a literal form, | ||||||
349 | /// without adding quotes around a string. | ||||||
350 | virtual std::string getAsUnquotedString() const { return getAsString(); } | ||||||
351 | |||||||
352 | /// Debugging method that may be called through a debugger; just | ||||||
353 | /// invokes print on stderr. | ||||||
354 | void dump() const; | ||||||
355 | |||||||
356 | /// If this value is convertible to type \p Ty, return a value whose | ||||||
357 | /// type is \p Ty, generating a !cast operation if required. | ||||||
358 | /// Otherwise, return null. | ||||||
359 | virtual Init *getCastTo(RecTy *Ty) const = 0; | ||||||
360 | |||||||
361 | /// Convert to a value whose type is \p Ty, or return null if this | ||||||
362 | /// is not possible. This can happen if the value's type is convertible | ||||||
363 | /// to \p Ty, but there are unresolved references. | ||||||
364 | virtual Init *convertInitializerTo(RecTy *Ty) const = 0; | ||||||
365 | |||||||
366 | /// This function is used to implement the bit range | ||||||
367 | /// selection operator. Given a value, it selects the specified bits, | ||||||
368 | /// returning them as a new \p Init of type \p bits. If it is not legal | ||||||
369 | /// to use the bit selection operator on this value, null is returned. | ||||||
370 | virtual Init *convertInitializerBitRange(ArrayRef<unsigned> Bits) const { | ||||||
371 | return nullptr; | ||||||
372 | } | ||||||
373 | |||||||
374 | /// This function is used to implement the list slice | ||||||
375 | /// selection operator. Given a value, it selects the specified list | ||||||
376 | /// elements, returning them as a new \p Init of type \p list. If it | ||||||
377 | /// is not legal to use the slice operator, null is returned. | ||||||
378 | virtual Init *convertInitListSlice(ArrayRef<unsigned> Elements) const { | ||||||
379 | return nullptr; | ||||||
380 | } | ||||||
381 | |||||||
382 | /// This function is used to implement the FieldInit class. | ||||||
383 | /// Implementors of this method should return the type of the named | ||||||
384 | /// field if they are of type record. | ||||||
385 | virtual RecTy *getFieldType(StringInit *FieldName) const { | ||||||
386 | return nullptr; | ||||||
387 | } | ||||||
388 | |||||||
389 | /// This function is used by classes that refer to other | ||||||
390 | /// variables which may not be defined at the time the expression is formed. | ||||||
391 | /// If a value is set for the variable later, this method will be called on | ||||||
392 | /// users of the value to allow the value to propagate out. | ||||||
393 | virtual Init *resolveReferences(Resolver &R) const { | ||||||
394 | return const_cast<Init *>(this); | ||||||
395 | } | ||||||
396 | |||||||
397 | /// Get the \p Init value of the specified bit. | ||||||
398 | virtual Init *getBit(unsigned Bit) const = 0; | ||||||
399 | }; | ||||||
400 | |||||||
401 | inline raw_ostream &operator<<(raw_ostream &OS, const Init &I) { | ||||||
402 | I.print(OS); return OS; | ||||||
403 | } | ||||||
404 | |||||||
405 | /// This is the common superclass of types that have a specific, | ||||||
406 | /// explicit type, stored in ValueTy. | ||||||
407 | class TypedInit : public Init { | ||||||
408 | RecTy *ValueTy; | ||||||
409 | |||||||
410 | protected: | ||||||
411 | explicit TypedInit(InitKind K, RecTy *T, uint8_t Opc = 0) | ||||||
412 | : Init(K, Opc), ValueTy(T) {} | ||||||
413 | |||||||
414 | public: | ||||||
415 | TypedInit(const TypedInit &) = delete; | ||||||
416 | TypedInit &operator=(const TypedInit &) = delete; | ||||||
417 | |||||||
418 | static bool classof(const Init *I) { | ||||||
419 | return I->getKind() >= IK_FirstTypedInit && | ||||||
420 | I->getKind() <= IK_LastTypedInit; | ||||||
421 | } | ||||||
422 | |||||||
423 | /// Get the type of the Init as a RecTy. | ||||||
424 | RecTy *getType() const { return ValueTy; } | ||||||
425 | |||||||
426 | Init *getCastTo(RecTy *Ty) const override; | ||||||
427 | Init *convertInitializerTo(RecTy *Ty) const override; | ||||||
428 | |||||||
429 | Init *convertInitializerBitRange(ArrayRef<unsigned> Bits) const override; | ||||||
430 | Init *convertInitListSlice(ArrayRef<unsigned> Elements) const override; | ||||||
431 | |||||||
432 | /// This method is used to implement the FieldInit class. | ||||||
433 | /// Implementors of this method should return the type of the named field if | ||||||
434 | /// they are of type record. | ||||||
435 | RecTy *getFieldType(StringInit *FieldName) const override; | ||||||
436 | }; | ||||||
437 | |||||||
438 | /// '?' - Represents an uninitialized value. | ||||||
439 | class UnsetInit : public Init { | ||||||
440 | UnsetInit() : Init(IK_UnsetInit) {} | ||||||
441 | |||||||
442 | public: | ||||||
443 | UnsetInit(const UnsetInit &) = delete; | ||||||
444 | UnsetInit &operator=(const UnsetInit &) = delete; | ||||||
445 | |||||||
446 | static bool classof(const Init *I) { | ||||||
447 | return I->getKind() == IK_UnsetInit; | ||||||
448 | } | ||||||
449 | |||||||
450 | /// Get the singleton unset Init. | ||||||
451 | static UnsetInit *get(); | ||||||
452 | |||||||
453 | Init *getCastTo(RecTy *Ty) const override; | ||||||
454 | Init *convertInitializerTo(RecTy *Ty) const override; | ||||||
455 | |||||||
456 | Init *getBit(unsigned Bit) const override { | ||||||
457 | return const_cast<UnsetInit*>(this); | ||||||
458 | } | ||||||
459 | |||||||
460 | /// Is this a complete value with no unset (uninitialized) subvalues? | ||||||
461 | bool isComplete() const override { return false; } | ||||||
462 | |||||||
463 | bool isConcrete() const override { return true; } | ||||||
464 | |||||||
465 | /// Get the string representation of the Init. | ||||||
466 | std::string getAsString() const override { return "?"; } | ||||||
467 | }; | ||||||
468 | |||||||
469 | /// 'true'/'false' - Represent a concrete initializer for a bit. | ||||||
470 | class BitInit final : public TypedInit { | ||||||
471 | bool Value; | ||||||
472 | |||||||
473 | explicit BitInit(bool V) : TypedInit(IK_BitInit, BitRecTy::get()), Value(V) {} | ||||||
474 | |||||||
475 | public: | ||||||
476 | BitInit(const BitInit &) = delete; | ||||||
477 | BitInit &operator=(BitInit &) = delete; | ||||||
478 | |||||||
479 | static bool classof(const Init *I) { | ||||||
480 | return I->getKind() == IK_BitInit; | ||||||
481 | } | ||||||
482 | |||||||
483 | static BitInit *get(bool V); | ||||||
484 | |||||||
485 | bool getValue() const { return Value; } | ||||||
486 | |||||||
487 | Init *convertInitializerTo(RecTy *Ty) const override; | ||||||
488 | |||||||
489 | Init *getBit(unsigned Bit) const override { | ||||||
490 | assert(Bit < 1 && "Bit index out of range!")(static_cast <bool> (Bit < 1 && "Bit index out of range!" ) ? void (0) : __assert_fail ("Bit < 1 && \"Bit index out of range!\"" , "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/llvm/include/llvm/TableGen/Record.h" , 490, __extension__ __PRETTY_FUNCTION__)); | ||||||
491 | return const_cast<BitInit*>(this); | ||||||
492 | } | ||||||
493 | |||||||
494 | bool isConcrete() const override { return true; } | ||||||
495 | std::string getAsString() const override { return Value ? "1" : "0"; } | ||||||
496 | }; | ||||||
497 | |||||||
498 | /// '{ a, b, c }' - Represents an initializer for a BitsRecTy value. | ||||||
499 | /// It contains a vector of bits, whose size is determined by the type. | ||||||
500 | class BitsInit final : public TypedInit, public FoldingSetNode, | ||||||
501 | public TrailingObjects<BitsInit, Init *> { | ||||||
502 | unsigned NumBits; | ||||||
503 | |||||||
504 | BitsInit(unsigned N) | ||||||
505 | : TypedInit(IK_BitsInit, BitsRecTy::get(N)), NumBits(N) {} | ||||||
506 | |||||||
507 | public: | ||||||
508 | BitsInit(const BitsInit &) = delete; | ||||||
509 | BitsInit &operator=(const BitsInit &) = delete; | ||||||
510 | |||||||
511 | // Do not use sized deallocation due to trailing objects. | ||||||
512 | void operator delete(void *p) { ::operator delete(p); } | ||||||
513 | |||||||
514 | static bool classof(const Init *I) { | ||||||
515 | return I->getKind() == IK_BitsInit; | ||||||
516 | } | ||||||
517 | |||||||
518 | static BitsInit *get(ArrayRef<Init *> Range); | ||||||
519 | |||||||
520 | void Profile(FoldingSetNodeID &ID) const; | ||||||
521 | |||||||
522 | unsigned getNumBits() const { return NumBits; } | ||||||
523 | |||||||
524 | Init *convertInitializerTo(RecTy *Ty) const override; | ||||||
525 | Init *convertInitializerBitRange(ArrayRef<unsigned> Bits) const override; | ||||||
526 | |||||||
527 | bool isComplete() const override { | ||||||
528 | for (unsigned i = 0; i != getNumBits(); ++i) | ||||||
529 | if (!getBit(i)->isComplete()) return false; | ||||||
530 | return true; | ||||||
531 | } | ||||||
532 | |||||||
533 | bool allInComplete() const { | ||||||
534 | for (unsigned i = 0; i != getNumBits(); ++i) | ||||||
535 | if (getBit(i)->isComplete()) return false; | ||||||
536 | return true; | ||||||
537 | } | ||||||
538 | |||||||
539 | bool isConcrete() const override; | ||||||
540 | std::string getAsString() const override; | ||||||
541 | |||||||
542 | Init *resolveReferences(Resolver &R) const override; | ||||||
543 | |||||||
544 | Init *getBit(unsigned Bit) const override { | ||||||
545 | assert(Bit < NumBits && "Bit index out of range!")(static_cast <bool> (Bit < NumBits && "Bit index out of range!" ) ? void (0) : __assert_fail ("Bit < NumBits && \"Bit index out of range!\"" , "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/llvm/include/llvm/TableGen/Record.h" , 545, __extension__ __PRETTY_FUNCTION__)); | ||||||
546 | return getTrailingObjects<Init *>()[Bit]; | ||||||
547 | } | ||||||
548 | }; | ||||||
549 | |||||||
550 | /// '7' - Represent an initialization by a literal integer value. | ||||||
551 | class IntInit : public TypedInit { | ||||||
552 | int64_t Value; | ||||||
553 | |||||||
554 | explicit IntInit(int64_t V) | ||||||
555 | : TypedInit(IK_IntInit, IntRecTy::get()), Value(V) {} | ||||||
556 | |||||||
557 | public: | ||||||
558 | IntInit(const IntInit &) = delete; | ||||||
559 | IntInit &operator=(const IntInit &) = delete; | ||||||
560 | |||||||
561 | static bool classof(const Init *I) { | ||||||
562 | return I->getKind() == IK_IntInit; | ||||||
563 | } | ||||||
564 | |||||||
565 | static IntInit *get(int64_t V); | ||||||
566 | |||||||
567 | int64_t getValue() const { return Value; } | ||||||
568 | |||||||
569 | Init *convertInitializerTo(RecTy *Ty) const override; | ||||||
570 | Init *convertInitializerBitRange(ArrayRef<unsigned> Bits) const override; | ||||||
571 | |||||||
572 | bool isConcrete() const override { return true; } | ||||||
573 | std::string getAsString() const override; | ||||||
574 | |||||||
575 | Init *getBit(unsigned Bit) const override { | ||||||
576 | return BitInit::get((Value & (1ULL << Bit)) != 0); | ||||||
577 | } | ||||||
578 | }; | ||||||
579 | |||||||
580 | /// "anonymous_n" - Represent an anonymous record name | ||||||
581 | class AnonymousNameInit : public TypedInit { | ||||||
582 | unsigned Value; | ||||||
583 | |||||||
584 | explicit AnonymousNameInit(unsigned V) | ||||||
585 | : TypedInit(IK_AnonymousNameInit, StringRecTy::get()), Value(V) {} | ||||||
586 | |||||||
587 | public: | ||||||
588 | AnonymousNameInit(const AnonymousNameInit &) = delete; | ||||||
589 | AnonymousNameInit &operator=(const AnonymousNameInit &) = delete; | ||||||
590 | |||||||
591 | static bool classof(const Init *I) { | ||||||
592 | return I->getKind() == IK_AnonymousNameInit; | ||||||
593 | } | ||||||
594 | |||||||
595 | static AnonymousNameInit *get(unsigned); | ||||||
596 | |||||||
597 | unsigned getValue() const { return Value; } | ||||||
598 | |||||||
599 | StringInit *getNameInit() const; | ||||||
600 | |||||||
601 | std::string getAsString() const override; | ||||||
602 | |||||||
603 | Init *resolveReferences(Resolver &R) const override; | ||||||
604 | |||||||
605 | Init *getBit(unsigned Bit) const override { | ||||||
606 | llvm_unreachable("Illegal bit reference off string")::llvm::llvm_unreachable_internal("Illegal bit reference off string" , "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/llvm/include/llvm/TableGen/Record.h" , 606); | ||||||
607 | } | ||||||
608 | }; | ||||||
609 | |||||||
610 | /// "foo" - Represent an initialization by a string value. | ||||||
611 | class StringInit : public TypedInit { | ||||||
612 | public: | ||||||
613 | enum StringFormat { | ||||||
614 | SF_String, // Format as "text" | ||||||
615 | SF_Code, // Format as [{text}] | ||||||
616 | }; | ||||||
617 | |||||||
618 | private: | ||||||
619 | StringRef Value; | ||||||
620 | StringFormat Format; | ||||||
621 | |||||||
622 | explicit StringInit(StringRef V, StringFormat Fmt) | ||||||
623 | : TypedInit(IK_StringInit, StringRecTy::get()), Value(V), Format(Fmt) {} | ||||||
624 | |||||||
625 | public: | ||||||
626 | StringInit(const StringInit &) = delete; | ||||||
627 | StringInit &operator=(const StringInit &) = delete; | ||||||
628 | |||||||
629 | static bool classof(const Init *I) { | ||||||
630 | return I->getKind() == IK_StringInit; | ||||||
631 | } | ||||||
632 | |||||||
633 | static StringInit *get(StringRef, StringFormat Fmt = SF_String); | ||||||
634 | |||||||
635 | static StringFormat determineFormat(StringFormat Fmt1, StringFormat Fmt2) { | ||||||
636 | return (Fmt1 == SF_Code || Fmt2 == SF_Code) ? SF_Code : SF_String; | ||||||
637 | } | ||||||
638 | |||||||
639 | StringRef getValue() const { return Value; } | ||||||
640 | StringFormat getFormat() const { return Format; } | ||||||
641 | bool hasCodeFormat() const { return Format == SF_Code; } | ||||||
642 | |||||||
643 | Init *convertInitializerTo(RecTy *Ty) const override; | ||||||
644 | |||||||
645 | bool isConcrete() const override { return true; } | ||||||
646 | |||||||
647 | std::string getAsString() const override { | ||||||
648 | if (Format == SF_String) | ||||||
649 | return "\"" + Value.str() + "\""; | ||||||
650 | else | ||||||
651 | return "[{" + Value.str() + "}]"; | ||||||
652 | } | ||||||
653 | |||||||
654 | std::string getAsUnquotedString() const override { | ||||||
655 | return std::string(Value); | ||||||
656 | } | ||||||
657 | |||||||
658 | Init *getBit(unsigned Bit) const override { | ||||||
659 | llvm_unreachable("Illegal bit reference off string")::llvm::llvm_unreachable_internal("Illegal bit reference off string" , "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/llvm/include/llvm/TableGen/Record.h" , 659); | ||||||
660 | } | ||||||
661 | }; | ||||||
662 | |||||||
663 | /// [AL, AH, CL] - Represent a list of defs | ||||||
664 | /// | ||||||
665 | class ListInit final : public TypedInit, public FoldingSetNode, | ||||||
666 | public TrailingObjects<ListInit, Init *> { | ||||||
667 | unsigned NumValues; | ||||||
668 | |||||||
669 | public: | ||||||
670 | using const_iterator = Init *const *; | ||||||
671 | |||||||
672 | private: | ||||||
673 | explicit ListInit(unsigned N, RecTy *EltTy) | ||||||
674 | : TypedInit(IK_ListInit, ListRecTy::get(EltTy)), NumValues(N) {} | ||||||
675 | |||||||
676 | public: | ||||||
677 | ListInit(const ListInit &) = delete; | ||||||
678 | ListInit &operator=(const ListInit &) = delete; | ||||||
679 | |||||||
680 | // Do not use sized deallocation due to trailing objects. | ||||||
681 | void operator delete(void *p) { ::operator delete(p); } | ||||||
682 | |||||||
683 | static bool classof(const Init *I) { | ||||||
684 | return I->getKind() == IK_ListInit; | ||||||
685 | } | ||||||
686 | static ListInit *get(ArrayRef<Init *> Range, RecTy *EltTy); | ||||||
687 | |||||||
688 | void Profile(FoldingSetNodeID &ID) const; | ||||||
689 | |||||||
690 | Init *getElement(unsigned i) const { | ||||||
691 | assert(i < NumValues && "List element index out of range!")(static_cast <bool> (i < NumValues && "List element index out of range!" ) ? void (0) : __assert_fail ("i < NumValues && \"List element index out of range!\"" , "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/llvm/include/llvm/TableGen/Record.h" , 691, __extension__ __PRETTY_FUNCTION__)); | ||||||
692 | return getTrailingObjects<Init *>()[i]; | ||||||
693 | } | ||||||
694 | RecTy *getElementType() const { | ||||||
695 | return cast<ListRecTy>(getType())->getElementType(); | ||||||
696 | } | ||||||
697 | |||||||
698 | Record *getElementAsRecord(unsigned i) const; | ||||||
699 | |||||||
700 | Init *convertInitListSlice(ArrayRef<unsigned> Elements) const override; | ||||||
701 | |||||||
702 | Init *convertInitializerTo(RecTy *Ty) const override; | ||||||
703 | |||||||
704 | /// This method is used by classes that refer to other | ||||||
705 | /// variables which may not be defined at the time they expression is formed. | ||||||
706 | /// If a value is set for the variable later, this method will be called on | ||||||
707 | /// users of the value to allow the value to propagate out. | ||||||
708 | /// | ||||||
709 | Init *resolveReferences(Resolver &R) const override; | ||||||
710 | |||||||
711 | bool isComplete() const override; | ||||||
712 | bool isConcrete() const override; | ||||||
713 | std::string getAsString() const override; | ||||||
714 | |||||||
715 | ArrayRef<Init*> getValues() const { | ||||||
716 | return makeArrayRef(getTrailingObjects<Init *>(), NumValues); | ||||||
717 | } | ||||||
718 | |||||||
719 | const_iterator begin() const { return getTrailingObjects<Init *>(); } | ||||||
720 | const_iterator end () const { return begin() + NumValues; } | ||||||
721 | |||||||
722 | size_t size () const { return NumValues; } | ||||||
723 | bool empty() const { return NumValues == 0; } | ||||||
724 | |||||||
725 | Init *getBit(unsigned Bit) const override { | ||||||
726 | llvm_unreachable("Illegal bit reference off list")::llvm::llvm_unreachable_internal("Illegal bit reference off list" , "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/llvm/include/llvm/TableGen/Record.h" , 726); | ||||||
727 | } | ||||||
728 | }; | ||||||
729 | |||||||
730 | /// Base class for operators | ||||||
731 | /// | ||||||
732 | class OpInit : public TypedInit { | ||||||
733 | protected: | ||||||
734 | explicit OpInit(InitKind K, RecTy *Type, uint8_t Opc) | ||||||
735 | : TypedInit(K, Type, Opc) {} | ||||||
736 | |||||||
737 | public: | ||||||
738 | OpInit(const OpInit &) = delete; | ||||||
739 | OpInit &operator=(OpInit &) = delete; | ||||||
740 | |||||||
741 | static bool classof(const Init *I) { | ||||||
742 | return I->getKind() >= IK_FirstOpInit && | ||||||
743 | I->getKind() <= IK_LastOpInit; | ||||||
744 | } | ||||||
745 | |||||||
746 | // Clone - Clone this operator, replacing arguments with the new list | ||||||
747 | virtual OpInit *clone(ArrayRef<Init *> Operands) const = 0; | ||||||
748 | |||||||
749 | virtual unsigned getNumOperands() const = 0; | ||||||
750 | virtual Init *getOperand(unsigned i) const = 0; | ||||||
751 | |||||||
752 | Init *getBit(unsigned Bit) const override; | ||||||
753 | }; | ||||||
754 | |||||||
755 | /// !op (X) - Transform an init. | ||||||
756 | /// | ||||||
757 | class UnOpInit : public OpInit, public FoldingSetNode { | ||||||
758 | public: | ||||||
759 | enum UnaryOp : uint8_t { CAST, NOT, HEAD, TAIL, SIZE, EMPTY, GETDAGOP }; | ||||||
760 | |||||||
761 | private: | ||||||
762 | Init *LHS; | ||||||
763 | |||||||
764 | UnOpInit(UnaryOp opc, Init *lhs, RecTy *Type) | ||||||
765 | : OpInit(IK_UnOpInit, Type, opc), LHS(lhs) {} | ||||||
766 | |||||||
767 | public: | ||||||
768 | UnOpInit(const UnOpInit &) = delete; | ||||||
769 | UnOpInit &operator=(const UnOpInit &) = delete; | ||||||
770 | |||||||
771 | static bool classof(const Init *I) { | ||||||
772 | return I->getKind() == IK_UnOpInit; | ||||||
773 | } | ||||||
774 | |||||||
775 | static UnOpInit *get(UnaryOp opc, Init *lhs, RecTy *Type); | ||||||
776 | |||||||
777 | void Profile(FoldingSetNodeID &ID) const; | ||||||
778 | |||||||
779 | // Clone - Clone this operator, replacing arguments with the new list | ||||||
780 | OpInit *clone(ArrayRef<Init *> Operands) const override { | ||||||
781 | assert(Operands.size() == 1 &&(static_cast <bool> (Operands.size() == 1 && "Wrong number of operands for unary operation" ) ? void (0) : __assert_fail ("Operands.size() == 1 && \"Wrong number of operands for unary operation\"" , "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/llvm/include/llvm/TableGen/Record.h" , 782, __extension__ __PRETTY_FUNCTION__)) | ||||||
782 | "Wrong number of operands for unary operation")(static_cast <bool> (Operands.size() == 1 && "Wrong number of operands for unary operation" ) ? void (0) : __assert_fail ("Operands.size() == 1 && \"Wrong number of operands for unary operation\"" , "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/llvm/include/llvm/TableGen/Record.h" , 782, __extension__ __PRETTY_FUNCTION__)); | ||||||
783 | return UnOpInit::get(getOpcode(), *Operands.begin(), getType()); | ||||||
784 | } | ||||||
785 | |||||||
786 | unsigned getNumOperands() const override { return 1; } | ||||||
787 | |||||||
788 | Init *getOperand(unsigned i) const override { | ||||||
789 | assert(i == 0 && "Invalid operand id for unary operator")(static_cast <bool> (i == 0 && "Invalid operand id for unary operator" ) ? void (0) : __assert_fail ("i == 0 && \"Invalid operand id for unary operator\"" , "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/llvm/include/llvm/TableGen/Record.h" , 789, __extension__ __PRETTY_FUNCTION__)); | ||||||
790 | return getOperand(); | ||||||
791 | } | ||||||
792 | |||||||
793 | UnaryOp getOpcode() const { return (UnaryOp)Opc; } | ||||||
794 | Init *getOperand() const { return LHS; } | ||||||
795 | |||||||
796 | // Fold - If possible, fold this to a simpler init. Return this if not | ||||||
797 | // possible to fold. | ||||||
798 | Init *Fold(Record *CurRec, bool IsFinal = false) const; | ||||||
799 | |||||||
800 | Init *resolveReferences(Resolver &R) const override; | ||||||
801 | |||||||
802 | std::string getAsString() const override; | ||||||
803 | }; | ||||||
804 | |||||||
805 | /// !op (X, Y) - Combine two inits. | ||||||
806 | class BinOpInit : public OpInit, public FoldingSetNode { | ||||||
807 | public: | ||||||
808 | enum BinaryOp : uint8_t { ADD, SUB, MUL, AND, OR, XOR, SHL, SRA, SRL, LISTCONCAT, | ||||||
809 | LISTSPLAT, STRCONCAT, INTERLEAVE, CONCAT, EQ, | ||||||
810 | NE, LE, LT, GE, GT, SETDAGOP }; | ||||||
811 | |||||||
812 | private: | ||||||
813 | Init *LHS, *RHS; | ||||||
814 | |||||||
815 | BinOpInit(BinaryOp opc, Init *lhs, Init *rhs, RecTy *Type) : | ||||||
816 | OpInit(IK_BinOpInit, Type, opc), LHS(lhs), RHS(rhs) {} | ||||||
817 | |||||||
818 | public: | ||||||
819 | BinOpInit(const BinOpInit &) = delete; | ||||||
820 | BinOpInit &operator=(const BinOpInit &) = delete; | ||||||
821 | |||||||
822 | static bool classof(const Init *I) { | ||||||
823 | return I->getKind() == IK_BinOpInit; | ||||||
824 | } | ||||||
825 | |||||||
826 | static BinOpInit *get(BinaryOp opc, Init *lhs, Init *rhs, | ||||||
827 | RecTy *Type); | ||||||
828 | static Init *getStrConcat(Init *lhs, Init *rhs); | ||||||
829 | static Init *getListConcat(TypedInit *lhs, Init *rhs); | ||||||
830 | |||||||
831 | void Profile(FoldingSetNodeID &ID) const; | ||||||
832 | |||||||
833 | // Clone - Clone this operator, replacing arguments with the new list | ||||||
834 | OpInit *clone(ArrayRef<Init *> Operands) const override { | ||||||
835 | assert(Operands.size() == 2 &&(static_cast <bool> (Operands.size() == 2 && "Wrong number of operands for binary operation" ) ? void (0) : __assert_fail ("Operands.size() == 2 && \"Wrong number of operands for binary operation\"" , "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/llvm/include/llvm/TableGen/Record.h" , 836, __extension__ __PRETTY_FUNCTION__)) | ||||||
836 | "Wrong number of operands for binary operation")(static_cast <bool> (Operands.size() == 2 && "Wrong number of operands for binary operation" ) ? void (0) : __assert_fail ("Operands.size() == 2 && \"Wrong number of operands for binary operation\"" , "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/llvm/include/llvm/TableGen/Record.h" , 836, __extension__ __PRETTY_FUNCTION__)); | ||||||
837 | return BinOpInit::get(getOpcode(), Operands[0], Operands[1], getType()); | ||||||
838 | } | ||||||
839 | |||||||
840 | unsigned getNumOperands() const override { return 2; } | ||||||
841 | Init *getOperand(unsigned i) const override { | ||||||
842 | switch (i) { | ||||||
843 | default: llvm_unreachable("Invalid operand id for binary operator")::llvm::llvm_unreachable_internal("Invalid operand id for binary operator" , "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/llvm/include/llvm/TableGen/Record.h" , 843); | ||||||
844 | case 0: return getLHS(); | ||||||
845 | case 1: return getRHS(); | ||||||
846 | } | ||||||
847 | } | ||||||
848 | |||||||
849 | BinaryOp getOpcode() const { return (BinaryOp)Opc; } | ||||||
850 | Init *getLHS() const { return LHS; } | ||||||
851 | Init *getRHS() const { return RHS; } | ||||||
852 | |||||||
853 | // Fold - If possible, fold this to a simpler init. Return this if not | ||||||
854 | // possible to fold. | ||||||
855 | Init *Fold(Record *CurRec) const; | ||||||
856 | |||||||
857 | Init *resolveReferences(Resolver &R) const override; | ||||||
858 | |||||||
859 | std::string getAsString() const override; | ||||||
860 | }; | ||||||
861 | |||||||
862 | /// !op (X, Y, Z) - Combine two inits. | ||||||
863 | class TernOpInit : public OpInit, public FoldingSetNode { | ||||||
864 | public: | ||||||
865 | enum TernaryOp : uint8_t { SUBST, FOREACH, FILTER, IF, DAG, SUBSTR, FIND }; | ||||||
866 | |||||||
867 | private: | ||||||
868 | Init *LHS, *MHS, *RHS; | ||||||
869 | |||||||
870 | TernOpInit(TernaryOp opc, Init *lhs, Init *mhs, Init *rhs, | ||||||
871 | RecTy *Type) : | ||||||
872 | OpInit(IK_TernOpInit, Type, opc), LHS(lhs), MHS(mhs), RHS(rhs) {} | ||||||
873 | |||||||
874 | public: | ||||||
875 | TernOpInit(const TernOpInit &) = delete; | ||||||
876 | TernOpInit &operator=(const TernOpInit &) = delete; | ||||||
877 | |||||||
878 | static bool classof(const Init *I) { | ||||||
879 | return I->getKind() == IK_TernOpInit; | ||||||
880 | } | ||||||
881 | |||||||
882 | static TernOpInit *get(TernaryOp opc, Init *lhs, | ||||||
883 | Init *mhs, Init *rhs, | ||||||
884 | RecTy *Type); | ||||||
885 | |||||||
886 | void Profile(FoldingSetNodeID &ID) const; | ||||||
887 | |||||||
888 | // Clone - Clone this operator, replacing arguments with the new list | ||||||
889 | OpInit *clone(ArrayRef<Init *> Operands) const override { | ||||||
890 | assert(Operands.size() == 3 &&(static_cast <bool> (Operands.size() == 3 && "Wrong number of operands for ternary operation" ) ? void (0) : __assert_fail ("Operands.size() == 3 && \"Wrong number of operands for ternary operation\"" , "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/llvm/include/llvm/TableGen/Record.h" , 891, __extension__ __PRETTY_FUNCTION__)) | ||||||
891 | "Wrong number of operands for ternary operation")(static_cast <bool> (Operands.size() == 3 && "Wrong number of operands for ternary operation" ) ? void (0) : __assert_fail ("Operands.size() == 3 && \"Wrong number of operands for ternary operation\"" , "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/llvm/include/llvm/TableGen/Record.h" , 891, __extension__ __PRETTY_FUNCTION__)); | ||||||
892 | return TernOpInit::get(getOpcode(), Operands[0], Operands[1], Operands[2], | ||||||
893 | getType()); | ||||||
894 | } | ||||||
895 | |||||||
896 | unsigned getNumOperands() const override { return 3; } | ||||||
897 | Init *getOperand(unsigned i) const override { | ||||||
898 | switch (i) { | ||||||
899 | default: llvm_unreachable("Invalid operand id for ternary operator")::llvm::llvm_unreachable_internal("Invalid operand id for ternary operator" , "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/llvm/include/llvm/TableGen/Record.h" , 899); | ||||||
900 | case 0: return getLHS(); | ||||||
901 | case 1: return getMHS(); | ||||||
902 | case 2: return getRHS(); | ||||||
903 | } | ||||||
904 | } | ||||||
905 | |||||||
906 | TernaryOp getOpcode() const { return (TernaryOp)Opc; } | ||||||
907 | Init *getLHS() const { return LHS; } | ||||||
908 | Init *getMHS() const { return MHS; } | ||||||
909 | Init *getRHS() const { return RHS; } | ||||||
910 | |||||||
911 | // Fold - If possible, fold this to a simpler init. Return this if not | ||||||
912 | // possible to fold. | ||||||
913 | Init *Fold(Record *CurRec) const; | ||||||
914 | |||||||
915 | bool isComplete() const override { | ||||||
916 | return LHS->isComplete() && MHS->isComplete() && RHS->isComplete(); | ||||||
917 | } | ||||||
918 | |||||||
919 | Init *resolveReferences(Resolver &R) const override; | ||||||
920 | |||||||
921 | std::string getAsString() const override; | ||||||
922 | }; | ||||||
923 | |||||||
924 | /// !cond(condition_1: value1, ... , condition_n: value) | ||||||
925 | /// Selects the first value for which condition is true. | ||||||
926 | /// Otherwise reports an error. | ||||||
927 | class CondOpInit final : public TypedInit, public FoldingSetNode, | ||||||
928 | public TrailingObjects<CondOpInit, Init *> { | ||||||
929 | unsigned NumConds; | ||||||
930 | RecTy *ValType; | ||||||
931 | |||||||
932 | CondOpInit(unsigned NC, RecTy *Type) | ||||||
933 | : TypedInit(IK_CondOpInit, Type), | ||||||
934 | NumConds(NC), ValType(Type) {} | ||||||
935 | |||||||
936 | size_t numTrailingObjects(OverloadToken<Init *>) const { | ||||||
937 | return 2*NumConds; | ||||||
938 | } | ||||||
939 | |||||||
940 | public: | ||||||
941 | CondOpInit(const CondOpInit &) = delete; | ||||||
942 | CondOpInit &operator=(const CondOpInit &) = delete; | ||||||
943 | |||||||
944 | static bool classof(const Init *I) { | ||||||
945 | return I->getKind() == IK_CondOpInit; | ||||||
946 | } | ||||||
947 | |||||||
948 | static CondOpInit *get(ArrayRef<Init*> C, ArrayRef<Init*> V, | ||||||
949 | RecTy *Type); | ||||||
950 | |||||||
951 | void Profile(FoldingSetNodeID &ID) const; | ||||||
952 | |||||||
953 | RecTy *getValType() const { return ValType; } | ||||||
954 | |||||||
955 | unsigned getNumConds() const { return NumConds; } | ||||||
956 | |||||||
957 | Init *getCond(unsigned Num) const { | ||||||
958 | assert(Num < NumConds && "Condition number out of range!")(static_cast <bool> (Num < NumConds && "Condition number out of range!" ) ? void (0) : __assert_fail ("Num < NumConds && \"Condition number out of range!\"" , "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/llvm/include/llvm/TableGen/Record.h" , 958, __extension__ __PRETTY_FUNCTION__)); | ||||||
959 | return getTrailingObjects<Init *>()[Num]; | ||||||
960 | } | ||||||
961 | |||||||
962 | Init *getVal(unsigned Num) const { | ||||||
963 | assert(Num < NumConds && "Val number out of range!")(static_cast <bool> (Num < NumConds && "Val number out of range!" ) ? void (0) : __assert_fail ("Num < NumConds && \"Val number out of range!\"" , "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/llvm/include/llvm/TableGen/Record.h" , 963, __extension__ __PRETTY_FUNCTION__)); | ||||||
964 | return getTrailingObjects<Init *>()[Num+NumConds]; | ||||||
965 | } | ||||||
966 | |||||||
967 | ArrayRef<Init *> getConds() const { | ||||||
968 | return makeArrayRef(getTrailingObjects<Init *>(), NumConds); | ||||||
969 | } | ||||||
970 | |||||||
971 | ArrayRef<Init *> getVals() const { | ||||||
972 | return makeArrayRef(getTrailingObjects<Init *>()+NumConds, NumConds); | ||||||
973 | } | ||||||
974 | |||||||
975 | Init *Fold(Record *CurRec) const; | ||||||
976 | |||||||
977 | Init *resolveReferences(Resolver &R) const override; | ||||||
978 | |||||||
979 | bool isConcrete() const override; | ||||||
980 | bool isComplete() const override; | ||||||
981 | std::string getAsString() const override; | ||||||
982 | |||||||
983 | using const_case_iterator = SmallVectorImpl<Init*>::const_iterator; | ||||||
984 | using const_val_iterator = SmallVectorImpl<Init*>::const_iterator; | ||||||
985 | |||||||
986 | inline const_case_iterator arg_begin() const { return getConds().begin(); } | ||||||
987 | inline const_case_iterator arg_end () const { return getConds().end(); } | ||||||
988 | |||||||
989 | inline size_t case_size () const { return NumConds; } | ||||||
990 | inline bool case_empty() const { return NumConds == 0; } | ||||||
991 | |||||||
992 | inline const_val_iterator name_begin() const { return getVals().begin();} | ||||||
993 | inline const_val_iterator name_end () const { return getVals().end(); } | ||||||
994 | |||||||
995 | inline size_t val_size () const { return NumConds; } | ||||||
996 | inline bool val_empty() const { return NumConds == 0; } | ||||||
997 | |||||||
998 | Init *getBit(unsigned Bit) const override; | ||||||
999 | }; | ||||||
1000 | |||||||
1001 | /// !foldl (a, b, expr, start, lst) - Fold over a list. | ||||||
1002 | class FoldOpInit : public TypedInit, public FoldingSetNode { | ||||||
1003 | private: | ||||||
1004 | Init *Start; | ||||||
1005 | Init *List; | ||||||
1006 | Init *A; | ||||||
1007 | Init *B; | ||||||
1008 | Init *Expr; | ||||||
1009 | |||||||
1010 | FoldOpInit(Init *Start, Init *List, Init *A, Init *B, Init *Expr, RecTy *Type) | ||||||
1011 | : TypedInit(IK_FoldOpInit, Type), Start(Start), List(List), A(A), B(B), | ||||||
1012 | Expr(Expr) {} | ||||||
1013 | |||||||
1014 | public: | ||||||
1015 | FoldOpInit(const FoldOpInit &) = delete; | ||||||
1016 | FoldOpInit &operator=(const FoldOpInit &) = delete; | ||||||
1017 | |||||||
1018 | static bool classof(const Init *I) { return I->getKind() == IK_FoldOpInit; } | ||||||
1019 | |||||||
1020 | static FoldOpInit *get(Init *Start, Init *List, Init *A, Init *B, Init *Expr, | ||||||
1021 | RecTy *Type); | ||||||
1022 | |||||||
1023 | void Profile(FoldingSetNodeID &ID) const; | ||||||
1024 | |||||||
1025 | // Fold - If possible, fold this to a simpler init. Return this if not | ||||||
1026 | // possible to fold. | ||||||
1027 | Init *Fold(Record *CurRec) const; | ||||||
1028 | |||||||
1029 | bool isComplete() const override { return false; } | ||||||
1030 | |||||||
1031 | Init *resolveReferences(Resolver &R) const override; | ||||||
1032 | |||||||
1033 | Init *getBit(unsigned Bit) const override; | ||||||
1034 | |||||||
1035 | std::string getAsString() const override; | ||||||
1036 | }; | ||||||
1037 | |||||||
1038 | /// !isa<type>(expr) - Dynamically determine the type of an expression. | ||||||
1039 | class IsAOpInit : public TypedInit, public FoldingSetNode { | ||||||
1040 | private: | ||||||
1041 | RecTy *CheckType; | ||||||
1042 | Init *Expr; | ||||||
1043 | |||||||
1044 | IsAOpInit(RecTy *CheckType, Init *Expr) | ||||||
1045 | : TypedInit(IK_IsAOpInit, IntRecTy::get()), CheckType(CheckType), | ||||||
1046 | Expr(Expr) {} | ||||||
1047 | |||||||
1048 | public: | ||||||
1049 | IsAOpInit(const IsAOpInit &) = delete; | ||||||
1050 | IsAOpInit &operator=(const IsAOpInit &) = delete; | ||||||
1051 | |||||||
1052 | static bool classof(const Init *I) { return I->getKind() == IK_IsAOpInit; } | ||||||
1053 | |||||||
1054 | static IsAOpInit *get(RecTy *CheckType, Init *Expr); | ||||||
1055 | |||||||
1056 | void Profile(FoldingSetNodeID &ID) const; | ||||||
1057 | |||||||
1058 | // Fold - If possible, fold this to a simpler init. Return this if not | ||||||
1059 | // possible to fold. | ||||||
1060 | Init *Fold() const; | ||||||
1061 | |||||||
1062 | bool isComplete() const override { return false; } | ||||||
1063 | |||||||
1064 | Init *resolveReferences(Resolver &R) const override; | ||||||
1065 | |||||||
1066 | Init *getBit(unsigned Bit) const override; | ||||||
1067 | |||||||
1068 | std::string getAsString() const override; | ||||||
1069 | }; | ||||||
1070 | |||||||
1071 | /// 'Opcode' - Represent a reference to an entire variable object. | ||||||
1072 | class VarInit : public TypedInit { | ||||||
1073 | Init *VarName; | ||||||
1074 | |||||||
1075 | explicit VarInit(Init *VN, RecTy *T) | ||||||
1076 | : TypedInit(IK_VarInit, T), VarName(VN) {} | ||||||
1077 | |||||||
1078 | public: | ||||||
1079 | VarInit(const VarInit &) = delete; | ||||||
1080 | VarInit &operator=(const VarInit &) = delete; | ||||||
1081 | |||||||
1082 | static bool classof(const Init *I) { | ||||||
1083 | return I->getKind() == IK_VarInit; | ||||||
1084 | } | ||||||
1085 | |||||||
1086 | static VarInit *get(StringRef VN, RecTy *T); | ||||||
1087 | static VarInit *get(Init *VN, RecTy *T); | ||||||
1088 | |||||||
1089 | StringRef getName() const; | ||||||
1090 | Init *getNameInit() const { return VarName; } | ||||||
1091 | |||||||
1092 | std::string getNameInitAsString() const { | ||||||
1093 | return getNameInit()->getAsUnquotedString(); | ||||||
1094 | } | ||||||
1095 | |||||||
1096 | /// This method is used by classes that refer to other | ||||||
1097 | /// variables which may not be defined at the time they expression is formed. | ||||||
1098 | /// If a value is set for the variable later, this method will be called on | ||||||
1099 | /// users of the value to allow the value to propagate out. | ||||||
1100 | /// | ||||||
1101 | Init *resolveReferences(Resolver &R) const override; | ||||||
1102 | |||||||
1103 | Init *getBit(unsigned Bit) const override; | ||||||
1104 | |||||||
1105 | std::string getAsString() const override { return std::string(getName()); } | ||||||
1106 | }; | ||||||
1107 | |||||||
1108 | /// Opcode{0} - Represent access to one bit of a variable or field. | ||||||
1109 | class VarBitInit final : public TypedInit { | ||||||
1110 | TypedInit *TI; | ||||||
1111 | unsigned Bit; | ||||||
1112 | |||||||
1113 | VarBitInit(TypedInit *T, unsigned B) | ||||||
1114 | : TypedInit(IK_VarBitInit, BitRecTy::get()), TI(T), Bit(B) { | ||||||
1115 | assert(T->getType() &&(static_cast <bool> (T->getType() && (isa< IntRecTy>(T->getType()) || (isa<BitsRecTy>(T-> getType()) && cast<BitsRecTy>(T->getType())-> getNumBits() > B)) && "Illegal VarBitInit expression!" ) ? void (0) : __assert_fail ("T->getType() && (isa<IntRecTy>(T->getType()) || (isa<BitsRecTy>(T->getType()) && cast<BitsRecTy>(T->getType())->getNumBits() > B)) && \"Illegal VarBitInit expression!\"" , "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/llvm/include/llvm/TableGen/Record.h" , 1119, __extension__ __PRETTY_FUNCTION__)) | ||||||
1116 | (isa<IntRecTy>(T->getType()) ||(static_cast <bool> (T->getType() && (isa< IntRecTy>(T->getType()) || (isa<BitsRecTy>(T-> getType()) && cast<BitsRecTy>(T->getType())-> getNumBits() > B)) && "Illegal VarBitInit expression!" ) ? void (0) : __assert_fail ("T->getType() && (isa<IntRecTy>(T->getType()) || (isa<BitsRecTy>(T->getType()) && cast<BitsRecTy>(T->getType())->getNumBits() > B)) && \"Illegal VarBitInit expression!\"" , "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/llvm/include/llvm/TableGen/Record.h" , 1119, __extension__ __PRETTY_FUNCTION__)) | ||||||
1117 | (isa<BitsRecTy>(T->getType()) &&(static_cast <bool> (T->getType() && (isa< IntRecTy>(T->getType()) || (isa<BitsRecTy>(T-> getType()) && cast<BitsRecTy>(T->getType())-> getNumBits() > B)) && "Illegal VarBitInit expression!" ) ? void (0) : __assert_fail ("T->getType() && (isa<IntRecTy>(T->getType()) || (isa<BitsRecTy>(T->getType()) && cast<BitsRecTy>(T->getType())->getNumBits() > B)) && \"Illegal VarBitInit expression!\"" , "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/llvm/include/llvm/TableGen/Record.h" , 1119, __extension__ __PRETTY_FUNCTION__)) | ||||||
1118 | cast<BitsRecTy>(T->getType())->getNumBits() > B)) &&(static_cast <bool> (T->getType() && (isa< IntRecTy>(T->getType()) || (isa<BitsRecTy>(T-> getType()) && cast<BitsRecTy>(T->getType())-> getNumBits() > B)) && "Illegal VarBitInit expression!" ) ? void (0) : __assert_fail ("T->getType() && (isa<IntRecTy>(T->getType()) || (isa<BitsRecTy>(T->getType()) && cast<BitsRecTy>(T->getType())->getNumBits() > B)) && \"Illegal VarBitInit expression!\"" , "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/llvm/include/llvm/TableGen/Record.h" , 1119, __extension__ __PRETTY_FUNCTION__)) | ||||||
1119 | "Illegal VarBitInit expression!")(static_cast <bool> (T->getType() && (isa< IntRecTy>(T->getType()) || (isa<BitsRecTy>(T-> getType()) && cast<BitsRecTy>(T->getType())-> getNumBits() > B)) && "Illegal VarBitInit expression!" ) ? void (0) : __assert_fail ("T->getType() && (isa<IntRecTy>(T->getType()) || (isa<BitsRecTy>(T->getType()) && cast<BitsRecTy>(T->getType())->getNumBits() > B)) && \"Illegal VarBitInit expression!\"" , "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/llvm/include/llvm/TableGen/Record.h" , 1119, __extension__ __PRETTY_FUNCTION__)); | ||||||
1120 | } | ||||||
1121 | |||||||
1122 | public: | ||||||
1123 | VarBitInit(const VarBitInit &) = delete; | ||||||
1124 | VarBitInit &operator=(const VarBitInit &) = delete; | ||||||
1125 | |||||||
1126 | static bool classof(const Init *I) { | ||||||
1127 | return I->getKind() == IK_VarBitInit; | ||||||
1128 | } | ||||||
1129 | |||||||
1130 | static VarBitInit *get(TypedInit *T, unsigned B); | ||||||
1131 | |||||||
1132 | Init *getBitVar() const { return TI; } | ||||||
1133 | unsigned getBitNum() const { return Bit; } | ||||||
1134 | |||||||
1135 | std::string getAsString() const override; | ||||||
1136 | Init *resolveReferences(Resolver &R) const override; | ||||||
1137 | |||||||
1138 | Init *getBit(unsigned B) const override { | ||||||
1139 | assert(B < 1 && "Bit index out of range!")(static_cast <bool> (B < 1 && "Bit index out of range!" ) ? void (0) : __assert_fail ("B < 1 && \"Bit index out of range!\"" , "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/llvm/include/llvm/TableGen/Record.h" , 1139, __extension__ __PRETTY_FUNCTION__)); | ||||||
1140 | return const_cast<VarBitInit*>(this); | ||||||
1141 | } | ||||||
1142 | }; | ||||||
1143 | |||||||
1144 | /// List[4] - Represent access to one element of a var or | ||||||
1145 | /// field. | ||||||
1146 | class VarListElementInit : public TypedInit { | ||||||
1147 | TypedInit *TI; | ||||||
1148 | unsigned Element; | ||||||
1149 | |||||||
1150 | VarListElementInit(TypedInit *T, unsigned E) | ||||||
1151 | : TypedInit(IK_VarListElementInit, | ||||||
1152 | cast<ListRecTy>(T->getType())->getElementType()), | ||||||
1153 | TI(T), Element(E) { | ||||||
1154 | assert(T->getType() && isa<ListRecTy>(T->getType()) &&(static_cast <bool> (T->getType() && isa< ListRecTy>(T->getType()) && "Illegal VarBitInit expression!" ) ? void (0) : __assert_fail ("T->getType() && isa<ListRecTy>(T->getType()) && \"Illegal VarBitInit expression!\"" , "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/llvm/include/llvm/TableGen/Record.h" , 1155, __extension__ __PRETTY_FUNCTION__)) | ||||||
1155 | "Illegal VarBitInit expression!")(static_cast <bool> (T->getType() && isa< ListRecTy>(T->getType()) && "Illegal VarBitInit expression!" ) ? void (0) : __assert_fail ("T->getType() && isa<ListRecTy>(T->getType()) && \"Illegal VarBitInit expression!\"" , "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/llvm/include/llvm/TableGen/Record.h" , 1155, __extension__ __PRETTY_FUNCTION__)); | ||||||
1156 | } | ||||||
1157 | |||||||
1158 | public: | ||||||
1159 | VarListElementInit(const VarListElementInit &) = delete; | ||||||
1160 | VarListElementInit &operator=(const VarListElementInit &) = delete; | ||||||
1161 | |||||||
1162 | static bool classof(const Init *I) { | ||||||
1163 | return I->getKind() == IK_VarListElementInit; | ||||||
1164 | } | ||||||
1165 | |||||||
1166 | static VarListElementInit *get(TypedInit *T, unsigned E); | ||||||
1167 | |||||||
1168 | TypedInit *getVariable() const { return TI; } | ||||||
1169 | unsigned getElementNum() const { return Element; } | ||||||
1170 | |||||||
1171 | std::string getAsString() const override; | ||||||
1172 | Init *resolveReferences(Resolver &R) const override; | ||||||
1173 | |||||||
1174 | Init *getBit(unsigned Bit) const override; | ||||||
1175 | }; | ||||||
1176 | |||||||
1177 | /// AL - Represent a reference to a 'def' in the description | ||||||
1178 | class DefInit : public TypedInit { | ||||||
1179 | friend class Record; | ||||||
1180 | |||||||
1181 | Record *Def; | ||||||
1182 | |||||||
1183 | explicit DefInit(Record *D); | ||||||
1184 | |||||||
1185 | public: | ||||||
1186 | DefInit(const DefInit &) = delete; | ||||||
1187 | DefInit &operator=(const DefInit &) = delete; | ||||||
1188 | |||||||
1189 | static bool classof(const Init *I) { | ||||||
1190 | return I->getKind() == IK_DefInit; | ||||||
1191 | } | ||||||
1192 | |||||||
1193 | static DefInit *get(Record*); | ||||||
1194 | |||||||
1195 | Init *convertInitializerTo(RecTy *Ty) const override; | ||||||
1196 | |||||||
1197 | Record *getDef() const { return Def; } | ||||||
1198 | |||||||
1199 | //virtual Init *convertInitializerBitRange(ArrayRef<unsigned> Bits); | ||||||
1200 | |||||||
1201 | RecTy *getFieldType(StringInit *FieldName) const override; | ||||||
1202 | |||||||
1203 | bool isConcrete() const override { return true; } | ||||||
1204 | std::string getAsString() const override; | ||||||
1205 | |||||||
1206 | Init *getBit(unsigned Bit) const override { | ||||||
1207 | llvm_unreachable("Illegal bit reference off def")::llvm::llvm_unreachable_internal("Illegal bit reference off def" , "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/llvm/include/llvm/TableGen/Record.h" , 1207); | ||||||
1208 | } | ||||||
1209 | }; | ||||||
1210 | |||||||
1211 | /// classname<targs...> - Represent an uninstantiated anonymous class | ||||||
1212 | /// instantiation. | ||||||
1213 | class VarDefInit final : public TypedInit, public FoldingSetNode, | ||||||
1214 | public TrailingObjects<VarDefInit, Init *> { | ||||||
1215 | Record *Class; | ||||||
1216 | DefInit *Def = nullptr; // after instantiation | ||||||
1217 | unsigned NumArgs; | ||||||
1218 | |||||||
1219 | explicit VarDefInit(Record *Class, unsigned N) | ||||||
1220 | : TypedInit(IK_VarDefInit, RecordRecTy::get(Class)), Class(Class), NumArgs(N) {} | ||||||
1221 | |||||||
1222 | DefInit *instantiate(); | ||||||
1223 | |||||||
1224 | public: | ||||||
1225 | VarDefInit(const VarDefInit &) = delete; | ||||||
1226 | VarDefInit &operator=(const VarDefInit &) = delete; | ||||||
1227 | |||||||
1228 | // Do not use sized deallocation due to trailing objects. | ||||||
1229 | void operator delete(void *p) { ::operator delete(p); } | ||||||
1230 | |||||||
1231 | static bool classof(const Init *I) { | ||||||
1232 | return I->getKind() == IK_VarDefInit; | ||||||
1233 | } | ||||||
1234 | static VarDefInit *get(Record *Class, ArrayRef<Init *> Args); | ||||||
1235 | |||||||
1236 | void Profile(FoldingSetNodeID &ID) const; | ||||||
1237 | |||||||
1238 | Init *resolveReferences(Resolver &R) const override; | ||||||
1239 | Init *Fold() const; | ||||||
1240 | |||||||
1241 | std::string getAsString() const override; | ||||||
1242 | |||||||
1243 | Init *getArg(unsigned i) const { | ||||||
1244 | assert(i < NumArgs && "Argument index out of range!")(static_cast <bool> (i < NumArgs && "Argument index out of range!" ) ? void (0) : __assert_fail ("i < NumArgs && \"Argument index out of range!\"" , "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/llvm/include/llvm/TableGen/Record.h" , 1244, __extension__ __PRETTY_FUNCTION__)); | ||||||
1245 | return getTrailingObjects<Init *>()[i]; | ||||||
1246 | } | ||||||
1247 | |||||||
1248 | using const_iterator = Init *const *; | ||||||
1249 | |||||||
1250 | const_iterator args_begin() const { return getTrailingObjects<Init *>(); } | ||||||
1251 | const_iterator args_end () const { return args_begin() + NumArgs; } | ||||||
1252 | |||||||
1253 | size_t args_size () const { return NumArgs; } | ||||||
1254 | bool args_empty() const { return NumArgs == 0; } | ||||||
1255 | |||||||
1256 | ArrayRef<Init *> args() const { return makeArrayRef(args_begin(), NumArgs); } | ||||||
1257 | |||||||
1258 | Init *getBit(unsigned Bit) const override { | ||||||
1259 | llvm_unreachable("Illegal bit reference off anonymous def")::llvm::llvm_unreachable_internal("Illegal bit reference off anonymous def" , "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/llvm/include/llvm/TableGen/Record.h" , 1259); | ||||||
1260 | } | ||||||
1261 | }; | ||||||
1262 | |||||||
1263 | /// X.Y - Represent a reference to a subfield of a variable | ||||||
1264 | class FieldInit : public TypedInit { | ||||||
1265 | Init *Rec; // Record we are referring to | ||||||
1266 | StringInit *FieldName; // Field we are accessing | ||||||
1267 | |||||||
1268 | FieldInit(Init *R, StringInit *FN) | ||||||
1269 | : TypedInit(IK_FieldInit, R->getFieldType(FN)), Rec(R), FieldName(FN) { | ||||||
1270 | #ifndef NDEBUG | ||||||
1271 | if (!getType()) { | ||||||
1272 | llvm::errs() << "In Record = " << Rec->getAsString() | ||||||
1273 | << ", got FieldName = " << *FieldName | ||||||
1274 | << " with non-record type!\n"; | ||||||
1275 | llvm_unreachable("FieldInit with non-record type!")::llvm::llvm_unreachable_internal("FieldInit with non-record type!" , "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/llvm/include/llvm/TableGen/Record.h" , 1275); | ||||||
1276 | } | ||||||
1277 | #endif | ||||||
1278 | } | ||||||
1279 | |||||||
1280 | public: | ||||||
1281 | FieldInit(const FieldInit &) = delete; | ||||||
1282 | FieldInit &operator=(const FieldInit &) = delete; | ||||||
1283 | |||||||
1284 | static bool classof(const Init *I) { | ||||||
1285 | return I->getKind() == IK_FieldInit; | ||||||
1286 | } | ||||||
1287 | |||||||
1288 | static FieldInit *get(Init *R, StringInit *FN); | ||||||
1289 | |||||||
1290 | Init *getRecord() const { return Rec; } | ||||||
1291 | StringInit *getFieldName() const { return FieldName; } | ||||||
1292 | |||||||
1293 | Init *getBit(unsigned Bit) const override; | ||||||
1294 | |||||||
1295 | Init *resolveReferences(Resolver &R) const override; | ||||||
1296 | Init *Fold(Record *CurRec) const; | ||||||
1297 | |||||||
1298 | bool isConcrete() const override; | ||||||
1299 | std::string getAsString() const override { | ||||||
1300 | return Rec->getAsString() + "." + FieldName->getValue().str(); | ||||||
1301 | } | ||||||
1302 | }; | ||||||
1303 | |||||||
1304 | /// (v a, b) - Represent a DAG tree value. DAG inits are required | ||||||
1305 | /// to have at least one value then a (possibly empty) list of arguments. Each | ||||||
1306 | /// argument can have a name associated with it. | ||||||
1307 | class DagInit final : public TypedInit, public FoldingSetNode, | ||||||
1308 | public TrailingObjects<DagInit, Init *, StringInit *> { | ||||||
1309 | friend TrailingObjects; | ||||||
1310 | |||||||
1311 | Init *Val; | ||||||
1312 | StringInit *ValName; | ||||||
1313 | unsigned NumArgs; | ||||||
1314 | unsigned NumArgNames; | ||||||
1315 | |||||||
1316 | DagInit(Init *V, StringInit *VN, unsigned NumArgs, unsigned NumArgNames) | ||||||
1317 | : TypedInit(IK_DagInit, DagRecTy::get()), Val(V), ValName(VN), | ||||||
1318 | NumArgs(NumArgs), NumArgNames(NumArgNames) {} | ||||||
1319 | |||||||
1320 | size_t numTrailingObjects(OverloadToken<Init *>) const { return NumArgs; } | ||||||
1321 | |||||||
1322 | public: | ||||||
1323 | DagInit(const DagInit &) = delete; | ||||||
1324 | DagInit &operator=(const DagInit &) = delete; | ||||||
1325 | |||||||
1326 | static bool classof(const Init *I) { | ||||||
1327 | return I->getKind() == IK_DagInit; | ||||||
1328 | } | ||||||
1329 | |||||||
1330 | static DagInit *get(Init *V, StringInit *VN, ArrayRef<Init *> ArgRange, | ||||||
1331 | ArrayRef<StringInit*> NameRange); | ||||||
1332 | static DagInit *get(Init *V, StringInit *VN, | ||||||
1333 | ArrayRef<std::pair<Init*, StringInit*>> Args); | ||||||
1334 | |||||||
1335 | void Profile(FoldingSetNodeID &ID) const; | ||||||
1336 | |||||||
1337 | Init *getOperator() const { return Val; } | ||||||
1338 | Record *getOperatorAsDef(ArrayRef<SMLoc> Loc) const; | ||||||
1339 | |||||||
1340 | StringInit *getName() const { return ValName; } | ||||||
1341 | |||||||
1342 | StringRef getNameStr() const { | ||||||
1343 | return ValName ? ValName->getValue() : StringRef(); | ||||||
1344 | } | ||||||
1345 | |||||||
1346 | unsigned getNumArgs() const { return NumArgs; } | ||||||
1347 | |||||||
1348 | Init *getArg(unsigned Num) const { | ||||||
1349 | assert(Num < NumArgs && "Arg number out of range!")(static_cast <bool> (Num < NumArgs && "Arg number out of range!" ) ? void (0) : __assert_fail ("Num < NumArgs && \"Arg number out of range!\"" , "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/llvm/include/llvm/TableGen/Record.h" , 1349, __extension__ __PRETTY_FUNCTION__)); | ||||||
1350 | return getTrailingObjects<Init *>()[Num]; | ||||||
1351 | } | ||||||
1352 | |||||||
1353 | StringInit *getArgName(unsigned Num) const { | ||||||
1354 | assert(Num < NumArgNames && "Arg number out of range!")(static_cast <bool> (Num < NumArgNames && "Arg number out of range!" ) ? void (0) : __assert_fail ("Num < NumArgNames && \"Arg number out of range!\"" , "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/llvm/include/llvm/TableGen/Record.h" , 1354, __extension__ __PRETTY_FUNCTION__)); | ||||||
1355 | return getTrailingObjects<StringInit *>()[Num]; | ||||||
1356 | } | ||||||
1357 | |||||||
1358 | StringRef getArgNameStr(unsigned Num) const { | ||||||
1359 | StringInit *Init = getArgName(Num); | ||||||
1360 | return Init ? Init->getValue() : StringRef(); | ||||||
1361 | } | ||||||
1362 | |||||||
1363 | ArrayRef<Init *> getArgs() const { | ||||||
1364 | return makeArrayRef(getTrailingObjects<Init *>(), NumArgs); | ||||||
1365 | } | ||||||
1366 | |||||||
1367 | ArrayRef<StringInit *> getArgNames() const { | ||||||
1368 | return makeArrayRef(getTrailingObjects<StringInit *>(), NumArgNames); | ||||||
1369 | } | ||||||
1370 | |||||||
1371 | Init *resolveReferences(Resolver &R) const override; | ||||||
1372 | |||||||
1373 | bool isConcrete() const override; | ||||||
1374 | std::string getAsString() const override; | ||||||
1375 | |||||||
1376 | using const_arg_iterator = SmallVectorImpl<Init*>::const_iterator; | ||||||
1377 | using const_name_iterator = SmallVectorImpl<StringInit*>::const_iterator; | ||||||
1378 | |||||||
1379 | inline const_arg_iterator arg_begin() const { return getArgs().begin(); } | ||||||
1380 | inline const_arg_iterator arg_end () const { return getArgs().end(); } | ||||||
1381 | |||||||
1382 | inline size_t arg_size () const { return NumArgs; } | ||||||
1383 | inline bool arg_empty() const { return NumArgs == 0; } | ||||||
1384 | |||||||
1385 | inline const_name_iterator name_begin() const { return getArgNames().begin();} | ||||||
1386 | inline const_name_iterator name_end () const { return getArgNames().end(); } | ||||||
1387 | |||||||
1388 | inline size_t name_size () const { return NumArgNames; } | ||||||
1389 | inline bool name_empty() const { return NumArgNames == 0; } | ||||||
1390 | |||||||
1391 | Init *getBit(unsigned Bit) const override { | ||||||
1392 | llvm_unreachable("Illegal bit reference off dag")::llvm::llvm_unreachable_internal("Illegal bit reference off dag" , "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/llvm/include/llvm/TableGen/Record.h" , 1392); | ||||||
1393 | } | ||||||
1394 | }; | ||||||
1395 | |||||||
1396 | //===----------------------------------------------------------------------===// | ||||||
1397 | // High-Level Classes | ||||||
1398 | //===----------------------------------------------------------------------===// | ||||||
1399 | |||||||
1400 | /// This class represents a field in a record, including its name, type, | ||||||
1401 | /// value, and source location. | ||||||
1402 | class RecordVal { | ||||||
1403 | friend class Record; | ||||||
1404 | |||||||
1405 | public: | ||||||
1406 | enum FieldKind { | ||||||
1407 | FK_Normal, // A normal record field. | ||||||
1408 | FK_NonconcreteOK, // A field that can be nonconcrete ('field' keyword). | ||||||
1409 | FK_TemplateArg, // A template argument. | ||||||
1410 | }; | ||||||
1411 | |||||||
1412 | private: | ||||||
1413 | Init *Name; | ||||||
1414 | SMLoc Loc; // Source location of definition of name. | ||||||
1415 | PointerIntPair<RecTy *, 2, FieldKind> TyAndKind; | ||||||
1416 | Init *Value; | ||||||
1417 | |||||||
1418 | public: | ||||||
1419 | RecordVal(Init *N, RecTy *T, FieldKind K); | ||||||
1420 | RecordVal(Init *N, SMLoc Loc, RecTy *T, FieldKind K); | ||||||
1421 | |||||||
1422 | /// Get the name of the field as a StringRef. | ||||||
1423 | StringRef getName() const; | ||||||
1424 | |||||||
1425 | /// Get the name of the field as an Init. | ||||||
1426 | Init *getNameInit() const { return Name; } | ||||||
1427 | |||||||
1428 | /// Get the name of the field as a std::string. | ||||||
1429 | std::string getNameInitAsString() const { | ||||||
1430 | return getNameInit()->getAsUnquotedString(); | ||||||
1431 | } | ||||||
1432 | |||||||
1433 | /// Get the source location of the point where the field was defined. | ||||||
1434 | const SMLoc &getLoc() const { return Loc; } | ||||||
1435 | |||||||
1436 | /// Is this a field where nonconcrete values are okay? | ||||||
1437 | bool isNonconcreteOK() const { | ||||||
1438 | return TyAndKind.getInt() == FK_NonconcreteOK; | ||||||
1439 | } | ||||||
1440 | |||||||
1441 | /// Is this a template argument? | ||||||
1442 | bool isTemplateArg() const { | ||||||
1443 | return TyAndKind.getInt() == FK_TemplateArg; | ||||||
1444 | } | ||||||
1445 | |||||||
1446 | /// Get the type of the field value as a RecTy. | ||||||
1447 | RecTy *getType() const { return TyAndKind.getPointer(); } | ||||||
1448 | |||||||
1449 | /// Get the type of the field for printing purposes. | ||||||
1450 | std::string getPrintType() const; | ||||||
1451 | |||||||
1452 | /// Get the value of the field as an Init. | ||||||
1453 | Init *getValue() const { return Value; } | ||||||
1454 | |||||||
1455 | /// Set the value of the field from an Init. | ||||||
1456 | bool setValue(Init *V); | ||||||
1457 | |||||||
1458 | /// Set the value and source location of the field. | ||||||
1459 | bool setValue(Init *V, SMLoc NewLoc); | ||||||
1460 | |||||||
1461 | void dump() const; | ||||||
1462 | |||||||
1463 | /// Print the value to an output stream, possibly with a semicolon. | ||||||
1464 | void print(raw_ostream &OS, bool PrintSem = true) const; | ||||||
1465 | }; | ||||||
1466 | |||||||
1467 | inline raw_ostream &operator<<(raw_ostream &OS, const RecordVal &RV) { | ||||||
1468 | RV.print(OS << " "); | ||||||
1469 | return OS; | ||||||
1470 | } | ||||||
1471 | |||||||
1472 | class Record { | ||||||
1473 | public: | ||||||
1474 | struct AssertionInfo { | ||||||
1475 | SMLoc Loc; | ||||||
1476 | Init *Condition; | ||||||
1477 | Init *Message; | ||||||
1478 | |||||||
1479 | // User-defined constructor to support std::make_unique(). It can be | ||||||
1480 | // removed in C++20 when braced initialization is supported. | ||||||
1481 | AssertionInfo(SMLoc Loc, Init *Condition, Init *Message) | ||||||
1482 | : Loc(Loc), Condition(Condition), Message(Message) {} | ||||||
1483 | }; | ||||||
1484 | |||||||
1485 | private: | ||||||
1486 | static unsigned LastID; | ||||||
1487 | |||||||
1488 | Init *Name; | ||||||
1489 | // Location where record was instantiated, followed by the location of | ||||||
1490 | // multiclass prototypes used. | ||||||
1491 | SmallVector<SMLoc, 4> Locs; | ||||||
1492 | SmallVector<Init *, 0> TemplateArgs; | ||||||
1493 | SmallVector<RecordVal, 0> Values; | ||||||
1494 | SmallVector<AssertionInfo, 0> Assertions; | ||||||
1495 | |||||||
1496 | // All superclasses in the inheritance forest in post-order (yes, it | ||||||
1497 | // must be a forest; diamond-shaped inheritance is not allowed). | ||||||
1498 | SmallVector<std::pair<Record *, SMRange>, 0> SuperClasses; | ||||||
1499 | |||||||
1500 | // Tracks Record instances. Not owned by Record. | ||||||
1501 | RecordKeeper &TrackedRecords; | ||||||
1502 | |||||||
1503 | // The DefInit corresponding to this record. | ||||||
1504 | DefInit *CorrespondingDefInit = nullptr; | ||||||
1505 | |||||||
1506 | // Unique record ID. | ||||||
1507 | unsigned ID; | ||||||
1508 | |||||||
1509 | bool IsAnonymous; | ||||||
1510 | bool IsClass; | ||||||
1511 | |||||||
1512 | void checkName(); | ||||||
1513 | |||||||
1514 | public: | ||||||
1515 | // Constructs a record. | ||||||
1516 | explicit Record(Init *N, ArrayRef<SMLoc> locs, RecordKeeper &records, | ||||||
1517 | bool Anonymous = false, bool Class = false) | ||||||
1518 | : Name(N), Locs(locs.begin(), locs.end()), TrackedRecords(records), | ||||||
1519 | ID(LastID++), IsAnonymous(Anonymous), IsClass(Class) { | ||||||
1520 | checkName(); | ||||||
1521 | } | ||||||
1522 | |||||||
1523 | explicit Record(StringRef N, ArrayRef<SMLoc> locs, RecordKeeper &records, | ||||||
1524 | bool Class = false) | ||||||
1525 | : Record(StringInit::get(N), locs, records, false, Class) {} | ||||||
1526 | |||||||
1527 | // When copy-constructing a Record, we must still guarantee a globally unique | ||||||
1528 | // ID number. Don't copy CorrespondingDefInit either, since it's owned by the | ||||||
1529 | // original record. All other fields can be copied normally. | ||||||
1530 | Record(const Record &O) | ||||||
1531 | : Name(O.Name), Locs(O.Locs), TemplateArgs(O.TemplateArgs), | ||||||
1532 | Values(O.Values), Assertions(O.Assertions), SuperClasses(O.SuperClasses), | ||||||
1533 | TrackedRecords(O.TrackedRecords), ID(LastID++), | ||||||
1534 | IsAnonymous(O.IsAnonymous), IsClass(O.IsClass) { } | ||||||
1535 | |||||||
1536 | static unsigned getNewUID() { return LastID++; } | ||||||
1537 | |||||||
1538 | unsigned getID() const { return ID; } | ||||||
1539 | |||||||
1540 | StringRef getName() const { return cast<StringInit>(Name)->getValue(); } | ||||||
1541 | |||||||
1542 | Init *getNameInit() const { | ||||||
1543 | return Name; | ||||||
1544 | } | ||||||
1545 | |||||||
1546 | const std::string getNameInitAsString() const { | ||||||
1547 | return getNameInit()->getAsUnquotedString(); | ||||||
1548 | } | ||||||
1549 | |||||||
1550 | void setName(Init *Name); // Also updates RecordKeeper. | ||||||
1551 | |||||||
1552 | ArrayRef<SMLoc> getLoc() const { return Locs; } | ||||||
1553 | void appendLoc(SMLoc Loc) { Locs.push_back(Loc); } | ||||||
1554 | |||||||
1555 | // Make the type that this record should have based on its superclasses. | ||||||
1556 | RecordRecTy *getType(); | ||||||
1557 | |||||||
1558 | /// get the corresponding DefInit. | ||||||
1559 | DefInit *getDefInit(); | ||||||
1560 | |||||||
1561 | bool isClass() const { return IsClass; } | ||||||
1562 | |||||||
1563 | ArrayRef<Init *> getTemplateArgs() const { | ||||||
1564 | return TemplateArgs; | ||||||
1565 | } | ||||||
1566 | |||||||
1567 | ArrayRef<RecordVal> getValues() const { return Values; } | ||||||
1568 | |||||||
1569 | ArrayRef<AssertionInfo> getAssertions() const { return Assertions; } | ||||||
1570 | |||||||
1571 | ArrayRef<std::pair<Record *, SMRange>> getSuperClasses() const { | ||||||
1572 | return SuperClasses; | ||||||
1573 | } | ||||||
1574 | |||||||
1575 | /// Determine whether this record has the specified direct superclass. | ||||||
1576 | bool hasDirectSuperClass(const Record *SuperClass) const; | ||||||
1577 | |||||||
1578 | /// Append the direct superclasses of this record to Classes. | ||||||
1579 | void getDirectSuperClasses(SmallVectorImpl<Record *> &Classes) const; | ||||||
1580 | |||||||
1581 | bool isTemplateArg(Init *Name) const { | ||||||
1582 | return llvm::is_contained(TemplateArgs, Name); | ||||||
1583 | } | ||||||
1584 | |||||||
1585 | const RecordVal *getValue(const Init *Name) const { | ||||||
1586 | for (const RecordVal &Val : Values) | ||||||
1587 | if (Val.Name == Name) return &Val; | ||||||
1588 | return nullptr; | ||||||
1589 | } | ||||||
1590 | |||||||
1591 | const RecordVal *getValue(StringRef Name) const { | ||||||
1592 | return getValue(StringInit::get(Name)); | ||||||
1593 | } | ||||||
1594 | |||||||
1595 | RecordVal *getValue(const Init *Name) { | ||||||
1596 | return const_cast<RecordVal *>(static_cast<const Record *>(this)->getValue(Name)); | ||||||
1597 | } | ||||||
1598 | |||||||
1599 | RecordVal *getValue(StringRef Name) { | ||||||
1600 | return const_cast<RecordVal *>(static_cast<const Record *>(this)->getValue(Name)); | ||||||
1601 | } | ||||||
1602 | |||||||
1603 | void addTemplateArg(Init *Name) { | ||||||
1604 | assert(!isTemplateArg(Name) && "Template arg already defined!")(static_cast <bool> (!isTemplateArg(Name) && "Template arg already defined!" ) ? void (0) : __assert_fail ("!isTemplateArg(Name) && \"Template arg already defined!\"" , "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/llvm/include/llvm/TableGen/Record.h" , 1604, __extension__ __PRETTY_FUNCTION__)); | ||||||
1605 | TemplateArgs.push_back(Name); | ||||||
1606 | } | ||||||
1607 | |||||||
1608 | void addValue(const RecordVal &RV) { | ||||||
1609 | assert(getValue(RV.getNameInit()) == nullptr && "Value already added!")(static_cast <bool> (getValue(RV.getNameInit()) == nullptr && "Value already added!") ? void (0) : __assert_fail ("getValue(RV.getNameInit()) == nullptr && \"Value already added!\"" , "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/llvm/include/llvm/TableGen/Record.h" , 1609, __extension__ __PRETTY_FUNCTION__)); | ||||||
1610 | Values.push_back(RV); | ||||||
1611 | } | ||||||
1612 | |||||||
1613 | void removeValue(Init *Name) { | ||||||
1614 | for (unsigned i = 0, e = Values.size(); i != e; ++i) | ||||||
1615 | if (Values[i].getNameInit() == Name) { | ||||||
1616 | Values.erase(Values.begin()+i); | ||||||
1617 | return; | ||||||
1618 | } | ||||||
1619 | llvm_unreachable("Cannot remove an entry that does not exist!")::llvm::llvm_unreachable_internal("Cannot remove an entry that does not exist!" , "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/llvm/include/llvm/TableGen/Record.h" , 1619); | ||||||
1620 | } | ||||||
1621 | |||||||
1622 | void removeValue(StringRef Name) { | ||||||
1623 | removeValue(StringInit::get(Name)); | ||||||
1624 | } | ||||||
1625 | |||||||
1626 | void addAssertion(SMLoc Loc, Init *Condition, Init *Message) { | ||||||
1627 | Assertions.push_back(AssertionInfo(Loc, Condition, Message)); | ||||||
1628 | } | ||||||
1629 | |||||||
1630 | void appendAssertions(const Record *Rec) { | ||||||
1631 | Assertions.append(Rec->Assertions); | ||||||
1632 | } | ||||||
1633 | |||||||
1634 | void checkRecordAssertions(); | ||||||
1635 | |||||||
1636 | bool isSubClassOf(const Record *R) const { | ||||||
1637 | for (const auto &SCPair : SuperClasses) | ||||||
1638 | if (SCPair.first == R) | ||||||
1639 | return true; | ||||||
1640 | return false; | ||||||
1641 | } | ||||||
1642 | |||||||
1643 | bool isSubClassOf(StringRef Name) const { | ||||||
1644 | for (const auto &SCPair : SuperClasses) { | ||||||
1645 | if (const auto *SI
| ||||||
1646 | if (SI->getValue() == Name) | ||||||
1647 | return true; | ||||||
1648 | } else if (SCPair.first->getNameInitAsString() == Name) { | ||||||
1649 | return true; | ||||||
1650 | } | ||||||
1651 | } | ||||||
1652 | return false; | ||||||
1653 | } | ||||||
1654 | |||||||
1655 | void addSuperClass(Record *R, SMRange Range) { | ||||||
1656 | assert(!CorrespondingDefInit &&(static_cast <bool> (!CorrespondingDefInit && "changing type of record after it has been referenced" ) ? void (0) : __assert_fail ("!CorrespondingDefInit && \"changing type of record after it has been referenced\"" , "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/llvm/include/llvm/TableGen/Record.h" , 1657, __extension__ __PRETTY_FUNCTION__)) | ||||||
1657 | "changing type of record after it has been referenced")(static_cast <bool> (!CorrespondingDefInit && "changing type of record after it has been referenced" ) ? void (0) : __assert_fail ("!CorrespondingDefInit && \"changing type of record after it has been referenced\"" , "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/llvm/include/llvm/TableGen/Record.h" , 1657, __extension__ __PRETTY_FUNCTION__)); | ||||||
1658 | assert(!isSubClassOf(R) && "Already subclassing record!")(static_cast <bool> (!isSubClassOf(R) && "Already subclassing record!" ) ? void (0) : __assert_fail ("!isSubClassOf(R) && \"Already subclassing record!\"" , "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/llvm/include/llvm/TableGen/Record.h" , 1658, __extension__ __PRETTY_FUNCTION__)); | ||||||
1659 | SuperClasses.push_back(std::make_pair(R, Range)); | ||||||
1660 | } | ||||||
1661 | |||||||
1662 | /// If there are any field references that refer to fields | ||||||
1663 | /// that have been filled in, we can propagate the values now. | ||||||
1664 | /// | ||||||
1665 | /// This is a final resolve: any error messages, e.g. due to undefined | ||||||
1666 | /// !cast references, are generated now. | ||||||
1667 | void resolveReferences(Init *NewName = nullptr); | ||||||
1668 | |||||||
1669 | /// Apply the resolver to the name of the record as well as to the | ||||||
1670 | /// initializers of all fields of the record except SkipVal. | ||||||
1671 | /// | ||||||
1672 | /// The resolver should not resolve any of the fields itself, to avoid | ||||||
1673 | /// recursion / infinite loops. | ||||||
1674 | void resolveReferences(Resolver &R, const RecordVal *SkipVal = nullptr); | ||||||
1675 | |||||||
1676 | RecordKeeper &getRecords() const { | ||||||
1677 | return TrackedRecords; | ||||||
1678 | } | ||||||
1679 | |||||||
1680 | bool isAnonymous() const { | ||||||
1681 | return IsAnonymous; | ||||||
1682 | } | ||||||
1683 | |||||||
1684 | void dump() const; | ||||||
1685 | |||||||
1686 | //===--------------------------------------------------------------------===// | ||||||
1687 | // High-level methods useful to tablegen back-ends | ||||||
1688 | // | ||||||
1689 | |||||||
1690 | ///Return the source location for the named field. | ||||||
1691 | SMLoc getFieldLoc(StringRef FieldName) const; | ||||||
1692 | |||||||
1693 | /// Return the initializer for a value with the specified name, | ||||||
1694 | /// or throw an exception if the field does not exist. | ||||||
1695 | Init *getValueInit(StringRef FieldName) const; | ||||||
1696 | |||||||
1697 | /// Return true if the named field is unset. | ||||||
1698 | bool isValueUnset(StringRef FieldName) const { | ||||||
1699 | return isa<UnsetInit>(getValueInit(FieldName)); | ||||||
1700 | } | ||||||
1701 | |||||||
1702 | /// This method looks up the specified field and returns | ||||||
1703 | /// its value as a string, throwing an exception if the field does not exist | ||||||
1704 | /// or if the value is not a string. | ||||||
1705 | StringRef getValueAsString(StringRef FieldName) const; | ||||||
1706 | |||||||
1707 | /// This method looks up the specified field and returns | ||||||
1708 | /// its value as a string, throwing an exception if the field if the value is | ||||||
1709 | /// not a string and llvm::Optional() if the field does not exist. | ||||||
1710 | llvm::Optional<StringRef> getValueAsOptionalString(StringRef FieldName) const; | ||||||
1711 | |||||||
1712 | /// This method looks up the specified field and returns | ||||||
1713 | /// its value as a BitsInit, throwing an exception if the field does not exist | ||||||
1714 | /// or if the value is not the right type. | ||||||
1715 | BitsInit *getValueAsBitsInit(StringRef FieldName) const; | ||||||
1716 | |||||||
1717 | /// This method looks up the specified field and returns | ||||||
1718 | /// its value as a ListInit, throwing an exception if the field does not exist | ||||||
1719 | /// or if the value is not the right type. | ||||||
1720 | ListInit *getValueAsListInit(StringRef FieldName) const; | ||||||
1721 | |||||||
1722 | /// This method looks up the specified field and | ||||||
1723 | /// returns its value as a vector of records, throwing an exception if the | ||||||
1724 | /// field does not exist or if the value is not the right type. | ||||||
1725 | std::vector<Record*> getValueAsListOfDefs(StringRef FieldName) const; | ||||||
1726 | |||||||
1727 | /// This method looks up the specified field and | ||||||
1728 | /// returns its value as a vector of integers, throwing an exception if the | ||||||
1729 | /// field does not exist or if the value is not the right type. | ||||||
1730 | std::vector<int64_t> getValueAsListOfInts(StringRef FieldName) const; | ||||||
1731 | |||||||
1732 | /// This method looks up the specified field and | ||||||
1733 | /// returns its value as a vector of strings, throwing an exception if the | ||||||
1734 | /// field does not exist or if the value is not the right type. | ||||||
1735 | std::vector<StringRef> getValueAsListOfStrings(StringRef FieldName) const; | ||||||
1736 | |||||||
1737 | /// This method looks up the specified field and returns its | ||||||
1738 | /// value as a Record, throwing an exception if the field does not exist or if | ||||||
1739 | /// the value is not the right type. | ||||||
1740 | Record *getValueAsDef(StringRef FieldName) const; | ||||||
1741 | |||||||
1742 | /// This method looks up the specified field and returns its value as a | ||||||
1743 | /// Record, returning null if the field exists but is "uninitialized" | ||||||
1744 | /// (i.e. set to `?`), and throwing an exception if the field does not | ||||||
1745 | /// exist or if its value is not the right type. | ||||||
1746 | Record *getValueAsOptionalDef(StringRef FieldName) const; | ||||||
1747 | |||||||
1748 | /// This method looks up the specified field and returns its | ||||||
1749 | /// value as a bit, throwing an exception if the field does not exist or if | ||||||
1750 | /// the value is not the right type. | ||||||
1751 | bool getValueAsBit(StringRef FieldName) const; | ||||||
1752 | |||||||
1753 | /// This method looks up the specified field and | ||||||
1754 | /// returns its value as a bit. If the field is unset, sets Unset to true and | ||||||
1755 | /// returns false. | ||||||
1756 | bool getValueAsBitOrUnset(StringRef FieldName, bool &Unset) const; | ||||||
1757 | |||||||
1758 | /// This method looks up the specified field and returns its | ||||||
1759 | /// value as an int64_t, throwing an exception if the field does not exist or | ||||||
1760 | /// if the value is not the right type. | ||||||
1761 | int64_t getValueAsInt(StringRef FieldName) const; | ||||||
1762 | |||||||
1763 | /// This method looks up the specified field and returns its | ||||||
1764 | /// value as an Dag, throwing an exception if the field does not exist or if | ||||||
1765 | /// the value is not the right type. | ||||||
1766 | DagInit *getValueAsDag(StringRef FieldName) const; | ||||||
1767 | }; | ||||||
1768 | |||||||
1769 | raw_ostream &operator<<(raw_ostream &OS, const Record &R); | ||||||
1770 | |||||||
1771 | class RecordKeeper { | ||||||
1772 | friend class RecordRecTy; | ||||||
1773 | |||||||
1774 | using RecordMap = std::map<std::string, std::unique_ptr<Record>, std::less<>>; | ||||||
1775 | using GlobalMap = std::map<std::string, Init *, std::less<>>; | ||||||
1776 | |||||||
1777 | std::string InputFilename; | ||||||
1778 | RecordMap Classes, Defs; | ||||||
1779 | mutable StringMap<std::vector<Record *>> ClassRecordsMap; | ||||||
1780 | FoldingSet<RecordRecTy> RecordTypePool; | ||||||
1781 | std::map<std::string, Init *, std::less<>> ExtraGlobals; | ||||||
1782 | unsigned AnonCounter = 0; | ||||||
1783 | |||||||
1784 | // These members are for the phase timing feature. We need a timer group, | ||||||
1785 | // the last timer started, and a flag to say whether the last timer | ||||||
1786 | // is the special "backend overall timer." | ||||||
1787 | TimerGroup *TimingGroup = nullptr; | ||||||
1788 | Timer *LastTimer = nullptr; | ||||||
1789 | bool BackendTimer = false; | ||||||
1790 | |||||||
1791 | public: | ||||||
1792 | /// Get the main TableGen input file's name. | ||||||
1793 | const std::string getInputFilename() const { return InputFilename; } | ||||||
1794 | |||||||
1795 | /// Get the map of classes. | ||||||
1796 | const RecordMap &getClasses() const { return Classes; } | ||||||
1797 | |||||||
1798 | /// Get the map of records (defs). | ||||||
1799 | const RecordMap &getDefs() const { return Defs; } | ||||||
1800 | |||||||
1801 | /// Get the map of global variables. | ||||||
1802 | const GlobalMap &getGlobals() const { return ExtraGlobals; } | ||||||
1803 | |||||||
1804 | /// Get the class with the specified name. | ||||||
1805 | Record *getClass(StringRef Name) const { | ||||||
1806 | auto I = Classes.find(Name); | ||||||
1807 | return I == Classes.end() ? nullptr : I->second.get(); | ||||||
1808 | } | ||||||
1809 | |||||||
1810 | /// Get the concrete record with the specified name. | ||||||
1811 | Record *getDef(StringRef Name) const { | ||||||
1812 | auto I = Defs.find(Name); | ||||||
1813 | return I == Defs.end() ? nullptr : I->second.get(); | ||||||
1814 | } | ||||||
1815 | |||||||
1816 | /// Get the \p Init value of the specified global variable. | ||||||
1817 | Init *getGlobal(StringRef Name) const { | ||||||
1818 | if (Record *R = getDef(Name)) | ||||||
1819 | return R->getDefInit(); | ||||||
1820 | auto It = ExtraGlobals.find(Name); | ||||||
1821 | return It == ExtraGlobals.end() ? nullptr : It->second; | ||||||
1822 | } | ||||||
1823 | |||||||
1824 | void saveInputFilename(std::string Filename) { | ||||||
1825 | InputFilename = Filename; | ||||||
1826 | } | ||||||
1827 | |||||||
1828 | void addClass(std::unique_ptr<Record> R) { | ||||||
1829 | bool Ins = Classes.insert(std::make_pair(std::string(R->getName()), | ||||||
1830 | std::move(R))).second; | ||||||
1831 | (void)Ins; | ||||||
1832 | assert(Ins && "Class already exists")(static_cast <bool> (Ins && "Class already exists" ) ? void (0) : __assert_fail ("Ins && \"Class already exists\"" , "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/llvm/include/llvm/TableGen/Record.h" , 1832, __extension__ __PRETTY_FUNCTION__)); | ||||||
1833 | } | ||||||
1834 | |||||||
1835 | void addDef(std::unique_ptr<Record> R) { | ||||||
1836 | bool Ins = Defs.insert(std::make_pair(std::string(R->getName()), | ||||||
1837 | std::move(R))).second; | ||||||
1838 | (void)Ins; | ||||||
1839 | assert(Ins && "Record already exists")(static_cast <bool> (Ins && "Record already exists" ) ? void (0) : __assert_fail ("Ins && \"Record already exists\"" , "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/llvm/include/llvm/TableGen/Record.h" , 1839, __extension__ __PRETTY_FUNCTION__)); | ||||||
1840 | } | ||||||
1841 | |||||||
1842 | void addExtraGlobal(StringRef Name, Init *I) { | ||||||
1843 | bool Ins = ExtraGlobals.insert(std::make_pair(std::string(Name), I)).second; | ||||||
1844 | (void)Ins; | ||||||
1845 | assert(!getDef(Name))(static_cast <bool> (!getDef(Name)) ? void (0) : __assert_fail ("!getDef(Name)", "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/llvm/include/llvm/TableGen/Record.h" , 1845, __extension__ __PRETTY_FUNCTION__)); | ||||||
1846 | assert(Ins && "Global already exists")(static_cast <bool> (Ins && "Global already exists" ) ? void (0) : __assert_fail ("Ins && \"Global already exists\"" , "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/llvm/include/llvm/TableGen/Record.h" , 1846, __extension__ __PRETTY_FUNCTION__)); | ||||||
1847 | } | ||||||
1848 | |||||||
1849 | Init *getNewAnonymousName(); | ||||||
1850 | |||||||
1851 | /// Start phase timing; called if the --time-phases option is specified. | ||||||
1852 | void startPhaseTiming() { | ||||||
1853 | TimingGroup = new TimerGroup("TableGen", "TableGen Phase Timing"); | ||||||
1854 | } | ||||||
1855 | |||||||
1856 | /// Start timing a phase. Automatically stops any previous phase timer. | ||||||
1857 | void startTimer(StringRef Name); | ||||||
1858 | |||||||
1859 | /// Stop timing a phase. | ||||||
1860 | void stopTimer(); | ||||||
1861 | |||||||
1862 | /// Start timing the overall backend. If the backend itself starts a timer, | ||||||
1863 | /// then this timer is cleared. | ||||||
1864 | void startBackendTimer(StringRef Name); | ||||||
1865 | |||||||
1866 | /// Stop timing the overall backend. | ||||||
1867 | void stopBackendTimer(); | ||||||
1868 | |||||||
1869 | /// Stop phase timing and print the report. | ||||||
1870 | void stopPhaseTiming() { | ||||||
1871 | if (TimingGroup) | ||||||
1872 | delete TimingGroup; | ||||||
1873 | } | ||||||
1874 | |||||||
1875 | //===--------------------------------------------------------------------===// | ||||||
1876 | // High-level helper methods, useful for tablegen backends. | ||||||
1877 | |||||||
1878 | /// Get all the concrete records that inherit from the one specified | ||||||
1879 | /// class. The class must be defined. | ||||||
1880 | std::vector<Record *> getAllDerivedDefinitions(StringRef ClassName) const; | ||||||
1881 | |||||||
1882 | /// Get all the concrete records that inherit from all the specified | ||||||
1883 | /// classes. The classes must be defined. | ||||||
1884 | std::vector<Record *> getAllDerivedDefinitions( | ||||||
1885 | ArrayRef<StringRef> ClassNames) const; | ||||||
1886 | |||||||
1887 | void dump() const; | ||||||
1888 | }; | ||||||
1889 | |||||||
1890 | /// Sorting predicate to sort record pointers by name. | ||||||
1891 | struct LessRecord { | ||||||
1892 | bool operator()(const Record *Rec1, const Record *Rec2) const { | ||||||
1893 | return StringRef(Rec1->getName()).compare_numeric(Rec2->getName()) < 0; | ||||||
1894 | } | ||||||
1895 | }; | ||||||
1896 | |||||||
1897 | /// Sorting predicate to sort record pointers by their | ||||||
1898 | /// unique ID. If you just need a deterministic order, use this, since it | ||||||
1899 | /// just compares two `unsigned`; the other sorting predicates require | ||||||
1900 | /// string manipulation. | ||||||
1901 | struct LessRecordByID { | ||||||
1902 | bool operator()(const Record *LHS, const Record *RHS) const { | ||||||
1903 | return LHS->getID() < RHS->getID(); | ||||||
1904 | } | ||||||
1905 | }; | ||||||
1906 | |||||||
1907 | /// Sorting predicate to sort record pointers by their | ||||||
1908 | /// name field. | ||||||
1909 | struct LessRecordFieldName { | ||||||
1910 | bool operator()(const Record *Rec1, const Record *Rec2) const { | ||||||
1911 | return Rec1->getValueAsString("Name") < Rec2->getValueAsString("Name"); | ||||||
1912 | } | ||||||
1913 | }; | ||||||
1914 | |||||||
1915 | struct LessRecordRegister { | ||||||
1916 | struct RecordParts { | ||||||
1917 | SmallVector<std::pair< bool, StringRef>, 4> Parts; | ||||||
1918 | |||||||
1919 | RecordParts(StringRef Rec) { | ||||||
1920 | if (Rec.empty()) | ||||||
1921 | return; | ||||||
1922 | |||||||
1923 | size_t Len = 0; | ||||||
1924 | const char *Start = Rec.data(); | ||||||
1925 | const char *Curr = Start; | ||||||
1926 | bool IsDigitPart = isDigit(Curr[0]); | ||||||
1927 | for (size_t I = 0, E = Rec.size(); I != E; ++I, ++Len) { | ||||||
1928 | bool IsDigit = isDigit(Curr[I]); | ||||||
1929 | if (IsDigit != IsDigitPart) { | ||||||
1930 | Parts.push_back(std::make_pair(IsDigitPart, StringRef(Start, Len))); | ||||||
1931 | Len = 0; | ||||||
1932 | Start = &Curr[I]; | ||||||
1933 | IsDigitPart = isDigit(Curr[I]); | ||||||
1934 | } | ||||||
1935 | } | ||||||
1936 | // Push the last part. | ||||||
1937 | Parts.push_back(std::make_pair(IsDigitPart, StringRef(Start, Len))); | ||||||
1938 | } | ||||||
1939 | |||||||
1940 | size_t size() { return Parts.size(); } | ||||||
1941 | |||||||
1942 | std::pair<bool, StringRef> getPart(size_t i) { | ||||||
1943 | assert (i < Parts.size() && "Invalid idx!")(static_cast <bool> (i < Parts.size() && "Invalid idx!" ) ? void (0) : __assert_fail ("i < Parts.size() && \"Invalid idx!\"" , "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/llvm/include/llvm/TableGen/Record.h" , 1943, __extension__ __PRETTY_FUNCTION__)); | ||||||
1944 | return Parts[i]; | ||||||
1945 | } | ||||||
1946 | }; | ||||||
1947 | |||||||
1948 | bool operator()(const Record *Rec1, const Record *Rec2) const { | ||||||
1949 | RecordParts LHSParts(StringRef(Rec1->getName())); | ||||||
1950 | RecordParts RHSParts(StringRef(Rec2->getName())); | ||||||
1951 | |||||||
1952 | size_t LHSNumParts = LHSParts.size(); | ||||||
1953 | size_t RHSNumParts = RHSParts.size(); | ||||||
1954 | assert (LHSNumParts && RHSNumParts && "Expected at least one part!")(static_cast <bool> (LHSNumParts && RHSNumParts && "Expected at least one part!") ? void (0) : __assert_fail ("LHSNumParts && RHSNumParts && \"Expected at least one part!\"" , "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/llvm/include/llvm/TableGen/Record.h" , 1954, __extension__ __PRETTY_FUNCTION__)); | ||||||
1955 | |||||||
1956 | if (LHSNumParts != RHSNumParts) | ||||||
1957 | return LHSNumParts < RHSNumParts; | ||||||
1958 | |||||||
1959 | // We expect the registers to be of the form [_a-zA-Z]+([0-9]*[_a-zA-Z]*)*. | ||||||
1960 | for (size_t I = 0, E = LHSNumParts; I < E; I+=2) { | ||||||
1961 | std::pair<bool, StringRef> LHSPart = LHSParts.getPart(I); | ||||||
1962 | std::pair<bool, StringRef> RHSPart = RHSParts.getPart(I); | ||||||
1963 | // Expect even part to always be alpha. | ||||||
1964 | assert (LHSPart.first == false && RHSPart.first == false &&(static_cast <bool> (LHSPart.first == false && RHSPart .first == false && "Expected both parts to be alpha." ) ? void (0) : __assert_fail ("LHSPart.first == false && RHSPart.first == false && \"Expected both parts to be alpha.\"" , "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/llvm/include/llvm/TableGen/Record.h" , 1965, __extension__ __PRETTY_FUNCTION__)) | ||||||
1965 | "Expected both parts to be alpha.")(static_cast <bool> (LHSPart.first == false && RHSPart .first == false && "Expected both parts to be alpha." ) ? void (0) : __assert_fail ("LHSPart.first == false && RHSPart.first == false && \"Expected both parts to be alpha.\"" , "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/llvm/include/llvm/TableGen/Record.h" , 1965, __extension__ __PRETTY_FUNCTION__)); | ||||||
1966 | if (int Res = LHSPart.second.compare(RHSPart.second)) | ||||||
1967 | return Res < 0; | ||||||
1968 | } | ||||||
1969 | for (size_t I = 1, E = LHSNumParts; I < E; I+=2) { | ||||||
1970 | std::pair<bool, StringRef> LHSPart = LHSParts.getPart(I); | ||||||
1971 | std::pair<bool, StringRef> RHSPart = RHSParts.getPart(I); | ||||||
1972 | // Expect odd part to always be numeric. | ||||||
1973 | assert (LHSPart.first == true && RHSPart.first == true &&(static_cast <bool> (LHSPart.first == true && RHSPart .first == true && "Expected both parts to be numeric." ) ? void (0) : __assert_fail ("LHSPart.first == true && RHSPart.first == true && \"Expected both parts to be numeric.\"" , "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/llvm/include/llvm/TableGen/Record.h" , 1974, __extension__ __PRETTY_FUNCTION__)) | ||||||
1974 | "Expected both parts to be numeric.")(static_cast <bool> (LHSPart.first == true && RHSPart .first == true && "Expected both parts to be numeric." ) ? void (0) : __assert_fail ("LHSPart.first == true && RHSPart.first == true && \"Expected both parts to be numeric.\"" , "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/llvm/include/llvm/TableGen/Record.h" , 1974, __extension__ __PRETTY_FUNCTION__)); | ||||||
1975 | if (LHSPart.second.size() != RHSPart.second.size()) | ||||||
1976 | return LHSPart.second.size() < RHSPart.second.size(); | ||||||
1977 | |||||||
1978 | unsigned LHSVal, RHSVal; | ||||||
1979 | |||||||
1980 | bool LHSFailed = LHSPart.second.getAsInteger(10, LHSVal); (void)LHSFailed; | ||||||
1981 | assert(!LHSFailed && "Unable to convert LHS to integer.")(static_cast <bool> (!LHSFailed && "Unable to convert LHS to integer." ) ? void (0) : __assert_fail ("!LHSFailed && \"Unable to convert LHS to integer.\"" , "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/llvm/include/llvm/TableGen/Record.h" , 1981, __extension__ __PRETTY_FUNCTION__)); | ||||||
1982 | bool RHSFailed = RHSPart.second.getAsInteger(10, RHSVal); (void)RHSFailed; | ||||||
1983 | assert(!RHSFailed && "Unable to convert RHS to integer.")(static_cast <bool> (!RHSFailed && "Unable to convert RHS to integer." ) ? void (0) : __assert_fail ("!RHSFailed && \"Unable to convert RHS to integer.\"" , "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/llvm/include/llvm/TableGen/Record.h" , 1983, __extension__ __PRETTY_FUNCTION__)); | ||||||
1984 | |||||||
1985 | if (LHSVal != RHSVal) | ||||||
1986 | return LHSVal < RHSVal; | ||||||
1987 | } | ||||||
1988 | return LHSNumParts < RHSNumParts; | ||||||
1989 | } | ||||||
1990 | }; | ||||||
1991 | |||||||
1992 | raw_ostream &operator<<(raw_ostream &OS, const RecordKeeper &RK); | ||||||
1993 | |||||||
1994 | //===----------------------------------------------------------------------===// | ||||||
1995 | // Resolvers | ||||||
1996 | //===----------------------------------------------------------------------===// | ||||||
1997 | |||||||
1998 | /// Interface for looking up the initializer for a variable name, used by | ||||||
1999 | /// Init::resolveReferences. | ||||||
2000 | class Resolver { | ||||||
2001 | Record *CurRec; | ||||||
2002 | bool IsFinal = false; | ||||||
2003 | |||||||
2004 | public: | ||||||
2005 | explicit Resolver(Record *CurRec) : CurRec(CurRec) {} | ||||||
2006 | virtual ~Resolver() {} | ||||||
2007 | |||||||
2008 | Record *getCurrentRecord() const { return CurRec; } | ||||||
2009 | |||||||
2010 | /// Return the initializer for the given variable name (should normally be a | ||||||
2011 | /// StringInit), or nullptr if the name could not be resolved. | ||||||
2012 | virtual Init *resolve(Init *VarName) = 0; | ||||||
2013 | |||||||
2014 | // Whether bits in a BitsInit should stay unresolved if resolving them would | ||||||
2015 | // result in a ? (UnsetInit). This behavior is used to represent instruction | ||||||
2016 | // encodings by keeping references to unset variables within a record. | ||||||
2017 | virtual bool keepUnsetBits() const { return false; } | ||||||
2018 | |||||||
2019 | // Whether this is the final resolve step before adding a record to the | ||||||
2020 | // RecordKeeper. Error reporting during resolve and related constant folding | ||||||
2021 | // should only happen when this is true. | ||||||
2022 | bool isFinal() const { return IsFinal; } | ||||||
2023 | |||||||
2024 | void setFinal(bool Final) { IsFinal = Final; } | ||||||
2025 | }; | ||||||
2026 | |||||||
2027 | /// Resolve arbitrary mappings. | ||||||
2028 | class MapResolver final : public Resolver { | ||||||
2029 | struct MappedValue { | ||||||
2030 | Init *V; | ||||||
2031 | bool Resolved; | ||||||
2032 | |||||||
2033 | MappedValue() : V(nullptr), Resolved(false) {} | ||||||
2034 | MappedValue(Init *V, bool Resolved) : V(V), Resolved(Resolved) {} | ||||||
2035 | }; | ||||||
2036 | |||||||
2037 | DenseMap<Init *, MappedValue> Map; | ||||||
2038 | |||||||
2039 | public: | ||||||
2040 | explicit MapResolver(Record *CurRec = nullptr) : Resolver(CurRec) {} | ||||||
2041 | |||||||
2042 | void set(Init *Key, Init *Value) { Map[Key] = {Value, false}; } | ||||||
2043 | |||||||
2044 | bool isComplete(Init *VarName) const { | ||||||
2045 | auto It = Map.find(VarName); | ||||||
2046 | assert(It != Map.end() && "key must be present in map")(static_cast <bool> (It != Map.end() && "key must be present in map" ) ? void (0) : __assert_fail ("It != Map.end() && \"key must be present in map\"" , "/build/llvm-toolchain-snapshot-14~++20210828111110+16086d47c0d0/llvm/include/llvm/TableGen/Record.h" , 2046, __extension__ __PRETTY_FUNCTION__)); | ||||||
2047 | return It->second.V->isComplete(); | ||||||
2048 | } | ||||||
2049 | |||||||
2050 | Init *resolve(Init *VarName) override; | ||||||
2051 | }; | ||||||
2052 | |||||||
2053 | /// Resolve all variables from a record except for unset variables. | ||||||
2054 | class RecordResolver final : public Resolver { | ||||||
2055 | DenseMap<Init *, Init *> Cache; | ||||||
2056 | SmallVector<Init *, 4> Stack; | ||||||
2057 | Init *Name = nullptr; | ||||||
2058 | |||||||
2059 | public: | ||||||
2060 | explicit RecordResolver(Record &R) : Resolver(&R) {} | ||||||
2061 | |||||||
2062 | void setName(Init *NewName) { Name = NewName; } | ||||||
2063 | |||||||
2064 | Init *resolve(Init *VarName) override; | ||||||
2065 | |||||||
2066 | bool keepUnsetBits() const override { return true; } | ||||||
2067 | }; | ||||||
2068 | |||||||
2069 | /// Delegate resolving to a sub-resolver, but shadow some variable names. | ||||||
2070 | class ShadowResolver final : public Resolver { | ||||||
2071 | Resolver &R; | ||||||
2072 | DenseSet<Init *> Shadowed; | ||||||
2073 | |||||||
2074 | public: | ||||||
2075 | explicit ShadowResolver(Resolver &R) | ||||||
2076 | : Resolver(R.getCurrentRecord()), R(R) { | ||||||
2077 | setFinal(R.isFinal()); | ||||||
2078 | } | ||||||
2079 | |||||||
2080 | void addShadow(Init *Key) { Shadowed.insert(Key); } | ||||||
2081 | |||||||
2082 | Init *resolve(Init *VarName) override { | ||||||
2083 | if (Shadowed.count(VarName)) | ||||||
2084 | return nullptr; | ||||||
2085 | return R.resolve(VarName); | ||||||
2086 | } | ||||||
2087 | }; | ||||||
2088 | |||||||
2089 | /// (Optionally) delegate resolving to a sub-resolver, and keep track whether | ||||||
2090 | /// there were unresolved references. | ||||||
2091 | class TrackUnresolvedResolver final : public Resolver { | ||||||
2092 | Resolver *R; | ||||||
2093 | bool FoundUnresolved = false; | ||||||
2094 | |||||||
2095 | public: | ||||||
2096 | explicit TrackUnresolvedResolver(Resolver *R = nullptr) | ||||||
2097 | : Resolver(R ? R->getCurrentRecord() : nullptr), R(R) {} | ||||||
2098 | |||||||
2099 | bool foundUnresolved() const { return FoundUnresolved; } | ||||||
2100 | |||||||
2101 | Init *resolve(Init *VarName) override; | ||||||
2102 | }; | ||||||
2103 | |||||||
2104 | /// Do not resolve anything, but keep track of whether a given variable was | ||||||
2105 | /// referenced. | ||||||
2106 | class HasReferenceResolver final : public Resolver { | ||||||
2107 | Init *VarNameToTrack; | ||||||
2108 | bool Found = false; | ||||||
2109 | |||||||
2110 | public: | ||||||
2111 | explicit HasReferenceResolver(Init *VarNameToTrack) | ||||||
2112 | : Resolver(nullptr), VarNameToTrack(VarNameToTrack) {} | ||||||
2113 | |||||||
2114 | bool found() const { return Found; } | ||||||
2115 | |||||||
2116 | Init *resolve(Init *VarName) override; | ||||||
2117 | }; | ||||||
2118 | |||||||
2119 | void EmitDetailedRecords(RecordKeeper &RK, raw_ostream &OS); | ||||||
2120 | void EmitJSON(RecordKeeper &RK, raw_ostream &OS); | ||||||
2121 | |||||||
2122 | } // end namespace llvm | ||||||
2123 | |||||||
2124 | #endif // LLVM_TABLEGEN_RECORD_H |