4 struct parsed_proto_arg {
7 struct parsed_proto *fptr;
14 struct parsed_proto_arg arg[16];
18 unsigned int is_func:1;
19 unsigned int is_stdcall:1;
20 unsigned int is_fptr:1;
21 unsigned int is_array:1;
24 static const char *hdrfn;
25 static int hdrfline = 0;
27 static int find_protostr(char *dst, size_t dlen, FILE *fhdr,
28 const char *fname, const char *sym_)
30 const char *sym = sym_;
31 const char *finc_name;
38 if (sym[0] == '_' && strncmp(fname, "stdc", 4) == 0)
44 while (fgets(dst, dlen, fhdr))
47 if (strncmp(dst, "//#include ", 11) == 0) {
49 p = strpbrk(finc_name, "\r\n ");
53 finc = fopen(finc_name, "r");
55 printf("%s:%d: can't open '%s'\n",
56 fname, line, finc_name);
59 ret = find_protostr(dst, dlen, finc,
66 if (strncmp(sskip(dst), "//", 2) == 0)
70 if (p != NULL && p > dst
71 && (my_isblank(p[-1]) || my_issep(p[-1]))
72 && (my_isblank(p[symlen]) || my_issep(p[symlen])))
80 p = dst + strlen(dst);
81 for (p--; p > dst && my_isblank(*p); --p)
87 static int get_regparm(char *dst, size_t dlen, char *p)
94 for (o = 0, i = 1; o < dlen; i++) {
106 static const char *known_types[] = {
133 static int typecmp(const char *n, const char *t)
135 for (; *t != 0; n++, t++) {
136 while (n[0] == ' ' && (n[1] == ' ' || n[1] == '*'))
138 while (t[0] == ' ' && (t[1] == ' ' || t[1] == '*'))
147 static const char *check_type(const char *name)
151 for (i = 0; i < ARRAY_SIZE(known_types); i++) {
152 if (typecmp(name, known_types[i]) == 0)
153 return known_types[i];
159 /* args are always expanded to 32bit */
160 static const char *map_reg(const char *reg)
162 const char *regs_f[] = { "eax", "ebx", "ecx", "edx", "esi", "edi" };
163 const char *regs_w[] = { "ax", "bx", "cx", "dx", "si", "di" };
164 const char *regs_b[] = { "al", "bl", "cl", "dl" };
167 for (i = 0; i < ARRAY_SIZE(regs_w); i++)
168 if (IS(reg, regs_w[i]))
171 for (i = 0; i < ARRAY_SIZE(regs_b); i++)
172 if (IS(reg, regs_b[i]))
178 static int parse_protostr(char *protostr, struct parsed_proto *pp)
180 struct parsed_proto_arg *arg;
191 if (p[0] == '/' && p[1] == '/') {
192 printf("%s:%d: commented out?\n", hdrfn, hdrfline);
196 // strip unneeded stuff
197 for (p1 = p; p1[0] != 0 && p1[1] != 0; p1++) {
198 if ((p1[0] == '/' && p1[1] == '*')
199 || (p1[0] == '*' && p1[1] == '/'))
203 if (strncmp(p, "extern ", 7) == 0)
208 printf("%s:%d:%ld: unhandled return in '%s'\n",
209 hdrfn, hdrfline, (p - protostr) + 1, protostr);
216 if (!strchr(p, ')')) {
217 p = next_idt(buf, sizeof(buf), p);
220 printf("%s:%d:%ld: var name missing\n",
221 hdrfn, hdrfline, (p - protostr) + 1);
224 strcpy(pp->name, buf);
241 p = next_word(cconv, sizeof(cconv), p);
244 printf("%s:%d:%ld: cconv missing\n",
245 hdrfn, hdrfline, (p - protostr) + 1);
248 if (IS(cconv, "__cdecl"))
250 else if (IS(cconv, "__stdcall"))
252 else if (IS(cconv, "__fastcall"))
254 else if (IS(cconv, "__thiscall"))
256 else if (IS(cconv, "__userpurge"))
257 pp->is_stdcall = 1; // in all cases seen..
258 else if (IS(cconv, "__usercall"))
259 pp->is_stdcall = 0; // ..or is it?
261 printf("%s:%d:%ld: unhandled cconv: '%s'\n",
262 hdrfn, hdrfline, (p - protostr) + 1, cconv);
268 printf("%s:%d:%ld: '*' expected\n",
269 hdrfn, hdrfline, (p - protostr) + 1);
275 p = next_idt(buf, sizeof(buf), p);
278 printf("%s:%d:%ld: func name missing\n",
279 hdrfn, hdrfline, (p - protostr) + 1);
282 strcpy(pp->name, buf);
284 ret = get_regparm(regparm, sizeof(regparm), p);
286 if (!IS(regparm, "eax") && !IS(regparm, "ax")
287 && !IS(regparm, "al"))
289 printf("%s:%d:%ld: bad regparm: %s\n",
290 hdrfn, hdrfline, (p - protostr) + 1, regparm);
299 printf("%s:%d:%ld: ')' expected\n",
300 hdrfn, hdrfline, (p - protostr) + 1);
307 printf("%s:%d:%ld: '(' expected, got '%c'\n",
308 hdrfn, hdrfline, (p - protostr) + 1, *p);
315 if ((!strncmp(p, "void", 4) || !strncmp(p, "VOID", 4))
316 && *sskip(p + 4) == ')')
328 arg = &pp->arg[xarg];
334 printf("%s:%d:%ld: unhandled type for arg%d\n",
335 hdrfn, hdrfline, (p - protostr) + 1, xarg);
344 arg->fptr = calloc(1, sizeof(*arg->fptr));
345 ret = parse_protostr(p1, arg->fptr);
347 printf("%s:%d:%ld: funcarg parse failed\n",
348 hdrfn, hdrfline, p1 - protostr);
351 // we'll treat it as void * for non-calls
352 arg->type = "void *";
357 p = next_idt(buf, sizeof(buf), p);
361 printf("%s:%d:%ld: idt missing for arg%d\n",
362 hdrfn, hdrfline, (p - protostr) + 1, xarg);
368 ret = get_regparm(regparm, sizeof(regparm), p);
373 arg->reg = strdup(map_reg(regparm));
377 if (xarg > 0 && (IS(cconv, "__fastcall") || IS(cconv, "__thiscall"))) {
378 if (pp->arg[0].reg != NULL) {
379 printf("%s:%d: %s with arg1 spec %s?\n",
380 hdrfn, hdrfline, cconv, pp->arg[0].reg);
382 pp->arg[0].reg = strdup("ecx");
385 if (xarg > 1 && IS(cconv, "__fastcall")) {
386 if (pp->arg[1].reg != NULL) {
387 printf("%s:%d: %s with arg2 spec %s?\n",
388 hdrfn, hdrfline, cconv, pp->arg[1].reg);
390 pp->arg[1].reg = strdup("edx");
395 for (i = 0; i < pp->argc; i++) {
396 if (pp->arg[i].reg == NULL)
405 static int proto_parse(FILE *fhdr, const char *sym, struct parsed_proto *pp)
410 memset(pp, 0, sizeof(*pp));
412 ret = find_protostr(protostr, sizeof(protostr), fhdr, hdrfn, sym);
414 printf("%s: sym '%s' is missing\n", hdrfn, sym);
418 return parse_protostr(protostr, pp) < 0 ? -1 : 0;
421 static void proto_release(struct parsed_proto *pp)
425 for (i = 0; i < pp->argc; i++) {
426 if (pp->arg[i].reg != NULL)
427 free(pp->arg[i].reg);
428 if (pp->arg[i].fptr != NULL)
429 free(pp->arg[i].fptr);