xbyak/sample/static_buf.cpp

46 lines
847 B
C++
Raw Normal View History

2012-03-23 05:31:08 -07:00
/*
sample to use static memory
*/
#include <stdio.h>
2013-06-21 05:20:54 -07:00
#define XBYAK_NO_OP_NAMES
2012-03-23 05:31:08 -07:00
#include "xbyak/xbyak.h"
MIE_ALIGN(4096) char buf[4096];
struct Code : Xbyak::CodeGenerator {
Code()
: Xbyak::CodeGenerator(sizeof(buf), buf)
{
puts("generate");
printf("ptr=%p, %p\n", getCode(), buf);
#ifdef XBYAK32
mov(eax, ptr [esp + 4]);
add(eax, ptr [esp + 8]);
#elif defined(XBYAK64_WIN)
lea(rax, ptr [rcx + rdx]);
#else
lea(rax, ptr [rdi + rsi]);
#endif
ret();
2018-07-24 02:07:35 -07:00
Xbyak::CodeArray::protect(buf, sizeof(buf), Xbyak::CodeArray::PROTECT_RE);
}
~Code()
{
Xbyak::CodeArray::protect(buf, sizeof(buf), Xbyak::CodeArray::PROTECT_RW);
2012-03-23 05:31:08 -07:00
}
} s_code;
inline int add(int a, int b)
{
2013-01-12 02:03:39 -07:00
return Xbyak::CastTo<int (*)(int,int)>(buf)(a, b);
2012-03-23 05:31:08 -07:00
}
int main()
{
int sum = 0;
for (int i = 0; i < 10; i++) {
sum += add(i, 5);
}
printf("sum=%d\n", sum);
}