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 | |
c36e914d |
26 | static void out_toasm_x86(FILE *f, char *sym, struct parsed_proto *pp) |
232aca37 |
27 | { |
232aca37 |
28 | int must_save = 0; |
29 | int sarg_ofs = 1; // stack offset to args, in DWORDs |
30 | int args_repushed = 0; |
4f12f671 |
31 | int argc_repush; |
232aca37 |
32 | int i; |
33 | |
4f12f671 |
34 | argc_repush = pp->argc; |
35 | if (pp->is_vararg) |
36 | argc_repush = ARRAY_SIZE(pp->arg); // hopefully enough? |
37 | |
c36e914d |
38 | for (i = 0; i < pp->argc; i++) { |
39 | if (pp->arg[i].reg != NULL) |
40 | must_save |= is_x86_reg_saved(pp->arg[i].reg); |
232aca37 |
41 | } |
42 | |
a51421fa |
43 | fprintf(f, ".global _%s\n", sym); |
44 | fprintf(f, "_%s:\n", sym); |
232aca37 |
45 | |
c36e914d |
46 | if (pp->argc_reg == 0 && !pp->is_stdcall) { |
232aca37 |
47 | fprintf(f, "\tjmp %s\n\n", sym); |
48 | return; |
49 | } |
50 | |
4f12f671 |
51 | if (pp->argc_stack == 0 && !must_save && !pp->is_stdcall |
52 | && !pp->is_vararg) |
53 | { |
232aca37 |
54 | // load arg regs |
c36e914d |
55 | for (i = 0; i < pp->argc; i++) { |
232aca37 |
56 | fprintf(f, "\tmovl %d(%%esp), %%%s\n", |
c36e914d |
57 | (i + sarg_ofs) * 4, pp->arg[i].reg); |
232aca37 |
58 | } |
59 | fprintf(f, "\tjmp %s\n\n", sym); |
60 | return; |
61 | } |
62 | |
63 | // save the regs |
c36e914d |
64 | for (i = 0; i < pp->argc; i++) { |
65 | if (pp->arg[i].reg != NULL && is_x86_reg_saved(pp->arg[i].reg)) { |
66 | fprintf(f, "\tpushl %%%s\n", pp->arg[i].reg); |
232aca37 |
67 | sarg_ofs++; |
68 | } |
69 | } |
70 | |
71 | // reconstruct arg stack |
4f12f671 |
72 | for (i = argc_repush - 1; i >= 0; i--) { |
c36e914d |
73 | if (pp->arg[i].reg == NULL) { |
232aca37 |
74 | fprintf(f, "\tmovl %d(%%esp), %%eax\n", |
75 | (i + sarg_ofs) * 4); |
76 | fprintf(f, "\tpushl %%eax\n"); |
77 | sarg_ofs++; |
78 | args_repushed++; |
79 | } |
80 | } |
4f12f671 |
81 | // my_assert(args_repushed, pp->argc_stack); |
232aca37 |
82 | |
83 | // load arg regs |
c36e914d |
84 | for (i = 0; i < pp->argc; i++) { |
85 | if (pp->arg[i].reg != NULL) { |
232aca37 |
86 | fprintf(f, "\tmovl %d(%%esp), %%%s\n", |
c36e914d |
87 | (i + sarg_ofs) * 4, pp->arg[i].reg); |
232aca37 |
88 | } |
89 | } |
90 | |
c36e914d |
91 | fprintf(f, "\n\t# %s\n", pp->is_stdcall ? "__stdcall" : "__cdecl"); |
232aca37 |
92 | fprintf(f, "\tcall %s\n\n", sym); |
93 | |
c36e914d |
94 | if (args_repushed && !pp->is_stdcall) |
a51421fa |
95 | fprintf(f, "\tadd $%d,%%esp\n", args_repushed * 4); |
232aca37 |
96 | |
97 | // restore regs |
c36e914d |
98 | for (i = pp->argc - 1; i >= 0; i--) { |
99 | if (pp->arg[i].reg != NULL && is_x86_reg_saved(pp->arg[i].reg)) |
100 | fprintf(f, "\tpopl %%%s\n", pp->arg[i].reg); |
232aca37 |
101 | } |
102 | |
103 | fprintf(f, "\tret\n\n"); |
104 | } |
105 | |
c36e914d |
106 | static void out_fromasm_x86(FILE *f, char *sym, struct parsed_proto *pp) |
232aca37 |
107 | { |
232aca37 |
108 | int sarg_ofs = 1; // stack offset to args, in DWORDs |
4f12f671 |
109 | int argc_repush; |
232aca37 |
110 | int stack_args; |
111 | int i; |
112 | |
4f12f671 |
113 | argc_repush = pp->argc; |
114 | stack_args = pp->argc_stack; |
115 | if (pp->is_vararg) { |
116 | argc_repush = ARRAY_SIZE(pp->arg); // hopefully enough? |
117 | stack_args = argc_repush - pp->argc_reg; |
118 | } |
119 | |
c36e914d |
120 | fprintf(f, "# %s\n", pp->is_stdcall ? "__stdcall" : "__cdecl"); |
232aca37 |
121 | fprintf(f, ".global %s\n", sym); |
122 | fprintf(f, "%s:\n", sym); |
123 | |
c36e914d |
124 | if (pp->argc_reg == 0 && !pp->is_stdcall) { |
232aca37 |
125 | fprintf(f, "\tjmp _%s\n\n", sym); |
126 | return; |
127 | } |
128 | |
129 | fprintf(f, "\tpushl %%edx\n"); // just in case.. |
130 | sarg_ofs++; |
131 | |
132 | // construct arg stack |
4f12f671 |
133 | for (i = argc_repush - 1; i >= 0; i--) { |
c36e914d |
134 | if (pp->arg[i].reg == NULL) { |
232aca37 |
135 | fprintf(f, "\tmovl %d(%%esp), %%edx\n", |
136 | (sarg_ofs + stack_args - 1) * 4); |
137 | fprintf(f, "\tpushl %%edx\n"); |
138 | stack_args--; |
139 | } |
140 | else { |
f3d05b09 |
141 | if (IS(pp->arg[i].reg, "edx")) |
142 | // must reload original edx |
143 | fprintf(f, "\tmovl %d(%%esp), %%edx\n", |
144 | (sarg_ofs - 2) * 4); |
145 | |
c36e914d |
146 | fprintf(f, "\tpushl %%%s\n", pp->arg[i].reg); |
232aca37 |
147 | } |
148 | sarg_ofs++; |
149 | } |
150 | |
151 | // no worries about calling conventions - always __cdecl |
152 | fprintf(f, "\n\tcall _%s\n\n", sym); |
153 | |
154 | if (sarg_ofs > 2) |
a51421fa |
155 | fprintf(f, "\tadd $%d,%%esp\n", (sarg_ofs - 2) * 4); |
232aca37 |
156 | |
157 | fprintf(f, "\tpopl %%edx\n"); |
158 | |
c36e914d |
159 | if (pp->is_stdcall && pp->argc_stack) |
160 | fprintf(f, "\tret $%d\n\n", pp->argc_stack * 4); |
232aca37 |
161 | else |
162 | fprintf(f, "\tret\n\n"); |
163 | } |
164 | |
57e4efe9 |
165 | int main(int argc, char *argv[]) |
166 | { |
232aca37 |
167 | FILE *fout, *fsyms_to, *fsyms_from, *fhdr; |
c36e914d |
168 | struct parsed_proto pp; |
57e4efe9 |
169 | char line[256]; |
170 | char sym[256]; |
57e4efe9 |
171 | int ret; |
172 | |
232aca37 |
173 | if (argc != 5) { |
174 | printf("usage:\n%s <bridge.s> <toasm_symf> <fromasm_symf> <hdrf>\n", |
57e4efe9 |
175 | argv[0]); |
176 | return 1; |
177 | } |
178 | |
232aca37 |
179 | hdrfn = argv[4]; |
57e4efe9 |
180 | fhdr = fopen(hdrfn, "r"); |
181 | my_assert_not(fhdr, NULL); |
182 | |
232aca37 |
183 | fsyms_from = fopen(argv[3], "r"); |
184 | my_assert_not(fsyms_from, NULL); |
185 | |
186 | fsyms_to = fopen(argv[2], "r"); |
187 | my_assert_not(fsyms_to, NULL); |
57e4efe9 |
188 | |
189 | fout = fopen(argv[1], "w"); |
190 | my_assert_not(fout, NULL); |
191 | |
192 | fprintf(fout, ".text\n\n"); |
232aca37 |
193 | fprintf(fout, "# to asm\n\n"); |
57e4efe9 |
194 | |
232aca37 |
195 | while (fgets(line, sizeof(line), fsyms_to)) |
57e4efe9 |
196 | { |
197 | next_word(sym, sizeof(sym), line); |
198 | if (sym[0] == 0 || sym[0] == ';' || sym[0] == '#') |
199 | continue; |
200 | |
c36e914d |
201 | ret = proto_parse(fhdr, sym, &pp); |
232aca37 |
202 | if (ret) |
203 | goto out; |
57e4efe9 |
204 | |
c36e914d |
205 | out_toasm_x86(fout, sym, &pp); |
206 | proto_release(&pp); |
232aca37 |
207 | } |
57e4efe9 |
208 | |
232aca37 |
209 | fprintf(fout, "# from asm\n\n"); |
57e4efe9 |
210 | |
232aca37 |
211 | while (fgets(line, sizeof(line), fsyms_from)) |
212 | { |
213 | next_word(sym, sizeof(sym), line); |
214 | if (sym[0] == 0 || sym[0] == ';' || sym[0] == '#') |
215 | continue; |
57e4efe9 |
216 | |
c36e914d |
217 | ret = proto_parse(fhdr, sym, &pp); |
232aca37 |
218 | if (ret) |
219 | goto out; |
57e4efe9 |
220 | |
c36e914d |
221 | out_fromasm_x86(fout, sym, &pp); |
222 | proto_release(&pp); |
57e4efe9 |
223 | } |
224 | |
232aca37 |
225 | ret = 0; |
226 | out: |
57e4efe9 |
227 | fclose(fout); |
232aca37 |
228 | fclose(fsyms_to); |
229 | fclose(fsyms_from); |
230 | fclose(fhdr); |
231 | if (ret) |
232 | remove(argv[1]); |
233 | |
234 | return ret; |
57e4efe9 |
235 | } |