57e4efe9 |
1 | #include <stdio.h> |
2 | #include <stdlib.h> |
3 | #include <string.h> |
4 | |
5 | #include "my_assert.h" |
6 | #include "my_str.h" |
7 | |
232aca37 |
8 | #define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0])) |
3e52f54c |
9 | #define IS(w, y) !strcmp(w, y) |
232aca37 |
10 | |
c36e914d |
11 | #include "protoparse.h" |
232aca37 |
12 | |
13 | static int is_x86_reg_saved(const char *reg) |
14 | { |
15 | static const char *nosave_regs[] = { "eax", "edx", "ecx" }; |
16 | int nosave = 0; |
17 | int r; |
18 | |
19 | for (r = 0; r < ARRAY_SIZE(nosave_regs); r++) |
20 | if (strcmp(reg, nosave_regs[r]) == 0) |
21 | nosave = 1; |
22 | |
23 | return !nosave; |
24 | } |
25 | |
54e763a1 |
26 | static void out_toasm_x86(FILE *f, const char *sym_in, |
27 | const char *sym_out, const struct parsed_proto *pp) |
232aca37 |
28 | { |
232aca37 |
29 | int must_save = 0; |
30 | int sarg_ofs = 1; // stack offset to args, in DWORDs |
31 | int args_repushed = 0; |
4f12f671 |
32 | int argc_repush; |
232aca37 |
33 | int i; |
34 | |
4f12f671 |
35 | argc_repush = pp->argc; |
36 | if (pp->is_vararg) |
37 | argc_repush = ARRAY_SIZE(pp->arg); // hopefully enough? |
38 | |
c36e914d |
39 | for (i = 0; i < pp->argc; i++) { |
40 | if (pp->arg[i].reg != NULL) |
41 | must_save |= is_x86_reg_saved(pp->arg[i].reg); |
232aca37 |
42 | } |
43 | |
c0050df6 |
44 | fprintf(f, ".global %s%s\n", pp->is_fastcall ? "@" : "_", sym_in); |
45 | fprintf(f, "%s%s:\n", pp->is_fastcall ? "@" : "_", sym_in); |
232aca37 |
46 | |
c0050df6 |
47 | if (pp->argc_reg == 0 || pp->is_fastcall) { |
48 | fprintf(f, "\t# %s\n", |
49 | pp->is_fastcall ? "__fastcall" : |
50 | (pp->is_stdcall ? "__stdcall" : "__cdecl")); |
54e763a1 |
51 | fprintf(f, "\tjmp %s\n\n", sym_out); |
232aca37 |
52 | return; |
53 | } |
54 | |
4f12f671 |
55 | if (pp->argc_stack == 0 && !must_save && !pp->is_stdcall |
56 | && !pp->is_vararg) |
57 | { |
232aca37 |
58 | // load arg regs |
c36e914d |
59 | for (i = 0; i < pp->argc; i++) { |
232aca37 |
60 | fprintf(f, "\tmovl %d(%%esp), %%%s\n", |
c36e914d |
61 | (i + sarg_ofs) * 4, pp->arg[i].reg); |
232aca37 |
62 | } |
54e763a1 |
63 | fprintf(f, "\tjmp %s\n\n", sym_out); |
232aca37 |
64 | return; |
65 | } |
66 | |
67 | // save the regs |
c36e914d |
68 | for (i = 0; i < pp->argc; i++) { |
69 | if (pp->arg[i].reg != NULL && is_x86_reg_saved(pp->arg[i].reg)) { |
70 | fprintf(f, "\tpushl %%%s\n", pp->arg[i].reg); |
232aca37 |
71 | sarg_ofs++; |
72 | } |
73 | } |
74 | |
75 | // reconstruct arg stack |
4f12f671 |
76 | for (i = argc_repush - 1; i >= 0; i--) { |
c36e914d |
77 | if (pp->arg[i].reg == NULL) { |
232aca37 |
78 | fprintf(f, "\tmovl %d(%%esp), %%eax\n", |
79 | (i + sarg_ofs) * 4); |
80 | fprintf(f, "\tpushl %%eax\n"); |
81 | sarg_ofs++; |
82 | args_repushed++; |
83 | } |
84 | } |
4f12f671 |
85 | // my_assert(args_repushed, pp->argc_stack); |
232aca37 |
86 | |
87 | // load arg regs |
c36e914d |
88 | for (i = 0; i < pp->argc; i++) { |
89 | if (pp->arg[i].reg != NULL) { |
232aca37 |
90 | fprintf(f, "\tmovl %d(%%esp), %%%s\n", |
c36e914d |
91 | (i + sarg_ofs) * 4, pp->arg[i].reg); |
232aca37 |
92 | } |
93 | } |
94 | |
c36e914d |
95 | fprintf(f, "\n\t# %s\n", pp->is_stdcall ? "__stdcall" : "__cdecl"); |
54e763a1 |
96 | fprintf(f, "\tcall %s\n\n", sym_out); |
232aca37 |
97 | |
c36e914d |
98 | if (args_repushed && !pp->is_stdcall) |
a51421fa |
99 | fprintf(f, "\tadd $%d,%%esp\n", args_repushed * 4); |
232aca37 |
100 | |
101 | // restore regs |
c36e914d |
102 | for (i = pp->argc - 1; i >= 0; i--) { |
103 | if (pp->arg[i].reg != NULL && is_x86_reg_saved(pp->arg[i].reg)) |
104 | fprintf(f, "\tpopl %%%s\n", pp->arg[i].reg); |
232aca37 |
105 | } |
106 | |
107 | fprintf(f, "\tret\n\n"); |
108 | } |
109 | |
54e763a1 |
110 | static void out_fromasm_x86(FILE *f, const char *sym, |
111 | const struct parsed_proto *pp) |
232aca37 |
112 | { |
232aca37 |
113 | int sarg_ofs = 1; // stack offset to args, in DWORDs |
7ae48d73 |
114 | int saved_regs = 0; |
4f12f671 |
115 | int argc_repush; |
232aca37 |
116 | int stack_args; |
7ae48d73 |
117 | int ret64; |
232aca37 |
118 | int i; |
119 | |
4f12f671 |
120 | argc_repush = pp->argc; |
121 | stack_args = pp->argc_stack; |
122 | if (pp->is_vararg) { |
123 | argc_repush = ARRAY_SIZE(pp->arg); // hopefully enough? |
124 | stack_args = argc_repush - pp->argc_reg; |
125 | } |
126 | |
7ae48d73 |
127 | ret64 = strstr(pp->ret_type.name, "int64") != NULL; |
128 | |
c0050df6 |
129 | fprintf(f, "# %s", |
130 | pp->is_fastcall ? "__fastcall" : |
131 | (pp->is_stdcall ? "__stdcall" : "__cdecl")); |
7ae48d73 |
132 | if (ret64) |
133 | fprintf(f, " ret64"); |
134 | fprintf(f, "\n.global %s\n", sym); |
232aca37 |
135 | fprintf(f, "%s:\n", sym); |
136 | |
c0050df6 |
137 | if (pp->argc_reg == 0 || pp->is_fastcall) { |
138 | fprintf(f, "\tjmp %s%s", |
139 | pp->is_fastcall ? "@" : "_", sym); |
54e763a1 |
140 | if (pp->is_stdcall && pp->argc > 0) |
141 | fprintf(f, "@%d", pp->argc * 4); |
142 | fprintf(f, "\n\n"); |
232aca37 |
143 | return; |
144 | } |
145 | |
7ae48d73 |
146 | // at least sc sub_47B150 needs edx to be preserved |
147 | // int64 returns use edx:eax - no edx save |
148 | // we use ecx also as scratch |
149 | fprintf(f, "\tpushl %%ecx\n"); |
150 | saved_regs++; |
232aca37 |
151 | sarg_ofs++; |
7ae48d73 |
152 | if (!ret64) { |
153 | fprintf(f, "\tpushl %%edx\n"); |
154 | saved_regs++; |
155 | sarg_ofs++; |
156 | } |
232aca37 |
157 | |
158 | // construct arg stack |
4f12f671 |
159 | for (i = argc_repush - 1; i >= 0; i--) { |
c36e914d |
160 | if (pp->arg[i].reg == NULL) { |
7ae48d73 |
161 | fprintf(f, "\tmovl %d(%%esp), %%ecx\n", |
232aca37 |
162 | (sarg_ofs + stack_args - 1) * 4); |
7ae48d73 |
163 | fprintf(f, "\tpushl %%ecx\n"); |
232aca37 |
164 | stack_args--; |
165 | } |
166 | else { |
7ae48d73 |
167 | if (IS(pp->arg[i].reg, "ecx")) |
168 | // must reload original ecx |
169 | fprintf(f, "\tmovl %d(%%esp), %%ecx\n", |
f3d05b09 |
170 | (sarg_ofs - 2) * 4); |
171 | |
c36e914d |
172 | fprintf(f, "\tpushl %%%s\n", pp->arg[i].reg); |
232aca37 |
173 | } |
174 | sarg_ofs++; |
175 | } |
176 | |
177 | // no worries about calling conventions - always __cdecl |
178 | fprintf(f, "\n\tcall _%s\n\n", sym); |
179 | |
7ae48d73 |
180 | if (sarg_ofs > saved_regs + 1) |
181 | fprintf(f, "\tadd $%d,%%esp\n", |
182 | (sarg_ofs - (saved_regs + 1)) * 4); |
232aca37 |
183 | |
7ae48d73 |
184 | if (!ret64) |
185 | fprintf(f, "\tpopl %%edx\n"); |
186 | fprintf(f, "\tpopl %%ecx\n"); |
232aca37 |
187 | |
c36e914d |
188 | if (pp->is_stdcall && pp->argc_stack) |
189 | fprintf(f, "\tret $%d\n\n", pp->argc_stack * 4); |
232aca37 |
190 | else |
191 | fprintf(f, "\tret\n\n"); |
192 | } |
193 | |
57e4efe9 |
194 | int main(int argc, char *argv[]) |
195 | { |
232aca37 |
196 | FILE *fout, *fsyms_to, *fsyms_from, *fhdr; |
bd96f656 |
197 | const struct parsed_proto *pp; |
57e4efe9 |
198 | char line[256]; |
54e763a1 |
199 | char sym_noat[256]; |
57e4efe9 |
200 | char sym[256]; |
54e763a1 |
201 | char *p; |
202 | int ret = 1; |
57e4efe9 |
203 | |
232aca37 |
204 | if (argc != 5) { |
205 | printf("usage:\n%s <bridge.s> <toasm_symf> <fromasm_symf> <hdrf>\n", |
57e4efe9 |
206 | argv[0]); |
207 | return 1; |
208 | } |
209 | |
232aca37 |
210 | hdrfn = argv[4]; |
57e4efe9 |
211 | fhdr = fopen(hdrfn, "r"); |
212 | my_assert_not(fhdr, NULL); |
213 | |
232aca37 |
214 | fsyms_from = fopen(argv[3], "r"); |
215 | my_assert_not(fsyms_from, NULL); |
216 | |
217 | fsyms_to = fopen(argv[2], "r"); |
218 | my_assert_not(fsyms_to, NULL); |
57e4efe9 |
219 | |
220 | fout = fopen(argv[1], "w"); |
221 | my_assert_not(fout, NULL); |
222 | |
223 | fprintf(fout, ".text\n\n"); |
232aca37 |
224 | fprintf(fout, "# to asm\n\n"); |
57e4efe9 |
225 | |
232aca37 |
226 | while (fgets(line, sizeof(line), fsyms_to)) |
57e4efe9 |
227 | { |
228 | next_word(sym, sizeof(sym), line); |
229 | if (sym[0] == 0 || sym[0] == ';' || sym[0] == '#') |
230 | continue; |
231 | |
54e763a1 |
232 | // IDA asm doesn't do '@' notation.. |
233 | strcpy(sym_noat, sym); |
234 | p = strchr(sym_noat, '@'); |
235 | if (p != NULL) |
236 | *p = 0; |
237 | |
36595fd2 |
238 | pp = proto_parse(fhdr, sym_noat, 0); |
bd96f656 |
239 | if (pp == NULL) |
232aca37 |
240 | goto out; |
57e4efe9 |
241 | |
54e763a1 |
242 | out_toasm_x86(fout, sym, sym_noat, pp); |
232aca37 |
243 | } |
57e4efe9 |
244 | |
232aca37 |
245 | fprintf(fout, "# from asm\n\n"); |
57e4efe9 |
246 | |
232aca37 |
247 | while (fgets(line, sizeof(line), fsyms_from)) |
248 | { |
249 | next_word(sym, sizeof(sym), line); |
250 | if (sym[0] == 0 || sym[0] == ';' || sym[0] == '#') |
251 | continue; |
57e4efe9 |
252 | |
36595fd2 |
253 | pp = proto_parse(fhdr, sym, 0); |
bd96f656 |
254 | if (pp == NULL) |
232aca37 |
255 | goto out; |
57e4efe9 |
256 | |
bd96f656 |
257 | out_fromasm_x86(fout, sym, pp); |
57e4efe9 |
258 | } |
259 | |
232aca37 |
260 | ret = 0; |
261 | out: |
57e4efe9 |
262 | fclose(fout); |
232aca37 |
263 | fclose(fsyms_to); |
264 | fclose(fsyms_from); |
265 | fclose(fhdr); |
266 | if (ret) |
267 | remove(argv[1]); |
268 | |
269 | return ret; |
57e4efe9 |
270 | } |