A small x86 DSL for C++
![]() The original version of this code used `size_t` for most integer types, which was: 1) incorrect (I neglected to use `uintptr_t`) when converting pointers to integers 2) specific to the compilation target, not the assembly target. This version uses `uint64_t` (which is the same as both `size_t` and `uintptr_t` on x64) to match the assembly target. |
||
---|---|---|
.clang-format | ||
.gitignore | ||
asm.hxx | ||
LICENSE | ||
README.md | ||
test.cxx |
asm.hxx --- A single-header C++ partial x64 assembly DSL
This is a small DSL for creating bytecode literals in C++. Example:
#include "asm.hxx"
#include <stdio>
// Any type can be used as a placeholder in the generated code.
struct A {};
int main(int argc, char *argv) {
using namespace asm_;
constexpr auto code =
Bytecode
<< MOV(R8,GetVar<A,U64>)
<< CALL(R8);
// placeholders may be set at runtime
// the code between them will be assembled at compile time.
//
// Output "\x49\xb8\x90\x49\x38\x20\x00\x00\x00\x00\x41\xff\xd0":
std::cout << (code << SetVar<A>(0x20384990)) << std::endl;
// Output "49 b8 27 70 a5 d3 ea 00 00 00 41 ff d0":
std::cout << (code << SetVar<A>(0xead3a57027)).human_readable() << std::endl;
}
As of now, the only commands covered are:
NOP
RET
CALL
MOV
PUSH
POP
I might add more in the future; the framework is there, but for now I just focused on what I needed to work. If you stumble onto this and want it extended, let me know what you need, and I'll add it.