commit | 8804abc109a7f4d8e820e50178ade48859279f2b | [log] [tgz] |
---|---|---|
author | MITSUNARI Shigeo <herumi@nifty.com> | Wed Apr 17 11:20:49 2013 +0900 |
committer | MITSUNARI Shigeo <herumi@nifty.com> | Wed Apr 17 11:20:49 2013 +0900 |
tree | ee03f56ec1a2bd8445c29937c1d66a8d9c2d382a | |
parent | 82024c13a8469d7b5f7d546e890c4c625d99273e [diff] |
avoid mov if unnecessary
This is a header file which enables dynamically to assemble x86(IA32), x64(AMD64, x86-64) mnemonic.
header file only you can use Xbyak's functions at once if xbyak.h is included.
MMX/MMX2/SSE/SSE2/SSE3/SSSE3/SSE4/FPU(partial)/AVX
Note: Xbyak uses and(), or(), xor(), not() functions, so “-fno-operator-names” option is required on gcc. Or define XBYAK_NO_OP_NAMES and use and_(), or_(), xor_(), not_() instead of them. and_(), or_(), xor_(), not_() are available if XBYAK_NO_OP_NAMES is not defined.
The following files are necessary. Please add the path to your compile directories.
Linux:
make install
These files are copied into /usr/local/include/xbyak
AutoGrow mode is a mode that Xbyak grows memory automatically if necessary. Call ready() before calling getCode() to calc address of jmp.
struct Code : Xbyak::CodeGenerator { Code() : Xbyak::CodeGenerator(<default memory size>, Xbyak::AutoGrow) { ... } }; Code c; c.ready(); // Don't forget to call this function
Don't use the address returned by getCurr() before calling ready(). It may be invalid address. RESTRICTION : rip addressing is not supported in AutoGrow
Make Xbyak::CodeGenerator and make the class method and get the function pointer by calling cgetCode() and casting the return value.
NASM Xbyak mov eax, ebx --> mov(eax, ebx); inc ecx inc(ecx); ret --> ret();
(ptr|dword|word|byte) [base + index * (1|2|4|8) + displacement] [rip + 32bit disp] ; x64 only NASM Xbyak mov eax, [ebx+ecx] --> mov (eax, ptr[ebx+ecx]); test byte [esp], 4 --> test (byte [esp], 4);
selector is not supported.
you can use ptr for almost memory access unless you specify the size of memory.
dword, word and byte are member variables, then don't use dword as unsigned int, for example.
You can omit a destination for almost 3-op mnemonics.
vaddps(xmm1, xmm2, xmm3); // xmm1 <- xmm2 + xmm3 vaddps(xmm2, xmm3); // xmm2 <- xmm2 + xmm3 vaddps(xmm2, xmm3, ptr [rax]); // use ptr to access memory
L("L1"); jmp ("L1"); jmp ("L2"); ... a few mnemonics(8-bit displacement jmp) ... L("L2"); jmp ("L3", T_NEAR); ... a lot of mnemonics(32-bit displacement jmp) ... L("L3");
Call hasUndefinedLabel() to verify your code has no undefined label. you can use a label for immediate value of mov like as mov (eax, “L2”);
L("@@"); // <A> jmp("@b"); // jmp to <A> jmp("@f"); // jmp to <B> L("@@"); // <B> jmp("@b"); // jmp to <B> mov(eax, "@b"); jmp(eax); // jmp to <B>
labels begining of period between inLocalLabel() and outLocalLabel() are dealed with local label. inLocalLabel() and outLocalLabel() can be nested.
void func1() { inLocalLabel(); L(".lp"); // <A> ; local label ... jmp(".lp"); // jmpt to <A> L("aaa"); // global label outLocalLabel(); } void func2() { inLocalLabel(); L(".lp"); // <B> ; local label func1(); jmp(".lp"); // jmp to <B> inLocalLabel(); }
The default max code size is 2048 bytes. Please set it in constructor of CodeGenerator() if you want to use large size.
class Quantize : public Xbyak::CodeGenerator { public: Quantize() : CodeGenerator(8192) { } ... };
You can make jit code on prepaired memory.
class Sample : public Xbyak::CodeGenerator { public: Sample(void *userPtr, size_t size) : Xbyak::CodeGenerator(size, userPtr) { ... } }; const size_t codeSize = 1024; uint8 buf[codeSize + 16]; // get 16-byte aligned address uint8 *p = Xbyak::CodeArray::getAlignedAddress(buf); // append executable attribute to the memory Xbyak::CodeArray::protect(p, codeSize, true); // construct your jit code on the memory Sample s(p, codeSize);
See sample/test0.cpp
The current version does not support 3D Now!, 80bit FPU load/store and some special mnemonics. Please mail to me if necessary.
modified new BSD License http://opensource.org/licenses/BSD-3-Clause
MITSUNARI Shigeo(herumi at nifty dot com)