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