查看文章 |
【推荐】Xbyak ; JIT assembler for x86(IA32), x64(AMD64, x86-64) by C++
2008-05-03 22:11
http://homepage1.nifty.com/herumi/soft/xbyak_e.html Xbyak ; JIT assembler for x86(IA32), x64(AMD64, x86-64) by C++ ----------------------------------------------------------------------------- <Abstract> This is a header file which enables dynamically to assemble x86(IA32), x64(AMD64, x86-64) mnemonic. ----------------------------------------------------------------------------- <Feature> header file only you can use Xbyak's functions at once if xbyak.h is included. MMX/MMX2/SSE/SSE2/SSE3/SSSE3/SSE4 are available. Windows Xp(32bit, 64bit), Vista, Linux(32bit, 64bit), Intel Mac ready support Visual Studio C++ 2005 Express Ed., VC2008 Pro, mingw 3.4.2, icc 7.2, gcc 4.1.1, and so on. Note: "-fno-operator-names" option is required on gcc to avoid analyzing "and", "or", etc. as operators. ----------------------------------------------------------------------------- <Install> The following files are necessary. Please add the path to your compile directories. xbyak.h xbyak_bin2hex.h xbyak_mnemonic.h Linu: >make install These files are copied into /usr/local/include/xbyak . ----------------------------------------------------------------------------- <Syntax> 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(); Addressing (ptr|dword|word|byte) [base + index * (1|2|4|8) + displacement] NASM Xbyak mov eax, [ebx+ecx] --> mov (eax, ptr[ebx+ecx]); test byte [esp], 4 --> test (byte [esp], 4); NB. dword, word and byte are class members, then don't use dword as unsigned int, for example. Label 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. Code size 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) { } ... }; See main.cpp ----------------------------------------------------------------------------- <Macro> XBYAK32 is defined on 32bit. XBYAK64 is defined on 64bit. XBYAK64_WIN is defined on 64bit Windows XBYAK64_GCC is defined on 64bit gcc ----------------------------------------------------------------------------- <Sample> test0.cpp ; tiny sample of Xbyak(x86, x64) quantize.cpp ; JIT optimized quantization by fast division(x86 only) calc.cpp ; assemble and estimate a given polynomial(x86, x64) bf.cpp ; JIT brainfuck(x86, x64) ----------------------------------------------------------------------------- <Remark> The current version does not support FPU/3D Now! and some special mnemonics. Please mail to me if necessary. ----------------------------------------------------------------------------- <License> modified new BSD License http://www.opensource.org/licenses/bsd-license.php ----------------------------------------------------------------------------- <History> 2008/Apr/30 add cmpxchg16b, cdqe 2008/Apr/29 support x86 2008/Apr/14 code refactoring 2008/Mar/12 add bsr/bsf 2008/Feb/14 fix output of sub eax, 1234 (thanks to Robert) 2007/Nov/5 support lock, xadd, xchg 2007/Nov/2 support SSSE3/SSE4 2007/Feb/4 fix the bug that exception doesn't occur under the condition which the offset of jmp mnemonic without T_NEAR is over 127. 2007/Jan/21 fix the bug to create address like [disp] select smaller representation for mov (eax|ax|al, [disp]) 2007/Jan/4 first version ----------------------------------------------------------------------------- <Author> MITSUNARI Shigeo(herumi at nifty dot com) --- $Revision: 1.23 $ $Date: 2008/04/30 00:47:54 $ |
最近读者: