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 as a 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: