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[] = {
138 static const char *ignored_keywords[] = {
146 // returns ptr to char after type ends
147 static int typecmp(const char *n, const char *t)
149 for (; *t != 0; n++, t++) {
150 while (n[0] == ' ' && (n[1] == ' ' || n[1] == '*'))
152 while (t[0] == ' ' && (t[1] == ' ' || t[1] == '*'))
161 static const char *skip_type_mod(const char *n)
166 for (i = 0; i < ARRAY_SIZE(known_type_mod); i++) {
167 len = strlen(known_type_mod[i]);
168 if (strncmp(n, known_type_mod[i], len) != 0)
172 while (my_isblank(*n))
180 static int check_type(const char *name, struct parsed_type *type)
186 n = skip_type_mod(name);
188 for (i = 0; i < ARRAY_SIZE(known_ptr_types); i++) {
189 if (typecmp(n, known_ptr_types[i]))
196 if (n[0] == 'L' && n[1] == 'P')
199 // assume single word
200 while (!my_isblank(*n) && !my_issep(*n))
205 while (my_isblank(*n))
216 type->name = strndup(name, ret);
220 /* args are always expanded to 32bit */
221 static const char *map_reg(const char *reg)
223 const char *regs_f[] = { "eax", "ebx", "ecx", "edx", "esi", "edi" };
224 const char *regs_w[] = { "ax", "bx", "cx", "dx", "si", "di" };
225 const char *regs_b[] = { "al", "bl", "cl", "dl" };
228 for (i = 0; i < ARRAY_SIZE(regs_w); i++)
229 if (IS(reg, regs_w[i]))
232 for (i = 0; i < ARRAY_SIZE(regs_b); i++)
233 if (IS(reg, regs_b[i]))
239 static int parse_protostr(char *protostr, struct parsed_proto *pp)
241 struct parsed_proto_arg *arg;
251 if (p[0] == '/' && p[1] == '/') {
252 printf("%s:%d: commented out?\n", hdrfn, hdrfline);
256 // strip unneeded stuff
257 for (p1 = p; p1[0] != 0 && p1[1] != 0; p1++) {
258 if ((p1[0] == '/' && p1[1] == '*')
259 || (p1[0] == '*' && p1[1] == '/'))
263 for (i = 0; i < ARRAY_SIZE(ignored_keywords); i++) {
264 l = strlen(ignored_keywords[i]);
265 if (!strncmp(p, ignored_keywords[i], l) && my_isblank(p[l]))
266 p = sskip(p + l + 1);
269 ret = check_type(p, &pp->ret_type);
271 printf("%s:%d:%zd: unhandled return in '%s'\n",
272 hdrfn, hdrfline, (p - protostr) + 1, protostr);
277 if (!strchr(p, ')')) {
278 p = next_idt(buf, sizeof(buf), p);
281 printf("%s:%d:%zd: var name missing\n",
282 hdrfn, hdrfline, (p - protostr) + 1);
285 strcpy(pp->name, buf);
290 pp->ret_type.is_array = 1;
302 p = next_word(cconv, sizeof(cconv), p);
305 printf("%s:%d:%zd: cconv missing\n",
306 hdrfn, hdrfline, (p - protostr) + 1);
309 if (IS(cconv, "__cdecl"))
311 else if (IS(cconv, "__stdcall"))
313 else if (IS(cconv, "__fastcall"))
315 else if (IS(cconv, "__thiscall"))
317 else if (IS(cconv, "__userpurge"))
318 pp->is_stdcall = 1; // in all cases seen..
319 else if (IS(cconv, "__usercall"))
320 pp->is_stdcall = 0; // ..or is it?
321 else if (IS(cconv, "WINAPI"))
324 printf("%s:%d:%zd: unhandled cconv: '%s'\n",
325 hdrfn, hdrfline, (p - protostr) + 1, cconv);
331 printf("%s:%d:%zd: '*' expected\n",
332 hdrfn, hdrfline, (p - protostr) + 1);
338 p = next_idt(buf, sizeof(buf), p);
341 printf("%s:%d:%zd: func name missing\n",
342 hdrfn, hdrfline, (p - protostr) + 1);
345 strcpy(pp->name, buf);
347 ret = get_regparm(regparm, sizeof(regparm), p);
349 if (!IS(regparm, "eax") && !IS(regparm, "ax")
350 && !IS(regparm, "al"))
352 printf("%s:%d:%zd: bad regparm: %s\n",
353 hdrfn, hdrfline, (p - protostr) + 1, regparm);
362 printf("%s:%d:%zd: ')' expected\n",
363 hdrfn, hdrfline, (p - protostr) + 1);
370 printf("%s:%d:%zd: '(' expected, got '%c'\n",
371 hdrfn, hdrfline, (p - protostr) + 1, *p);
378 if ((!strncmp(p, "void", 4) || !strncmp(p, "VOID", 4))
379 && *sskip(p + 4) == ')')
391 if (!strncmp(p, "...", 3)) {
398 printf("%s:%d:%zd: ')' expected\n",
399 hdrfn, hdrfline, (p - protostr) + 1);
403 arg = &pp->arg[xarg];
407 ret = check_type(p, &arg->type);
409 printf("%s:%d:%zd: unhandled type for arg%d\n",
410 hdrfn, hdrfline, (p - protostr) + 1, xarg);
417 arg->fptr = calloc(1, sizeof(*arg->fptr));
418 ret = parse_protostr(p1, arg->fptr);
420 printf("%s:%d:%zd: funcarg parse failed\n",
421 hdrfn, hdrfline, p1 - protostr);
424 // we'll treat it as void * for non-calls
425 arg->type.name = "void *";
426 arg->type.is_ptr = 1;
431 p = next_idt(buf, sizeof(buf), p);
435 printf("%s:%d:%zd: idt missing for arg%d\n",
436 hdrfn, hdrfline, (p - protostr) + 1, xarg);
442 ret = get_regparm(regparm, sizeof(regparm), p);
447 arg->reg = strdup(map_reg(regparm));
451 if (xarg > 0 && (IS(cconv, "__fastcall") || IS(cconv, "__thiscall"))) {
452 if (pp->arg[0].reg != NULL) {
453 printf("%s:%d: %s with arg1 spec %s?\n",
454 hdrfn, hdrfline, cconv, pp->arg[0].reg);
456 pp->arg[0].reg = strdup("ecx");
459 if (xarg > 1 && IS(cconv, "__fastcall")) {
460 if (pp->arg[1].reg != NULL) {
461 printf("%s:%d: %s with arg2 spec %s?\n",
462 hdrfn, hdrfline, cconv, pp->arg[1].reg);
464 pp->arg[1].reg = strdup("edx");
467 if (pp->is_vararg && pp->is_stdcall) {
468 printf("%s:%d: vararg stdcall?\n", hdrfn, hdrfline);
474 for (i = 0; i < pp->argc; i++) {
475 if (pp->arg[i].reg == NULL)
484 static int proto_parse(FILE *fhdr, const char *sym, struct parsed_proto *pp)
489 memset(pp, 0, sizeof(*pp));
491 ret = find_protostr(protostr, sizeof(protostr), fhdr, hdrfn, sym);
493 printf("%s: sym '%s' is missing\n", hdrfn, sym);
497 return parse_protostr(protostr, pp) < 0 ? -1 : 0;
500 static void proto_release(struct parsed_proto *pp)
504 for (i = 0; i < pp->argc; i++) {
505 if (pp->arg[i].reg != NULL)
506 free(pp->arg[i].reg);
507 if (pp->arg[i].type.name != NULL)
508 free(pp->arg[i].type.name);
509 if (pp->arg[i].fptr != NULL)
510 free(pp->arg[i].fptr);
512 if (pp->ret_type.name != NULL)
513 free(pp->ret_type.name);