5 * This work is licensed under the terms of 3-clause BSD license.
6 * See COPYING file in the top-level directory.
13 unsigned int is_array:1;
14 unsigned int is_ptr:1;
15 unsigned int is_struct:1; // split for args
16 unsigned int is_retreg:1; // register to return
19 struct parsed_proto_arg {
21 struct parsed_type type;
22 struct parsed_proto *fptr;
29 struct parsed_type ret_type;
30 struct parsed_type type;
32 struct parsed_proto_arg arg[16];
36 unsigned int is_func:1;
37 unsigned int is_stdcall:1;
38 unsigned int is_fastcall:1;
39 unsigned int is_vararg:1; // vararg func
40 unsigned int is_fptr:1;
41 unsigned int is_noreturn:1;
42 unsigned int is_unresolved:1;
43 unsigned int is_userstack:1;
44 unsigned int is_oslib:1; // OS/system library func
45 unsigned int is_arg:1; // declared in some func arg
46 unsigned int has_structarg:1;
47 unsigned int has_retreg:1;
50 static const char *hdrfn;
51 static int hdrfline = 0;
53 static void pp_copy_arg(struct parsed_proto_arg *d,
54 const struct parsed_proto_arg *s);
56 static int b_pp_c_handler(char *proto, const char *fname, int is_oslib);
58 static int do_protostrs(FILE *fhdr, const char *fname)
60 const char *finc_name;
61 const char *hdrfn_saved;
74 is_oslib = strstr(fname, "stdc.hlist")
75 || strstr(fname, "win32.hlist");
77 while (fgets(protostr, sizeof(protostr), fhdr))
80 if (strncmp(protostr, "//#include ", 11) == 0) {
81 finc_name = protostr + 11;
82 p = strpbrk(finc_name, "\r\n ");
87 p = strrchr(hdrfn_saved, '/');
89 memcpy(path, hdrfn_saved,
91 path[p - hdrfn_saved + 1] = 0;
93 snprintf(fname_inc, sizeof(fname_inc), "%s%s",
95 finc = fopen(fname_inc, "r");
97 printf("%s:%d: can't open '%s'\n",
98 fname_inc, line, finc_name);
101 ret = do_protostrs(finc, finc_name);
107 if (strncmp(sskip(protostr), "//", 2) == 0)
110 p = protostr + strlen(protostr);
111 for (p--; p >= protostr && my_isblank(*p); --p)
118 ret = b_pp_c_handler(protostr, hdrfn, is_oslib);
131 static int get_regparm(char *dst, size_t dlen, char *p, int *retreg)
146 for (o = 0; o < dlen; i++) {
158 static const char *known_type_mod[] = {
168 static const char *known_ptr_types[] = {
183 "HIMC", // DWORD in mingw, ptr in wine..
202 "PMEMORY_BASIC_INFORMATION",
214 static const char *ignored_keywords[] = {
222 // returns ptr to char after type ends
223 static int typecmp(const char *n, const char *t)
225 for (; *t != 0; n++, t++) {
226 while (n[0] == ' ' && (n[1] == ' ' || n[1] == '*'))
228 while (t[0] == ' ' && (t[1] == ' ' || t[1] == '*'))
237 static const char *skip_type_mod(const char *n)
242 for (i = 0; i < ARRAY_SIZE(known_type_mod); i++) {
243 len = strlen(known_type_mod[i]);
244 if (strncmp(n, known_type_mod[i], len) != 0)
246 if (!my_isblank(n[len]))
250 while (my_isblank(*n))
258 static int check_type(const char *name, struct parsed_type *type)
264 n = skip_type_mod(name);
266 for (i = 0; i < ARRAY_SIZE(known_ptr_types); i++) {
267 if (typecmp(n, known_ptr_types[i]))
274 if (n[0] == 'L' && n[1] == 'P' && strncmp(n, "LPARAM", 6))
277 // assume single word
278 while (!my_isblank(*n) && !my_issep(*n))
283 while (my_isblank(*n))
294 type->name = strndup(name, ret);
295 if (IS(type->name, "VOID"))
296 memcpy(type->name, "void", 4);
301 /* args are always expanded to 32bit */
302 static const char *map_reg(const char *reg)
304 const char *regs_f[] = { "eax", "ebx", "ecx", "edx", "esi", "edi" };
305 const char *regs_w[] = { "ax", "bx", "cx", "dx", "si", "di" };
306 const char *regs_b[] = { "al", "bl", "cl", "dl" };
309 for (i = 0; i < ARRAY_SIZE(regs_w); i++)
310 if (IS(reg, regs_w[i]))
313 for (i = 0; i < ARRAY_SIZE(regs_b); i++)
314 if (IS(reg, regs_b[i]))
320 static int check_struct_arg(struct parsed_proto_arg *arg)
322 if (IS(arg->type.name, "POINT"))
328 static int parse_protostr(char *protostr, struct parsed_proto *pp)
330 struct parsed_proto_arg *arg;
341 if (p[0] == '/' && p[1] == '/') {
342 printf("%s:%d: commented out?\n", hdrfn, hdrfline);
346 // strip unneeded stuff
347 for (p1 = p; p1[0] != 0 && p1[1] != 0; p1++) {
348 if ((p1[0] == '/' && p1[1] == '*')
349 || (p1[0] == '*' && p1[1] == '/'))
353 if (!strncmp(p, "DECLSPEC_NORETURN ", 18)) {
358 for (i = 0; i < ARRAY_SIZE(ignored_keywords); i++) {
359 l = strlen(ignored_keywords[i]);
360 if (!strncmp(p, ignored_keywords[i], l) && my_isblank(p[l]))
361 p = sskip(p + l + 1);
364 ret = check_type(p, &pp->ret_type);
366 printf("%s:%d:%zd: unhandled return in '%s'\n",
367 hdrfn, hdrfline, (p - protostr) + 1, protostr);
372 if (!strncmp(p, "noreturn ", 9)) {
377 if (!strchr(p, ')')) {
378 p = next_idt(buf, sizeof(buf), p);
381 printf("%s:%d:%zd: var name missing\n",
382 hdrfn, hdrfline, (p - protostr) + 1);
385 strcpy(pp->name, buf);
390 pp->ret_type.is_array = 1;
402 p = next_word(cconv, sizeof(cconv), p);
405 printf("%s:%d:%zd: cconv missing\n",
406 hdrfn, hdrfline, (p - protostr) + 1);
409 if (IS(cconv, "__cdecl"))
411 else if (IS(cconv, "__stdcall"))
413 else if (IS(cconv, "__fastcall")) {
415 pp->is_stdcall = 1; // sort of..
417 else if (IS(cconv, "__thiscall"))
419 else if (IS(cconv, "__userpurge"))
420 pp->is_stdcall = 1; // IDA
421 else if (IS(cconv, "__usercall"))
422 pp->is_stdcall = 0; // IDA
423 else if (IS(cconv, "__userstack")) {
424 pp->is_stdcall = 0; // custom
425 pp->is_userstack = 1;
427 else if (IS(cconv, "WINAPI"))
430 printf("%s:%d:%zd: unhandled cconv: '%s'\n",
431 hdrfn, hdrfline, (p - protostr) + 1, cconv);
437 printf("%s:%d:%zd: '*' expected\n",
438 hdrfn, hdrfline, (p - protostr) + 1);
442 // XXX: skipping extra asterisks, for now
448 p = next_idt(buf, sizeof(buf), p);
451 //printf("%s:%d:%zd: func name missing\n",
452 // hdrfn, hdrfline, (p - protostr) + 1);
455 strcpy(pp->name, buf);
457 ret = get_regparm(regparm, sizeof(regparm), p, &is_retreg);
459 if (!IS(regparm, "eax") && !IS(regparm, "ax")
460 && !IS(regparm, "al") && !IS(regparm, "edx:eax"))
462 printf("%s:%d:%zd: bad regparm: %s\n",
463 hdrfn, hdrfline, (p - protostr) + 1, regparm);
472 // not really ret_type is array, but ohwell
473 pp->ret_type.is_array = 1;
474 p = strchr(p + 1, ']');
476 printf("%s:%d:%zd: ']' expected\n",
477 hdrfn, hdrfline, (p - protostr) + 1);
483 printf("%s:%d:%zd: ')' expected\n",
484 hdrfn, hdrfline, (p - protostr) + 1);
491 printf("%s:%d:%zd: '(' expected, got '%c'\n",
492 hdrfn, hdrfline, (p - protostr) + 1, *p);
499 if ((!strncmp(p, "void", 4) || !strncmp(p, "VOID", 4))
500 && *sskip(p + 4) == ')')
511 printf("%s:%d:%zd: ',' expected\n",
512 hdrfn, hdrfline, (p - protostr) + 1);
518 if (!strncmp(p, "...", 3)) {
525 printf("%s:%d:%zd: ')' expected\n",
526 hdrfn, hdrfline, (p - protostr) + 1);
530 arg = &pp->arg[xarg];
534 ret = check_type(p, &arg->type);
536 printf("%s:%d:%zd: unhandled type for arg%d\n",
537 hdrfn, hdrfline, (p - protostr) + 1, xarg);
544 arg->fptr = calloc(1, sizeof(*arg->fptr));
545 ret = parse_protostr(p1, arg->fptr);
547 printf("%s:%d:%zd: funcarg parse failed\n",
548 hdrfn, hdrfline, p1 - protostr);
551 arg->fptr->is_arg = 1;
552 // we don't use actual names right now..
553 snprintf(arg->fptr->name,
554 sizeof(arg->fptr->name), "a%d", xarg);
555 // we'll treat it as void * for non-calls
556 arg->type.name = strdup("void *");
557 arg->type.is_ptr = 1;
562 p = next_idt(buf, sizeof(buf), p);
566 printf("%s:%d:%zd: idt missing for arg%d\n",
567 hdrfn, hdrfline, (p - protostr) + 1, xarg);
573 ret = get_regparm(regparm, sizeof(regparm), p, &is_retreg);
578 arg->reg = strdup(map_reg(regparm));
579 arg->type.is_retreg = is_retreg;
580 pp->has_retreg |= is_retreg;
583 if (strstr(arg->type.name, "int64")
584 || IS(arg->type.name, "double"))
587 free(arg->type.name);
588 arg->type.name = strdup("int");
589 pp_copy_arg(&pp->arg[xarg], arg);
593 ret = check_struct_arg(arg);
595 pp->has_structarg = 1;
596 arg->type.is_struct = 1;
597 free(arg->type.name);
598 arg->type.name = strdup("int");
599 for (l = 0; l < ret; l++) {
600 pp_copy_arg(&pp->arg[xarg], arg);
606 if (xarg > 0 && (IS(cconv, "__fastcall") || IS(cconv, "__thiscall"))) {
607 if (pp->arg[0].reg != NULL) {
608 printf("%s:%d: %s with arg1 spec %s?\n",
609 hdrfn, hdrfline, cconv, pp->arg[0].reg);
611 pp->arg[0].reg = strdup("ecx");
614 if (xarg > 1 && IS(cconv, "__fastcall")) {
615 if (pp->arg[1].reg != NULL) {
616 printf("%s:%d: %s with arg2 spec %s?\n",
617 hdrfn, hdrfline, cconv, pp->arg[1].reg);
619 pp->arg[1].reg = strdup("edx");
624 for (i = 0; i < pp->argc; i++) {
625 if (pp->arg[i].reg == NULL)
631 if (pp->argc == 1 && pp->arg[0].reg != NULL
632 && IS(pp->arg[0].reg, "ecx"))
636 else if (pp->argc_reg == 2
637 && pp->arg[0].reg != NULL && IS(pp->arg[0].reg, "ecx")
638 && pp->arg[1].reg != NULL && IS(pp->arg[1].reg, "edx"))
643 if (pp->is_vararg && (pp->is_stdcall || pp->is_fastcall)) {
644 printf("%s:%d: vararg %s?\n", hdrfn, hdrfline, cconv);
651 static int pp_name_cmp(const void *p1, const void *p2)
653 const struct parsed_proto *pp1 = p1, *pp2 = p2;
654 return strcmp(pp1->name, pp2->name);
657 static struct parsed_proto *pp_cache;
658 static int pp_cache_size;
659 static int pp_cache_alloc;
661 static int b_pp_c_handler(char *proto, const char *fname, int is_oslib)
665 if (pp_cache_size >= pp_cache_alloc) {
666 pp_cache_alloc = pp_cache_alloc * 2 + 64;
667 pp_cache = realloc(pp_cache, pp_cache_alloc
668 * sizeof(pp_cache[0]));
669 my_assert_not(pp_cache, NULL);
670 memset(pp_cache + pp_cache_size, 0,
671 (pp_cache_alloc - pp_cache_size)
672 * sizeof(pp_cache[0]));
675 ret = parse_protostr(proto, &pp_cache[pp_cache_size]);
679 pp_cache[pp_cache_size].is_oslib = is_oslib;
684 static void build_pp_cache(FILE *fhdr)
692 ret = do_protostrs(fhdr, hdrfn);
696 qsort(pp_cache, pp_cache_size, sizeof(pp_cache[0]), pp_name_cmp);
697 fseek(fhdr, pos, SEEK_SET);
700 static const struct parsed_proto *proto_parse(FILE *fhdr, const char *sym,
703 const struct parsed_proto *pp_ret;
704 struct parsed_proto pp_search;
707 if (pp_cache == NULL)
708 build_pp_cache(fhdr);
710 if (sym[0] == '_') // && strncmp(fname, "stdc", 4) == 0)
713 strcpy(pp_search.name, sym);
714 p = strchr(pp_search.name, '@');
718 pp_ret = bsearch(&pp_search, pp_cache, pp_cache_size,
719 sizeof(pp_cache[0]), pp_name_cmp);
720 if (pp_ret == NULL && !quiet)
721 printf("%s: sym '%s' is missing\n", hdrfn, sym);
726 static void pp_copy_arg(struct parsed_proto_arg *d,
727 const struct parsed_proto_arg *s)
729 memcpy(d, s, sizeof(*d));
731 if (s->reg != NULL) {
732 d->reg = strdup(s->reg);
733 my_assert_not(d->reg, NULL);
735 if (s->type.name != NULL) {
736 d->type.name = strdup(s->type.name);
737 my_assert_not(d->type.name, NULL);
739 if (s->fptr != NULL) {
740 d->fptr = malloc(sizeof(*d->fptr));
741 my_assert_not(d->fptr, NULL);
742 memcpy(d->fptr, s->fptr, sizeof(*d->fptr));
746 struct parsed_proto *proto_clone(const struct parsed_proto *pp_c)
748 struct parsed_proto *pp;
751 pp = malloc(sizeof(*pp));
752 my_assert_not(pp, NULL);
753 memcpy(pp, pp_c, sizeof(*pp)); // lazy..
755 // do the actual deep copy..
756 for (i = 0; i < pp_c->argc; i++)
757 pp_copy_arg(&pp->arg[i], &pp_c->arg[i]);
758 if (pp_c->ret_type.name != NULL)
759 pp->ret_type.name = strdup(pp_c->ret_type.name);
765 static inline int pp_cmp_func(const struct parsed_proto *pp1,
766 const struct parsed_proto *pp2)
770 if (pp1->argc != pp2->argc || pp1->argc_reg != pp2->argc_reg)
773 for (i = 0; i < pp1->argc; i++) {
774 if ((pp1->arg[i].reg != NULL) != (pp2->arg[i].reg != NULL))
777 if ((pp1->arg[i].reg != NULL)
778 && !IS(pp1->arg[i].reg, pp2->arg[i].reg))
788 static inline void pp_print(char *buf, size_t buf_size,
789 const struct parsed_proto *pp)
794 snprintf(buf, buf_size, "%s %s(", pp->ret_type.name, pp->name);
797 for (i = 0; i < pp->argc_reg; i++) {
798 snprintf(buf + l, buf_size - l, "%s%s",
799 i == 0 ? "" : ", ", pp->arg[i].reg);
802 if (pp->argc_stack > 0) {
803 snprintf(buf + l, buf_size - l, "%s{%d stack}",
804 i == 0 ? "" : ", ", pp->argc_stack);
807 snprintf(buf + l, buf_size - l, ")");
810 static inline void proto_release(struct parsed_proto *pp)
814 for (i = 0; i < pp->argc; i++) {
815 if (pp->arg[i].reg != NULL)
816 free(pp->arg[i].reg);
817 if (pp->arg[i].type.name != NULL)
818 free(pp->arg[i].type.name);
819 if (pp->arg[i].fptr != NULL)
820 free(pp->arg[i].fptr);
822 if (pp->ret_type.name != NULL)
823 free(pp->ret_type.name);