Skip to content

RomanFesenko/x64InstructionEncoder

Repository files navigation

Lightweight library for encoding a subset of x64 architecture instructions, with partial support for x87 and SSE. The purpose is to create small JIT compilers, for example, for arithmetic expressions.

Minimal encoding example:

#include "encoding.h"
int main()
{
    using namespace encoding_x64;
    CCode c;
    c.finit();
    c.fld(qword_ptr(rax));// pointer to function arguments pass via rax register
    c.fadd(qword_ptr(rax,8));
    c.fstp(qword_ptr(rax));
    c.ret();
    auto f=c.Compile();
    std::cout<<f.execute<double,double,double>(1.5,2.5)<<std::endl; // 4
    return 0;
}

JIT implementation of arithmetic expressions is in expression_jit.cpp. Usage example:

#include <assert.h>
#include "expression_jit.h"

int main()
{
    Cx87Compiler comp;
    auto expr=comp.Compile("exp(sin(x)*sin(x)+cos(x)*cos(x)-1)",{"x"}); //Code generation
    assert(expr);
    std::cout<<expr(0.1234)<<std::endl; //1, Code executing, not interpreter!
    return 0;
}

Releases

No releases published

Packages

No packages published