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 to caller
17 unsigned int is_va_list:1;
20 struct parsed_proto_arg {
22 struct parsed_type type;
23 struct parsed_proto *pp; // fptr or struct
30 struct parsed_type ret_type;
31 struct parsed_type type;
33 struct parsed_proto_arg arg[16];
37 unsigned int is_func:1;
38 unsigned int is_stdcall:1;
39 unsigned int is_fastcall:1;
40 unsigned int is_vararg:1; // vararg func
41 unsigned int is_fptr:1;
42 unsigned int is_noreturn:1;
43 unsigned int is_unresolved:1;
44 unsigned int is_userstack:1;
45 unsigned int is_include:1; // not from top-level header
46 unsigned int is_osinc:1; // OS/system library func
47 unsigned int is_cinc:1; // crt library func
48 unsigned int is_arg:1; // declared in some func arg
49 unsigned int has_structarg:1;
50 unsigned int has_retreg:1;
53 struct parsed_struct {
57 struct parsed_proto pp;
62 static const char *hdrfn;
63 static int hdrfline = 0;
65 static void pp_copy_arg(struct parsed_proto_arg *d,
66 const struct parsed_proto_arg *s);
68 static int b_pp_c_handler(char *proto, const char *fname,
69 int is_include, int is_osinc, int is_cinc);
70 static int struct_handler(FILE *fhdr, char *proto, int *line);
72 static int do_protostrs(FILE *fhdr, const char *fname, int is_include)
74 const char *finc_name;
75 const char *hdrfn_saved;
89 is_cinc = strstr(fname, "stdc.hlist") != NULL;
90 is_osinc = is_cinc || strstr(fname, "win32.hlist") != NULL;
92 while (fgets(protostr, sizeof(protostr), fhdr))
95 if (strncmp(protostr, "//#include ", 11) == 0) {
96 finc_name = protostr + 11;
97 p = strpbrk(finc_name, "\r\n ");
102 p = strrchr(hdrfn_saved, '/');
104 memcpy(path, hdrfn_saved,
105 p - hdrfn_saved + 1);
106 path[p - hdrfn_saved + 1] = 0;
108 snprintf(fname_inc, sizeof(fname_inc), "%s%s",
110 finc = fopen(fname_inc, "r");
112 printf("%s:%d: can't open '%s'\n",
113 fname_inc, line, finc_name);
116 ret = do_protostrs(finc, finc_name, 1);
122 if (strncmp(sskip(protostr), "//", 2) == 0)
125 p = protostr + strlen(protostr);
126 for (p--; p >= protostr && my_isblank(*p); --p)
133 if (!strncmp(protostr, "struct", 6)
134 && strchr(protostr, '{') != NULL)
135 ret = struct_handler(fhdr, protostr, &line);
137 ret = b_pp_c_handler(protostr, hdrfn,
138 is_include, is_osinc, is_cinc);
151 static int get_regparm(char *dst, size_t dlen, char *p, int *retreg)
166 for (o = 0; o < dlen; i++) {
178 static const char *known_type_mod[] = {
187 static const char *known_ptr_types[] = {
202 "HIMC", // DWORD in mingw, ptr in wine..
221 "PMEMORY_BASIC_INFORMATION",
233 static const char *ignored_keywords[] = {
241 static int typecmp(const char *n, const char *t)
243 for (; *t != 0; n++, t++) {
244 while (n[0] == ' ' && (n[1] == ' ' || n[1] == '*'))
246 while (t[0] == ' ' && (t[1] == ' ' || t[1] == '*'))
255 static const char *skip_type_mod(const char *n)
260 for (i = 0; i < ARRAY_SIZE(known_type_mod); i++) {
261 len = strlen(known_type_mod[i]);
262 if (strncmp(n, known_type_mod[i], len) != 0)
264 if (!my_isblank(n[len]))
268 while (my_isblank(*n))
276 static int check_type(const char *name, struct parsed_type *type)
282 n = skip_type_mod(name);
284 if (!strncmp(n, "struct", 6) && my_isblank(n[6])) {
288 while (my_isblank(*n))
292 for (i = 0; i < ARRAY_SIZE(known_ptr_types); i++) {
293 if (typecmp(n, known_ptr_types[i]))
300 if (n[0] == 'L' && n[1] == 'P' && strncmp(n, "LPARAM", 6))
303 // assume single word
304 while (!my_isblank(*n) && !my_issep(*n))
309 while (my_isblank(*n))
320 type->name = strndup(name, ret);
321 if (IS(type->name, "__VALIST") || IS(type->name, "va_list"))
322 type->is_va_list = 1;
323 if (IS(type->name, "VOID"))
324 memcpy(type->name, "void", 4);
329 /* args are always expanded to 32bit */
330 static const char *map_reg(const char *reg)
332 const char *regs_f[] = { "eax", "ebx", "ecx", "edx", "esi", "edi" };
333 const char *regs_w[] = { "ax", "bx", "cx", "dx", "si", "di" };
334 const char *regs_b[] = { "al", "bl", "cl", "dl" };
337 for (i = 0; i < ARRAY_SIZE(regs_w); i++)
338 if (IS(reg, regs_w[i]))
341 for (i = 0; i < ARRAY_SIZE(regs_b); i++)
342 if (IS(reg, regs_b[i]))
348 static int check_struct_arg(struct parsed_proto_arg *arg)
350 if (IS(arg->type.name, "POINT"))
356 static int parse_protostr(char *protostr, struct parsed_proto *pp);
358 static int parse_arg(char **p_, struct parsed_proto_arg *arg, int xarg)
365 arg->pp = calloc(1, sizeof(*arg->pp));
366 my_assert_not(arg->pp, NULL);
371 pe = strpbrk(pe, ",()");
374 if (*pe == ',' || *pe == ')')
376 pe = strchr(pe, ')');
382 if (pe - p > sizeof(buf) - 1)
384 memcpy(buf, p, pe - p);
387 ret = parse_protostr(buf, arg->pp);
391 // we don't use actual names right now...
392 snprintf(arg->pp->name, sizeof(arg->pp->name), "a%d", xarg);
394 if (!arg->type.is_struct)
395 // we'll treat it as void * for non-calls
396 arg->type.name = strdup("void *");
397 arg->type.is_ptr = 1;
404 static int parse_protostr(char *protostr, struct parsed_proto *pp)
406 struct parsed_proto_arg *arg;
417 if (p[0] == '/' && p[1] == '/') {
418 printf("%s:%d: commented out?\n", hdrfn, hdrfline);
422 // allow start of line comment
423 if (p[0] == '/' && p[1] == '*') {
424 p = strstr(p + 2, "*/");
426 printf("%s:%d: multiline comments unsupported\n",
433 // we need remaining hints in comments, so strip / *
434 for (p1 = p; p1[0] != 0 && p1[1] != 0; p1++) {
435 if ((p1[0] == '/' && p1[1] == '*')
436 || (p1[0] == '*' && p1[1] == '/'))
440 if (!strncmp(p, "DECLSPEC_NORETURN ", 18)) {
445 for (i = 0; i < ARRAY_SIZE(ignored_keywords); i++) {
446 l = strlen(ignored_keywords[i]);
447 if (!strncmp(p, ignored_keywords[i], l) && my_isblank(p[l]))
448 p = sskip(p + l + 1);
451 ret = check_type(p, &pp->ret_type);
453 printf("%s:%d:%zd: unhandled return in '%s'\n",
454 hdrfn, hdrfline, (p - protostr) + 1, protostr);
459 if (!strncmp(p, "noreturn ", 9)) {
464 if (!strchr(p, ')')) {
465 p = next_idt(buf, sizeof(buf), p);
467 if (!pp->is_arg && buf[0] == 0) {
468 printf("%s:%d:%zd: var name is missing\n",
469 hdrfn, hdrfline, (p - protostr) + 1);
472 strcpy(pp->name, buf);
477 pp->ret_type.is_array = 1;
489 p = next_word(cconv, sizeof(cconv), p);
492 printf("%s:%d:%zd: cconv missing\n",
493 hdrfn, hdrfline, (p - protostr) + 1);
496 if (IS(cconv, "__cdecl"))
498 else if (IS(cconv, "__stdcall"))
500 else if (IS(cconv, "__fastcall")) {
502 pp->is_stdcall = 1; // sort of..
504 else if (IS(cconv, "__thiscall"))
506 else if (IS(cconv, "__userpurge"))
507 pp->is_stdcall = 1; // IDA
508 else if (IS(cconv, "__usercall"))
509 pp->is_stdcall = 0; // IDA
510 else if (IS(cconv, "__userstack")) {
511 pp->is_stdcall = 0; // custom
512 pp->is_userstack = 1;
514 else if (IS(cconv, "WINAPI"))
517 printf("%s:%d:%zd: unhandled cconv: '%s'\n",
518 hdrfn, hdrfline, (p - protostr) + 1, cconv);
524 printf("%s:%d:%zd: '*' expected\n",
525 hdrfn, hdrfline, (p - protostr) + 1);
529 // XXX: skipping extra asterisks, for now
535 p = next_idt(buf, sizeof(buf), p);
538 //printf("%s:%d:%zd: func name missing\n",
539 // hdrfn, hdrfline, (p - protostr) + 1);
542 strcpy(pp->name, buf);
544 ret = get_regparm(regparm, sizeof(regparm), p, &is_retreg);
546 if (!IS(regparm, "eax") && !IS(regparm, "ax")
547 && !IS(regparm, "al") && !IS(regparm, "edx:eax"))
549 printf("%s:%d:%zd: bad regparm: %s\n",
550 hdrfn, hdrfline, (p - protostr) + 1, regparm);
559 // not really ret_type is array, but ohwell
560 pp->ret_type.is_array = 1;
561 p = strchr(p + 1, ']');
563 printf("%s:%d:%zd: ']' expected\n",
564 hdrfn, hdrfline, (p - protostr) + 1);
570 printf("%s:%d:%zd: ')' expected\n",
571 hdrfn, hdrfline, (p - protostr) + 1);
578 printf("%s:%d:%zd: '(' expected, got '%c'\n",
579 hdrfn, hdrfline, (p - protostr) + 1, *p);
586 if ((!strncmp(p, "void", 4) || !strncmp(p, "VOID", 4))
587 && *sskip(p + 4) == ')')
598 printf("%s:%d:%zd: ',' expected\n",
599 hdrfn, hdrfline, (p - protostr) + 1);
605 if (!strncmp(p, "...", 3)) {
612 printf("%s:%d:%zd: ')' expected\n",
613 hdrfn, hdrfline, (p - protostr) + 1);
617 arg = &pp->arg[xarg];
621 ret = check_type(p, &arg->type);
623 printf("%s:%d:%zd: unhandled type for arg%d\n",
624 hdrfn, hdrfline, (p - protostr) + 1, xarg);
629 if (*p == '(' || arg->type.is_struct) {
630 // func ptr or struct
631 ret = parse_arg(&p1, arg, xarg);
633 printf("%s:%d:%zd: funcarg parse failed\n",
634 hdrfn, hdrfline, p1 - protostr);
640 p = next_idt(buf, sizeof(buf), p);
644 printf("%s:%d:%zd: idt missing for arg%d\n",
645 hdrfn, hdrfline, (p - protostr) + 1, xarg);
651 ret = get_regparm(regparm, sizeof(regparm), p, &is_retreg);
656 arg->reg = strdup(map_reg(regparm));
657 arg->type.is_retreg = is_retreg;
658 pp->has_retreg |= is_retreg;
661 if (strstr(arg->type.name, "int64")
662 || IS(arg->type.name, "double"))
665 free(arg->type.name);
666 arg->type.name = strdup("int");
667 pp_copy_arg(&pp->arg[xarg], arg);
671 ret = check_struct_arg(arg);
673 pp->has_structarg = 1;
674 arg->type.is_struct = 1;
675 free(arg->type.name);
676 arg->type.name = strdup("int");
677 for (l = 0; l < ret; l++) {
678 pp_copy_arg(&pp->arg[xarg], arg);
684 if (xarg > 0 && (IS(cconv, "__fastcall") || IS(cconv, "__thiscall"))) {
685 if (pp->arg[0].reg != NULL) {
686 printf("%s:%d: %s with arg1 spec %s?\n",
687 hdrfn, hdrfline, cconv, pp->arg[0].reg);
689 pp->arg[0].reg = strdup("ecx");
692 if (xarg > 1 && IS(cconv, "__fastcall")) {
693 if (pp->arg[1].reg != NULL) {
694 printf("%s:%d: %s with arg2 spec %s?\n",
695 hdrfn, hdrfline, cconv, pp->arg[1].reg);
697 pp->arg[1].reg = strdup("edx");
702 for (i = 0; i < pp->argc; i++) {
703 if (pp->arg[i].reg == NULL)
709 if (pp->argc == 1 && pp->arg[0].reg != NULL
710 && IS(pp->arg[0].reg, "ecx"))
714 else if (pp->argc_reg == 2
715 && pp->arg[0].reg != NULL && IS(pp->arg[0].reg, "ecx")
716 && pp->arg[1].reg != NULL && IS(pp->arg[1].reg, "edx"))
721 if (pp->is_vararg && (pp->is_stdcall || pp->is_fastcall)) {
722 printf("%s:%d: vararg %s?\n", hdrfn, hdrfline, cconv);
729 static int pp_name_cmp(const void *p1, const void *p2)
731 const struct parsed_proto *pp1 = p1, *pp2 = p2;
732 return strcmp(pp1->name, pp2->name);
735 static int ps_name_cmp(const void *p1, const void *p2)
737 const struct parsed_struct *ps1 = p1, *ps2 = p2;
738 return strcmp(ps1->name, ps2->name);
741 // parsed struct cache
742 static struct parsed_struct *ps_cache;
743 static int ps_cache_size;
744 static int ps_cache_alloc;
746 static int struct_handler(FILE *fhdr, char *proto, int *line)
748 struct parsed_struct *ps;
754 if (ps_cache_size >= ps_cache_alloc) {
755 ps_cache_alloc = ps_cache_alloc * 2 + 64;
756 ps_cache = realloc(ps_cache, ps_cache_alloc
757 * sizeof(ps_cache[0]));
758 my_assert_not(ps_cache, NULL);
759 memset(ps_cache + ps_cache_size, 0,
760 (ps_cache_alloc - ps_cache_size)
761 * sizeof(ps_cache[0]));
764 ps = &ps_cache[ps_cache_size++];
765 ret = sscanf(proto, "struct %255s {", ps->name);
767 printf("%s:%d: struct parse failed\n", hdrfn, *line);
771 while (fgets(lstr, sizeof(lstr), fhdr))
776 if (p[0] == '/' && p[1] == '/')
781 if (m >= ARRAY_SIZE(ps->members)) {
782 printf("%s:%d: too many struct members\n",
788 ret = parse_protostr(p, &ps->members[m].pp);
790 printf("%s:%d: struct member #%d/%02x "
791 "doesn't parse\n", hdrfn, *line,
795 ps->members[m].offset = offset;
800 ps->member_count = m;
805 // parsed proto cache
806 static struct parsed_proto *pp_cache;
807 static int pp_cache_size;
808 static int pp_cache_alloc;
810 static int b_pp_c_handler(char *proto, const char *fname,
811 int is_include, int is_osinc, int is_cinc)
815 if (pp_cache_size >= pp_cache_alloc) {
816 pp_cache_alloc = pp_cache_alloc * 2 + 64;
817 pp_cache = realloc(pp_cache, pp_cache_alloc
818 * sizeof(pp_cache[0]));
819 my_assert_not(pp_cache, NULL);
820 memset(pp_cache + pp_cache_size, 0,
821 (pp_cache_alloc - pp_cache_size)
822 * sizeof(pp_cache[0]));
825 ret = parse_protostr(proto, &pp_cache[pp_cache_size]);
829 pp_cache[pp_cache_size].is_include = is_include;
830 pp_cache[pp_cache_size].is_osinc = is_osinc;
831 pp_cache[pp_cache_size].is_cinc = is_cinc;
836 static void build_caches(FILE *fhdr)
844 ret = do_protostrs(fhdr, hdrfn, 0);
848 qsort(pp_cache, pp_cache_size, sizeof(pp_cache[0]), pp_name_cmp);
849 qsort(ps_cache, ps_cache_size, sizeof(ps_cache[0]), ps_name_cmp);
850 fseek(fhdr, pos, SEEK_SET);
853 static const struct parsed_proto *proto_parse(FILE *fhdr, const char *sym,
856 const struct parsed_proto *pp_ret;
857 struct parsed_proto pp_search;
860 if (pp_cache == NULL)
863 if (sym[0] == '_') // && strncmp(fname, "stdc", 4) == 0)
866 strcpy(pp_search.name, sym);
867 p = strchr(pp_search.name, '@');
871 pp_ret = bsearch(&pp_search, pp_cache, pp_cache_size,
872 sizeof(pp_cache[0]), pp_name_cmp);
873 if (pp_ret == NULL && !quiet)
874 printf("%s: sym '%s' is missing\n", hdrfn, sym);
879 static const struct parsed_proto *proto_lookup_struct(FILE *fhdr,
880 const char *type, int offset)
882 struct parsed_struct ps_search, *ps;
885 if (pp_cache == NULL)
887 if (ps_cache_size == 0)
890 while (my_isblank(*type))
892 if (!strncmp(type, "struct", 6) && my_isblank(type[6]))
895 if (sscanf(type, "%255s", ps_search.name) != 1)
898 ps = bsearch(&ps_search, ps_cache, ps_cache_size,
899 sizeof(ps_cache[0]), ps_name_cmp);
901 printf("%s: struct '%s' is missing\n",
902 hdrfn, ps_search.name);
906 for (m = 0; m < ps->member_count; m++) {
907 if (ps->members[m].offset == offset)
908 return &ps->members[m].pp;
914 static void pp_copy_arg(struct parsed_proto_arg *d,
915 const struct parsed_proto_arg *s)
917 memcpy(d, s, sizeof(*d));
919 if (s->reg != NULL) {
920 d->reg = strdup(s->reg);
921 my_assert_not(d->reg, NULL);
923 if (s->type.name != NULL) {
924 d->type.name = strdup(s->type.name);
925 my_assert_not(d->type.name, NULL);
928 d->pp = malloc(sizeof(*d->pp));
929 my_assert_not(d->pp, NULL);
930 memcpy(d->pp, s->pp, sizeof(*d->pp));
934 struct parsed_proto *proto_clone(const struct parsed_proto *pp_c)
936 struct parsed_proto *pp;
939 pp = malloc(sizeof(*pp));
940 my_assert_not(pp, NULL);
941 memcpy(pp, pp_c, sizeof(*pp)); // lazy..
943 // do the actual deep copy..
944 for (i = 0; i < pp_c->argc; i++)
945 pp_copy_arg(&pp->arg[i], &pp_c->arg[i]);
946 if (pp_c->ret_type.name != NULL)
947 pp->ret_type.name = strdup(pp_c->ret_type.name);
953 static inline int pp_cmp_func(const struct parsed_proto *pp1,
954 const struct parsed_proto *pp2)
958 if (pp1->argc != pp2->argc || pp1->argc_reg != pp2->argc_reg)
961 for (i = 0; i < pp1->argc; i++) {
962 if ((pp1->arg[i].reg != NULL) != (pp2->arg[i].reg != NULL))
965 if ((pp1->arg[i].reg != NULL)
966 && !IS(pp1->arg[i].reg, pp2->arg[i].reg))
976 static inline void pp_print(char *buf, size_t buf_size,
977 const struct parsed_proto *pp)
982 snprintf(buf, buf_size, "%s %s(", pp->ret_type.name, pp->name);
985 for (i = 0; i < pp->argc_reg; i++) {
986 snprintf(buf + l, buf_size - l, "%s%s",
987 i == 0 ? "" : ", ", pp->arg[i].reg);
990 if (pp->argc_stack > 0) {
991 snprintf(buf + l, buf_size - l, "%s{%d stack}",
992 i == 0 ? "" : ", ", pp->argc_stack);
995 snprintf(buf + l, buf_size - l, ")");
998 static inline void proto_release(struct parsed_proto *pp)
1002 for (i = 0; i < pp->argc; i++) {
1003 if (pp->arg[i].reg != NULL)
1004 free(pp->arg[i].reg);
1005 if (pp->arg[i].type.name != NULL)
1006 free(pp->arg[i].type.name);
1007 if (pp->arg[i].pp != NULL)
1008 free(pp->arg[i].pp);
1010 if (pp->ret_type.name != NULL)
1011 free(pp->ret_type.name);
1014 (void)proto_lookup_struct;