6 unsigned int is_array:1;
10 struct parsed_proto_arg {
12 struct parsed_type type;
13 struct parsed_proto *fptr;
20 struct parsed_type ret_type;
21 struct parsed_type type;
23 struct parsed_proto_arg arg[16];
27 unsigned int is_func:1;
28 unsigned int is_stdcall:1;
29 unsigned int is_vararg:1;
30 unsigned int is_fptr:1;
33 static const char *hdrfn;
34 static int hdrfline = 0;
36 static int find_protostr(char *dst, size_t dlen, FILE *fhdr,
37 const char *fname, const char *sym_)
39 const char *sym = sym_;
40 const char *finc_name;
47 if (sym[0] == '_' && strncmp(fname, "stdc", 4) == 0)
53 while (fgets(dst, dlen, fhdr))
56 if (strncmp(dst, "//#include ", 11) == 0) {
58 p = strpbrk(finc_name, "\r\n ");
62 finc = fopen(finc_name, "r");
64 printf("%s:%d: can't open '%s'\n",
65 fname, line, finc_name);
68 ret = find_protostr(dst, dlen, finc,
75 if (strncmp(sskip(dst), "//", 2) == 0)
79 if (p != NULL && p > dst
80 && (my_isblank(p[-1]) || my_issep(p[-1]))
81 && (my_isblank(p[symlen]) || my_issep(p[symlen])))
89 p = dst + strlen(dst);
90 for (p--; p > dst && my_isblank(*p); --p)
96 static int get_regparm(char *dst, size_t dlen, char *p)
103 for (o = 0, i = 1; o < dlen; i++) {
115 static const char *known_type_mod[] = {
123 static const char *known_ptr_types[] = {
136 static const char *ignored_keywords[] = {
142 // returns ptr to char after type ends
143 static int typecmp(const char *n, const char *t)
145 for (; *t != 0; n++, t++) {
146 while (n[0] == ' ' && (n[1] == ' ' || n[1] == '*'))
148 while (t[0] == ' ' && (t[1] == ' ' || t[1] == '*'))
157 static const char *skip_type_mod(const char *n)
162 for (i = 0; i < ARRAY_SIZE(known_type_mod); i++) {
163 len = strlen(known_type_mod[i]);
164 if (strncmp(n, known_type_mod[i], len) != 0)
168 while (my_isblank(*n))
176 static int check_type(const char *name, struct parsed_type *type)
182 n = skip_type_mod(name);
184 for (i = 0; i < ARRAY_SIZE(known_ptr_types); i++) {
185 if (typecmp(n, known_ptr_types[i]))
192 if (n[0] == 'L' && n[1] == 'P')
195 // assume single word
196 while (!my_isblank(*n) && !my_issep(*n))
201 while (my_isblank(*n))
212 type->name = strndup(name, ret);
216 /* args are always expanded to 32bit */
217 static const char *map_reg(const char *reg)
219 const char *regs_f[] = { "eax", "ebx", "ecx", "edx", "esi", "edi" };
220 const char *regs_w[] = { "ax", "bx", "cx", "dx", "si", "di" };
221 const char *regs_b[] = { "al", "bl", "cl", "dl" };
224 for (i = 0; i < ARRAY_SIZE(regs_w); i++)
225 if (IS(reg, regs_w[i]))
228 for (i = 0; i < ARRAY_SIZE(regs_b); i++)
229 if (IS(reg, regs_b[i]))
235 static int parse_protostr(char *protostr, struct parsed_proto *pp)
237 struct parsed_proto_arg *arg;
247 if (p[0] == '/' && p[1] == '/') {
248 printf("%s:%d: commented out?\n", hdrfn, hdrfline);
252 // strip unneeded stuff
253 for (p1 = p; p1[0] != 0 && p1[1] != 0; p1++) {
254 if ((p1[0] == '/' && p1[1] == '*')
255 || (p1[0] == '*' && p1[1] == '/'))
259 for (i = 0; i < ARRAY_SIZE(ignored_keywords); i++) {
260 l = strlen(ignored_keywords[i]);
261 if (!strncmp(p, ignored_keywords[i], l) && my_isblank(p[l]))
262 p = sskip(p + l + 1);
265 ret = check_type(p, &pp->ret_type);
267 printf("%s:%d:%zd: unhandled return in '%s'\n",
268 hdrfn, hdrfline, (p - protostr) + 1, protostr);
273 if (!strchr(p, ')')) {
274 p = next_idt(buf, sizeof(buf), p);
277 printf("%s:%d:%zd: var name missing\n",
278 hdrfn, hdrfline, (p - protostr) + 1);
281 strcpy(pp->name, buf);
286 pp->ret_type.is_array = 1;
298 p = next_word(cconv, sizeof(cconv), p);
301 printf("%s:%d:%zd: cconv missing\n",
302 hdrfn, hdrfline, (p - protostr) + 1);
305 if (IS(cconv, "__cdecl"))
307 else if (IS(cconv, "__stdcall"))
309 else if (IS(cconv, "__fastcall"))
311 else if (IS(cconv, "__thiscall"))
313 else if (IS(cconv, "__userpurge"))
314 pp->is_stdcall = 1; // in all cases seen..
315 else if (IS(cconv, "__usercall"))
316 pp->is_stdcall = 0; // ..or is it?
317 else if (IS(cconv, "WINAPI"))
320 printf("%s:%d:%zd: unhandled cconv: '%s'\n",
321 hdrfn, hdrfline, (p - protostr) + 1, cconv);
327 printf("%s:%d:%zd: '*' expected\n",
328 hdrfn, hdrfline, (p - protostr) + 1);
334 p = next_idt(buf, sizeof(buf), p);
337 printf("%s:%d:%zd: func name missing\n",
338 hdrfn, hdrfline, (p - protostr) + 1);
341 strcpy(pp->name, buf);
343 ret = get_regparm(regparm, sizeof(regparm), p);
345 if (!IS(regparm, "eax") && !IS(regparm, "ax")
346 && !IS(regparm, "al"))
348 printf("%s:%d:%zd: bad regparm: %s\n",
349 hdrfn, hdrfline, (p - protostr) + 1, regparm);
358 printf("%s:%d:%zd: ')' expected\n",
359 hdrfn, hdrfline, (p - protostr) + 1);
366 printf("%s:%d:%zd: '(' expected, got '%c'\n",
367 hdrfn, hdrfline, (p - protostr) + 1, *p);
374 if ((!strncmp(p, "void", 4) || !strncmp(p, "VOID", 4))
375 && *sskip(p + 4) == ')')
387 if (!strncmp(p, "...", 3)) {
394 printf("%s:%d:%zd: ')' expected\n",
395 hdrfn, hdrfline, (p - protostr) + 1);
399 arg = &pp->arg[xarg];
403 ret = check_type(p, &arg->type);
405 printf("%s:%d:%zd: unhandled type for arg%d\n",
406 hdrfn, hdrfline, (p - protostr) + 1, xarg);
413 arg->fptr = calloc(1, sizeof(*arg->fptr));
414 ret = parse_protostr(p1, arg->fptr);
416 printf("%s:%d:%zd: funcarg parse failed\n",
417 hdrfn, hdrfline, p1 - protostr);
420 // we'll treat it as void * for non-calls
421 arg->type.name = "void *";
422 arg->type.is_ptr = 1;
427 p = next_idt(buf, sizeof(buf), p);
431 printf("%s:%d:%zd: idt missing for arg%d\n",
432 hdrfn, hdrfline, (p - protostr) + 1, xarg);
438 ret = get_regparm(regparm, sizeof(regparm), p);
443 arg->reg = strdup(map_reg(regparm));
447 if (xarg > 0 && (IS(cconv, "__fastcall") || IS(cconv, "__thiscall"))) {
448 if (pp->arg[0].reg != NULL) {
449 printf("%s:%d: %s with arg1 spec %s?\n",
450 hdrfn, hdrfline, cconv, pp->arg[0].reg);
452 pp->arg[0].reg = strdup("ecx");
455 if (xarg > 1 && IS(cconv, "__fastcall")) {
456 if (pp->arg[1].reg != NULL) {
457 printf("%s:%d: %s with arg2 spec %s?\n",
458 hdrfn, hdrfline, cconv, pp->arg[1].reg);
460 pp->arg[1].reg = strdup("edx");
463 if (pp->is_vararg && pp->is_stdcall) {
464 printf("%s:%d: vararg stdcall?\n", hdrfn, hdrfline);
470 for (i = 0; i < pp->argc; i++) {
471 if (pp->arg[i].reg == NULL)
480 static int proto_parse(FILE *fhdr, const char *sym, struct parsed_proto *pp)
485 memset(pp, 0, sizeof(*pp));
487 ret = find_protostr(protostr, sizeof(protostr), fhdr, hdrfn, sym);
489 printf("%s: sym '%s' is missing\n", hdrfn, sym);
493 return parse_protostr(protostr, pp) < 0 ? -1 : 0;
496 static void proto_release(struct parsed_proto *pp)
500 for (i = 0; i < pp->argc; i++) {
501 if (pp->arg[i].reg != NULL)
502 free(pp->arg[i].reg);
503 if (pp->arg[i].type.name != NULL)
504 free(pp->arg[i].type.name);
505 if (pp->arg[i].fptr != NULL)
506 free(pp->arg[i].fptr);
508 if (pp->ret_type.name != NULL)
509 free(pp->ret_type.name);