虽然CMinus到中间指令的代码是精心控制的,但是中间指令到x86的代码却是很随意写出来的。现在看来用窥孔优化只是一种治标不治本的方法。打算在CMinus整个都完成了之后,将中间指令到x86的代码生成那部分重写,使用一种广为人知的寄存器分配算法来做。下面是冒泡排序的程序:
CMinus代码:
1 void BubbleSort(int* Input , int Count)
2 {
3 int i=0;
4 while(i<Count-1)
5 {
6 int j=Count-2;
7 while(j>=i)
8 {
9 if(Input[j]>Input[j+1])
10 {
11 int Temp=Input[j];
12 Input[j]=Input[j+1];
13 Input[j+1]=Temp;
14 }
15 j-=1;
16 }
17 i+=1;
18 }
19 }
下面是生成的x86指令:
1 0072103A FF F5 push ebp
2 0072103C 89 E5 mov ebp,esp
3 0072103E 81 EC C0 00 00 00 sub esp,0C0h
4 00721044 60 pushad
5 00721045 B8 00 00 00 00 mov eax,0
6 0072104A 89 85 58 FF FF FF mov dword ptr [ebp+FFFFFF58h],eax
7 00721050 B8 01 00 00 00 mov eax,1
8 00721055 89 C2 mov edx,eax
9 00721057 8B 85 0C 00 00 00 mov eax,dword ptr [ebp+0000000Ch]
10 0072105D 29 D0 sub eax,edx
11 0072105F 89 85 48 FF FF FF mov dword ptr [ebp+FFFFFF48h],eax
12 00721065 8B 85 48 FF FF FF mov eax,dword ptr [ebp+FFFFFF48h]
13 0072106B 89 C2 mov edx,eax
14 0072106D 8B 85 58 FF FF FF mov eax,dword ptr [ebp+FFFFFF58h]
15 00721073 31 C9 xor ecx,ecx
16 00721075 39 D0 cmp eax,edx
17 00721077 0F 9C F9 setl cl
18 0072107A 89 C8 mov eax,ecx
19 0072107C 88 85 4C FF FF FF mov byte ptr [ebp+FFFFFF4Ch],al
20 00721082 31 C0 xor eax,eax
21 00721084 8A 85 4C FF FF FF mov al,byte ptr [ebp+FFFFFF4Ch]
22 0072108A A9 FF FF FF FF test eax,0FFFFFFFFh
23 0072108F 0F 84 52 02 00 00 je 007212E7
24 00721095 B8 02 00 00 00 mov eax,2
25 0072109A 89 C2 mov edx,eax
26 0072109C 8B 85 0C 00 00 00 mov eax,dword ptr [ebp+0000000Ch]
27 007210A2 29 D0 sub eax,edx
28 007210A4 89 85 5C FF FF FF mov dword ptr [ebp+FFFFFF5Ch],eax
29 007210AA 8B 85 58 FF FF FF mov eax,dword ptr [ebp+FFFFFF58h]
30 007210B0 89 C2 mov edx,eax
31 007210B2 8B 85 5C FF FF FF mov eax,dword ptr [ebp+FFFFFF5Ch]
32 007210B8 31 C9 xor ecx,ecx
33 007210BA 39 D0 cmp eax,edx
34 007210BC 0F 9D F9 setge cl
35 007210BF 89 C8 mov eax,ecx
36 007210C1 88 85 48 FF FF FF mov byte ptr [ebp+FFFFFF48h],al
37 007210C7 31 C0 xor eax,eax
38 007210C9 8A 85 48 FF FF FF mov al,byte ptr [ebp+FFFFFF48h]
39 007210CF A9 FF FF FF FF test eax,0FFFFFFFFh
40 007210D4 0F 84 E3 01 00 00 je 007212BD
41 007210DA B8 04 00 00 00 mov eax,4
42 007210DF 89 C2 mov edx,eax
43 007210E1 8B 85 5C FF FF FF mov eax,dword ptr [ebp+FFFFFF5Ch]
44 007210E7 F7 EA imul edx
45 007210E9 89 85 4C FF FF FF mov dword ptr [ebp+FFFFFF4Ch],eax
46 007210EF 8B 85 4C FF FF FF mov eax,dword ptr [ebp+FFFFFF4Ch]
47 007210F5 89 C2 mov edx,eax
48 007210F7 8B 85 08 00 00 00 mov eax,dword ptr [ebp+00000008h]
49 007210FD 01 D0 add eax,edx
50 007210FF 89 85 48 FF FF FF mov dword ptr [ebp+FFFFFF48h],eax
51 00721105 8B 8D 48 FF FF FF mov ecx,dword ptr [ebp+FFFFFF48h]
52 0072110B 8B 01 mov eax,dword ptr [ecx]
53 0072110D 89 85 50 FF FF FF mov dword ptr [ebp+FFFFFF50h],eax
54 00721113 B8 01 00 00 00 mov eax,1
55 00721118 89 C2 mov edx,eax
56 0072111A 8B 85 5C FF FF FF mov eax,dword ptr [ebp+FFFFFF5Ch]
57 00721120 01 D0 add eax,edx
58 00721122 89 85 48 FF FF FF mov dword ptr [ebp+FFFFFF48h],eax
59 00721128 B8 04 00 00 00 mov eax,4
60 0072112D 89 C2 mov edx,eax
61 0072112F 8B 85 48 FF FF FF mov eax,dword ptr [ebp+FFFFFF48h]
62 00721135 F7 EA imul edx
63 00721137 89 85 4C FF FF FF mov dword ptr [ebp+FFFFFF4Ch],eax
64 0072113D 8B 85 4C FF FF FF mov eax,dword ptr [ebp+FFFFFF4Ch]
65 00721143 89 C2 mov edx,eax
66 00721145 8B 85 08 00 00 00 mov eax,dword ptr [ebp+00000008h]
67 0072114B 01 D0 add eax,edx
68 0072114D 89 85 48 FF FF FF mov dword ptr [ebp+FFFFFF48h],eax
69 00721153 8B 8D 48 FF FF FF mov ecx,dword ptr [ebp+FFFFFF48h]
70 00721159 8B 01 mov eax,dword ptr [ecx]
71 0072115B 89 85 54 FF FF FF mov dword ptr [ebp+FFFFFF54h],eax
72 00721161 8B 85 54 FF FF FF mov eax,dword ptr [ebp+FFFFFF54h]
73 00721167 89 C2 mov edx,eax
74 00721169 8B 85 50 FF FF FF mov eax,dword ptr [ebp+FFFFFF50h]
75 0072116F 31 C9 xor ecx,ecx
76 00721171 39 D0 cmp eax,edx
77 00721173 0F 9F F9 setg cl
78 00721176 89 C8 mov eax,ecx
79 00721178 88 85 48 FF FF FF mov byte ptr [ebp+FFFFFF48h],al
80 0072117E 31 C0 xor eax,eax
81 00721180 8A 85 48 FF FF FF mov al,byte ptr [ebp+FFFFFF48h]
82 00721186 A9 FF FF FF FF test eax,0FFFFFFFFh
83 0072118B 0F 84 02 01 00 00 je 00721293
84 00721191 B8 04 00 00 00 mov eax,4
85 00721196 89 C2 mov edx,eax
86 00721198 8B 85 5C FF FF FF mov eax,dword ptr [ebp+FFFFFF5Ch]
87 0072119E F7 EA imul edx
88 007211A0 89 85 4C FF FF FF mov dword ptr [ebp+FFFFFF4Ch],eax
89 007211A6 8B 85 4C FF FF FF mov eax,dword ptr [ebp+FFFFFF4Ch]
90 007211AC 89 C2 mov edx,eax
91 007211AE 8B 85 08 00 00 00 mov eax,dword ptr [ebp+00000008h]
92 007211B4 01 D0 add eax,edx
93 007211B6 89 85 50 FF FF FF mov dword ptr [ebp+FFFFFF50h],eax
94 007211BC 8B 8D 50 FF FF FF mov ecx,dword ptr [ebp+FFFFFF50h]
95 007211C2 8B 01 mov eax,dword ptr [ecx]
96 007211C4 89 85 60 FF FF FF mov dword ptr [ebp+FFFFFF60h],eax
97 007211CA B8 04 00 00 00 mov eax,4
98 007211CF 89 C2 mov edx,eax
99 007211D1 8B 85 5C FF FF FF mov eax,dword ptr [ebp+FFFFFF5Ch]
100 007211D7 F7 EA imul edx
101 007211D9 89 85 48 FF FF FF mov dword ptr [ebp+FFFFFF48h],eax
102 007211DF 8B 85 48 FF FF FF mov eax,dword ptr [ebp+FFFFFF48h]
103 007211E5 89 C2 mov edx,eax
104 007211E7 8B 85 08 00 00 00 mov eax,dword ptr [ebp+00000008h]
105 007211ED 01 D0 add eax,edx
106 007211EF 89 85 54 FF FF FF mov dword ptr [ebp+FFFFFF54h],eax
107 007211F5 B8 01 00 00 00 mov eax,1
108 007211FA 89 C2 mov edx,eax
109 007211FC 8B 85 5C FF FF FF mov eax,dword ptr [ebp+FFFFFF5Ch]
110 00721202 01 D0 add eax,edx
111 00721204 89 85 50 FF FF FF mov dword ptr [ebp+FFFFFF50h],eax
112 0072120A B8 04 00 00 00 mov eax,4
113 0072120F 89 C2 mov edx,eax
114 00721211 8B 85 50 FF FF FF mov eax,dword ptr [ebp+FFFFFF50h]
115 00721217 F7 EA imul edx
116 00721219 89 85 4C FF FF FF mov dword ptr [ebp+FFFFFF4Ch],eax
117 0072121F 8B 85 4C FF FF FF mov eax,dword ptr [ebp+FFFFFF4Ch]
118 00721225 89 C2 mov edx,eax
119 00721227 8B 85 08 00 00 00 mov eax,dword ptr [ebp+00000008h]
120 0072122D 01 D0 add eax,edx
121 0072122F 89 85 48 FF FF FF mov dword ptr [ebp+FFFFFF48h],eax
122 00721235 8B 8D 48 FF FF FF mov ecx,dword ptr [ebp+FFFFFF48h]
123 0072123B 8B 01 mov eax,dword ptr [ecx]
124 0072123D 8B 8D 54 FF FF FF mov ecx,dword ptr [ebp+FFFFFF54h]
125 00721243 89 01 mov dword ptr [ecx],eax
126 00721245 B8 01 00 00 00 mov eax,1
127 0072124A 89 C2 mov edx,eax
128 0072124C 8B 85 5C FF FF FF mov eax,dword ptr [ebp+FFFFFF5Ch]
129 00721252 01 D0 add eax,edx
130 00721254 89 85 48 FF FF FF mov dword ptr [ebp+FFFFFF48h],eax
131 0072125A B8 04 00 00 00 mov eax,4
132 0072125F 89 C2 mov edx,eax
133 00721261 8B 85 48 FF FF FF mov eax,dword ptr [ebp+FFFFFF48h]
134 00721267 F7 EA imul edx
135 00721269 89 85 4C FF FF FF mov dword ptr [ebp+FFFFFF4Ch],eax
136 0072126F 8B 85 4C FF FF FF mov eax,dword ptr [ebp+FFFFFF4Ch]
137 00721275 89 C2 mov edx,eax
138 00721277 8B 85 08 00 00 00 mov eax,dword ptr [ebp+00000008h]
139 0072127D 01 D0 add eax,edx
140 0072127F 89 85 50 FF FF FF mov dword ptr [ebp+FFFFFF50h],eax
141 00721285 8B 85 60 FF FF FF mov eax,dword ptr [ebp+FFFFFF60h]
142 0072128B 8B 8D 50 FF FF FF mov ecx,dword ptr [ebp+FFFFFF50h]
143 00721291 89 01 mov dword ptr [ecx],eax
144 00721293 8D 85 5C FF FF FF lea eax,[ebp+FFFFFF5Ch]
145 00721299 89 85 54 FF FF FF mov dword ptr [ebp+FFFFFF54h],eax
146 0072129F B8 01 00 00 00 mov eax,1
147 007212A4 89 C2 mov edx,eax
148 007212A6 8B 8D 54 FF FF FF mov ecx,dword ptr [ebp+FFFFFF54h]
149 007212AC 8B 01 mov eax,dword ptr [ecx]
150 007212AE 29 D0 sub eax,edx
151 007212B0 8B 8D 54 FF FF FF mov ecx,dword ptr [ebp+FFFFFF54h]
152 007212B6 89 01 mov dword ptr [ecx],eax
153 007212B8 E9 ED FD FF FF jmp 007210AA
154 007212BD 8D 85 58 FF FF FF lea eax,[ebp+FFFFFF58h]
155 007212C3 89 85 48 FF FF FF mov dword ptr [ebp+FFFFFF48h],eax
156 007212C9 B8 01 00 00 00 mov eax,1
157 007212CE 89 C2 mov edx,eax
158 007212D0 8B 8D 48 FF FF FF mov ecx,dword ptr [ebp+FFFFFF48h]
159 007212D6 8B 01 mov eax,dword ptr [ecx]
160 007212D8 01 D0 add eax,edx
161 007212DA 8B 8D 48 FF FF FF mov ecx,dword ptr [ebp+FFFFFF48h]
162 007212E0 89 01 mov dword ptr [ecx],eax
163 007212E2 E9 69 FD FF FF jmp 00721050
164 007212E7 61 popad
165 007212E8 8B 85 FC FF FF FF mov eax,dword ptr [ebp+FFFFFFFCh]
166 007212EE 89 EC mov esp,ebp
167 007212F0 8F C5 pop ebp
168 007212F2 C2 08 00 ret 8
整个看起来都很糟糕啊……
posted on 2009-05-19 16:51
陈梓瀚(vczh) 阅读(2962)
评论(3) 编辑 收藏 引用 所属分类:
JIT