AsmJit
Low-Latency Machine Code Generation
Globals, code storage, and emitter interface.
AsmJit library uses CodeHolder to hold code during code generation and emitters inheriting from BaseEmitter to emit code. CodeHolder uses containers to manage its data:
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 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.
A decorator that is used to decorate API that AsmJit exports when built as a shared library.
This is basically a workaround.
When using MSVC and marking class as DLL export everything gets exported, which is unwanted in most projects. MSVC automatically exports typeinfo and vtable if at least one symbol of the class is exported. However, GCC has some strange behavior that even if one or more symbol is exported it doesn't export typeinfo unless the class itself is decorated with "visibility(default)" (i.e. ASMJIT_API).
Decorator to force inlining of functions, uses either __attribute__((__always_inline__))
or __forceinline, depending on C++ compiler.
Like ASMJIT_FORCE_INLINE, but uses additionally __nodebug__
or __artificial__
attribute to make the debugging of some AsmJit functions easier, especially getters and one-line abstractions where usually you don't want to step in.
Decorator to avoid inlining of functions, uses either __attribute__((__noinline__))
or __declspec(noinline)
depending on C++ compiler.
Decorator that marks functions that should never return.
Typically used to implement assertion handlers that terminate, so the function never returns.
CDECL function attribute - either __attribute__((__cdecl__))
or __cdecl
.
STDCALL function attribute - either __attribute__((__stdcall__))
or __stdcall
.
FASTCALL function attribute - either __attribute__((__fastcall__))
or __fastcall
.
Expands to __attribute__((__regparm__(N)))
when compiled by GCC or clang, nothing otherwise.
VECTORCALL function attribute - either __attribute__((__vectorcall__))
or __vectorcall
.
Instruction set architecture (ISA).
Identifier used to represent names of different data types across architectures.
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.
Constant | Description |
---|---|
kNoHints | No feature hints. |
kRegSwap | Architecture supports a register swap by using a single instruction. |
kPushPop | Architecture provides push/pop instructions. |
Flags used by CodeBuffer.
Constant | Description |
---|---|
kNone | No flags. |
kIsExternal | Buffer is external (not allocated by asmjit). |
kIsFixed | Buffer is fixed (cannot be reallocated). |
Operator type that can be used within an Expression.
Constant | Description |
---|---|
kAdd | Addition. |
kSub | Subtraction. |
kMul | Multiplication. |
kSll | Logical left shift. |
kSrl | Logical right shift. |
kSra | Arithmetic right shift. |
Value type that can be used within an Expression.
Constant | Description |
---|---|
kNone | No value or invalid. |
kConstant | Value is 64-bit unsigned integer (constant). |
kLabel | Value is LabelEntry, which references a Label. |
kExpression | Value is Expression. |
Section flags, used by Section.
Constant | Description |
---|---|
kNone | No flags. |
kExecutable | Executable (.text sections). |
kReadOnly | Read-only (.text and .data sections). |
kZeroInitialized | Zero initialized by the loader (BSS). |
kComment | Info / comment flag. |
kImplicit | Section created implicitly, can be deleted by Target. |
Flags that can be used with CodeHolder::copySectionData() and CodeHolder::copyFlattenedData().
Constant | Description |
---|---|
kNone | No flags. |
kPadSectionBuffer | 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. |
kPadTargetBuffer | Clears the target buffer if the flattened data is less than the destination size. This option works only with CodeHolder::copyFlattenedData() as it processes multiple sections. It is ignored by CodeHolder::copySectionData(). |
Offset format type, used by OffsetFormat.
Relocation type.
Type of the Label.
Align mode, used by BaseEmitter::align().
Constant | Description |
---|---|
kCode | Align executable code. |
kData | Align non-executable code. |
kZero | Align by a sequence of zeros. |
kMaxValue | Maximum value of |
Emitter type used by BaseEmitter.
Constant | Description |
---|---|
kNone | Unknown or uninitialized. |
kAssembler | Emitter inherits from BaseAssembler. |
kBuilder | Emitter inherits from BaseBuilder. |
kCompiler | Emitter inherits from BaseCompiler. |
kMaxValue | Maximum value of |
Emitter flags, used by BaseEmitter.
Constant | Description |
---|---|
kNone | No flags. |
kAttached | Emitter is attached to CodeHolder. |
kLogComments | The emitter must emit comments. |
kOwnLogger | The emitter has its own Logger (not propagated from CodeHolder). |
kOwnErrorHandler | The emitter has its own ErrorHandler (not propagated from CodeHolder). |
kFinalized | The emitter was finalized. |
kDestroyed | The emitter was destroyed. This flag is used for a very short time when an emitter is being destroyed by CodeHolder. |
Encoding options.
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.
Constant | Description |
---|---|
kNone | No validation options. |
kValidateAssembler | 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. Default: false. |
kValidateIntermediate | Perform strict validation in BaseBuilder::emit() and BaseCompiler::emit() implementations. 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. Default: false. |
kRAAnnotate | Annotate all nodes processed by register allocator (Compiler/RA).
|
kRADebugCFG | Debug CFG generation and other related algorithms / operations (Compiler/RA). |
kRADebugLiveness | Debug liveness analysis (Compiler/RA). |
kRADebugAssignment | Debug register allocation assignment (Compiler/RA). |
kRADebugUnreachable | Debug the removal of code part of unreachable blocks. |
kRADebugAll | Enable all debug options (Compiler/RA). |
Platform - runtime environment or operating system.
Platform ABI (application binary interface).
Floating point ABI (ARM).
Object format.
Constant | Description |
---|---|
kUnknown | Unknown or uninitialized object format. |
kJIT | JIT code generation object, most likely JitRuntime or a custom Target implementation. |
kELF | Executable and linkable format (ELF). |
kCOFF | Common object file format. |
kXCOFF | Extended COFF object format. |
kMachO | Mach object file format. |
kMaxValue | 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.
Casts a void*
pointer func
to a function pointer Func
.
Casts a function pointer func
to a void pointer void*
.