Low-Latency Machine Code Generation
Globals, code storage, and emitter interface.
To generate code you would need to instantiate at least the following classes:
There are also other core classes that are important:
AsmJit also provides global constants:
The example below shows how the mentioned classes interact to generate X86 code:
The example above used x86::Assembler as an emitter. AsmJit provides the following emitters that offer various levels of abstraction:
AsmJit's Target is an interface that provides basic target abstraction. At the moment AsmJit provides only one implementation called JitRuntime, which as the name suggests provides JIT code target and execution runtime. JitRuntime provides all the necessary stuff to implement a simple JIT compiler with basic memory management. It only provides JitRuntime::add() and JitRuntime::release() functions that are used to either add code to the runtime or release it. JitRuntime doesn't do any decisions on when the code should be released, the decision is up to the developer.
See more at Virtual Memory group.
In the previous example the Environment is retrieved from JitRuntime. It's logical as JitRuntime always returns an Environment that is compatible with the host. For example if your application runs on X86_64 CPU the Environment returned will use Arch::kX64 architecture in contrast to Arch::kX86, which will be used in 32-bit mode on an X86 target.
AsmJit allows to setup the Environment manually and to select a different architecture and ABI when necessary. So let's do something else this time, let's always generate a 32-bit code and print its binary representation. To do that, we can create our own Environment and initialize it to Arch::kX86.
In addition to Environment, CodeHolder can be configured to specify a base-address (or a virtual base address in a linker terminology), which could be static (useful when you know the location where the target's machine code will be) or dynamic. AsmJit assumes dynamic base-address by default and relocates the code held by CodeHolder to a user provided address on-demand. To be able to relocate to a user provided address it needs to store some information about relocations, which is represented by RelocEntry. Relocation entries are only required if you call external functions from the generated code that cannot be encoded by using a 32-bit displacement (64-bit displacements are not provided by aby supported architecture).
There is also a concept called LabelLink - label link is a lightweight data structure that doesn't have any identifier and is stored in LabelEntry as a single-linked list. Label link represents either unbound yet used label and cross-sections links (only relevant to code that uses multiple sections). Since crossing sections is something that cannot be resolved immediately these links persist until offsets of these sections are assigned and until CodeHolder::resolveUnresolvedLinks() is called. It's an error if you end up with code that has unresolved label links after flattening. You can verify it by calling CodeHolder::hasUnresolvedLinks(), which inspects the value returned by CodeHolder::unresolvedLinkCount().
AsmJit can flatten code that uses multiple sections by assigning each section an incrementing offset that respects its alignment. Use CodeHolder::flatten() to do that. After the sections are flattened their offsets and virtual sizes are adjusted to respect each section's buffer size and alignment. The CodeHolder::resolveUnresolvedLinks() function must be called before relocating the code held by CodeHolder. You can also flatten your code manually by iterating over all sections and calculating their offsets (relative to base) by your own algorithm. In that case CodeHolder::flatten() should not be called, however, CodeHolder::resolveUnresolvedLinks() should be.
The example below shows how to use a built-in virtual memory allocator JitAllocator instead of using JitRuntime (just in case you want to use your own memory management) and how to relocate the generated code into your own memory block - you can use your own virtual memory allocator if you prefer that, but that's OS specific and not covered by the documentation.
The following code is similar to the previous one, but implements a function working in both 32-bit and 64-bit environments:
If you know the base-address in advance (before the code generation) it can be passed as a second argument to CodeHolder::init(). In that case the Assembler will know the absolute position of each instruction and would be able to use it during instruction encoding to prevent relocations where possible. The following example shows how to configure the base address:
When a label that is not yet bound is used by the Assembler, it creates a LabelLink, which is then added to a LabelEntry. These links are also created if a label is used in a different section than in which it was bound. Let's examine some functions that can be used to check whether there are any unresolved links.
There is no function that would return the number of unbound labels as this is completely unimportant from CodeHolder's perspective. If a label is not used then it doesn't matter whether it's bound or not, only actually used labels matter. After a Label is bound it's possible to query its offset offset relative to the start of the section where it was bound:
AsmJit allows to create multiple sections within the same CodeHolder. A test-case asmjit_test_x86_sections.cpp can be used as a reference point although the following example should also provide a useful insight:
The last line in the example above shows that a LabelLink would be created even for bound labels that cross sections. In this case a referenced label was bound in another section, which means that the link couldn't be resolved at that moment. If your code uses sections, but you wish AsmJit to flatten these sections (you don't plan to flatten them manually) then there is an API for that.
Makes a 32-bit integer that represents AsmJit version in
(major << 16) | (minor << 8) | patch form.
AsmJit library version, see ASMJIT_LIBRARY_MAKE_VERSION for a version format reference.
AsmJit ABI namespace is an inline namespace within asmjit namespace.
It's used to make sure that when user links to an incompatible version of AsmJit, it won't link. It has also some additional properties as well. When
ASMJIT_ABI_NAMESPACE is defined by the user it would override the AsmJit default, which makes it possible to use multiple AsmJit libraries within a single project, totally controlled by users. This is useful especially in cases in which some of such library comes from third party.
Instruction set architecture (ISA).
Unknown or uninitialized ISA.
32-bit X86 ISA.
64-bit X86 ISA also known as X64, X86_64, and AMD64.
32-bit RISC-V ISA.
64-bit RISC-V ISA.
32-bit ARM ISA (little endian).
64-bit ARM ISA in (little endian).
32-bit ARM ISA in Thumb mode (little endian).
32-bit MIPS ISA in (little endian).
64-bit MIPS ISA in (little endian).
32-bit ARM ISA (big endian).
64-bit ARM ISA in (big endian).
32-bit ARM ISA in Thumb mode (big endian).
32-bit MIPS ISA in (big endian).
64-bit MIPS ISA in (big endian).
Maximum value of
Mask used by 32-bit ISAs (odd are 32-bit, even are 64-bit).
First big-endian architecture.
ISA detected at compile-time (ISA of the host).
Identifier used to represent names of different data types across architectures.
Describes 'db' (X86/X86_64 convention, always 8-bit quantity).
Describes 'dw' (X86/X86_64 convention, always 16-bit word).
Describes 'dd' (X86/X86_64 convention, always 32-bit word).
Describes 'dq' (X86/X86_64 convention, always 64-bit word).
Describes 'byte' (always 8-bit quantity).
Describes 'half' (most likely 16-bit word).
Describes 'word' (either 16-bit or 32-bit word).
Describes 'hword' (most likely 16-bit word).
Describes 'dword' (either 32-bit or 64-bit word).
Describes 'qword' (64-bit word).
Describes 'xword' (64-bit word).
Describes 'short' (always 16-bit word).
Describes 'long' (most likely 32-bit word).
Describes 'quad' (64-bit word).
Maximum value of
Instruction feature hints for each register group provided by ArchTraits.
Instruction feature hints describe miscellaneous instructions provided by the architecture that can be used by register allocator to make certain things simpler - like register swaps or emitting register push/pop sequences.
No feature hints.
Architecture supports a register swap by using a single instruction.
Architecture provides push/pop instructions.
Flags used by CodeBuffer.
Buffer is external (not allocated by asmjit).
Buffer is fixed (cannot be reallocated).
Operator type that can be used within an Expression.
Logical left shift.
Logical right shift.
Arithmetic right shift.
Section flags, used by Section.
Executable (.text sections).
Read-only (.text and .data sections).
Zero initialized by the loader (BSS).
Info / comment flag.
Section created implicitly, can be deleted by Target.
If virtual size of a section is greater than the size of its CodeBuffer then all bytes between the buffer size and virtual size will be zeroed.
If this option is not set then those bytes would be left as is, which means that if the user didn't initialize them they would have a previous content, which may be unwanted.
Clears the target buffer if the flattened data is less than the destination size.
Offset format type, used by OffsetFormat.
A value having
This offset type is sufficient for many targets that store offset as a continuous set bits within an instruction word / sequence of bytes.
An unsigned value having
AARCH64 ADR format of
AARCH64 ADRP format of
Maximum value of
None/deleted (no relocation).
Relocate absolute to absolute.
Relocate relative to absolute.
Relocate absolute to relative.
Relocate absolute to relative or use trampoline.
Type of the Label.
Anonymous label that can optionally have a name, which is only used for debugging purposes.
Local label (always has parentId).
Global label (never has parentId).
External label (references an external symbol).
Maximum value of
Align mode, used by BaseEmitter::align().
Align executable code.
Align non-executable code.
Align by a sequence of zeros.
Maximum value of
Emitter flags, used by BaseEmitter.
Emitter is attached to CodeHolder.
The emitter must emit comments.
The emitter was finalized.
The emitter was destroyed.
This flag is used for a very short time when an emitter is being destroyed by CodeHolder.
No encoding options.
Emit instructions that are optimized for size, if possible.
When this option is set it the assembler will try to fix instructions if possible into operation equivalent instructions that take less bytes by taking advantage of implicit zero extension. For example instruction like
Emit optimized code-alignment sequences.
Default align sequence used by X86 architecture is one-byte (0x90) opcode that is often shown by disassemblers as NOP. However there are more optimized align sequences for 2-11 bytes that may execute faster on certain CPUs. If this feature is enabled AsmJit will generate specialized sequences for alignment between 2 to 11 bytes.
Emit jump-prediction hints.
Jump prediction is usually based on the direction of the jump. If the jump is backward it is usually predicted as taken; and if the jump is forward it is usually predicted as not-taken. The reason is that loops generally use backward jumps and conditions usually use forward jumps. However this behavior can be overridden by using instruction prefixes. If this option is enabled these hints will be emitted.
This feature is disabled by default, because the only processor that used to take into consideration prediction hints was P4. Newer processors implement heuristics for branch prediction and ignore static hints. This means that this feature can be only used for annotation purposes.
Diagnostic options are used to tell emitters and their passes to perform diagnostics when emitting or processing user code.
These options control validation and extra diagnostics that can be performed by higher level emitters.
BaseAssembler implementation perform by default only basic checks that are necessary to identify all variations of an instruction so the correct encoding can be selected. This is fine for production-ready code as the assembler doesn't have to perform checks that would slow it down. However, sometimes these checks are beneficial especially when the project that uses AsmJit is in a development phase, in which mistakes happen often. To make the experience of using AsmJit seamless it offers validation features that can be controlled by DiagnosticOptions.
Diagnostic options work with BaseCompiler passes (precisely with its register allocation pass). These options can be used to enable logging of all operations that the Compiler does.
No validation options.
Perform strict validation in BaseAssembler::emit() implementations.
This flag ensures that each instruction is checked before it's encoded into a binary representation. This flag is only relevant for BaseAssembler implementations, but can be set in any other emitter type, in that case if that emitter needs to create an assembler on its own, for the purpose of BaseEmitter::finalize() it would propagate this flag to such assembler so all instructions passed to it are explicitly validated.
This flag ensures that each instruction is checked before an InstNode representing the instruction is created by BaseBuilder or BaseCompiler. This option could be more useful than kValidateAssembler in cases in which there is an invalid instruction passed to an assembler, which was invalid much earlier, most likely when such instruction was passed to Builder/Compiler.
This is a separate option that was introduced, because it's possible to manipulate the instruction stream emitted by BaseBuilder and BaseCompiler - this means that it's allowed to emit invalid instructions (for example with missing operands) that will be fixed later before finalizing it.
Annotate all nodes processed by register allocator (Compiler/RA).
Debug CFG generation and other related algorithms / operations (Compiler/RA).
Debug liveness analysis (Compiler/RA).
Debug register allocation assignment (Compiler/RA).
Debug the removal of code part of unreachable blocks.
Enable all debug options (Compiler/RA).
Platform - runtime environment or operating system.
Unknown or uninitialized platform.
Other platform that is not Windows, most likely POSIX based.
DragonFly BSD OS.
Maximum value of
Platform detected at compile-time (platform of the host).
Unknown or uninitialized object format.
Executable and linkable format (ELF).
Common object file format.
Extended COFF object format.
Mach object file format.
Maximum value of
Type identifier provides a minimalist type system used across AsmJit library.
This is an additional information that can be used to describe a value-type of physical or virtual register. It's used mostly by BaseCompiler to describe register representation (the group of data stored in the register and the width used) and it's also used by APIs that allow to describe and work with function signatures.
Abstract signed integer type that has a native size.
Abstract unsigned integer type that has a native size.
8-bit signed integer type.
8-bit unsigned integer type.
16-bit signed integer type.
16-bit unsigned integer type.
32-bit signed integer type.
32-bit unsigned integer type.
64-bit signed integer type.
64-bit unsigned integer type.
32-bit floating point type.
64-bit floating point type.
80-bit floating point type.
8-bit opmask register (K).
16-bit opmask register (K).
32-bit opmask register (K).
64-bit opmask register (K).
64-bit MMX register only used for 32 bits.
64-bit MMX register.