Line data Source code
1 : #ifdef __PLUMED_HAS_ASMJIT
2 : // This file is part of AsmJit project <https://asmjit.com>
3 : //
4 : // See asmjit.h or LICENSE.md for license and copyright information
5 : // SPDX-License-Identifier: Zlib
6 :
7 : #ifndef ASMJIT_CORE_BUILDER_H_INCLUDED
8 : #define ASMJIT_CORE_BUILDER_H_INCLUDED
9 :
10 : #include "api-config.h"
11 : #ifndef ASMJIT_NO_BUILDER
12 :
13 : #include "assembler.h"
14 : #include "codeholder.h"
15 : #include "constpool.h"
16 : #include "formatter.h"
17 : #include "inst.h"
18 : #include "operand.h"
19 : #include "moved_string.h"
20 : #include "support.h"
21 : #include "type.h"
22 : #include "zone.h"
23 : #include "zonevector.h"
24 :
25 : ASMJIT_BEGIN_NAMESPACE
26 :
27 : //! \addtogroup asmjit_builder
28 : //! \{
29 :
30 : class BaseBuilder;
31 : class Pass;
32 :
33 : class BaseNode;
34 : class InstNode;
35 : class SectionNode;
36 : class LabelNode;
37 : class AlignNode;
38 : class EmbedDataNode;
39 : class EmbedLabelNode;
40 : class ConstPoolNode;
41 : class CommentNode;
42 : class SentinelNode;
43 : class LabelDeltaNode;
44 :
45 : //! Type of node used by \ref BaseBuilder and \ref BaseCompiler.
46 : enum class NodeType : uint8_t {
47 : //! Invalid node (internal, don't use).
48 : kNone = 0,
49 :
50 : // [BaseBuilder]
51 :
52 : //! Node is \ref InstNode or \ref InstExNode.
53 : kInst = 1,
54 : //! Node is \ref SectionNode.
55 : kSection = 2,
56 : //! Node is \ref LabelNode.
57 : kLabel = 3,
58 : //! Node is \ref AlignNode.
59 : kAlign = 4,
60 : //! Node is \ref EmbedDataNode.
61 : kEmbedData = 5,
62 : //! Node is \ref EmbedLabelNode.
63 : kEmbedLabel = 6,
64 : //! Node is \ref EmbedLabelDeltaNode.
65 : kEmbedLabelDelta = 7,
66 : //! Node is \ref ConstPoolNode.
67 : kConstPool = 8,
68 : //! Node is \ref CommentNode.
69 : kComment = 9,
70 : //! Node is \ref SentinelNode.
71 : kSentinel = 10,
72 :
73 : // [BaseCompiler]
74 :
75 : //! Node is \ref JumpNode (acts as InstNode).
76 : kJump = 15,
77 : //! Node is \ref FuncNode (acts as LabelNode).
78 : kFunc = 16,
79 : //! Node is \ref FuncRetNode (acts as InstNode).
80 : kFuncRet = 17,
81 : //! Node is \ref InvokeNode (acts as InstNode).
82 : kInvoke = 18,
83 :
84 : // [UserDefined]
85 :
86 : //! First id of a user-defined node.
87 : kUser = 32
88 : };
89 :
90 : //! Node flags, specify what the node is and/or does.
91 : enum class NodeFlags : uint8_t {
92 : //! No flags.
93 : kNone = 0,
94 : //! Node is code that can be executed (instruction, label, align, etc...).
95 : kIsCode = 0x01u,
96 : //! Node is data that cannot be executed (data, const-pool, etc...).
97 : kIsData = 0x02u,
98 : //! Node is informative, can be removed and ignored.
99 : kIsInformative = 0x04u,
100 : //! Node can be safely removed if unreachable.
101 : kIsRemovable = 0x08u,
102 : //! Node does nothing when executed (label, align, explicit nop).
103 : kHasNoEffect = 0x10u,
104 : //! Node is an instruction or acts as it.
105 : kActsAsInst = 0x20u,
106 : //! Node is a label or acts as it.
107 : kActsAsLabel = 0x40u,
108 : //! Node is active (part of the code).
109 : kIsActive = 0x80u
110 : };
111 1068577 : ASMJIT_DEFINE_ENUM_FLAGS(NodeFlags)
112 :
113 : //! Type of the sentinel (purery informative purpose).
114 : enum class SentinelType : uint8_t {
115 : //! Type of the sentinel is not known.
116 : kUnknown = 0u,
117 : //! This is a sentinel used at the end of \ref FuncNode.
118 : kFuncEnd = 1u
119 : };
120 :
121 : //! Builder interface.
122 : //!
123 : //! `BaseBuilder` interface was designed to be used as a \ref BaseAssembler replacement in case pre-processing or
124 : //! post-processing of the generated code is required. The code can be modified during or after code generation.
125 : //! Pre processing or post processing can be done manually or through a \ref Pass object. \ref BaseBuilder stores
126 : //! the emitted code as a double-linked list of nodes, which allows O(1) insertion and removal during processing.
127 : //!
128 : //! Check out architecture specific builders for more details and examples:
129 : //!
130 : //! - \ref x86::Builder - X86/X64 builder implementation.
131 : class ASMJIT_VIRTAPI BaseBuilder : public BaseEmitter {
132 : public:
133 : ASMJIT_NONCOPYABLE(BaseBuilder)
134 : typedef BaseEmitter Base;
135 :
136 : //! \name Members
137 : //! \{
138 :
139 : //! Base zone used to allocate nodes and passes.
140 : Zone _codeZone;
141 : //! Data zone used to allocate data and names.
142 : Zone _dataZone;
143 : //! Pass zone, passed to `Pass::run()`.
144 : Zone _passZone;
145 : //! Allocator that uses `_codeZone`.
146 : ZoneAllocator _allocator;
147 :
148 : //! Array of `Pass` objects.
149 : ZoneVector<Pass*> _passes {};
150 : //! Maps section indexes to `LabelNode` nodes.
151 : ZoneVector<SectionNode*> _sectionNodes {};
152 : //! Maps label indexes to `LabelNode` nodes.
153 : ZoneVector<LabelNode*> _labelNodes {};
154 :
155 : //! Current node (cursor).
156 : BaseNode* _cursor = nullptr;
157 : //! First node of the current section.
158 : BaseNode* _firstNode = nullptr;
159 : //! Last node of the current section.
160 : BaseNode* _lastNode = nullptr;
161 :
162 : //! Flags assigned to each new node.
163 : NodeFlags _nodeFlags = NodeFlags::kNone;
164 : //! The sections links are dirty (used internally).
165 : bool _dirtySectionLinks = false;
166 :
167 : //! \}
168 :
169 : //! \name Construction & Destruction
170 : //! \{
171 :
172 : //! Creates a new `BaseBuilder` instance.
173 : ASMJIT_API BaseBuilder() noexcept;
174 : //! Destroys the `BaseBuilder` instance.
175 : ASMJIT_API virtual ~BaseBuilder() noexcept;
176 :
177 : //! \}
178 :
179 : //! \name Node Management
180 : //! \{
181 :
182 : //! Returns the first node.
183 27492 : inline BaseNode* firstNode() const noexcept { return _firstNode; }
184 : //! Returns the last node.
185 0 : inline BaseNode* lastNode() const noexcept { return _lastNode; }
186 :
187 : //! Allocates and instantiates a new node of type `T` and returns its instance. If the allocation fails `nullptr`
188 : //! is returned.
189 : //!
190 : //! The template argument `T` must be a type that is extends \ref BaseNode.
191 : //!
192 : //! \remarks The pointer returned (if non-null) is owned by the Builder or Compiler. When the Builder/Compiler
193 : //! is destroyed it destroys all nodes it created so no manual memory management is required.
194 : template<typename T, typename... Args>
195 147099 : inline Error _newNodeT(T** ASMJIT_NONNULL(out), Args&&... args) {
196 147099 : *out = _allocator.newT<T>(this, std::forward<Args>(args)...);
197 147099 : if (ASMJIT_UNLIKELY(!*out))
198 0 : return reportError(DebugUtils::errored(kErrorOutOfMemory));
199 : return kErrorOk;
200 : }
201 :
202 : //! Creates a new \ref InstNode.
203 : ASMJIT_API Error newInstNode(InstNode** ASMJIT_NONNULL(out), InstId instId, InstOptions instOptions, uint32_t opCount);
204 : //! Creates a new \ref LabelNode.
205 : ASMJIT_API Error newLabelNode(LabelNode** ASMJIT_NONNULL(out));
206 : //! Creates a new \ref AlignNode.
207 : ASMJIT_API Error newAlignNode(AlignNode** ASMJIT_NONNULL(out), AlignMode alignMode, uint32_t alignment);
208 : //! Creates a new \ref EmbedDataNode.
209 : ASMJIT_API Error newEmbedDataNode(EmbedDataNode** ASMJIT_NONNULL(out), TypeId typeId, const void* data, size_t itemCount, size_t repeatCount = 1);
210 : //! Creates a new \ref ConstPoolNode.
211 : ASMJIT_API Error newConstPoolNode(ConstPoolNode** ASMJIT_NONNULL(out));
212 : //! Creates a new \ref CommentNode.
213 : ASMJIT_API Error newCommentNode(CommentNode** ASMJIT_NONNULL(out), const char* data, size_t size);
214 :
215 : //! Adds `node` after the current and sets the current node to the given `node`.
216 : ASMJIT_API BaseNode* addNode(BaseNode* ASMJIT_NONNULL(node)) noexcept;
217 : //! Inserts the given `node` after `ref`.
218 : ASMJIT_API BaseNode* addAfter(BaseNode* ASMJIT_NONNULL(node), BaseNode* ASMJIT_NONNULL(ref)) noexcept;
219 : //! Inserts the given `node` before `ref`.
220 : ASMJIT_API BaseNode* addBefore(BaseNode* ASMJIT_NONNULL(node), BaseNode* ASMJIT_NONNULL(ref)) noexcept;
221 : //! Removes the given `node`.
222 : ASMJIT_API BaseNode* removeNode(BaseNode* ASMJIT_NONNULL(node)) noexcept;
223 : //! Removes multiple nodes.
224 : ASMJIT_API void removeNodes(BaseNode* first, BaseNode* last) noexcept;
225 :
226 : //! Returns the cursor.
227 : //!
228 : //! When the Builder/Compiler is created it automatically creates a '.text' \ref SectionNode, which will be the
229 : //! initial one. When instructions are added they are always added after the cursor and the cursor is changed
230 : //! to be that newly added node. Use `setCursor()` to change where new nodes are inserted.
231 27492 : inline BaseNode* cursor() const noexcept { return _cursor; }
232 :
233 : //! Sets the current node to `node` and return the previous one.
234 : ASMJIT_API BaseNode* setCursor(BaseNode* node) noexcept;
235 :
236 : //! Sets the current node without returning the previous node.
237 : //!
238 : //! Only use this function if you are concerned about performance and want this inlined (for example if you set
239 : //! the cursor in a loop, etc...).
240 119607 : inline void _setCursor(BaseNode* node) noexcept { _cursor = node; }
241 :
242 : //! \}
243 :
244 : //! \name Section Management
245 : //! \{
246 :
247 : //! Returns a vector of SectionNode objects.
248 : //!
249 : //! \note If a section of some id is not associated with the Builder/Compiler it would be null, so always check
250 : //! for nulls if you iterate over the vector.
251 : inline const ZoneVector<SectionNode*>& sectionNodes() const noexcept {
252 : return _sectionNodes;
253 : }
254 :
255 : //! Tests whether the `SectionNode` of the given `sectionId` was registered.
256 : inline bool hasRegisteredSectionNode(uint32_t sectionId) const noexcept {
257 : return sectionId < _sectionNodes.size() && _sectionNodes[sectionId] != nullptr;
258 : }
259 :
260 : //! Returns or creates a `SectionNode` that matches the given `sectionId`.
261 : //!
262 : //! \remarks This function will either get the existing `SectionNode` or create it in case it wasn't created before.
263 : //! You can check whether a section has a registered `SectionNode` by using `BaseBuilder::hasRegisteredSectionNode()`.
264 : ASMJIT_API Error sectionNodeOf(SectionNode** ASMJIT_NONNULL(out), uint32_t sectionId);
265 :
266 : ASMJIT_API Error section(Section* ASMJIT_NONNULL(section)) override;
267 :
268 : //! Returns whether the section links of active section nodes are dirty. You can update these links by calling
269 : //! `updateSectionLinks()` in such case.
270 0 : inline bool hasDirtySectionLinks() const noexcept { return _dirtySectionLinks; }
271 :
272 : //! Updates links of all active section nodes.
273 : ASMJIT_API void updateSectionLinks() noexcept;
274 :
275 : //! \}
276 :
277 : //! \name Label Management
278 : //! \{
279 :
280 : //! Returns a vector of \ref LabelNode nodes.
281 : //!
282 : //! \note If a label of some id is not associated with the Builder/Compiler it would be null, so always check for
283 : //! nulls if you iterate over the vector.
284 : inline const ZoneVector<LabelNode*>& labelNodes() const noexcept { return _labelNodes; }
285 :
286 : //! Tests whether the `LabelNode` of the given `labelId` was registered.
287 : inline bool hasRegisteredLabelNode(uint32_t labelId) const noexcept {
288 : return labelId < _labelNodes.size() && _labelNodes[labelId] != nullptr;
289 : }
290 :
291 : //! \overload
292 : inline bool hasRegisteredLabelNode(const Label& label) const noexcept {
293 : return hasRegisteredLabelNode(label.id());
294 : }
295 :
296 : //! Gets or creates a \ref LabelNode that matches the given `labelId`.
297 : //!
298 : //! \remarks This function will either get the existing `LabelNode` or create it in case it wasn't created before.
299 : //! You can check whether a label has a registered `LabelNode` by calling \ref BaseBuilder::hasRegisteredLabelNode().
300 : ASMJIT_API Error labelNodeOf(LabelNode** ASMJIT_NONNULL(out), uint32_t labelId);
301 :
302 : //! \overload
303 : inline Error labelNodeOf(LabelNode** ASMJIT_NONNULL(out), const Label& label) {
304 0 : return labelNodeOf(out, label.id());
305 : }
306 :
307 : //! Registers this \ref LabelNode (internal).
308 : //!
309 : //! This function is used internally to register a newly created `LabelNode` with this instance of Builder/Compiler.
310 : //! Use \ref labelNodeOf() functions to get back \ref LabelNode from a label or its identifier.
311 : ASMJIT_API Error registerLabelNode(LabelNode* ASMJIT_NONNULL(node));
312 :
313 : ASMJIT_API Label newLabel() override;
314 : ASMJIT_API Label newNamedLabel(const char* name, size_t nameSize = SIZE_MAX, LabelType type = LabelType::kGlobal, uint32_t parentId = Globals::kInvalidId) override;
315 : ASMJIT_API Error bind(const Label& label) override;
316 :
317 : //! \}
318 :
319 : //! \name Passes
320 : //! \{
321 :
322 : //! Returns a vector of `Pass` instances that will be executed by `runPasses()`.
323 : inline const ZoneVector<Pass*>& passes() const noexcept { return _passes; }
324 :
325 : //! Allocates and instantiates a new pass of type `T` and returns its instance. If the allocation fails `nullptr` is
326 : //! returned.
327 : //!
328 : //! The template argument `T` must be a type that is extends \ref Pass.
329 : //!
330 : //! \remarks The pointer returned (if non-null) is owned by the Builder or Compiler. When the Builder/Compiler is
331 : //! destroyed it destroys all passes it created so no manual memory management is required.
332 : template<typename T>
333 54984 : inline T* newPassT() noexcept { return _codeZone.newT<T>(); }
334 :
335 : //! \overload
336 : template<typename T, typename... Args>
337 : inline T* newPassT(Args&&... args) noexcept { return _codeZone.newT<T>(std::forward<Args>(args)...); }
338 :
339 : template<typename T>
340 54984 : inline Error addPassT() { return addPass(newPassT<T>()); }
341 :
342 : template<typename T, typename... Args>
343 : inline Error addPassT(Args&&... args) { return addPass(newPassT<T, Args...>(std::forward<Args>(args)...)); }
344 :
345 : //! Returns `Pass` by name.
346 : //!
347 : //! If the pass having the given `name` doesn't exist `nullptr` is returned.
348 : ASMJIT_API Pass* passByName(const char* name) const noexcept;
349 : //! Adds `pass` to the list of passes.
350 : ASMJIT_API Error addPass(Pass* pass) noexcept;
351 : //! Removes `pass` from the list of passes and delete it.
352 : ASMJIT_API Error deletePass(Pass* pass) noexcept;
353 :
354 : //! Runs all passes in order.
355 : ASMJIT_API Error runPasses();
356 :
357 : //! \}
358 :
359 : //! \name Emit
360 : //! \{
361 :
362 : ASMJIT_API Error _emit(InstId instId, const Operand_& o0, const Operand_& o1, const Operand_& o2, const Operand_* opExt) override;
363 :
364 : //! \}
365 :
366 : //! \name Align
367 : //! \{
368 :
369 : ASMJIT_API Error align(AlignMode alignMode, uint32_t alignment) override;
370 :
371 : //! \}
372 :
373 : //! \name Embed
374 : //! \{
375 :
376 : ASMJIT_API Error embed(const void* data, size_t dataSize) override;
377 : ASMJIT_API Error embedDataArray(TypeId typeId, const void* data, size_t count, size_t repeat = 1) override;
378 : ASMJIT_API Error embedConstPool(const Label& label, const ConstPool& pool) override;
379 :
380 : ASMJIT_API Error embedLabel(const Label& label, size_t dataSize = 0) override;
381 : ASMJIT_API Error embedLabelDelta(const Label& label, const Label& base, size_t dataSize = 0) override;
382 :
383 : //! \}
384 :
385 : //! \name Comment
386 : //! \{
387 :
388 : ASMJIT_API Error comment(const char* data, size_t size = SIZE_MAX) override;
389 :
390 : //! \}
391 :
392 : //! \name Serialization
393 : //! \{
394 :
395 : //! Serializes everything the given emitter `dst`.
396 : //!
397 : //! Although not explicitly required the emitter will most probably be of Assembler type. The reason is that
398 : //! there is no known use of serializing nodes held by Builder/Compiler into another Builder-like emitter.
399 : ASMJIT_API Error serializeTo(BaseEmitter* dst);
400 :
401 : //! \}
402 :
403 : //! \name Events
404 : //! \{
405 :
406 : ASMJIT_API Error onAttach(CodeHolder* code) noexcept override;
407 : ASMJIT_API Error onDetach(CodeHolder* code) noexcept override;
408 :
409 : //! \}
410 : };
411 :
412 : //! Base node.
413 : //!
414 : //! Every node represents a building-block used by \ref BaseBuilder. It can be instruction, data, label, comment,
415 : //! directive, or any other high-level representation that can be transformed to the building blocks mentioned.
416 : //! Every class that inherits \ref BaseBuilder can define its own high-level nodes that can be later lowered to
417 : //! basic nodes like instructions.
418 : class BaseNode {
419 : public:
420 : ASMJIT_NONCOPYABLE(BaseNode)
421 :
422 : //! \name Members
423 : //! \{
424 :
425 : union {
426 : struct {
427 : //! Previous node.
428 : BaseNode* _prev;
429 : //! Next node.
430 : BaseNode* _next;
431 : };
432 : //! Links (an alternative view to previous and next nodes).
433 : BaseNode* _links[2];
434 : };
435 :
436 : //! Data shared between all types of nodes.
437 : struct AnyData {
438 : //! Node type.
439 : NodeType _nodeType;
440 : //! Node flags.
441 : NodeFlags _nodeFlags;
442 : //! Not used by BaseNode.
443 : uint8_t _reserved0;
444 : //! Not used by BaseNode.
445 : uint8_t _reserved1;
446 : };
447 :
448 : //! Data used by \ref AlignNode.
449 : struct AlignData {
450 : //! Node type.
451 : NodeType _nodeType;
452 : //! Node flags.
453 : NodeFlags _nodeFlags;
454 : //! Align mode.
455 : AlignMode _alignMode;
456 : //! Not used by AlignNode.
457 : uint8_t _reserved;
458 : };
459 :
460 : //! Data used by \ref InstNode.
461 : struct InstData {
462 : //! Node type.
463 : NodeType _nodeType;
464 : //! Node flags.
465 : NodeFlags _nodeFlags;
466 : //! Instruction operands count (used).
467 : uint8_t _opCount;
468 : //! Instruction operands capacity (allocated).
469 : uint8_t _opCapacity;
470 : };
471 :
472 : //! Data used by \ref EmbedDataNode.
473 : struct EmbedData {
474 : //! Node type.
475 : NodeType _nodeType;
476 : //! Node flags.
477 : NodeFlags _nodeFlags;
478 : //! Type id.
479 : TypeId _typeId;
480 : //! Size of `_typeId`.
481 : uint8_t _typeSize;
482 : };
483 :
484 : //! Data used by \ref SentinelNode.
485 : struct SentinelData {
486 : //! Node type.
487 : NodeType _nodeType;
488 : //! Node flags.
489 : NodeFlags _nodeFlags;
490 : //! Sentinel type.
491 : SentinelType _sentinelType;
492 : //! Not used by BaseNode.
493 : uint8_t _reserved1;
494 : };
495 :
496 : //! Data that can have different meaning dependning on \ref NodeType.
497 : union {
498 : //! Data useful by any node type.
499 : AnyData _any;
500 : //! Data specific to \ref AlignNode.
501 : AlignData _alignData;
502 : //! Data specific to \ref InstNode.
503 : InstData _inst;
504 : //! Data specific to \ref EmbedDataNode.
505 : EmbedData _embed;
506 : //! Data specific to \ref SentinelNode.
507 : SentinelData _sentinel;
508 : };
509 :
510 : //! Node position in code (should be unique).
511 : uint32_t _position;
512 :
513 : //! Value reserved for AsmJit users never touched by AsmJit itself.
514 : union {
515 : //! User data as 64-bit integer.
516 : uint64_t _userDataU64;
517 : //! User data as pointer.
518 : void* _userDataPtr;
519 : };
520 :
521 : //! Data used exclusively by the current `Pass`.
522 : void* _passData;
523 :
524 : //! Inline comment/annotation or nullptr if not used.
525 : const char* _inlineComment;
526 :
527 : //! \}
528 :
529 : //! \name Construction & Destruction
530 : //! \{
531 :
532 : //! Creates a new `BaseNode` - always use `BaseBuilder` to allocate nodes.
533 529469 : inline BaseNode(BaseBuilder* cb, NodeType nodeType, NodeFlags nodeFlags = NodeFlags::kNone) noexcept {
534 529469 : _prev = nullptr;
535 529469 : _next = nullptr;
536 501977 : _any._nodeType = nodeType;
537 529469 : _any._nodeFlags = nodeFlags | cb->_nodeFlags;
538 82476 : _any._reserved0 = 0;
539 109968 : _any._reserved1 = 0;
540 529469 : _position = 0;
541 529469 : _userDataU64 = 0;
542 529469 : _passData = nullptr;
543 529469 : _inlineComment = nullptr;
544 : }
545 :
546 : //! \}
547 :
548 : //! \name Accessors
549 : //! \{
550 :
551 : //! Casts this node to `T*`.
552 : template<typename T>
553 : inline T* as() noexcept { return static_cast<T*>(this); }
554 : //! Casts this node to `const T*`.
555 : template<typename T>
556 : inline const T* as() const noexcept { return static_cast<const T*>(this); }
557 :
558 : //! Returns previous node or `nullptr` if this node is either first or not
559 : //! part of Builder/Compiler node-list.
560 912469 : inline BaseNode* prev() const noexcept { return _prev; }
561 : //! Returns next node or `nullptr` if this node is either last or not part
562 : //! of Builder/Compiler node-list.
563 1647813 : inline BaseNode* next() const noexcept { return _next; }
564 :
565 : //! Returns the type of the node, see `NodeType`.
566 3550968 : inline NodeType type() const noexcept { return _any._nodeType; }
567 :
568 : //! Sets the type of the node, see `NodeType` (internal).
569 : //!
570 : //! \remarks You should never set a type of a node to anything else than the initial value. This function is only
571 : //! provided for users that use custom nodes and need to change the type either during construction or later.
572 9639 : inline void setType(NodeType type) noexcept { _any._nodeType = type; }
573 :
574 : //! Tests whether this node is either `InstNode` or extends it.
575 : inline bool isInst() const noexcept { return hasFlag(NodeFlags::kActsAsInst); }
576 : //! Tests whether this node is `SectionNode`.
577 0 : inline bool isSection() const noexcept { return type() == NodeType::kSection; }
578 : //! Tests whether this node is either `LabelNode` or extends it.
579 : inline bool isLabel() const noexcept { return hasFlag(NodeFlags::kActsAsLabel); }
580 : //! Tests whether this node is `AlignNode`.
581 : inline bool isAlign() const noexcept { return type() == NodeType::kAlign; }
582 : //! Tests whether this node is `EmbedDataNode`.
583 : inline bool isEmbedData() const noexcept { return type() == NodeType::kEmbedData; }
584 : //! Tests whether this node is `EmbedLabelNode`.
585 : inline bool isEmbedLabel() const noexcept { return type() == NodeType::kEmbedLabel; }
586 : //! Tests whether this node is `EmbedLabelDeltaNode`.
587 : inline bool isEmbedLabelDelta() const noexcept { return type() == NodeType::kEmbedLabelDelta; }
588 : //! Tests whether this node is `ConstPoolNode`.
589 : inline bool isConstPool() const noexcept { return type() == NodeType::kConstPool; }
590 : //! Tests whether this node is `CommentNode`.
591 : inline bool isComment() const noexcept { return type() == NodeType::kComment; }
592 : //! Tests whether this node is `SentinelNode`.
593 : inline bool isSentinel() const noexcept { return type() == NodeType::kSentinel; }
594 :
595 : //! Tests whether this node is `FuncNode`.
596 : inline bool isFunc() const noexcept { return type() == NodeType::kFunc; }
597 : //! Tests whether this node is `FuncRetNode`.
598 : inline bool isFuncRet() const noexcept { return type() == NodeType::kFuncRet; }
599 : //! Tests whether this node is `InvokeNode`.
600 : inline bool isInvoke() const noexcept { return type() == NodeType::kInvoke; }
601 :
602 : //! Returns the node flags.
603 : inline NodeFlags flags() const noexcept { return _any._nodeFlags; }
604 : //! Tests whether the node has the given `flag` set.
605 2578614 : inline bool hasFlag(NodeFlags flag) const noexcept { return Support::test(_any._nodeFlags, flag); }
606 : //! Replaces node flags with `flags`.
607 27492 : inline void setFlags(NodeFlags flags) noexcept { _any._nodeFlags = flags; }
608 : //! Adds the given `flags` to node flags.
609 : inline void addFlags(NodeFlags flags) noexcept { _any._nodeFlags |= flags; }
610 : //! Clears the given `flags` from node flags.
611 : inline void clearFlags(NodeFlags flags) noexcept { _any._nodeFlags &= ~flags; }
612 :
613 : //! Tests whether the node is code that can be executed.
614 : inline bool isCode() const noexcept { return hasFlag(NodeFlags::kIsCode); }
615 : //! Tests whether the node is data that cannot be executed.
616 : inline bool isData() const noexcept { return hasFlag(NodeFlags::kIsData); }
617 : //! Tests whether the node is informative only (is never encoded like comment, etc...).
618 : inline bool isInformative() const noexcept { return hasFlag(NodeFlags::kIsInformative); }
619 : //! Tests whether the node is removable if it's in an unreachable code block.
620 : inline bool isRemovable() const noexcept { return hasFlag(NodeFlags::kIsRemovable); }
621 : //! Tests whether the node has no effect when executed (label, .align, nop, ...).
622 : inline bool hasNoEffect() const noexcept { return hasFlag(NodeFlags::kHasNoEffect); }
623 : //! Tests whether the node is part of the code.
624 : inline bool isActive() const noexcept { return hasFlag(NodeFlags::kIsActive); }
625 :
626 : //! Tests whether the node has a position assigned.
627 : //!
628 : //! \remarks Returns `true` if node position is non-zero.
629 0 : inline bool hasPosition() const noexcept { return _position != 0; }
630 : //! Returns node position.
631 37225 : inline uint32_t position() const noexcept { return _position; }
632 : //! Sets node position.
633 : //!
634 : //! Node position is a 32-bit unsigned integer that is used by Compiler to track where the node is relatively to
635 : //! the start of the function. It doesn't describe a byte position in a binary, instead it's just a pseudo position
636 : //! used by liveness analysis and other tools around Compiler.
637 : //!
638 : //! If you don't use Compiler then you may use `position()` and `setPosition()` freely for your own purposes if
639 : //! the 32-bit value limit is okay for you.
640 331996 : inline void setPosition(uint32_t position) noexcept { _position = position; }
641 :
642 : //! Returns user data casted to `T*`.
643 : //!
644 : //! User data is decicated to be used only by AsmJit users and not touched by the library. The data has a pointer
645 : //! size so you can either store a pointer or `intptr_t` value through `setUserDataAsIntPtr()`.
646 : template<typename T>
647 : inline T* userDataAsPtr() const noexcept { return static_cast<T*>(_userDataPtr); }
648 : //! Returns user data casted to `int64_t`.
649 : inline int64_t userDataAsInt64() const noexcept { return int64_t(_userDataU64); }
650 : //! Returns user data casted to `uint64_t`.
651 : inline uint64_t userDataAsUInt64() const noexcept { return _userDataU64; }
652 :
653 : //! Sets user data to `data`.
654 : template<typename T>
655 : inline void setUserDataAsPtr(T* data) noexcept { _userDataPtr = static_cast<void*>(data); }
656 : //! Sets used data to the given 64-bit signed `value`.
657 : inline void setUserDataAsInt64(int64_t value) noexcept { _userDataU64 = uint64_t(value); }
658 : //! Sets used data to the given 64-bit unsigned `value`.
659 : inline void setUserDataAsUInt64(uint64_t value) noexcept { _userDataU64 = value; }
660 :
661 : //! Resets user data to zero / nullptr.
662 : inline void resetUserData() noexcept { _userDataU64 = 0; }
663 :
664 : //! Tests whether the node has an associated pass data.
665 27492 : inline bool hasPassData() const noexcept { return _passData != nullptr; }
666 : //! Returns the node pass data - data used during processing & transformations.
667 : template<typename T>
668 1345085 : inline T* passData() const noexcept { return (T*)_passData; }
669 : //! Sets the node pass data to `data`.
670 : template<typename T>
671 359300 : inline void setPassData(T* data) noexcept { _passData = (void*)data; }
672 : //! Resets the node pass data to nullptr.
673 331808 : inline void resetPassData() noexcept { _passData = nullptr; }
674 :
675 : //! Tests whether the node has an inline comment/annotation.
676 0 : inline bool hasInlineComment() const noexcept { return _inlineComment != nullptr; }
677 : //! Returns an inline comment/annotation string.
678 501977 : inline const char* inlineComment() const noexcept { return _inlineComment; }
679 : //! Sets an inline comment/annotation string to `s`.
680 0 : inline void setInlineComment(const char* s) noexcept { _inlineComment = s; }
681 : //! Resets an inline comment/annotation string to nullptr.
682 : inline void resetInlineComment() noexcept { _inlineComment = nullptr; }
683 :
684 : //! \}
685 : };
686 :
687 : //! Instruction node.
688 : //!
689 : //! Wraps an instruction with its options and operands.
690 : class InstNode : public BaseNode {
691 : public:
692 : ASMJIT_NONCOPYABLE(InstNode)
693 :
694 : //! \name Constants
695 : //! \{
696 :
697 : enum : uint32_t {
698 : //! Count of embedded operands per `InstNode` that are always allocated as a part of the instruction. Minimum
699 : //! embedded operands is 4, but in 32-bit more pointers are smaller and we can embed 5. The rest (up to 6 operands)
700 : //! is always stored in `InstExNode`.
701 : kBaseOpCapacity = uint32_t((128 - sizeof(BaseNode) - sizeof(BaseInst)) / sizeof(Operand_))
702 : };
703 :
704 : //! \}
705 :
706 : //! \name Members
707 : //! \{
708 :
709 : //! Base instruction data.
710 : BaseInst _baseInst;
711 : //! First 4 or 5 operands (indexed from 0).
712 : Operand_ _opArray[kBaseOpCapacity];
713 :
714 : //! \}
715 :
716 : //! \name Construction & Destruction
717 : //! \{
718 :
719 : //! Creates a new `InstNode` instance.
720 : inline InstNode(BaseBuilder* cb, InstId instId, InstOptions options, uint32_t opCount, uint32_t opCapacity = kBaseOpCapacity) noexcept
721 419501 : : BaseNode(cb, NodeType::kInst, NodeFlags::kIsCode | NodeFlags::kIsRemovable | NodeFlags::kActsAsInst),
722 : _baseInst(instId, options) {
723 419501 : _inst._opCapacity = uint8_t(opCapacity);
724 419501 : _inst._opCount = uint8_t(opCount);
725 : }
726 :
727 : //! \cond INTERNAL
728 : //! Reset all built-in operands, including `extraReg`.
729 9639 : inline void _resetOps() noexcept {
730 : _baseInst.resetExtraReg();
731 : resetOpRange(0, opCapacity());
732 9639 : }
733 : //! \endcond
734 :
735 : //! \}
736 :
737 : //! \name Instruction Object
738 : //! \{
739 :
740 304316 : inline BaseInst& baseInst() noexcept { return _baseInst; }
741 0 : inline const BaseInst& baseInst() const noexcept { return _baseInst; }
742 :
743 : //! \}
744 :
745 : //! \name Instruction Id
746 : //! \{
747 :
748 : //! Returns the instruction id, see `BaseInst::Id`.
749 : inline InstId id() const noexcept { return _baseInst.id(); }
750 : //! Returns the instruction real id, see `BaseInst::Id`.
751 : inline InstId realId() const noexcept { return _baseInst.realId(); }
752 :
753 : //! Sets the instruction id to `id`, see `BaseInst::Id`.
754 0 : inline void setId(InstId id) noexcept { _baseInst.setId(id); }
755 :
756 : //! \}
757 :
758 : //! \name Instruction Options
759 : //! \{
760 :
761 : inline InstOptions options() const noexcept { return _baseInst.options(); }
762 : inline bool hasOption(InstOptions option) const noexcept { return _baseInst.hasOption(option); }
763 : inline void setOptions(InstOptions options) noexcept { _baseInst.setOptions(options); }
764 : inline void addOptions(InstOptions options) noexcept { _baseInst.addOptions(options); }
765 : inline void clearOptions(InstOptions options) noexcept { _baseInst.clearOptions(options); }
766 : inline void resetOptions() noexcept { _baseInst.resetOptions(); }
767 :
768 : //! \}
769 :
770 : //! \name Extra Register
771 : //! \{
772 :
773 : //! Tests whether the node has an extra register operand.
774 : inline bool hasExtraReg() const noexcept { return _baseInst.hasExtraReg(); }
775 : //! Returns extra register operand.
776 : inline RegOnly& extraReg() noexcept { return _baseInst.extraReg(); }
777 : //! \overload
778 : inline const RegOnly& extraReg() const noexcept { return _baseInst.extraReg(); }
779 : //! Sets extra register operand to `reg`.
780 : inline void setExtraReg(const BaseReg& reg) noexcept { _baseInst.setExtraReg(reg); }
781 : //! Sets extra register operand to `reg`.
782 : inline void setExtraReg(const RegOnly& reg) noexcept { _baseInst.setExtraReg(reg); }
783 : //! Resets extra register operand.
784 : inline void resetExtraReg() noexcept { _baseInst.resetExtraReg(); }
785 :
786 : //! \}
787 :
788 : //! \name Instruction Operands
789 : //! \{
790 :
791 : //! Returns operand count.
792 392009 : inline uint32_t opCount() const noexcept { return _inst._opCount; }
793 : //! Returns operand capacity.
794 46770 : inline uint32_t opCapacity() const noexcept { return _inst._opCapacity; }
795 :
796 : //! Sets operand count.
797 37131 : inline void setOpCount(uint32_t opCount) noexcept { _inst._opCount = uint8_t(opCount); }
798 :
799 : //! Returns operands array.
800 1193505 : inline Operand* operands() noexcept { return (Operand*)_opArray; }
801 : //! Returns operands array (const).
802 0 : inline const Operand* operands() const noexcept { return (const Operand*)_opArray; }
803 :
804 : //! Returns operand at the given `index`.
805 : inline Operand& op(uint32_t index) noexcept {
806 : ASMJIT_ASSERT(index < opCapacity());
807 0 : return _opArray[index].as<Operand>();
808 : }
809 :
810 : //! Returns operand at the given `index` (const).
811 : inline const Operand& op(uint32_t index) const noexcept {
812 : ASMJIT_ASSERT(index < opCapacity());
813 : return _opArray[index].as<Operand>();
814 : }
815 :
816 : //! Sets operand at the given `index` to `op`.
817 : inline void setOp(uint32_t index, const Operand_& op) noexcept {
818 : ASMJIT_ASSERT(index < opCapacity());
819 421634 : _opArray[index].copyFrom(op);
820 0 : }
821 :
822 : //! Resets operand at the given `index` to none.
823 : inline void resetOp(uint32_t index) noexcept {
824 : ASMJIT_ASSERT(index < opCapacity());
825 : _opArray[index].reset();
826 : }
827 :
828 : //! Resets operands at `[start, end)` range.
829 : inline void resetOpRange(uint32_t start, uint32_t end) noexcept {
830 1233721 : for (uint32_t i = start; i < end; i++)
831 804581 : _opArray[i].reset();
832 : }
833 :
834 : //! \}
835 :
836 : //! \name Utilities
837 : //! \{
838 :
839 : inline bool hasOpType(OperandType opType) const noexcept {
840 : for (uint32_t i = 0, count = opCount(); i < count; i++)
841 : if (_opArray[i].opType() == opType)
842 : return true;
843 : return false;
844 : }
845 :
846 : inline bool hasRegOp() const noexcept { return hasOpType(OperandType::kReg); }
847 : inline bool hasMemOp() const noexcept { return hasOpType(OperandType::kMem); }
848 : inline bool hasImmOp() const noexcept { return hasOpType(OperandType::kImm); }
849 : inline bool hasLabelOp() const noexcept { return hasOpType(OperandType::kLabel); }
850 :
851 : inline uint32_t indexOfOpType(OperandType opType) const noexcept {
852 : uint32_t i = 0;
853 : uint32_t count = opCount();
854 :
855 : while (i < count) {
856 : if (_opArray[i].opType() == opType)
857 : break;
858 : i++;
859 : }
860 :
861 : return i;
862 : }
863 :
864 : inline uint32_t indexOfMemOp() const noexcept { return indexOfOpType(OperandType::kMem); }
865 : inline uint32_t indexOfImmOp() const noexcept { return indexOfOpType(OperandType::kImm); }
866 : inline uint32_t indexOfLabelOp() const noexcept { return indexOfOpType(OperandType::kLabel); }
867 :
868 : //! \}
869 :
870 : //! \name Rewriting
871 : //! \{
872 :
873 : //! \cond INTERNAL
874 623686 : inline uint32_t* _getRewriteArray() noexcept { return &_baseInst._extraReg._id; }
875 646381 : inline const uint32_t* _getRewriteArray() const noexcept { return &_baseInst._extraReg._id; }
876 :
877 : inline uint32_t getRewriteIndex(const uint32_t* id) const noexcept {
878 : const uint32_t* array = _getRewriteArray();
879 : ASMJIT_ASSERT(array <= id);
880 :
881 646381 : size_t index = (size_t)(id - array);
882 : ASMJIT_ASSERT(index < 32);
883 :
884 646381 : return uint32_t(index);
885 : }
886 :
887 : inline void rewriteIdAtIndex(uint32_t index, uint32_t id) noexcept {
888 : uint32_t* array = _getRewriteArray();
889 623686 : array[index] = id;
890 623686 : }
891 : //! \endcond
892 :
893 : //! \}
894 :
895 : //! \name Static Functions
896 : //! \{
897 :
898 : //! \cond INTERNAL
899 : static inline uint32_t capacityOfOpCount(uint32_t opCount) noexcept {
900 382370 : return opCount <= kBaseOpCapacity ? kBaseOpCapacity : Globals::kMaxOpCount;
901 : }
902 :
903 : static inline size_t nodeSizeOfOpCapacity(uint32_t opCapacity) noexcept {
904 : size_t base = sizeof(InstNode) - kBaseOpCapacity * sizeof(Operand);
905 382370 : return base + opCapacity * sizeof(Operand);
906 : }
907 : //! \endcond
908 :
909 : //! \}
910 : };
911 :
912 : //! Instruction node with maximum number of operands.
913 : //!
914 : //! This node is created automatically by Builder/Compiler in case that the required number of operands exceeds
915 : //! the default capacity of `InstNode`.
916 : class InstExNode : public InstNode {
917 : public:
918 : ASMJIT_NONCOPYABLE(InstExNode)
919 :
920 : //! \name Members
921 : //! \{
922 :
923 : //! Continued `_opArray[]` to hold up to `kMaxOpCount` operands.
924 : Operand_ _opArrayEx[Globals::kMaxOpCount - kBaseOpCapacity];
925 :
926 : //! \}
927 :
928 : //! \name Construction & Destruction
929 : //! \{
930 :
931 : //! Creates a new `InstExNode` instance.
932 : inline InstExNode(BaseBuilder* cb, InstId instId, InstOptions options, uint32_t opCapacity = Globals::kMaxOpCount) noexcept
933 : : InstNode(cb, instId, options, opCapacity) {}
934 :
935 : //! \}
936 : };
937 :
938 : //! Section node.
939 : class SectionNode : public BaseNode {
940 : public:
941 : ASMJIT_NONCOPYABLE(SectionNode)
942 :
943 : //! \name Members
944 : //! \{
945 :
946 : //! Section id.
947 : uint32_t _id;
948 :
949 : //! Next section node that follows this section.
950 : //!
951 : //! This link is only valid when the section is active (is part of the code) and when `Builder::hasDirtySectionLinks()`
952 : //! returns `false`. If you intend to use this field you should always call `Builder::updateSectionLinks()` before you
953 : //! do so.
954 : SectionNode* _nextSection;
955 :
956 : //! \}
957 :
958 : //! \name Construction & Destruction
959 : //! \{
960 :
961 : //! Creates a new `SectionNode` instance.
962 : inline SectionNode(BaseBuilder* cb, uint32_t secionId = 0) noexcept
963 27492 : : BaseNode(cb, NodeType::kSection, NodeFlags::kHasNoEffect),
964 27492 : _id(secionId),
965 27492 : _nextSection(nullptr) {}
966 :
967 : //! \}
968 :
969 : //! \name Accessors
970 : //! \{
971 :
972 : //! Returns the section id.
973 27492 : inline uint32_t id() const noexcept { return _id; }
974 :
975 : //! \}
976 : };
977 :
978 : //! Label node.
979 : class LabelNode : public BaseNode {
980 : public:
981 : ASMJIT_NONCOPYABLE(LabelNode)
982 :
983 : //! \name Members
984 : //! \{
985 :
986 : //! Label identifier.
987 : uint32_t _labelId;
988 :
989 : //! \}
990 :
991 : //! \name Construction & Destruction
992 : //! \{
993 :
994 : //! Creates a new `LabelNode` instance.
995 : inline LabelNode(BaseBuilder* cb, uint32_t labelId = 0) noexcept
996 54984 : : BaseNode(cb, NodeType::kLabel, NodeFlags::kHasNoEffect | NodeFlags::kActsAsLabel),
997 54984 : _labelId(labelId) {}
998 :
999 : //! \}
1000 :
1001 : //! \name Accessors
1002 : //! \{
1003 :
1004 : //! Returns \ref Label representation of the \ref LabelNode.
1005 54984 : inline Label label() const noexcept { return Label(_labelId); }
1006 : //! Returns the id of the label.
1007 54984 : inline uint32_t labelId() const noexcept { return _labelId; }
1008 :
1009 : //! \}
1010 : };
1011 :
1012 : //! Align directive (BaseBuilder).
1013 : //!
1014 : //! Wraps `.align` directive.
1015 : class AlignNode : public BaseNode {
1016 : public:
1017 : ASMJIT_NONCOPYABLE(AlignNode)
1018 :
1019 : //! \name Members
1020 : //! \{
1021 :
1022 : //! Alignment (in bytes).
1023 : uint32_t _alignment;
1024 :
1025 : //! \}
1026 :
1027 : //! \name Construction & Destruction
1028 : //! \{
1029 :
1030 : //! Creates a new `AlignNode` instance.
1031 : inline AlignNode(BaseBuilder* cb, AlignMode alignMode, uint32_t alignment) noexcept
1032 0 : : BaseNode(cb, NodeType::kAlign, NodeFlags::kIsCode | NodeFlags::kHasNoEffect) {
1033 :
1034 0 : _alignData._alignMode = alignMode;
1035 0 : _alignment = alignment;
1036 : }
1037 :
1038 : //! \}
1039 :
1040 : //! \name Accessors
1041 : //! \{
1042 :
1043 : //! Returns align mode.
1044 0 : inline AlignMode alignMode() const noexcept { return _alignData._alignMode; }
1045 : //! Sets align mode to `alignMode`.
1046 : inline void setAlignMode(AlignMode alignMode) noexcept { _alignData._alignMode = alignMode; }
1047 :
1048 : //! Returns align offset in bytes.
1049 0 : inline uint32_t alignment() const noexcept { return _alignment; }
1050 : //! Sets align offset in bytes to `offset`.
1051 : inline void setAlignment(uint32_t alignment) noexcept { _alignment = alignment; }
1052 :
1053 : //! \}
1054 : };
1055 :
1056 : //! Embed data node.
1057 : //!
1058 : //! Wraps `.data` directive. The node contains data that will be placed at the node's position in the assembler
1059 : //! stream. The data is considered to be RAW; no analysis nor byte-order conversion is performed on RAW data.
1060 : class EmbedDataNode : public BaseNode {
1061 : public:
1062 : ASMJIT_NONCOPYABLE(EmbedDataNode)
1063 :
1064 : //! \cond INTERNAL
1065 : enum : uint32_t {
1066 : kInlineBufferSize = 128 - (sizeof(BaseNode) + sizeof(size_t) * 2)
1067 : };
1068 : //! \endcond
1069 :
1070 : //! \name Members
1071 : //! \{
1072 :
1073 : size_t _itemCount;
1074 : size_t _repeatCount;
1075 :
1076 : union {
1077 : uint8_t* _externalData;
1078 : uint8_t _inlineData[kInlineBufferSize];
1079 : };
1080 :
1081 : //! \}
1082 :
1083 : //! \name Construction & Destruction
1084 : //! \{
1085 :
1086 : //! Creates a new `EmbedDataNode` instance.
1087 0 : inline EmbedDataNode(BaseBuilder* cb) noexcept
1088 0 : : BaseNode(cb, NodeType::kEmbedData, NodeFlags::kIsData),
1089 0 : _itemCount(0),
1090 0 : _repeatCount(0) {
1091 0 : _embed._typeId = TypeId::kUInt8;
1092 0 : _embed._typeSize = uint8_t(1);
1093 0 : memset(_inlineData, 0, kInlineBufferSize);
1094 0 : }
1095 :
1096 : //! \}
1097 :
1098 : //! \name Accessors
1099 : //! \{
1100 :
1101 : //! Returns data type as \ref TypeId.
1102 0 : inline TypeId typeId() const noexcept { return _embed._typeId; }
1103 : //! Returns the size of a single data element.
1104 0 : inline uint32_t typeSize() const noexcept { return _embed._typeSize; }
1105 :
1106 : //! Returns a pointer to the data casted to `uint8_t`.
1107 : inline uint8_t* data() const noexcept {
1108 0 : return dataSize() <= kInlineBufferSize ? const_cast<uint8_t*>(_inlineData) : _externalData;
1109 : }
1110 :
1111 : //! Returns a pointer to the data casted to `T`.
1112 : template<typename T>
1113 : inline T* dataAs() const noexcept { return reinterpret_cast<T*>(data()); }
1114 :
1115 : //! Returns the number of (typed) items in the array.
1116 0 : inline size_t itemCount() const noexcept { return _itemCount; }
1117 :
1118 : //! Returns how many times the data is repeated (default 1).
1119 : //!
1120 : //! Repeated data is useful when defining constants for SIMD, for example.
1121 0 : inline size_t repeatCount() const noexcept { return _repeatCount; }
1122 :
1123 : //! Returns the size of the data, not considering the number of times it repeats.
1124 : //!
1125 : //! \note The returned value is the same as `typeSize() * itemCount()`.
1126 0 : inline size_t dataSize() const noexcept { return typeSize() * _itemCount; }
1127 :
1128 : //! \}
1129 : };
1130 :
1131 : //! Label data node.
1132 : class EmbedLabelNode : public BaseNode {
1133 : public:
1134 : ASMJIT_NONCOPYABLE(EmbedLabelNode)
1135 :
1136 : //! \name Members
1137 : //! \{
1138 :
1139 : uint32_t _labelId;
1140 : uint32_t _dataSize;
1141 :
1142 : //! \}
1143 :
1144 : //! \name Construction & Destruction
1145 : //! \{
1146 :
1147 : //! Creates a new `EmbedLabelNode` instance.
1148 : inline EmbedLabelNode(BaseBuilder* cb, uint32_t labelId = 0, uint32_t dataSize = 0) noexcept
1149 0 : : BaseNode(cb, NodeType::kEmbedLabel, NodeFlags::kIsData),
1150 0 : _labelId(labelId),
1151 0 : _dataSize(dataSize) {}
1152 :
1153 : //! \}
1154 :
1155 : //! \name Accessors
1156 : //! \{
1157 :
1158 : //! Returns the label to embed as \ref Label operand.
1159 0 : inline Label label() const noexcept { return Label(_labelId); }
1160 : //! Returns the id of the label.
1161 0 : inline uint32_t labelId() const noexcept { return _labelId; }
1162 :
1163 : //! Sets the label id from `label` operand.
1164 : inline void setLabel(const Label& label) noexcept { setLabelId(label.id()); }
1165 : //! Sets the label id (use with caution, improper use can break a lot of things).
1166 : inline void setLabelId(uint32_t labelId) noexcept { _labelId = labelId; }
1167 :
1168 : //! Returns the data size.
1169 0 : inline uint32_t dataSize() const noexcept { return _dataSize; }
1170 : //! Sets the data size.
1171 : inline void setDataSize(uint32_t dataSize) noexcept { _dataSize = dataSize; }
1172 :
1173 : //! \}
1174 : };
1175 :
1176 : //! Label data node.
1177 : class EmbedLabelDeltaNode : public BaseNode {
1178 : public:
1179 : ASMJIT_NONCOPYABLE(EmbedLabelDeltaNode)
1180 :
1181 : //! \name Members
1182 : //! \{
1183 :
1184 : uint32_t _labelId;
1185 : uint32_t _baseLabelId;
1186 : uint32_t _dataSize;
1187 :
1188 : //! \}
1189 :
1190 : //! \name Construction & Destruction
1191 : //! \{
1192 :
1193 : //! Creates a new `EmbedLabelDeltaNode` instance.
1194 : inline EmbedLabelDeltaNode(BaseBuilder* cb, uint32_t labelId = 0, uint32_t baseLabelId = 0, uint32_t dataSize = 0) noexcept
1195 0 : : BaseNode(cb, NodeType::kEmbedLabelDelta, NodeFlags::kIsData),
1196 0 : _labelId(labelId),
1197 0 : _baseLabelId(baseLabelId),
1198 0 : _dataSize(dataSize) {}
1199 :
1200 : //! \}
1201 :
1202 : //! \name Accessors
1203 : //! \{
1204 :
1205 : //! Returns the label as `Label` operand.
1206 0 : inline Label label() const noexcept { return Label(_labelId); }
1207 : //! Returns the id of the label.
1208 0 : inline uint32_t labelId() const noexcept { return _labelId; }
1209 :
1210 : //! Sets the label id from `label` operand.
1211 : inline void setLabel(const Label& label) noexcept { setLabelId(label.id()); }
1212 : //! Sets the label id.
1213 : inline void setLabelId(uint32_t labelId) noexcept { _labelId = labelId; }
1214 :
1215 : //! Returns the base label as `Label` operand.
1216 0 : inline Label baseLabel() const noexcept { return Label(_baseLabelId); }
1217 : //! Returns the id of the base label.
1218 0 : inline uint32_t baseLabelId() const noexcept { return _baseLabelId; }
1219 :
1220 : //! Sets the base label id from `label` operand.
1221 : inline void setBaseLabel(const Label& baseLabel) noexcept { setBaseLabelId(baseLabel.id()); }
1222 : //! Sets the base label id.
1223 : inline void setBaseLabelId(uint32_t baseLabelId) noexcept { _baseLabelId = baseLabelId; }
1224 :
1225 : //! Returns the size of the embedded label address.
1226 0 : inline uint32_t dataSize() const noexcept { return _dataSize; }
1227 : //! Sets the size of the embedded label address.
1228 : inline void setDataSize(uint32_t dataSize) noexcept { _dataSize = dataSize; }
1229 :
1230 : //! \}
1231 : };
1232 :
1233 : //! A node that wraps `ConstPool`.
1234 : class ConstPoolNode : public LabelNode {
1235 : public:
1236 : ASMJIT_NONCOPYABLE(ConstPoolNode)
1237 :
1238 : //! \name Members
1239 : //! \{
1240 :
1241 : ConstPool _constPool;
1242 :
1243 : //! \}
1244 :
1245 : //! \name Construction & Destruction
1246 : //! \{
1247 :
1248 : //! Creates a new `ConstPoolNode` instance.
1249 0 : inline ConstPoolNode(BaseBuilder* cb, uint32_t id = 0) noexcept
1250 0 : : LabelNode(cb, id),
1251 0 : _constPool(&cb->_codeZone) {
1252 :
1253 : setType(NodeType::kConstPool);
1254 : addFlags(NodeFlags::kIsData);
1255 : clearFlags(NodeFlags::kIsCode | NodeFlags::kHasNoEffect);
1256 0 : }
1257 :
1258 : //! \}
1259 :
1260 : //! \name Accessors
1261 : //! \{
1262 :
1263 : //! Tests whether the constant-pool is empty.
1264 : inline bool empty() const noexcept { return _constPool.empty(); }
1265 : //! Returns the size of the constant-pool in bytes.
1266 : inline size_t size() const noexcept { return _constPool.size(); }
1267 : //! Returns minimum alignment.
1268 : inline size_t alignment() const noexcept { return _constPool.alignment(); }
1269 :
1270 : //! Returns the wrapped `ConstPool` instance.
1271 0 : inline ConstPool& constPool() noexcept { return _constPool; }
1272 : //! Returns the wrapped `ConstPool` instance (const).
1273 : inline const ConstPool& constPool() const noexcept { return _constPool; }
1274 :
1275 : //! \}
1276 :
1277 : //! \name Utilities
1278 : //! \{
1279 :
1280 : //! See `ConstPool::add()`.
1281 : inline Error add(const void* data, size_t size, size_t& dstOffset) noexcept {
1282 0 : return _constPool.add(data, size, dstOffset);
1283 : }
1284 :
1285 : //! \}
1286 : };
1287 :
1288 : //! Comment node.
1289 : class CommentNode : public BaseNode {
1290 : public:
1291 : ASMJIT_NONCOPYABLE(CommentNode)
1292 :
1293 : //! \name Construction & Destruction
1294 : //! \{
1295 :
1296 : //! Creates a new `CommentNode` instance.
1297 : inline CommentNode(BaseBuilder* cb, const char* comment) noexcept
1298 : : BaseNode(cb, NodeType::kComment, NodeFlags::kIsInformative | NodeFlags::kHasNoEffect | NodeFlags::kIsRemovable) {
1299 0 : _inlineComment = comment;
1300 : }
1301 :
1302 : //! \}
1303 : };
1304 :
1305 : //! Sentinel node.
1306 : //!
1307 : //! Sentinel is a marker that is completely ignored by the code builder. It's used to remember a position in a code
1308 : //! as it never gets removed by any pass.
1309 : class SentinelNode : public BaseNode {
1310 : public:
1311 : ASMJIT_NONCOPYABLE(SentinelNode)
1312 :
1313 : //! \name Construction & Destruction
1314 : //! \{
1315 :
1316 : //! Creates a new `SentinelNode` instance.
1317 : inline SentinelNode(BaseBuilder* cb, SentinelType sentinelType = SentinelType::kUnknown) noexcept
1318 : : BaseNode(cb, NodeType::kSentinel, NodeFlags::kIsInformative | NodeFlags::kHasNoEffect) {
1319 :
1320 27492 : _sentinel._sentinelType = sentinelType;
1321 : }
1322 :
1323 : //! \}
1324 :
1325 : //! \name Accessors
1326 : //! \{
1327 :
1328 : //! Returns the type of the sentinel.
1329 : inline SentinelType sentinelType() const noexcept {
1330 0 : return _sentinel._sentinelType;
1331 : }
1332 :
1333 : //! Sets the type of the sentinel.
1334 : inline void setSentinelType(SentinelType type) noexcept {
1335 : _sentinel._sentinelType = type;
1336 : }
1337 :
1338 : //! \}
1339 : };
1340 :
1341 : //! Pass can be used to implement code transformations, analysis, and lowering.
1342 : class ASMJIT_VIRTAPI Pass {
1343 : public:
1344 : ASMJIT_BASE_CLASS(Pass)
1345 : ASMJIT_NONCOPYABLE(Pass)
1346 :
1347 : //! \name Members
1348 : //! \{
1349 :
1350 : //! BaseBuilder this pass is assigned to.
1351 : BaseBuilder* _cb = nullptr;
1352 : //! Name of the pass.
1353 : const char* _name = nullptr;
1354 :
1355 : //! \}
1356 :
1357 : //! \name Construction & Destruction
1358 : //! \{
1359 :
1360 : ASMJIT_API Pass(const char* name) noexcept;
1361 : ASMJIT_API virtual ~Pass() noexcept;
1362 :
1363 : //! \}
1364 :
1365 : //! \name Accessors
1366 : //! \{
1367 :
1368 : //! Returns \ref BaseBuilder associated with the pass.
1369 27492 : inline const BaseBuilder* cb() const noexcept { return _cb; }
1370 : //! Returns the name of the pass.
1371 0 : inline const char* name() const noexcept { return _name; }
1372 :
1373 : //! \}
1374 :
1375 : //! \name Pass Interface
1376 : //! \{
1377 :
1378 : //! Processes the code stored in Builder or Compiler.
1379 : //!
1380 : //! This is the only function that is called by the `BaseBuilder` to process the code. It passes `zone`,
1381 : //! which will be reset after the `run()` finishes.
1382 : virtual Error run(Zone* zone, Logger* logger) = 0;
1383 :
1384 : //! \}
1385 : };
1386 :
1387 : //! \}
1388 :
1389 : ASMJIT_END_NAMESPACE
1390 :
1391 : #endif // !ASMJIT_NO_BUILDER
1392 : #endif // ASMJIT_CORE_BUILDER_H_INCLUDED
1393 :
1394 : #endif // __PLUMED_HAS_ASMJIT
|