15 static const char *hdrfn;
16 static int hdrfline = 0;
18 static int find_protostr(char *dst, size_t dlen, FILE *fhdr, const char *sym)
25 while (fgets(dst, dlen, fhdr))
28 if (strstr(dst, sym) != NULL)
36 p = dst + strlen(dst);
37 for (p--; p > dst && my_isblank(*p); --p)
43 static int get_regparm(char *dst, size_t dlen, char *p)
50 for (o = 0, i = 1; o < dlen; i++) {
62 static const char *known_types[] = {
89 static const char *check_type(const char *name)
93 for (i = 0; i < ARRAY_SIZE(known_types); i++) {
94 l = strlen(known_types[i]);
95 if (strncmp(known_types[i], name, l) == 0)
96 return known_types[i];
102 /* args are always expanded to 32bit */
103 static const char *map_reg(const char *reg)
105 const char *regs_f[] = { "eax", "ebx", "ecx", "edx", "esi", "edi" };
106 const char *regs_w[] = { "ax", "bx", "cx", "dx", "si", "di" };
107 const char *regs_b[] = { "al", "bl", "cl", "dl" };
110 for (i = 0; i < ARRAY_SIZE(regs_w); i++)
111 if (IS(reg, regs_w[i]))
114 for (i = 0; i < ARRAY_SIZE(regs_b); i++)
115 if (IS(reg, regs_b[i]))
121 static int parse_protostr(char *protostr, struct parsed_proto *pp)
133 if (p[0] == '/' && p[1] == '/') {
134 //printf("warning: decl for sym '%s' is commented out\n", sym);
140 printf("%s:%d:%ld: unhandled return in '%s'\n",
141 hdrfn, hdrfline, (p - protostr) + 1, protostr);
148 p = next_word(cconv, sizeof(cconv), p);
151 printf("%s:%d:%ld: cconv missing\n",
152 hdrfn, hdrfline, (p - protostr) + 1);
155 if (IS(cconv, "__cdecl"))
157 else if (IS(cconv, "__stdcall"))
159 else if (IS(cconv, "__fastcall"))
161 else if (IS(cconv, "__thiscall"))
163 else if (IS(cconv, "__userpurge"))
164 pp->is_stdcall = 1; // in all cases seen..
165 else if (IS(cconv, "__usercall"))
166 pp->is_stdcall = 0; // ..or is it?
168 printf("%s:%d:%ld: unhandled cconv: '%s'\n",
169 hdrfn, hdrfline, (p - protostr) + 1, cconv);
173 p = next_idt(buf, sizeof(buf), p);
176 printf("%s:%d:%ld: func name missing\n",
177 hdrfn, hdrfline, (p - protostr) + 1);
181 ret = get_regparm(regparm, sizeof(regparm), p);
183 if (!IS(regparm, "eax") && !IS(regparm, "ax")
184 && !IS(regparm, "al"))
186 printf("%s:%d:%ld: bad regparm: %s\n",
187 hdrfn, hdrfline, (p - protostr) + 1, regparm);
195 printf("%s:%d:%ld: '(' expected, got '%c'\n",
196 hdrfn, hdrfline, (p - protostr) + 1, *p);
212 printf("%s:%d:%ld: unhandled type for arg%d\n",
213 hdrfn, hdrfline, (p - protostr) + 1, xarg);
216 pp->arg[xarg - 1].type = kt;
220 p = next_idt(buf, sizeof(buf), p);
224 printf("%s:%d:%ld: idt missing for arg%d\n",
225 hdrfn, hdrfline, (p - protostr) + 1, xarg);
229 pp->arg[xarg - 1].reg = NULL;
231 ret = get_regparm(regparm, sizeof(regparm), p);
236 pp->arg[xarg - 1].reg = strdup(map_reg(regparm));
240 if (xarg > 0 && (IS(cconv, "__fastcall") || IS(cconv, "__thiscall"))) {
241 if (pp->arg[0].reg != NULL) {
242 printf("%s:%d: %s with arg1 spec %s?\n",
243 hdrfn, hdrfline, cconv, pp->arg[0].reg);
245 pp->arg[0].reg = strdup("ecx");
248 if (xarg > 1 && IS(cconv, "__fastcall")) {
249 if (pp->arg[1].reg != NULL) {
250 printf("%s:%d: %s with arg2 spec %s?\n",
251 hdrfn, hdrfline, cconv, pp->arg[1].reg);
253 pp->arg[1].reg = strdup("edx");
258 for (i = 0; i < pp->argc; i++) {
259 if (pp->arg[i].reg == NULL)
268 static int proto_parse(FILE *fhdr, const char *sym, struct parsed_proto *pp)
273 memset(pp, 0, sizeof(*pp));
275 ret = find_protostr(protostr, sizeof(protostr), fhdr, sym);
277 printf("%s: sym '%s' is missing\n", hdrfn, sym);
281 return parse_protostr(protostr, pp);
284 static void proto_release(struct parsed_proto *pp)
288 for (i = 0; i < pp->argc; i++) {
289 if (pp->arg[i].reg == NULL)
290 free(pp->arg[i].reg);