Machine Code Generation for C++
Logging and formatting.
The initial phase of a project that generates machine code is not always smooth. Failure cases are common not just at the beginning phase, but also during the development or refactoring. AsmJit provides logging functionality to address this issue. AsmJit does already a good job with function overloading to prevent from emitting unencodable instructions, but it can't prevent from emitting machine code that is correct at instruction level, but doesn't work when it's executed asa whole. Logging has always been an important part of AsmJit's infrastructure and looking at logs can sometimes reveal code generation issues quickly.
AsmJit provides API for logging and formatting:
AsmJit's Logger serves the following purposes:
AsmJit's FormatOptions provides the following to customize the formatting of instructions and operands through:
If output to FILE stream is not desired it's possible to use StringLogger, which concatenates everything into a multi-line string:
AsmJit uses Formatter to format inputs that are then passed to Logger. Formatting is public and can be used by AsmJit users as well. The most important thing to know regarding formatting is that Formatter always appends to the output string, so it can be used to build complex strings without having to concatenate intermediate strings.
The first example illustrates how to format operands:
Next example illustrates how to format whole instructions:
And finally, the example below illustrates how to use a built-in function to format the content of BaseBuilder, which consists of nodes:
No formatting flags.
Show also binary form of each logged instruction (Assembler).
Show a text explanation of some immediate values.
Use hexadecimal notation of immediate values.
Use hexadecimal notation of addresses and offsets in addresses.
Show casts between virtual register types (Compiler output).
Show positions associated with nodes (Compiler output).
Format indentation group, used by FormatOptions.
Indentation used for instructions and directives.
Indentation used for labels and function nodes.
Indentation used for comments (not inline comments).
Maximum value of
Format padding group, used by FormatOptions.
Describes padding of a regular line, which can represent instruction, data, or assembler directives.
Describes padding of machine code dump that is visible next to the instruction, if enabled.
Maximum value of