move from branch
[reactos.git] / rosapps / devutils / cputointel / ConvertToIA32Process.c
1
2 #include <windows.h>
3 #include <winnt.h>
4
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include "misc.h"
9 #include "any_op.h"
10
11 /*
12 * eax = register 3
13 * edx = register 4
14 * esp = register 1
15 * ebp = register 31
16
17 * ecx = 8
18 * ebx = 9
19 * esi = 10
20 * edi = 11
21 * mmx/sse/fpu 0 = 12
22 * mmx/sse/fpu 1 = 14
23 * mmx/sse/fpu 2 = 16
24 * mmx/sse/fpu 3 = 18
25 * mmx/sse/fpu 4 = 20
26 * mmx/sse/fpu 5 = 22
27 * mmx/sse/fpu 6 = 24
28 * mmx/sse/fpu 7 = 28
29 */
30
31 static void standardreg(CPU_INT *RegTableCount, CPU_INT reg, CPU_INT setup_ebp, FILE *outfp)
32 {
33 /* eax */
34 if (reg == RegTableCount[3])
35 {
36 fprintf(outfp,"eax");
37 }
38 /* ebp */
39 else if (reg == RegTableCount[31])
40 {
41 fprintf(outfp,"ebp");
42 }
43 /* edx */
44 else if (reg == RegTableCount[4])
45 {
46 fprintf(outfp,"edx");
47 }
48 /* esp */
49 else if (reg == RegTableCount[1])
50 {
51 fprintf(outfp,"esp");
52 }
53 /* ecx */
54 else if (reg == RegTableCount[8])
55 {
56 fprintf(outfp,"ecx");
57 }
58 /* ebx */
59 else if (reg == RegTableCount[9])
60 {
61 fprintf(outfp,"ebx");
62 }
63 /* esi */
64 else if (reg == RegTableCount[10])
65 {
66 fprintf(outfp,"esi");
67 }
68 /* edi */
69 else if (reg == RegTableCount[11])
70 {
71 fprintf(outfp,"edi");
72 }
73 else
74 {
75 if (setup_ebp == 1)
76 fprintf(outfp,"dword [ebx - %d]");
77 else
78 fprintf(outfp,"; unsuported should not happen it happen :(\n");
79 }
80 }
81
82 CPU_INT ConvertToIA32Process( FILE *outfp,
83 PMYBrainAnalys pMystart,
84 PMYBrainAnalys pMyend, CPU_INT regbits,
85 CPU_INT HowManyRegInUse,
86 CPU_INT *RegTableCount)
87 {
88
89 CPU_INT stack = 0;
90 CPU_UNINT tmp;
91 CPU_INT setup_ebp = 0 ; /* 0 = no, 1 = yes */
92 CPU_INT t=0;
93
94 /* Fixme optimze the RegTableCount table */
95
96 //if (HowManyRegInUse > 9)
97 if (HowManyRegInUse > 8)
98 {
99 setup_ebp =1; /* we will use ebx as ebp */
100 stack = HowManyRegInUse * regbits;
101 }
102
103 if (RegTableCount[1]!=0)
104 t++;
105 if (RegTableCount[3]!=0)
106 t++;
107 if (RegTableCount[4]!=0)
108 t++;
109 if (RegTableCount[8]!=0)
110 t++;
111 if (RegTableCount[9]!=0)
112 t++;
113 if (RegTableCount[10]!=0)
114 t++;
115 if (RegTableCount[11]!=0)
116 t++;
117 if (RegTableCount[31]!=0)
118 t++;
119
120 if (HowManyRegInUse != t)
121 {
122 /* fixme optimze the table or active the frame pointer */
123 setup_ebp =1; /* we will use ebx as ebp */
124 stack = HowManyRegInUse * regbits;
125 }
126
127 fprintf(outfp,"BITS 32\n");
128 fprintf(outfp,"GLOBAL _main\n");
129 fprintf(outfp,"SECTION .text\n\n");
130 fprintf(outfp,"; compile with nasm filename.asm -f win32, ld filename.obj -o filename.exe\n\n");
131 fprintf(outfp,"_main:\n");
132
133 /* setup a frame pointer */
134
135 if (setup_ebp == 1)
136 {
137 fprintf(outfp,"\n; Setup frame pointer \n");
138 fprintf(outfp,"push ebx\n");
139 fprintf(outfp,"mov ebx,esp\n");
140 fprintf(outfp,"sub esp, %d ; Alloc %d bytes for reg\n\n",stack,stack);
141 }
142
143
144 fprintf(outfp,"; Start the program \n");
145 while (pMystart!=NULL)
146 {
147 /* fixme the line lookup from anaylysing process */
148
149 /* mov not full implement */
150 if (pMystart->op == OP_ANY_mov)
151 {
152 printf("waring OP_ANY_mov are not full implement\n");
153
154 if ((pMystart->type & 8)== 8)
155 {
156 /* dst are register */
157 tmp = stack - (pMystart->dst*regbits);
158
159 if ((pMystart->type & 2)== 2)
160 {
161 fprintf(outfp,"mov ");
162 standardreg( RegTableCount,
163 pMystart->dst,
164 setup_ebp, outfp);
165 fprintf(outfp," , ");
166 standardreg( RegTableCount,
167 pMystart->src,
168 setup_ebp, outfp);
169 fprintf(outfp,"\n");
170
171 }
172 if ((pMystart->type & 16)== 16)
173 {
174 /* source are imm */
175 if ((pMystart->src == 0) &&
176 (setup_ebp == 0))
177 {
178 /* small optimze */
179 fprintf(outfp,"xor ");
180 standardreg( RegTableCount,
181 pMystart->dst,
182 setup_ebp, outfp);
183 fprintf(outfp,",");
184 standardreg( RegTableCount,
185 pMystart->dst,
186 setup_ebp, outfp);
187 fprintf(outfp,"\n");
188 }
189 else
190 {
191 fprintf(outfp,"mov ");
192 standardreg( RegTableCount,
193 pMystart->dst,
194 setup_ebp, outfp);
195 fprintf(outfp,",%llu\n",pMystart->src);
196 }
197 } /* end "source are imm" */
198 } /* end pMyBrainAnalys->type & 8 */
199
200 if ((pMystart->type & 64)== 64)
201 {
202 if ((pMystart->type & 2)== 2)
203 {
204 /* dest [eax - 0x20], source reg */
205
206 fprintf(outfp,"mov dword [");
207 standardreg( RegTableCount,
208 pMystart->dst,
209 setup_ebp, outfp);
210 if (pMystart->dst_extra>=0)
211 fprintf(outfp," +%d], ",pMystart->dst_extra);
212 else
213 fprintf(outfp," %d], ",pMystart->dst_extra);
214
215 standardreg( RegTableCount,
216 pMystart->src,
217 setup_ebp, outfp);
218 fprintf(outfp,"\n");
219
220 if ((pMystart->type & 128)== 128)
221 {
222 fprintf(outfp,"mov ");
223 standardreg( RegTableCount,
224 pMystart->src,
225 setup_ebp, outfp);
226 fprintf(outfp," , ");
227 standardreg( RegTableCount,
228 pMystart->dst,
229 setup_ebp, outfp);
230 fprintf(outfp," %d\n",pMystart->dst_extra);
231 }
232 }
233 }
234
235
236
237
238 }
239
240 /* return */
241 if (pMystart->op == OP_ANY_ret)
242 {
243 if (pMyBrainAnalys->ptr_next == NULL)
244 {
245 if (setup_ebp == 1)
246 {
247 fprintf(outfp,"\n; clean up after the frame \n");
248 fprintf(outfp,"mov esp, ebx\n");
249 fprintf(outfp,"pop ebx\n");
250 }
251 }
252 fprintf(outfp,"ret\n");
253 }
254 if (pMystart == pMyend)
255 pMystart=NULL;
256 else
257 pMystart = (PMYBrainAnalys) pMystart->ptr_next;
258
259 }
260 return 0;
261 }