odin-blend2d

Odin bindings to Blend2D
Log | Files | Refs | README | LICENSE

codeholder.h (52222B)


      1 // This file is part of AsmJit project <https://asmjit.com>
      2 //
      3 // See <asmjit/core.h> or LICENSE.md for license and copyright information
      4 // SPDX-License-Identifier: Zlib
      5 
      6 #ifndef ASMJIT_CORE_CODEHOLDER_H_INCLUDED
      7 #define ASMJIT_CORE_CODEHOLDER_H_INCLUDED
      8 
      9 #include "../core/archtraits.h"
     10 #include "../core/arena.h"
     11 #include "../core/arenahash.h"
     12 #include "../core/arenapool.h"
     13 #include "../core/arenastring.h"
     14 #include "../core/arenatree.h"
     15 #include "../core/arenavector.h"
     16 #include "../core/codebuffer.h"
     17 #include "../core/errorhandler.h"
     18 #include "../core/fixup.h"
     19 #include "../core/operand.h"
     20 #include "../core/span.h"
     21 #include "../core/string.h"
     22 #include "../core/support.h"
     23 #include "../core/target.h"
     24 
     25 ASMJIT_BEGIN_NAMESPACE
     26 
     27 //! \addtogroup asmjit_core
     28 //! \{
     29 
     30 class BaseEmitter;
     31 class CodeHolder;
     32 class LabelEntry;
     33 class Logger;
     34 
     35 //! Operator type that can be used within an \ref Expression.
     36 enum class ExpressionOpType : uint8_t {
     37   //! Addition.
     38   kAdd = 0,
     39   //! Subtraction.
     40   kSub = 1,
     41   //! Multiplication
     42   kMul = 2,
     43   //! Logical left shift.
     44   kSll = 3,
     45   //! Logical right shift.
     46   kSrl = 4,
     47   //! Arithmetic right shift.
     48   kSra = 5
     49 };
     50 
     51 //! Value type that can be used within an \ref Expression.
     52 enum class ExpressionValueType : uint8_t {
     53   //! No value or invalid.
     54   kNone = 0,
     55   //! Value is 64-bit unsigned integer (constant).
     56   kConstant = 1,
     57   //! Value is \ref LabelEntry, which references a \ref Label.
     58   kLabel = 2,
     59   //! Value is \ref Expression
     60   kExpression = 3
     61 };
     62 
     63 //! Expression node that can reference constants, labels, and another expressions.
     64 struct Expression {
     65   //! Expression value.
     66   union Value {
     67     //! Constant.
     68     uint64_t constant;
     69     //! Pointer to another expression.
     70     Expression* expression;
     71     //! Label identifier
     72     uint32_t label_id;
     73   };
     74 
     75   //! \name Members
     76   //! \{
     77 
     78   //! Operation type.
     79   ExpressionOpType op_type;
     80   //! Value types of \ref value.
     81   ExpressionValueType value_type[2];
     82   //! Reserved for future use, should be initialized to zero.
     83   uint8_t reserved[5];
     84   //! Expression left and right values.
     85   Value value[2];
     86 
     87   //! \}
     88 
     89   //! \name Accessors
     90   //! \{
     91 
     92   //! Resets the whole expression.
     93   //!
     94   //! Changes both values to \ref ExpressionValueType::kNone.
     95   ASMJIT_INLINE_NODEBUG void reset() noexcept { *this = Expression{}; }
     96 
     97   //! Sets the value type at `index` to \ref ExpressionValueType::kConstant and its content to `constant`.
     98   ASMJIT_INLINE_NODEBUG void set_value_as_constant(size_t index, uint64_t constant) noexcept {
     99     value_type[index] = ExpressionValueType::kConstant;
    100     value[index].constant = constant;
    101   }
    102 
    103   //! Sets the value type at `index` to \ref ExpressionValueType::kLabel and its content to `label_entry`.
    104   ASMJIT_INLINE_NODEBUG void set_value_as_label_id(size_t index, uint32_t label_id) noexcept {
    105     value_type[index] = ExpressionValueType::kLabel;
    106     value[index].label_id = label_id;
    107   }
    108 
    109   //! Sets the value type at `index` to \ref ExpressionValueType::kExpression and its content to `expression`.
    110   ASMJIT_INLINE_NODEBUG void set_value_as_expression(size_t index, Expression* expression) noexcept {
    111     value_type[index] = ExpressionValueType::kExpression;
    112     value[index].expression = expression;
    113   }
    114 
    115   //! \}
    116 };
    117 
    118 //! Relocation type.
    119 enum class RelocType : uint32_t {
    120   //! None/deleted (no relocation).
    121   kNone = 0,
    122   //! Expression evaluation, `_payload` is pointer to `Expression`.
    123   kExpression = 1,
    124   //! Relative relocation from one section to another.
    125   kSectionRelative = 2,
    126   //! Relocate absolute to absolute.
    127   kAbsToAbs = 3,
    128   //! Relocate relative to absolute.
    129   kRelToAbs = 4,
    130   //! Relocate absolute to relative.
    131   kAbsToRel = 5,
    132   //! Relocate absolute to relative or use trampoline.
    133   kX64AddressEntry = 6
    134 };
    135 
    136 //! Type of the \ref Label.
    137 enum class LabelType : uint8_t {
    138   //! Anonymous label that can optionally have a name, which is only used for debugging purposes.
    139   kAnonymous = 0u,
    140   //! Local label (always has parent_id).
    141   kLocal = 1u,
    142   //! Global label (never has parent_id).
    143   kGlobal = 2u,
    144   //! External label (references an external symbol).
    145   kExternal = 3u,
    146 
    147   //! Maximum value of `LabelType`.
    148   kMaxValue = kExternal
    149 };
    150 
    151 //! Label flags describe some details about labels used by \ref LabelEntry, mostly for AsmJit's own use.
    152 enum class LabelFlags : uint8_t {
    153   //! No flags.
    154   kNone = 0x00u,
    155   //! Label has associated extra data with it that it owns.
    156   kHasOwnExtraData = 0x01u,
    157   //! Label has a name.
    158   kHasName = 0x02u,
    159   //! Label has a parent (only a local label can have a parent).
    160   kHasParent = 0x04u
    161 };
    162 ASMJIT_DEFINE_ENUM_FLAGS(LabelFlags)
    163 
    164 //! Section flags, used by \ref Section.
    165 enum class SectionFlags : uint32_t {
    166   //! No flags.
    167   kNone = 0,
    168   //! Executable (.text sections).
    169   kExecutable = 0x0001u,
    170   //! Read-only (.text and .data sections).
    171   kReadOnly = 0x0002u,
    172   //! Zero initialized by the loader (BSS).
    173   kZeroInitialized = 0x0004u,
    174   //! Info / comment flag.
    175   kComment = 0x0008u,
    176   //! Section is built in and created by default (.text section).
    177   kBuiltIn = 0x4000u,
    178   //! Section created implicitly, can be deleted by \ref Target.
    179   kImplicit = 0x8000u
    180 };
    181 ASMJIT_DEFINE_ENUM_FLAGS(SectionFlags)
    182 
    183 //! Flags that can be used with \ref CodeHolder::copy_section_data() and \ref CodeHolder::copy_flattened_data().
    184 enum class CopySectionFlags : uint32_t {
    185   //! No flags.
    186   kNone = 0,
    187 
    188   //! If virtual size of a section is greater than the size of its \ref CodeBuffer then all bytes between the buffer
    189   //! size and virtual size will be zeroed. If this option is not set then those bytes would be left as is, which
    190   //! means that if the user didn't initialize them they would have a previous content, which may be unwanted.
    191   kPadSectionBuffer = 0x00000001u,
    192 
    193   //! Clears the target buffer if the flattened data is less than the destination size. This option works
    194   //! only with \ref CodeHolder::copy_flattened_data() as it processes multiple sections. It is ignored by
    195   //! \ref CodeHolder::copy_section_data().
    196   kPadTargetBuffer = 0x00000002u
    197 };
    198 ASMJIT_DEFINE_ENUM_FLAGS(CopySectionFlags)
    199 
    200 //! Base class for both \ref Section and \ref LabelEntry::ExtraData.
    201 class SectionOrLabelEntryExtraHeader {
    202 public:
    203   //! \name Members
    204   //! \{
    205 
    206   //! Section id - describes either a section where a \ref Label is bound or it's a real section id of \ref Section.
    207   uint32_t _section_id;
    208 
    209   //! Internal label type is only used by \ref LabelEntry::ExtraData. \ref Section always leaves this field zero,
    210   //! which describes an anonymous label. Anonymous labels are default and always used when there is no
    211   //! \ref LabelEntry::ExtraData
    212   LabelType _internal_label_type;
    213 
    214   //! Internal label flags, used by \ref LabelEntry::ExtraData. \ref Section doesn't use these flags and sets them
    215   //! to zero.
    216   LabelFlags _internal_label_flags;
    217 
    218   //! Internal data used freely by \ref Section and \ref LabelEntry::ExtraData.
    219   uint16_t _internal_uint16_data;
    220 
    221   //! \}
    222 };
    223 
    224 //! Section entry.
    225 class Section : public SectionOrLabelEntryExtraHeader {
    226 public:
    227   //! \name Members
    228   //! \{
    229 
    230   //! Section alignment requirements (0 if no requirements).
    231   uint32_t _alignment;
    232   //! Order (lower value means higher priority).
    233   int32_t _order;
    234   //! Offset of this section from base-address.
    235   uint64_t _offset;
    236   //! Virtual size of the section (zero initialized sections).
    237   uint64_t _virtual_size;
    238   //! Section name (max 35 characters, PE allows max 8).
    239   FixedString<Globals::kMaxSectionNameSize + 1> _name;
    240   //! Code or data buffer.
    241   CodeBuffer _buffer;
    242 
    243   //! \}
    244 
    245   //! \name Accessors
    246   //! \{
    247 
    248   //! Returns the section id.
    249   [[nodiscard]]
    250   ASMJIT_INLINE_NODEBUG uint32_t section_id() const noexcept { return _section_id; }
    251 
    252   //! Returns the section name, as a null terminated string.
    253   [[nodiscard]]
    254   ASMJIT_INLINE_NODEBUG const char* name() const noexcept { return _name.str; }
    255 
    256   //! Returns the section data.
    257   [[nodiscard]]
    258   ASMJIT_INLINE_NODEBUG uint8_t* data() noexcept { return _buffer.data(); }
    259 
    260   //! \overload
    261   [[nodiscard]]
    262   ASMJIT_INLINE_NODEBUG const uint8_t* data() const noexcept { return _buffer.data(); }
    263 
    264   //! Returns the section flags.
    265   [[nodiscard]]
    266   ASMJIT_INLINE_NODEBUG SectionFlags flags() const noexcept { return SectionFlags(_internal_uint16_data); }
    267 
    268   //! Tests whether the section has the given `flag`.
    269   [[nodiscard]]
    270   ASMJIT_INLINE_NODEBUG bool has_flag(SectionFlags flag) const noexcept { return Support::test(_internal_uint16_data, uint32_t(flag)); }
    271 
    272   //! Assigns `flags` to the section (replaces all existing flags).
    273   ASMJIT_INLINE_NODEBUG void assign_flags(SectionFlags flags) noexcept { _internal_uint16_data = uint16_t(flags); }
    274 
    275   //! Adds `flags` to the section flags.
    276   ASMJIT_INLINE_NODEBUG void add_flags(SectionFlags flags) noexcept { _internal_uint16_data = uint16_t(_internal_uint16_data | uint32_t(flags)); }
    277 
    278   //! Removes `flags` from the section flags.
    279   ASMJIT_INLINE_NODEBUG void clear_flags(SectionFlags flags) noexcept { _internal_uint16_data = uint16_t(_internal_uint16_data | ~uint32_t(flags)); }
    280 
    281   //! Returns the minimum section alignment
    282   [[nodiscard]]
    283   ASMJIT_INLINE_NODEBUG uint32_t alignment() const noexcept { return _alignment; }
    284 
    285   //! Sets the minimum section alignment
    286   ASMJIT_INLINE_NODEBUG void set_alignment(uint32_t alignment) noexcept { _alignment = alignment; }
    287 
    288   //! Returns the section order, which has a higher priority than section id.
    289   [[nodiscard]]
    290   ASMJIT_INLINE_NODEBUG int32_t order() const noexcept { return _order; }
    291 
    292   //! Returns the section offset, relative to base.
    293   [[nodiscard]]
    294   ASMJIT_INLINE_NODEBUG uint64_t offset() const noexcept { return _offset; }
    295 
    296   //! Set the section offset.
    297   ASMJIT_INLINE_NODEBUG void set_offset(uint64_t offset) noexcept { _offset = offset; }
    298 
    299   //! Returns the virtual size of the section.
    300   //!
    301   //! Virtual size is initially zero and is never changed by AsmJit. It's normal if virtual size is smaller than
    302   //! size returned by `buffer_size()` as the buffer stores real data emitted by assemblers or appended by users.
    303   //!
    304   //! Use `real_size()` to get the real and final size of this section.
    305   [[nodiscard]]
    306   ASMJIT_INLINE_NODEBUG uint64_t virtual_size() const noexcept { return _virtual_size; }
    307 
    308   //! Sets the virtual size of the section.
    309   ASMJIT_INLINE_NODEBUG void set_virtual_size(uint64_t virtual_size) noexcept { _virtual_size = virtual_size; }
    310 
    311   //! Returns the buffer size of the section.
    312   [[nodiscard]]
    313   ASMJIT_INLINE_NODEBUG size_t buffer_size() const noexcept { return _buffer.size(); }
    314 
    315   //! Returns the real size of the section calculated from virtual and buffer sizes.
    316   [[nodiscard]]
    317   ASMJIT_INLINE_NODEBUG uint64_t real_size() const noexcept { return Support::max<uint64_t>(virtual_size(), buffer_size()); }
    318 
    319   //! Returns the `CodeBuffer` used by this section.
    320   [[nodiscard]]
    321   ASMJIT_INLINE_NODEBUG CodeBuffer& buffer() noexcept { return _buffer; }
    322 
    323   //! Returns the `CodeBuffer` used by this section (const).
    324   [[nodiscard]]
    325   ASMJIT_INLINE_NODEBUG const CodeBuffer& buffer() const noexcept { return _buffer; }
    326 
    327   //! \}
    328 };
    329 
    330 //! Entry in an address table.
    331 class AddressTableEntry : public ArenaTreeNodeT<AddressTableEntry> {
    332 public:
    333   ASMJIT_NONCOPYABLE(AddressTableEntry)
    334 
    335   //! \name Members
    336   //! \{
    337 
    338   //! Address.
    339   uint64_t _address;
    340   //! Slot.
    341   uint32_t _slot;
    342 
    343   //! \}
    344 
    345   //! \name Construction & Destruction
    346   //! \{
    347 
    348   ASMJIT_INLINE_NODEBUG explicit AddressTableEntry(uint64_t address) noexcept
    349     : _address(address),
    350       _slot(0xFFFFFFFFu) {}
    351 
    352   //! \}
    353 
    354   //! \name Accessors
    355   //! \{
    356 
    357   [[nodiscard]]
    358   ASMJIT_INLINE_NODEBUG uint64_t address() const noexcept { return _address; }
    359 
    360   [[nodiscard]]
    361   ASMJIT_INLINE_NODEBUG uint32_t slot() const noexcept { return _slot; }
    362 
    363   [[nodiscard]]
    364   ASMJIT_INLINE_NODEBUG bool has_assigned_slot() const noexcept { return _slot != 0xFFFFFFFFu; }
    365 
    366   [[nodiscard]]
    367   ASMJIT_INLINE_NODEBUG bool operator<(const AddressTableEntry& other) const noexcept { return _address < other._address; }
    368 
    369   [[nodiscard]]
    370   ASMJIT_INLINE_NODEBUG bool operator>(const AddressTableEntry& other) const noexcept { return _address > other._address; }
    371 
    372   [[nodiscard]]
    373   ASMJIT_INLINE_NODEBUG bool operator<(uint64_t query_address) const noexcept { return _address < query_address; }
    374 
    375   [[nodiscard]]
    376   ASMJIT_INLINE_NODEBUG bool operator>(uint64_t query_address) const noexcept { return _address > query_address; }
    377 
    378   //! \}
    379 };
    380 
    381 //! Relocation entry.
    382 struct RelocEntry {
    383   //! \name Members
    384   //! \{
    385 
    386   //! Relocation id.
    387   uint32_t _id;
    388   //! Type of the relocation.
    389   RelocType _reloc_type;
    390   //! Format of the relocated value.
    391   OffsetFormat _format;
    392   //! Source section id.
    393   uint32_t _source_section_id;
    394   //! Target section id.
    395   uint32_t _target_section_id;
    396   //! Source offset (relative to start of the section).
    397   uint64_t _source_offset;
    398   //! Payload (target offset, target address, expression, etc).
    399   uint64_t _payload;
    400 
    401   //! \}
    402 
    403   //! \name Accessors
    404   //! \{
    405 
    406   [[nodiscard]]
    407   ASMJIT_INLINE_NODEBUG uint32_t id() const noexcept { return _id; }
    408 
    409   [[nodiscard]]
    410   ASMJIT_INLINE_NODEBUG RelocType reloc_type() const noexcept { return _reloc_type; }
    411 
    412   [[nodiscard]]
    413   ASMJIT_INLINE_NODEBUG const OffsetFormat& format() const noexcept { return _format; }
    414 
    415   [[nodiscard]]
    416   ASMJIT_INLINE_NODEBUG uint32_t source_section_id() const noexcept { return _source_section_id; }
    417 
    418   [[nodiscard]]
    419   ASMJIT_INLINE_NODEBUG uint32_t target_section_id() const noexcept { return _target_section_id; }
    420 
    421   [[nodiscard]]
    422   ASMJIT_INLINE_NODEBUG uint64_t source_offset() const noexcept { return _source_offset; }
    423 
    424   [[nodiscard]]
    425   ASMJIT_INLINE_NODEBUG uint64_t payload() const noexcept { return _payload; }
    426 
    427   [[nodiscard]]
    428   ASMJIT_INLINE_NODEBUG Expression* payload_as_expression() const noexcept {
    429     return reinterpret_cast<Expression*>(uintptr_t(_payload));
    430   }
    431 
    432   //! \}
    433 };
    434 
    435 //! Label entry provides data stored by \ref CodeHolder for each \ref Label.
    436 //!
    437 //! Label entry is used mostly internall by AsmJit, but it's possibly to use it to query various information about
    438 //! a label. For example to get its type, flags, name, and fixups (if the label is not bound) or offset (if the label
    439 //! is bound).
    440 //!
    441 //! To make the entry small, it's currently split into two data structures - \ref LabelEntry, which is stored in an
    442 //! array as a value, and \ref LabelEntry::ExtraData, which can be pointed to via \ref LabelEntry::_object_data. Extra
    443 //! data of unnamed anonymous labels is shared (and immutable), thus all unnamed anonymous labels would only use
    444 //! \ref LabelEntry (16 bytes per label).
    445 class LabelEntry {
    446 public:
    447   //! Contains extra data that is only created when the label is not anonymous or has a name.
    448   struct ExtraData : public SectionOrLabelEntryExtraHeader {
    449     //! Label parent id or zero.
    450     uint32_t _parent_id;
    451     //! Label name length.
    452     uint32_t _name_size;
    453 
    454     //! Returns a name associated with this extra data - a valid pointer is only returned when the label has a name, which
    455     //! is marked by \ref LabelFlags::kHasName flag.
    456     ASMJIT_INLINE_NODEBUG const char* name() const noexcept { return Support::offset_ptr<char>(this, sizeof(ExtraData)); }
    457   };
    458 
    459   //! \name Members
    460   //! \{
    461 
    462   //! Either references a \ref Section where the label is bound or \ref ExtraData.
    463   SectionOrLabelEntryExtraHeader* _object_data;
    464 
    465   //! Label entry payload.
    466   //!
    467   //! When a Label is bound, `_offset_or_fixups` is the relative offset from the start of the section where
    468   //! the \ref Label has been bound, otherwise `_offset_or_fixups` is a pointer to the first \ref Fixup.
    469   uint64_t _offset_or_fixups;
    470 
    471   //! \}
    472 
    473   //! \name Accessors
    474   //! \{
    475 
    476   //! Returns the type of the label.
    477   //!
    478   //! The type of the label depends on how it was created. Most JIT code uses unnamed anonymous labels created by
    479   //! emitters, for example \ref BaseEmitter::new_label() returns a \ref Label instance having id that was created
    480   //! by \ref CodeHolder::new_label_id.
    481   [[nodiscard]]
    482   ASMJIT_INLINE_NODEBUG LabelType label_type() const noexcept { return _object_data->_internal_label_type; }
    483 
    484   //! Returns label flags.
    485   //!
    486   //! \note Label flags are mostly for internal use, there is probably no reason to use them in user code.
    487   [[nodiscard]]
    488   ASMJIT_INLINE_NODEBUG LabelFlags label_flags() const noexcept { return _object_data->_internal_label_flags; }
    489 
    490   //! Tests whether the label has the given `flag` set.
    491   //!
    492   //! \note Using other getters instead is advised, for example using \ref has_name() and \ref has_parent() is better
    493   //! (and shorter) than checking label flags.
    494   [[nodiscard]]
    495   ASMJIT_INLINE_NODEBUG bool has_label_flag(LabelFlags flag) const noexcept { return Support::test(_object_data->_internal_label_flags, flag); }
    496 
    497   //! Tests whether the LabelEntry has own extra data (see \ref LabelEntry::ExtraData).
    498   //!
    499   //! \note This should only be used by AsmJit for internal purposes. Own extra data means that the LabelEntry has
    500   //! a mutable extra data separately allocated. This information should not be necessary to users as LabelEntry
    501   //! getters should encapsulate label introspection.
    502   [[nodiscard]]
    503   ASMJIT_INLINE_NODEBUG bool _has_own_extra_data() const noexcept { return has_label_flag(LabelFlags::kHasOwnExtraData); }
    504 
    505   //! Tests whether the Label represented by this LabelEntry has a name.
    506   [[nodiscard]]
    507   ASMJIT_INLINE_NODEBUG bool has_name() const noexcept { return has_label_flag(LabelFlags::kHasName); }
    508 
    509   //! Tests whether the Label represented by this LabelEntry has a parent label.
    510   [[nodiscard]]
    511   ASMJIT_INLINE_NODEBUG bool has_parent() const noexcept { return has_label_flag(LabelFlags::kHasParent); }
    512 
    513   //! Tests whether the label represented by this LabelEntry is bound.
    514   //!
    515   //! Bound label means that it has an associated \ref Section and a position in such section. Labels are bound by
    516   //! calling \ref BaseEmitter::bind() method with \ref Label operand.
    517   [[nodiscard]]
    518   ASMJIT_INLINE_NODEBUG bool is_bound() const noexcept { return _object_data->_section_id != Globals::kInvalidId; }
    519 
    520   //! Tests whether the label is bound to a the given `section`.
    521   [[nodiscard]]
    522   ASMJIT_INLINE_NODEBUG bool is_bound_to(const Section* section) const noexcept { return _object_data->_section_id == section->section_id(); }
    523 
    524   //! Tests whether the label is bound to a the given `section_id`.
    525   [[nodiscard]]
    526   ASMJIT_INLINE_NODEBUG bool is_bound_to(uint32_t section_id) const noexcept { return _object_data->_section_id == section_id; }
    527 
    528   //! Returns the section where the label was bound.
    529   //!
    530   //! If the label was not yet bound the return value is `nullptr`.
    531   [[nodiscard]]
    532   ASMJIT_INLINE_NODEBUG uint32_t section_id() const noexcept { return _object_data->_section_id; }
    533 
    534   [[nodiscard]]
    535   ASMJIT_INLINE ExtraData* _own_extra_data() const noexcept {
    536     ASMJIT_ASSERT(_has_own_extra_data());
    537     return static_cast<ExtraData*>(_object_data);
    538   }
    539 
    540   //! Returns label's parent id or \ref Globals::kInvalidId if the label has no parent.
    541   [[nodiscard]]
    542   ASMJIT_INLINE uint32_t parent_id() const noexcept {
    543     return _has_own_extra_data() ? _own_extra_data()->_parent_id : Globals::kInvalidId;
    544   }
    545 
    546   //! Returns the label's name.
    547   //!
    548   //! \note Local labels will return their local name without their parent part, for example ".L1".
    549   [[nodiscard]]
    550   ASMJIT_INLINE_NODEBUG const char* name() const noexcept {
    551     return has_name() ? _own_extra_data()->name() : nullptr;
    552   }
    553 
    554   //! Returns size of label's name.
    555   //!
    556   //! \note Label name is always null terminated, so you can use `strlen()` to get it, however, it's also cached in
    557   //! `LabelEntry` itself, so if you want to know the size the fastest way is to call `LabelEntry::name_size()`.
    558   [[nodiscard]]
    559   ASMJIT_INLINE_NODEBUG uint32_t name_size() const noexcept {
    560     return has_name() ? _own_extra_data()->_name_size : uint32_t(0);
    561   }
    562 
    563   //! Returns unresolved fixups associated with this label.
    564   [[nodiscard]]
    565   ASMJIT_INLINE_NODEBUG bool has_fixups() const noexcept {
    566     return Support::bool_and(!is_bound(), _offset_or_fixups != 0u);
    567   }
    568 
    569   [[nodiscard]]
    570   ASMJIT_INLINE_NODEBUG Fixup* _get_fixups() const noexcept { return reinterpret_cast<Fixup*>(uintptr_t(_offset_or_fixups)); }
    571 
    572   ASMJIT_INLINE_NODEBUG void _set_fixups(Fixup* first) noexcept { _offset_or_fixups = reinterpret_cast<uintptr_t>(first); }
    573 
    574   //! Returns unresolved fixups associated with this label.
    575   [[nodiscard]]
    576   ASMJIT_INLINE_NODEBUG Fixup* unresolved_fixups() const noexcept { return !is_bound() ? _get_fixups() : nullptr; }
    577 
    578   //! Returns the label offset (can only be used after the label is bound).
    579   //!
    580   //! \note This would trigger an assertion failure in debug builds when called on an unbound label. When accessing
    581   //! offsets, always check whether the label is bound. Unbound labels don't have offsets.
    582   [[nodiscard]]
    583   ASMJIT_INLINE uint64_t offset() const noexcept {
    584     ASMJIT_ASSERT(is_bound());
    585     return _offset_or_fixups;
    586   }
    587 
    588   //! \}
    589 };
    590 
    591 //! Holds assembled code and data (including sections, labels, and relocation information).
    592 //!
    593 //! CodeHolder connects emitters with their targets. It provides them interface that can be used to query information
    594 //! about the target environment (architecture, etc...) and API to create labels, sections, relocations, and to write
    595 //! data to a \ref CodeBuffer, which is always part of \ref Section. More than one emitter can be attached to a single
    596 //! CodeHolder instance at a time, which is used in practice
    597 //!
    598 //! CodeHolder provides interface for all emitter types. Assemblers use CodeHolder to write into \ref CodeBuffer, and
    599 //! higher level emitters like Builder and Compiler use CodeHolder to manage labels and sections so higher level code
    600 //! can be serialized to Assembler by \ref BaseEmitter::finalize() and \ref BaseBuilder::serialize_to().
    601 //!
    602 //! In order to use CodeHolder, it must be first initialized by \ref init(). After the CodeHolder has been successfully
    603 //! initialized it can be used to hold assembled code, sections, labels, relocations, and to attach / detach code
    604 //! emitters. After the end of code generation it can be used to query physical locations of labels and to relocate
    605 //! the assembled code into the right address. Please not that calling \ref init() twice doesn't work and would return
    606 //! an error - to reuse CodeHolder it has to be first \ref reset() or reinitialized by calling \ref reinit().
    607 //!
    608 //! Multiple Functions
    609 //! ------------------
    610 //!
    611 //! CodeHolder can be used to hold a single function or multiple functions - when it's holding multiple functions it's
    612 //! considered like a module (or library, or something that provides more than just a single function). When a code is
    613 //! relocated and moved into executable memory, you typically get a single pointer back. When CodeHolder holds a single
    614 //! function, it's the pointer to such function. However, when CodeHolder holds multiple functions, that pointer is
    615 //! basically start of the code, which is usually the first function.
    616 //!
    617 //! In order to get a pointer to more functions, it's necessary to use \ref Label for each function and then to get the
    618 //! offset to each such function via \ref CodeHolder::label_offset_from_base() - which returns an offset, which is relative
    619 //! to the start of the assembled code. When using higher level emitters such as \ref asmjit_compiler labels are created
    620 //! automatically - \ref FuncNode inherits from \ref LabelNode, so a function is a label at the same time.
    621 //!
    622 //! To query and apply an offset, consider the following code, which uses \ref x86::Compiler to create two functions:
    623 //!
    624 //! ```
    625 //! #include <asmjit/x86.h>
    626 //! #include <stdio.h>
    627 //! #include <string.h>
    628 //!
    629 //! int main(int argc, char* argv[]) {
    630 //!   using namespace asmjit;
    631 //!
    632 //!   JitRuntime rt;
    633 //!   CodeHolder code;
    634 //!   code.init(rt.environment());
    635 //!
    636 //!   x86::Compiler cc(&code);
    637 //!
    638 //!   // Generate first function.
    639 //!   FuncNode* func1_node = cc.add_func(FuncSignature::build<uint32_t>());
    640 //!   Label func1_label = func1_node->label();
    641 //!
    642 //!   {
    643 //!     x86::Gp r = cc.new_gp32("r0");
    644 //!     cc.mov(r, 0);
    645 //!     cc.ret(r);
    646 //!     cc.end_func();
    647 //!   }
    648 //!
    649 //!   // Generate second function.
    650 //!   FuncNode* func2_node = cc.add_func(FuncSignature::build<uint32_t>());
    651 //!   Label func2_label = func2_node->label();
    652 //!
    653 //!   {
    654 //!     x86::Gp r = cc.new_gp32("r1");
    655 //!     cc.mov(r, 1);
    656 //!     cc.ret(r);
    657 //!     cc.end_func();
    658 //!   }
    659 //!
    660 //!   // Finalize the generated code - this would also call `serialize_to()`.
    661 //!   Error err = cc.finalize();
    662 //!   if (err != Error::kOk) {
    663 //!     printf("ERROR during finalization: %s\n", DebugUtils::error_as_string(err));
    664 //!     return 1;
    665 //!   }
    666 //!
    667 //!   // We have deliberately used void* as a pointer type as it's start of an assembled module.
    668 //!   void* module;
    669 //!   err = rt.add(&module, &code);
    670 //!
    671 //!   if (err != Error::kOk) {
    672 //!     printf("ERROR during allocation/relocation: %s\n", DebugUtils::error_as_string(err));
    673 //!     return 1;
    674 //!   }
    675 //!
    676 //!   // Normally both CodeHolder and Compiler are not needed after the code has been finalized
    677 //!   // and allocated/relocated into an executable memory. However, in order to get the required
    678 //!   // offsets it's necessary to query CodeHolder for positions in code, and to get these it's
    679 //!   // required to either have `FuncNode` or `Label`.
    680 //!   size_t func1_offset = code.label_offset_from_base(func1_label);
    681 //!   size_t func2_offset = code.label_offset_from_base(func2_label);
    682 //!
    683 //!   using Fn = uint32_t(*)(void);
    684 //!
    685 //!   Fn fn1 = ptr_as_func<Fn>(module, func1_offset);
    686 //!   Fn fn2 = ptr_as_func<Fn>(module, func2_offset);
    687 //!
    688 //!   printf("fn1()=%u fn2()=%u\n", fn1(), fn2());
    689 //!
    690 //!   // The module has to be released at once - individual functions cannot be released.
    691 //!   rt.release(module);
    692 //!
    693 //!   return 0;
    694 //! }
    695 //! ```
    696 //!
    697 //! CodeHolder Reusability
    698 //! ----------------------
    699 //!
    700 //! If you intend to generate a lot of code, or tiny code, it's advised to reuse CodeHolder and emitter instances.
    701 //! There are currently two ways of reusing CodeHolder and emitters - one is using \ref CodeHolder::init() followed
    702 //! by \ref CodeHolder::reset(), and another is initializing once by \ref CodeHolder::init() and then reinitializing
    703 //! by \ref CodeHolder::reinit(). The first strategy is shown below:
    704 //!
    705 //! ```
    706 //! // All of them will be reused for code generation by using an 'init()/reset()' strategy.
    707 //! Environment env = ...; // Environment to use, for example from JitRuntime.
    708 //! CodeHolder code;       // CodeHolder to reuse (all allocated memory will be held by it until it's destroyed).
    709 //! x86::Compiler cc;      // Emitter to reuse (for example x86::Compiler).
    710 //!
    711 //! for (size_t i = 0; i < ...; i++) {
    712 //!   // Initialize the CodeHolder first.
    713 //!   code.init(env);
    714 //!   code.attach(&emitter);
    715 //!
    716 //!   [[code generation as usual]]
    717 //!
    718 //!   code.reset();
    719 //! }
    720 //! ```
    721 //!
    722 //! While this approach is good for many use-cases, there is even a faster strategy called reinitialization, which is
    723 //! provided by \ref CodeHolder::reinit(). The idea of reinit is to reinitialize the CodeHolder into a state, which
    724 //! was achieved by initializing it by \ref CodeHolder::init(), by optionally attaching \ref Logger, \ref ErrorHandler,
    725 //! and emitters of any kind. See an example below:
    726 //!
    727 //! ```
    728 //! // All of them will be reused for code generation by using a 'reinit()' strategy.
    729 //! Environment env = ...; // Environment to use, for example from JitRuntime.
    730 //! CodeHolder code;       // CodeHolder to reuse (all allocated memory will be held by it until it's destroyed).
    731 //! x86::Compiler cc;      // Emitter to reuse (for example x86::Compiler).
    732 //!
    733 //! // Initialize the CodeHolder and attach emitters to it (attaching ErrorHandler is advised!)
    734 //! code.init(env);
    735 //! code.attach(&emitter);
    736 //!
    737 //! for (size_t i = 0; i < ...; i++) {
    738 //!   [[code generation as usual]]
    739 //!
    740 //!   // Optionally you can start the loop with 'code.reinit()', but this is cleaner as it wipes out all intermediate
    741 //!   // states of CodeHolder and the attached emitters. It won't detach Logger, ErrorHandler, nor attached emitters.
    742 //!   code.reinit();
    743 //! }
    744 //! ```
    745 //!
    746 //! \note \ref CodeHolder has an ability to attach an \ref ErrorHandler, however, the error handler is not triggered
    747 //! by \ref CodeHolder itself, it's instead propagated to all emitters that attach to it.
    748 class CodeHolder {
    749 public:
    750   ASMJIT_NONCOPYABLE(CodeHolder)
    751 
    752   //! \name Types
    753   //! \{
    754 
    755   //! \cond INTERNAL
    756   struct NamedLabelExtraData : public ArenaHashNode {
    757     LabelEntry::ExtraData extra_data;
    758 
    759     ASMJIT_INLINE_NODEBUG uint32_t label_id() const noexcept { return _custom_data; }
    760   };
    761   //! \endcond
    762 
    763   //! An informative data structure that is filled with some details that happened during \ref relocate_to_base().
    764   struct RelocationSummary {
    765     //! The number of bytes the final code has been reduced by.
    766     //!
    767     //! At the moment this is the same as the number of bytes that the address table was shrunk, because it was
    768     //! possible to avoid certain entries during relocation - the functions that would be otherwise present were
    769     //! close enough to avoid them in the .addrtab section.
    770     size_t code_size_reduction;
    771   };
    772 
    773   //! \}
    774 
    775   //! \name Members
    776   //! \{
    777 
    778   //! Environment information.
    779   Environment _environment;
    780   //! CPU features of the target architecture.
    781   CpuFeatures _cpu_features;
    782   //! Base address or \ref Globals::kNoBaseAddress.
    783   uint64_t _base_address;
    784 
    785   //! Attached `Logger`, used by all consumers.
    786   Logger* _logger;
    787   //! Attached `ErrorHandler`.
    788   ErrorHandler* _error_handler;
    789 
    790   //! Arena allocator used to allocate core structures.
    791   Arena _arena;
    792 
    793   //! First emitter attached to this CodeHolder (double-linked list).
    794   BaseEmitter* _attached_first;
    795   //! Last emitter attached to this CodeHolder (double-linked list).
    796   BaseEmitter* _attached_last;
    797 
    798   //! Section entries.
    799   ArenaVector<Section*> _sections;
    800   //! Section entries sorted by section order and then section id.
    801   ArenaVector<Section*> _sections_by_order;
    802 
    803   //! Label entries.
    804   ArenaVector<LabelEntry> _label_entries;
    805   //! Relocation entries.
    806   ArenaVector<RelocEntry*> _relocations;
    807   //! Label name -> LabelEntry::ExtraData (only used by labels that have a name and are not anonymous).
    808   ArenaHash<NamedLabelExtraData> _named_labels;
    809   //! Unresolved fixups that are most likely references across sections.
    810   Fixup* _fixups;
    811   //! Pool containing \ref Fixup instances for quickly recycling them.
    812   ArenaPool<Fixup> _fixup_data_pool;
    813   //! Count of unresolved fixups of unbound labels (at the end of assembling this should be zero).
    814   size_t _unresolved_fixup_count;
    815 
    816   //! Text section - always one part of a CodeHolder itself.
    817   Section _text_section;
    818 
    819   //! Pointer to an address table section (or null if this section doesn't exist).
    820   Section* _address_table_section;
    821   //! Address table entries.
    822   ArenaTree<AddressTableEntry> _address_table_entries;
    823 
    824   //! \}
    825 
    826   //! \name Construction & Destruction
    827   //! \{
    828 
    829   //! Creates an uninitialized CodeHolder (you must init() it before it can be used).
    830   //!
    831   //! An optional `temporary` argument can be used to initialize the first block of \ref Arena
    832   //! that \ref CodeHolder uses into a temporary memory provided by the user.
    833   ASMJIT_API explicit CodeHolder(Span<uint8_t> static_arena_memory = Span<uint8_t>{}) noexcept;
    834 
    835   //! Destroys the CodeHolder and frees all resources it has allocated.
    836   ASMJIT_API ~CodeHolder() noexcept;
    837 
    838   //! Tests whether the `CodeHolder` has been initialized.
    839   //!
    840   //! Emitters can be only attached to initialized `CodeHolder` instances.
    841   [[nodiscard]]
    842   ASMJIT_INLINE_NODEBUG bool is_initialized() const noexcept { return _environment.is_initialized(); }
    843 
    844   //! Initializes CodeHolder to hold code described by the given `environment` and `base_address`.
    845   ASMJIT_API Error init(const Environment& environment, uint64_t base_address = Globals::kNoBaseAddress) noexcept;
    846   //! Initializes CodeHolder to hold code described by the given `environment`, `cpu_features`, and `base_address`.
    847   ASMJIT_API Error init(const Environment& environment, const CpuFeatures& cpu_features, uint64_t base_address = Globals::kNoBaseAddress) noexcept;
    848 
    849   //! Reinitializes CodeHolder with the same environment, cpu features, and base address as it had, and notifies
    850   //! all attached emitters of reinitialization. If the \ref CodeHolder was not initialized, \ref Error::kNotInitialized
    851   //! is returned.
    852   //!
    853   //! Reinitialization is designed to be a faster alternative compared to \ref reset() followed by \ref init() chain.
    854   //! The purpose of reinitialization is a very quick reuse of \ref CodeHolder and all attached emitters (most likely
    855   //! Assembler or Compiler) without paying the cost of complete initialization and then assignment of all the loggers,
    856   //! error handlers, and emitters.
    857   //!
    858   //! \note Semantically reinit() is the same as using \ref reset() with \ref ResetPolicy::kSoft parameter followed by
    859   //! \ref init(), and then by attaching loggers, error handlers, and emitters that were attached previously. This
    860   //! means that after reinitialization you will get a clean and ready for use \ref CodeHolder, which was initialized
    861   //! the same way as before.
    862   ASMJIT_API Error reinit() noexcept;
    863 
    864   //! Detaches all code-generators attached and resets the `CodeHolder`.
    865   ASMJIT_API void reset(ResetPolicy reset_policy = ResetPolicy::kSoft) noexcept;
    866 
    867   //! \}
    868 
    869   //! \name Attach & Detach
    870   //! \{
    871 
    872   //! Attaches an emitter to this `CodeHolder`.
    873   ASMJIT_API Error attach(BaseEmitter* emitter) noexcept;
    874   //! Detaches an emitter from this `CodeHolder`.
    875   ASMJIT_API Error detach(BaseEmitter* emitter) noexcept;
    876 
    877   //! \}
    878 
    879   //! \name Memory Allocators
    880   //! \{
    881 
    882   //! Returns the allocator that the `CodeHolder` uses.
    883   //!
    884   //! \note This should be only used for AsmJit's purposes. Code holder uses arena allocator to allocate everything,
    885   //! so anything allocated through this allocator will be invalidated by \ref CodeHolder::reset() or by CodeHolder's
    886   //! destructor.
    887   [[nodiscard]]
    888   ASMJIT_INLINE_NODEBUG Arena& arena() const noexcept { return const_cast<Arena&>(_arena); }
    889 
    890   //! \}
    891 
    892   //! \name Code & Architecture
    893   //! \{
    894 
    895   //! Returns the target environment information.
    896   [[nodiscard]]
    897   ASMJIT_INLINE_NODEBUG const Environment& environment() const noexcept { return _environment; }
    898 
    899   //! Returns the target architecture.
    900   [[nodiscard]]
    901   ASMJIT_INLINE_NODEBUG Arch arch() const noexcept { return environment().arch(); }
    902 
    903   //! Returns the target sub-architecture.
    904   [[nodiscard]]
    905   ASMJIT_INLINE_NODEBUG SubArch sub_arch() const noexcept { return environment().sub_arch(); }
    906 
    907   //! Returns the minimum CPU features of the target architecture.
    908   [[nodiscard]]
    909   ASMJIT_INLINE_NODEBUG const CpuFeatures& cpu_features() const noexcept { return _cpu_features; }
    910 
    911   //! Tests whether a static base-address is set.
    912   [[nodiscard]]
    913   ASMJIT_INLINE_NODEBUG bool has_base_address() const noexcept { return _base_address != Globals::kNoBaseAddress; }
    914 
    915   //! Returns a static base-address or \ref Globals::kNoBaseAddress, if not set.
    916   [[nodiscard]]
    917   ASMJIT_INLINE_NODEBUG uint64_t base_address() const noexcept { return _base_address; }
    918 
    919   //! \}
    920 
    921   //! \name Attached Emitters
    922   //! \{
    923 
    924   //! Returns a vector of attached emitters.
    925   [[nodiscard]]
    926   ASMJIT_INLINE_NODEBUG BaseEmitter* attached_first() noexcept { return _attached_first; }
    927 
    928   [[nodiscard]]
    929   ASMJIT_INLINE_NODEBUG BaseEmitter* attached_last() noexcept { return _attached_last; }
    930 
    931   [[nodiscard]]
    932   ASMJIT_INLINE_NODEBUG const BaseEmitter* attached_first() const noexcept { return _attached_first; }
    933 
    934   [[nodiscard]]
    935   ASMJIT_INLINE_NODEBUG const BaseEmitter* attached_last() const noexcept { return _attached_last; }
    936 
    937   //! \}
    938 
    939   //! \name Logging
    940   //! \{
    941 
    942   //! Returns the attached logger.
    943   [[nodiscard]]
    944   ASMJIT_INLINE_NODEBUG Logger* logger() const noexcept { return _logger; }
    945 
    946   //! Attaches a `logger` to CodeHolder and propagates it to all attached emitters.
    947   ASMJIT_API void set_logger(Logger* logger) noexcept;
    948 
    949   //! Resets the logger to none.
    950   ASMJIT_INLINE_NODEBUG void reset_logger() noexcept { set_logger(nullptr); }
    951 
    952   //! \name Error Handling
    953   //! \{
    954 
    955   //! Tests whether the CodeHolder has an attached error handler, see \ref ErrorHandler.
    956   [[nodiscard]]
    957   ASMJIT_INLINE_NODEBUG bool has_error_handler() const noexcept { return _error_handler != nullptr; }
    958 
    959   //! Returns the attached error handler.
    960   [[nodiscard]]
    961   ASMJIT_INLINE_NODEBUG ErrorHandler* error_handler() const noexcept { return _error_handler; }
    962 
    963   //! Attach an error handler to this `CodeHolder`.
    964   ASMJIT_API void set_error_handler(ErrorHandler* error_handler) noexcept;
    965 
    966   //! Resets the error handler to none.
    967   ASMJIT_INLINE_NODEBUG void reset_error_handler() noexcept { set_error_handler(nullptr); }
    968 
    969   //! \}
    970 
    971   //! \name Code Buffer
    972   //! \{
    973 
    974   //! Makes sure that at least `n` bytes can be added to CodeHolder's buffer `cb`.
    975   //!
    976   //! \note The buffer `cb` must be managed by `CodeHolder` - otherwise the behavior of the function is undefined.
    977   ASMJIT_API Error grow_buffer(CodeBuffer* cb, size_t n) noexcept;
    978 
    979   //! Reserves the size of `cb` to at least `n` bytes.
    980   //!
    981   //! \note The buffer `cb` must be managed by `CodeHolder` - otherwise the behavior of the function is undefined.
    982   ASMJIT_API Error reserve_buffer(CodeBuffer* cb, size_t n) noexcept;
    983 
    984   //! \}
    985 
    986   //! \name Sections
    987   //! \{
    988 
    989   //! Returns an array of `Section*` records.
    990   [[nodiscard]]
    991   ASMJIT_INLINE_NODEBUG Span<Section*> sections() const noexcept { return _sections.as_span(); }
    992 
    993   //! Returns an array of `Section*` records sorted according to section order first, then section id.
    994   [[nodiscard]]
    995   ASMJIT_INLINE_NODEBUG Span<Section*> sections_by_order() const noexcept { return _sections_by_order.as_span(); }
    996 
    997   //! Returns the number of sections.
    998   [[nodiscard]]
    999   ASMJIT_INLINE_NODEBUG size_t section_count() const noexcept { return _sections.size(); }
   1000 
   1001   //! Tests whether the given `section_id` is valid.
   1002   [[nodiscard]]
   1003   ASMJIT_INLINE_NODEBUG bool is_section_valid(uint32_t section_id) const noexcept { return section_id < _sections.size(); }
   1004 
   1005   //! Creates a new section and return its pointer in `section_out`.
   1006   //!
   1007   //! Returns `Error`, does not report a possible error to `ErrorHandler`.
   1008   ASMJIT_API Error new_section(Out<Section*> section_out, const char* name, size_t name_size = SIZE_MAX, SectionFlags flags = SectionFlags::kNone, uint32_t alignment = 1, int32_t order = 0) noexcept;
   1009 
   1010   //! Returns a section entry of the given index.
   1011   [[nodiscard]]
   1012   ASMJIT_INLINE_NODEBUG Section* section_by_id(uint32_t section_id) const noexcept { return _sections[section_id]; }
   1013 
   1014   //! Returns section-id that matches the given `name`.
   1015   //!
   1016   //! If there is no such section `Section::kInvalidId` is returned.
   1017   [[nodiscard]]
   1018   ASMJIT_API Section* section_by_name(const char* name, size_t name_size = SIZE_MAX) const noexcept;
   1019 
   1020   //! Returns '.text' section (section that commonly represents code).
   1021   //!
   1022   //! \note Text section is always the first section in \ref CodeHolder::sections() array.
   1023   [[nodiscard]]
   1024   ASMJIT_INLINE_NODEBUG Section* text_section() const noexcept { return _sections[0]; }
   1025 
   1026   //! Tests whether '.addrtab' section exists.
   1027   [[nodiscard]]
   1028   ASMJIT_INLINE_NODEBUG bool has_address_table_section() const noexcept { return _address_table_section != nullptr; }
   1029 
   1030   //! Returns '.addrtab' section.
   1031   //!
   1032   //! This section is used exclusively by AsmJit to store absolute 64-bit
   1033   //! addresses that cannot be encoded in instructions like 'jmp' or 'call'.
   1034   //!
   1035   //! \note This section is created on demand, the returned pointer can be null.
   1036   [[nodiscard]]
   1037   ASMJIT_INLINE_NODEBUG Section* address_table_section() const noexcept { return _address_table_section; }
   1038 
   1039   //! Ensures that '.addrtab' section exists (creates it if it doesn't) and
   1040   //! returns it. Can return `nullptr` on out of memory condition.
   1041   [[nodiscard]]
   1042   ASMJIT_API Section* ensure_address_table_section() noexcept;
   1043 
   1044   //! Used to add an address to an address table.
   1045   //!
   1046   //! This implicitly calls `ensure_address_table_section()` and then creates `AddressTableEntry` that is inserted
   1047   //! to `_address_table_entries`. If the address already exists this operation does nothing as the same addresses
   1048   //! use the same slot.
   1049   //!
   1050   //! This function should be considered internal as it's used by assemblers to insert an absolute address into the
   1051   //! address table. Inserting address into address table without creating a particular relocation entry makes no sense.
   1052   ASMJIT_API Error add_address_to_address_table(uint64_t address) noexcept;
   1053 
   1054   //! \}
   1055 
   1056   //! \name Labels & Symbols
   1057   //! \{
   1058 
   1059   //! Returns array of `LabelEntry` records.
   1060   [[nodiscard]]
   1061   ASMJIT_INLINE_NODEBUG Span<LabelEntry> label_entries() const noexcept { return _label_entries.as_span(); }
   1062 
   1063   //! Returns number of labels created.
   1064   [[nodiscard]]
   1065   ASMJIT_INLINE_NODEBUG size_t label_count() const noexcept { return _label_entries.size(); }
   1066 
   1067   //! Tests whether the label having `label_id` is valid (i.e. created by `new_label_id()`).
   1068   [[nodiscard]]
   1069   ASMJIT_INLINE_NODEBUG bool is_label_valid(uint32_t label_id) const noexcept {
   1070     return label_id < _label_entries.size();
   1071   }
   1072 
   1073   //! Tests whether the `label` is valid (i.e. created by `new_label_id()`).
   1074   [[nodiscard]]
   1075   ASMJIT_INLINE_NODEBUG bool is_label_valid(const Label& label) const noexcept {
   1076     return is_label_valid(label.id());
   1077   }
   1078 
   1079   //! Tests whether a label having `label_id` is already bound.
   1080   //!
   1081   //! Returns `false` if the `label_id` is not valid.
   1082   [[nodiscard]]
   1083   ASMJIT_INLINE_NODEBUG bool is_label_bound(uint32_t label_id) const noexcept {
   1084     return is_label_valid(label_id) && _label_entries[label_id].is_bound();
   1085   }
   1086 
   1087   //! Tests whether the `label` is already bound.
   1088   //!
   1089   //! Returns `false` if the `label` is not valid.
   1090   [[nodiscard]]
   1091   ASMJIT_INLINE_NODEBUG bool is_label_bound(const Label& label) const noexcept {
   1092     return is_label_bound(label.id());
   1093   }
   1094 
   1095   //! Returns LabelEntry of the given label identifier `label_id` (or `label` if you are using overloads).
   1096   //!
   1097   //! \attention The passed `label_id` must be valid as it's used as an index to `_label_entries[]` array. In debug
   1098   //! builds the array access uses an assertion, but such assertion is not present in release builds. To get whether
   1099   //! a label is valid, check out \ref CodeHolder::is_label_valid() function.
   1100   [[nodiscard]]
   1101   ASMJIT_INLINE_NODEBUG LabelEntry& label_entry_of(uint32_t label_id) noexcept {
   1102     return _label_entries[label_id];
   1103   }
   1104 
   1105   //! \overload
   1106   [[nodiscard]]
   1107   ASMJIT_INLINE_NODEBUG const LabelEntry& label_entry_of(uint32_t label_id) const noexcept {
   1108     return _label_entries[label_id];
   1109   }
   1110 
   1111   //! \overload
   1112   [[nodiscard]]
   1113   ASMJIT_INLINE_NODEBUG LabelEntry& label_entry_of(const Label& label) noexcept {
   1114     return label_entry_of(label.id());
   1115   }
   1116 
   1117   //! \overload
   1118   [[nodiscard]]
   1119   ASMJIT_INLINE_NODEBUG const LabelEntry& label_entry_of(const Label& label) const noexcept {
   1120     return label_entry_of(label.id());
   1121   }
   1122 
   1123   //! Returns offset of a `Label` by its `label_id`.
   1124   //!
   1125   //! The offset returned is relative to the start of the section where the label is bound. Zero offset is returned
   1126   //! for unbound labels, which is their initial offset value.
   1127   //!
   1128   //! \attention The passed `label_id` must be valid as it's used as an index to `_label_entries[]` array. In debug
   1129   //! builds the array access uses an assertion, but such assertion is not present in release builds. To get whether
   1130   //! a label is valid, check out \ref CodeHolder::is_label_valid() function.
   1131   [[nodiscard]]
   1132   ASMJIT_INLINE_NODEBUG uint64_t label_offset(uint32_t label_id) const noexcept {
   1133     ASMJIT_ASSERT(is_label_valid(label_id));
   1134     return _label_entries[label_id].offset();
   1135   }
   1136 
   1137   //! \overload
   1138   [[nodiscard]]
   1139   ASMJIT_INLINE_NODEBUG uint64_t label_offset(const Label& label) const noexcept {
   1140     return label_offset(label.id());
   1141   }
   1142 
   1143   //! Returns offset of a label by it's `label_id` relative to the base offset.
   1144   //!
   1145   //! \attention The passed `label_id` must be valid as it's used as an index to `_label_entries[]` array. In debug
   1146   //! builds the array access uses an assertion, but such assertion is not present in release builds. To get whether
   1147   //! a label is valid, check out \ref CodeHolder::is_label_valid() function.
   1148   //!
   1149   //! \note The offset of the section where the label is bound must be valid in order to use this function, otherwise
   1150   //! the value returned will not be reliable. Typically, sections have offsets when they are flattened, see \ref
   1151   //! CodeHolder::flatten() function for more details.
   1152   [[nodiscard]]
   1153   inline uint64_t label_offset_from_base(uint32_t label_id) const noexcept {
   1154     ASMJIT_ASSERT(is_label_valid(label_id));
   1155 
   1156     const LabelEntry& le = _label_entries[label_id];
   1157     return (le.is_bound() ? _sections[le.section_id()]->offset() : uint64_t(0)) + le.offset();
   1158   }
   1159 
   1160   //! \overload
   1161   [[nodiscard]]
   1162   inline uint64_t label_offset_from_base(const Label& label) const noexcept {
   1163     return label_offset_from_base(label.id());
   1164   }
   1165 
   1166   //! Creates a new anonymous label and return its id in `label_id_out`.
   1167   //!
   1168   //! Returns `Error`, does not report error to `ErrorHandler`.
   1169   [[nodiscard]]
   1170   ASMJIT_API Error new_label_id(Out<uint32_t> label_id_out) noexcept;
   1171 
   1172   //! Creates a new named \ref LabelEntry of the given label `type`.
   1173   //!
   1174   //! \param label_id_out Where to store the created \ref Label id.
   1175   //! \param name The name of the label.
   1176   //! \param name_size The length of `name` argument, or `SIZE_MAX` if `name` is a null terminated string, which
   1177   //!        means that the `CodeHolder` will use `strlen()` to determine the length.
   1178   //! \param type The type of the label to create, see \ref LabelType.
   1179   //! \param parent_id Parent id of a local label, otherwise it must be \ref Globals::kInvalidId.
   1180   //! \retval Always returns \ref Error, does not report a possible error to the attached \ref ErrorHandler.
   1181   //!
   1182   //! AsmJit has a support for local labels (\ref LabelType::kLocal) which require a parent label id (parent_id).
   1183   //! The names of local labels can conflict with names of other local labels that have a different parent. In
   1184   //! addition, AsmJit supports named anonymous labels, which are useful only for debugging purposes as the
   1185   //! anonymous name will have a name, which will be formatted, but the label itself cannot be queried by such
   1186   //! name.
   1187   [[nodiscard]]
   1188   ASMJIT_API Error new_named_label_id(Out<uint32_t> label_id_out, const char* name, size_t name_size, LabelType type, uint32_t parent_id = Globals::kInvalidId) noexcept;
   1189 
   1190   //! Returns a label by name.
   1191   //!
   1192   //! \remarks If the named label doesn't exist a default constructed \ref Label is returned, which has its id set
   1193   //! to \ref Globals::kInvalidId. In other words, this function doesn't create new labels, it can only be used to
   1194   //! query an existing \ref Label by name.
   1195   [[nodiscard]]
   1196   ASMJIT_INLINE_NODEBUG Label label_by_name(const char* name, size_t name_size = SIZE_MAX, uint32_t parent_id = Globals::kInvalidId) noexcept {
   1197     return Label(label_id_by_name(name, name_size, parent_id));
   1198   }
   1199 
   1200   //! \overload
   1201   [[nodiscard]]
   1202   ASMJIT_API Label label_by_name(Span<const char> name, uint32_t parent_id = Globals::kInvalidId) noexcept {
   1203     return label_by_name(name.data(), name.size(), parent_id);
   1204   }
   1205 
   1206   //! Returns a label id by name.
   1207   //!
   1208   //! \remarks If the named label doesn't exist \ref Globals::kInvalidId is returned. In other words, this function
   1209   //! doesn't create new labels, it can only be used to query an existing label identifier by name.
   1210   [[nodiscard]]
   1211   ASMJIT_API uint32_t label_id_by_name(const char* name, size_t name_size = SIZE_MAX, uint32_t parent_id = Globals::kInvalidId) noexcept;
   1212 
   1213   //! \overload
   1214   [[nodiscard]]
   1215   ASMJIT_API uint32_t label_id_by_name(Span<const char> name, uint32_t parent_id = Globals::kInvalidId) noexcept {
   1216     return label_id_by_name(name.data(), name.size(), parent_id);
   1217   }
   1218 
   1219   //! Tests whether there are any unresolved fixups related to unbound labels.
   1220   [[nodiscard]]
   1221   ASMJIT_INLINE_NODEBUG bool has_unresolved_fixups() const noexcept { return _unresolved_fixup_count != 0u; }
   1222 
   1223   //! Returns the number of unresolved fixups.
   1224   [[nodiscard]]
   1225   ASMJIT_INLINE_NODEBUG size_t unresolved_fixup_count() const noexcept { return _unresolved_fixup_count; }
   1226 
   1227   //! Creates a new label-link used to store information about yet unbound labels.
   1228   //!
   1229   //! Returns `null` if the allocation failed.
   1230   [[nodiscard]]
   1231   ASMJIT_API Fixup* new_fixup(LabelEntry& le, uint32_t section_id, size_t offset, intptr_t rel, const OffsetFormat& format) noexcept;
   1232 
   1233   //! Resolves cross-section fixups associated with each label that was used as a destination in code of a different
   1234   //! section. It's only useful to people that use multiple sections as it will do nothing if the code only contains
   1235   //! a single section in which cross-section fixups are not possible.
   1236   ASMJIT_API Error resolve_cross_section_fixups() noexcept;
   1237 
   1238   //! Binds a label to a given `section_id` and `offset` (relative to start of the section).
   1239   //!
   1240   //! This function is generally used by `BaseAssembler::bind()` to do the heavy lifting.
   1241   ASMJIT_API Error bind_label(const Label& label, uint32_t section_id, uint64_t offset) noexcept;
   1242 
   1243   //! \}
   1244 
   1245   //! \name Relocations
   1246   //! \{
   1247 
   1248   //! Tests whether the code contains relocation entries.
   1249   [[nodiscard]]
   1250   ASMJIT_INLINE_NODEBUG bool has_reloc_entries() const noexcept { return !_relocations.is_empty(); }
   1251 
   1252   //! Returns array of `RelocEntry*` records.
   1253   [[nodiscard]]
   1254   ASMJIT_INLINE_NODEBUG Span<RelocEntry*> reloc_entries() const noexcept { return _relocations.as_span(); }
   1255 
   1256   //! Returns a RelocEntry of the given `id`.
   1257   [[nodiscard]]
   1258   ASMJIT_INLINE_NODEBUG RelocEntry* reloc_entry_of(uint32_t id) const noexcept { return _relocations[id]; }
   1259 
   1260   //! Creates a new relocation entry of type `reloc_type`.
   1261   //!
   1262   //! Additional fields can be set after the relocation entry was created.
   1263   [[nodiscard]]
   1264   ASMJIT_API Error new_reloc_entry(Out<RelocEntry*> dst, RelocType reloc_type) noexcept;
   1265 
   1266   //! \}
   1267 
   1268   //! \name Utilities
   1269   //! \{
   1270 
   1271   //! Flattens all sections by recalculating their offsets, starting at 0.
   1272   //!
   1273   //! \note This should never be called more than once.
   1274   ASMJIT_API Error flatten() noexcept;
   1275 
   1276   //! Returns computed the size of code & data of all sections.
   1277   //!
   1278   //! \note All sections will be iterated over and the code size returned would represent the minimum code size of
   1279   //! all combined sections after applying minimum alignment. Code size may decrease after calling `flatten()` and
   1280   //! `relocate_to_base()`.
   1281   [[nodiscard]]
   1282   ASMJIT_API size_t code_size() const noexcept;
   1283 
   1284   //! Relocates the code to the given `base_address`.
   1285   //!
   1286   //! \param base_address Absolute base address where the code will be relocated to. Please note that nothing is
   1287   //! copied to such base address, it's just an absolute value used by the relocation code to resolve all stored
   1288   //! relocations.
   1289   //!
   1290   //! \param summary_out Optional argument that can be used to get back information about the relocation.
   1291   //!
   1292   //! \note This should never be called more than once.
   1293   ASMJIT_API Error relocate_to_base(uint64_t base_address, RelocationSummary* summary_out = nullptr) noexcept;
   1294 
   1295   //! Copies a single section into `dst`.
   1296   ASMJIT_API Error copy_section_data(void* dst, size_t dst_size, uint32_t section_id, CopySectionFlags copy_flags = CopySectionFlags::kNone) noexcept;
   1297 
   1298   //! Copies all sections into `dst`.
   1299   //!
   1300   //! This should only be used if the data was flattened and there are no gaps between the sections. The `dst_size`
   1301   //! is always checked and the copy will never write anything outside the provided buffer.
   1302   ASMJIT_API Error copy_flattened_data(void* dst, size_t dst_size, CopySectionFlags copy_flags = CopySectionFlags::kNone) noexcept;
   1303 
   1304   //! \}
   1305 };
   1306 
   1307 //! \}
   1308 
   1309 ASMJIT_END_NAMESPACE
   1310 
   1311 #endif // ASMJIT_CORE_CODEHOLDER_H_INCLUDED