上一篇對Luajit的代碼結(jié)構(gòu)和編譯過程做了簡單的描述,這一篇就講一下buildvm在第一步預(yù)處理dasc文件的過程和DynASM這個輪子。
官方連接:http://luajit.org/dynasm.html
是為了讓你更優(yōu)雅的C里面擼匯編的一個工具,我記得以前看過一個老外的blog對比過同樣功能的jit code generator的語法,Luajit的作者顯然品位還是很高的。
我們先來看看如果不用工具硬生生擼代碼的話會發(fā)生什么。
1、你往一段內(nèi)存里面寫0xB8,0x00,0x01....
2、你在文件里定義好多l(xiāng)abel,寫個copy section的宏往內(nèi)存里面復(fù)制,你還不能確定里面到底是什么。(哦。。這個的術(shù)語叫Threaded。。。)
然后再對比下AsmJit或者Xbyak的例子看看(他們的功能差不多),DynASM還提供了.marco實現(xiàn),就會發(fā)現(xiàn)語法真是sweeeet~
這是我寫著玩的一個草泥馬語jit解釋器(https://github.com/pwq1989/GMHjit)語法真是清新自然啊,如果你想看工業(yè)級的應(yīng)用,可以看看Google的Haberman寫的protobuf的upb庫,里面用DynASM進行了jit,號稱快了多少多少(不去考證了),或者是agentzh寫的sregex正則庫,也是用它做了jit。一般來說DSL配上jit的話一定會快很多就錯不了了。
下面給一個DynASM的Demo程序(摘抄自這個blog)
1 // DynASM directives.
2 |.arch x64
3 |.actionlist actions
4
5 // This define affects "|" DynASM lines. "Dst" must
6 // resolve to a dasm_State** that points to a dasm_State*.
7 #define Dst &state
8
9 int main(int argc, char *argv[]) {
10 if (argc < 2) {
11 fprintf(stderr, "Usage: jit1 <integer>\n");
12 return 1;
13 }
14
15 int num = atoi(argv[1]);
16 dasm_State *state;
17 initjit(&state, actions);
18
19 // Generate the code. Each line appends to a buffer in
20 // "state", but the code in this buffer is not fully linked
21 // yet because labels can be referenced before they are
22 // defined.
23 //
24 // The run-time value of C variable "num" is substituted
25 // into the immediate value of the instruction.
26 | mov eax, num
27 | ret
28
29 // Link the code and write it to executable memory.
30 int (*fptr)() = jitcode(&state);
31
32 // Call the JIT-ted function.
33 int ret = fptr();
34 assert(num == ret);
35
36 // Free the machine code.
37 free_jitcode(fptr);
38
39 return ret;
40 }
2 |.arch x64
3 |.actionlist actions
4
5 // This define affects "|" DynASM lines. "Dst" must
6 // resolve to a dasm_State** that points to a dasm_State*.
7 #define Dst &state
8
9 int main(int argc, char *argv[]) {
10 if (argc < 2) {
11 fprintf(stderr, "Usage: jit1 <integer>\n");
12 return 1;
13 }
14
15 int num = atoi(argv[1]);
16 dasm_State *state;
17 initjit(&state, actions);
18
19 // Generate the code. Each line appends to a buffer in
20 // "state", but the code in this buffer is not fully linked
21 // yet because labels can be referenced before they are
22 // defined.
23 //
24 // The run-time value of C variable "num" is substituted
25 // into the immediate value of the instruction.
26 | mov eax, num
27 | ret
28
29 // Link the code and write it to executable memory.
30 int (*fptr)() = jitcode(&state);
31
32 // Call the JIT-ted function.
33 int ret = fptr();
34 assert(num == ret);
35
36 // Free the machine code.
37 free_jitcode(fptr);
38
39 return ret;
40 }
預(yù)處理之后那就會變成這樣子:
1 //|.arch x64
2 //|.actionlist actions
3 static const unsigned char actions[4] = {
4 184,237,195,255
5 };
6
7 // [
]
8
9 //| mov eax, num
10 //| ret
11 dasm_put(Dst, 0, num);
dasm_put就是把num參數(shù)和actions[]一起放入了Dst(#define Dst &state)的制定的內(nèi)存中,這時候已經(jīng)是機器碼的形式了。2 //|.actionlist actions
3 static const unsigned char actions[4] = {
4 184,237,195,255
5 };
6
7 // [

8
9 //| mov eax, num
10 //| ret
11 dasm_put(Dst, 0, num);
下面是對于acitons[]數(shù)組內(nèi)容的解釋:
184(B8)-- mov eax, [immediate] 指令的第一個字節(jié)
237 -- 內(nèi)置的標(biāo)志DASM_IMM_D, 指明應(yīng)該放入一個4字節(jié)寬度的參數(shù),與上一條指令完成一個MOV
195(C3)-- 對應(yīng)ret指令
255 -- 內(nèi)置的標(biāo)志DASM_STOP
以上就是最簡單的例子,dasm_growpc()是內(nèi)置的函數(shù),用來增長maxpc, 這樣在程序里面就可以方便寫出jmp => label 這樣的指令了。
由于DynASM的文檔很少,幸虧還有幾個例子,除了例子唯一能看的就是源碼了,所以在用的時候出現(xiàn)問題是很痛苦的。。當(dāng)時寫GMHjit就發(fā)現(xiàn)了蛋疼的pre-process period bug,后來繞過去了。
源碼文件有這么幾個
-- dynasm.lua
-- dynasm_proto.h
-- dynasm_*.lua
-- dynasm_*.h // * x64 x86 ppc mips arm 等target
用起來就是lua dynasm.lua a.dasm > a.h
下面就從dynasm.lua開始分析下他的源碼
入口是parseargs函數(shù),里面給的g_opt參數(shù)賦默認的值,一個repeat 中調(diào)用parseopt解析參數(shù),opt_map就是option對args的函數(shù)映射。
函數(shù)wline,wcomment,wsync,wdumplines都是對輸出的目標(biāo)文件的操作。
真正的主函數(shù)是 translate,把input file變成 output file,在readfile中的doline函數(shù)是真正的處理過程,里面判斷是否是Assembler line之后Emit C code,調(diào)用dostmt(aline)。里面繼續(xù)有map_coreop[*]來處理section macro arch nop_ error_1 include if endif elseif 等關(guān)鍵字,想深入研究的可以自己去看,其中在loadarch中根據(jù)arch加載不同的lua庫
如果arch是x64的話,本質(zhì)還是require x86
來看dasm_x86.lua文件
_M.mergemaps這是關(guān)鍵的方法,設(shè)置了2個Map的元方法,然后返回,相當(dāng)于是把方法綁定在table里面?zhèn)鬟f了出去。處理后文件中關(guān)鍵的actionlist[]數(shù)組和Dasm_put(Dst, ...)的輸出就是這個lua文件的方法。
里面提供了很多dump方法,可以供我們遇到問題時候調(diào)試處理過程。action_names就是以后生成的action_list中的內(nèi)置標(biāo)志定義,必須與dasm_x86.h中的enum定義一致。
表明了代表的參數(shù)和長度等信息。這個文件里面所有的函數(shù)就是做了一件事,把你的 |... 這樣子的代碼處理成數(shù)組輸出到目標(biāo)文件中(我是匯編渣渣,里面貌似支持SSE2、3、4+,看不懂,等到以后看到traced jit的時候再去翻手冊把)
預(yù)處理完成之后,就是#include "dasm_x86.h",里面有最關(guān)鍵的dasm_State結(jié)構(gòu)體的定義,幾乎里面所有的函數(shù)都是對外的API,有init,setup,free等等,除去初始化與free之外,有三個步驟是需要出現(xiàn)在你都代碼中:
1、dasm_put(Dst,...) 這個是自動生成的,不用我們操心,根據(jù)actionlist[]和運行時的參數(shù)寫入到Dst指定的內(nèi)存(Dst->section)中.
2、dasm_link() 第二個參數(shù)是返回的代碼長度大小,這個函數(shù)把section合并到一起,處理偏移等等。
3、dasm_encode() 第二個參數(shù)是一個接受encode輸出的buffer指針。
然后就可以用一個函數(shù)指針,比如聲明一個 int (*f)(*int), int ret = f(param) 直接運行剛剛生成的機器碼了。