AsmJitC/C++ 编译器
AsmJit 是一个完整的 JIT(Just-In-Time,运行时刻)的针对 C++ 语言的汇编器,可以生成兼容 x86 和 x64 架构的原生代码,不仅支持整个 x86/x64 的指令集(包括传统的 MMX 和最新的 AVX2 指令集),而且提供了一套可以在编译时刻进行语义检查的 API。
AsmJit 的使用也没有任何的限制,适用于多媒体,虚拟机的后端,远程代码生成等等。
特性
-
完全支持 x86/x64 指令集(包括 MMX,SSEx,AVX1/2,BMI,XOP,FMA3 和 FMA4)
-
底层次和高层次的代码生成概念
-
内置检测处理器特性功能
-
实现虚拟内存的管理,类似于 malloc 和 free
-
强大的日志记录和错误处理能力
-
体积小,可直接嵌入项目,编译后的体积在150至200kb之间
-
独立性强,不需要依赖其他任何的库(包括 STL 和 RTTI )
环境
1. 操作系统
-
BSD系列
-
Linux
-
Mac
-
Windows
2. C++编译器
-
Borland C++
-
Clang
-
GCC
-
MinGW
-
MSVC
-
其他的在”build.h”中文件中定义过的编译器
3. 后端
-
X86
-
X64
软件简介引自:http://www.cnblogs.com/lanrenxinxin/p/5021641.html
// Create simple DWORD memory copy function for 32 bit x86 platform:
// (for AsmJit version 0.8+)
//
// void ASMJIT_CDECL memcpy32(UInt32* dst, const UInt32* src, SysUInt len);
// AsmJit library
#include <AsmJit/AsmJitAssembler.h>
#include <AsmJit/AsmJitVM.h>
// C library - printf
#include <stdio.h>
using namespace AsmJit;
// This is type of function we will generate
typedef void (*MemCpy32Fn)(UInt32*, const UInt32*, SysUInt);
int main(int argc, char* argv[])
{
// ==========================================================================
// Part 1:
// Create Assembler
Assembler a;
// Constants
const int arg_offset = 8; // Arguments offset (STDCALL EBP)
const int arg_size = 12; // Arguments size
// Labels
Label L_Loop;
Label L_Exit;
// Prolog
a.push(ebp);
a.mov(ebp, esp);
a.push(esi);
a.push(edi);
// Fetch arguments
a.mov(edi, dword_ptr(ebp, arg_offset + 0)); // get dst
a.mov(esi, dword_ptr(ebp, arg_offset + 4)); // get src
a.mov(ecx, dword_ptr(ebp, arg_offset + 8)); // get len
// exit if length is zero
a.jz(&L_Exit);
// Bind L_Loop label to here
a.bind(&L_Loop);
a.mov(eax, dword_ptr(esi));
a.mov(dword_ptr(edi), eax);
a.add(esi, 4);
a.add(edi, 4);
// Loop until ecx is not zero
a.dec(ecx);
a.jnz(&L_Loop);
// Exit
a.bind(&L_Exit);
// Epilog
a.pop(edi);
a.pop(esi);
a.mov(esp, ebp);
a.pop(ebp);
// Return
a.ret();
// ==========================================================================
// ==========================================================================
// Part 2:
// Make JIT function
MemCpy32Fn fn = function_cast<MemCpy32Fn>(a.make());
// Ensure that everything is ok
if (!fn)
{
printf("Error making jit function (%u).\n", a.error());
return 1;
}
// Create some data
UInt32 dst[128];
UInt32 src[128];
// Call JIT function
fn(dst, src, 128);
// If you don't need the function anymore, it should be freed
MemoryManager::global()->free((void*)fn);
// ==========================================================================
return 0;
}评论
