目前其实只能编译单目运算符和return语句,主要是为了看看整个大框架有没有错误。如果没有发现错误的话,其他种类的表达式和语句的代码生成很容易加上去。下面是一个CMinus程序的例子和编译结果(汇编):
CMinus代码:
1 int ReturnZero()
2 {
3 return 0;
4 }
5
6 int Negative(int Number)
7 {
8 return -+-+-Number;
9 }
下面是编译之后的机器码:
1 000D0000 FF F5 push ebp
2 000D0002 89 E5 mov ebp,esp
3 000D0004 81 EC 0C 00 00 00 sub esp,0Ch
4 000D000A 60 pushad
5 000D000B B8 00 00 00 00 mov eax,0
6 000D0010 89 85 FC FF FF FF mov dword ptr [ebp+FFFFFFFCh],eax
7 000D0016 E9 00 00 00 00 jmp 000D001B
8 000D001B 61 popad
9 000D001C 8B 85 FC FF FF FF mov eax,dword ptr [ebp+FFFFFFFCh]
10 000D0022 89 EC mov esp,ebp
11 000D0024 8F C5 pop ebp
12 000D0026 C2 00 00 ret 0
13 000D0029 FF F5 push ebp
14 000D002B 89 E5 mov ebp,esp
15 000D002D 81 EC 14 00 00 00 sub esp,14h
16 000D0033 60 pushad
17 000D0034 8B 85 08 00 00 00 mov eax,dword ptr [ebp+00000008h]
18 000D003A F7 D8 neg eax
19 000D003C 89 85 F8 FF FF FF mov dword ptr [ebp+FFFFFFF8h],eax
20 000D0042 8B 85 F8 FF FF FF mov eax,dword ptr [ebp+FFFFFFF8h]
21 000D0048 F7 D8 neg eax
22 000D004A 89 85 F4 FF FF FF mov dword ptr [ebp+FFFFFFF4h],eax
23 000D0050 8B 85 F4 FF FF FF mov eax,dword ptr [ebp+FFFFFFF4h]
24 000D0056 F7 D8 neg eax
25 000D0058 89 85 FC FF FF FF mov dword ptr [ebp+FFFFFFFCh],eax
26 000D005E E9 00 00 00 00 jmp 000D0063
27 000D0063 61 popad
28 000D0064 8B 85 FC FF FF FF mov eax,dword ptr [ebp+FFFFFFFCh]
29 000D006A 89 EC mov esp,ebp
30 000D006C 8F C5 pop ebp
31 000D006E C2 04 00 ret 4
可以根据两个ret指令来确定函数边界。窥孔优化没有做,代码比较囧。
posted on 2009-05-17 23:00
陈梓瀚(vczh) 阅读(2582)
评论(1) 编辑 收藏 引用 所属分类:
JIT