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
25 unsigned int is_saved:1; // not set here, for tool use
31 struct parsed_type ret_type;
32 struct parsed_type type;
34 struct parsed_proto_arg arg[16];
38 unsigned int is_func:1;
39 unsigned int is_stdcall:1;
40 unsigned int is_fastcall:1;
41 unsigned int is_vararg:1; // vararg func
42 unsigned int is_fptr:1;
43 unsigned int is_noreturn:1;
44 unsigned int is_unresolved:1;
45 unsigned int is_userstack:1;
46 unsigned int is_include:1; // not from top-level header
47 unsigned int is_osinc:1; // OS/system library func
48 unsigned int is_cinc:1; // crt library func
49 unsigned int is_arg:1; // declared in some func arg
50 unsigned int has_structarg:1;
51 unsigned int has_retreg:1;
54 struct parsed_struct {
58 struct parsed_proto pp;
63 static const char *hdrfn;
64 static int hdrfline = 0;
66 static void pp_copy_arg(struct parsed_proto_arg *d,
67 const struct parsed_proto_arg *s);
69 static int b_pp_c_handler(char *proto, const char *fname,
70 int is_include, int is_osinc, int is_cinc);
71 static int struct_handler(FILE *fhdr, char *proto, int *line);
73 static int do_protostrs(FILE *fhdr, const char *fname, int is_include)
75 const char *finc_name;
76 const char *hdrfn_saved;
90 is_cinc = strstr(fname, "stdc.hlist") != NULL;
91 is_osinc = is_cinc || strstr(fname, "win32.hlist") != NULL;
93 while (fgets(protostr, sizeof(protostr), fhdr))
96 if (strncmp(protostr, "//#include ", 11) == 0) {
97 finc_name = protostr + 11;
98 p = strpbrk(finc_name, "\r\n ");
103 p = strrchr(hdrfn_saved, '/');
105 memcpy(path, hdrfn_saved,
106 p - hdrfn_saved + 1);
107 path[p - hdrfn_saved + 1] = 0;
109 snprintf(fname_inc, sizeof(fname_inc), "%s%s",
111 finc = fopen(fname_inc, "r");
113 printf("%s:%d: can't open '%s'\n",
114 fname_inc, line, finc_name);
117 ret = do_protostrs(finc, finc_name, 1);
123 if (strncmp(sskip(protostr), "//", 2) == 0)
126 p = protostr + strlen(protostr);
127 for (p--; p >= protostr && my_isblank(*p); --p)
134 if (!strncmp(protostr, "struct", 6)
135 && strchr(protostr, '{') != NULL)
136 ret = struct_handler(fhdr, protostr, &line);
138 ret = b_pp_c_handler(protostr, hdrfn,
139 is_include, is_osinc, is_cinc);
152 static int get_regparm(char *dst, size_t dlen, char *p, int *retreg)
167 for (o = 0; o < dlen; i++) {
179 static const char *known_type_mod[] = {
188 static const char *known_ptr_types[] = {
205 "HIMC", // DWORD in mingw, ptr in wine..
225 "PMEMORY_BASIC_INFORMATION",
242 static const char *ignored_keywords[] = {
250 static int typecmp(const char *n, const char *t)
252 for (; *t != 0; n++, t++) {
253 while (n[0] == ' ' && (n[1] == ' ' || n[1] == '*'))
255 while (t[0] == ' ' && (t[1] == ' ' || t[1] == '*'))
264 static const char *skip_type_mod(const char *n)
269 for (i = 0; i < ARRAY_SIZE(known_type_mod); i++) {
270 len = strlen(known_type_mod[i]);
271 if (strncmp(n, known_type_mod[i], len) != 0)
273 if (!my_isblank(n[len]))
277 while (my_isblank(*n))
285 static int check_type(const char *name, struct parsed_type *type)
291 n = skip_type_mod(name);
293 if (!strncmp(n, "struct", 6) && my_isblank(n[6])) {
297 while (my_isblank(*n))
301 for (i = 0; i < ARRAY_SIZE(known_ptr_types); i++) {
302 if (typecmp(n, known_ptr_types[i]))
309 if (n[0] == 'L' && n[1] == 'P' && strncmp(n, "LPARAM", 6))
312 // assume single word
313 while (!my_isblank(*n) && !my_issep(*n))
318 while (my_isblank(*n))
329 type->name = strndup(name, ret);
330 if (IS(type->name, "__VALIST") || IS(type->name, "va_list"))
331 type->is_va_list = 1;
332 if (IS(type->name, "VOID"))
333 memcpy(type->name, "void", 4);
338 /* args are always expanded to 32bit */
339 static const char *map_reg(const char *reg)
341 const char *regs_f[] = { "eax", "ebx", "ecx", "edx", "esi", "edi" };
342 const char *regs_w[] = { "ax", "bx", "cx", "dx", "si", "di" };
343 const char *regs_b[] = { "al", "bl", "cl", "dl" };
346 for (i = 0; i < ARRAY_SIZE(regs_w); i++)
347 if (IS(reg, regs_w[i]))
350 for (i = 0; i < ARRAY_SIZE(regs_b); i++)
351 if (IS(reg, regs_b[i]))
357 static int check_struct_arg(struct parsed_proto_arg *arg)
359 if (IS(arg->type.name, "POINT"))
365 static int parse_protostr(char *protostr, struct parsed_proto *pp);
367 static int parse_arg(char **p_, struct parsed_proto_arg *arg, int xarg)
374 arg->pp = calloc(1, sizeof(*arg->pp));
375 my_assert_not(arg->pp, NULL);
380 pe = strpbrk(pe, ",()");
383 if (*pe == ',' || *pe == ')')
385 pe = strchr(pe, ')');
391 if (pe - p > sizeof(buf) - 1)
393 memcpy(buf, p, pe - p);
396 ret = parse_protostr(buf, arg->pp);
400 // we don't use actual names right now...
401 snprintf(arg->pp->name, sizeof(arg->pp->name), "a%d", xarg);
403 if (!arg->type.is_struct)
404 // we'll treat it as void * for non-calls
405 arg->type.name = strdup("void *");
406 arg->type.is_ptr = 1;
413 static int parse_protostr(char *protostr, struct parsed_proto *pp)
415 struct parsed_proto_arg *arg;
426 if (p[0] == '/' && p[1] == '/') {
427 printf("%s:%d: commented out?\n", hdrfn, hdrfline);
431 // allow start of line comment
432 if (p[0] == '/' && p[1] == '*') {
433 p = strstr(p + 2, "*/");
435 printf("%s:%d: multiline comments unsupported\n",
442 // we need remaining hints in comments, so strip / *
443 for (p1 = p; p1[0] != 0 && p1[1] != 0; p1++) {
444 if ((p1[0] == '/' && p1[1] == '*')
445 || (p1[0] == '*' && p1[1] == '/'))
449 if (!strncmp(p, "DECLSPEC_NORETURN ", 18)) {
454 for (i = 0; i < ARRAY_SIZE(ignored_keywords); i++) {
455 l = strlen(ignored_keywords[i]);
456 if (!strncmp(p, ignored_keywords[i], l) && my_isblank(p[l]))
457 p = sskip(p + l + 1);
460 ret = check_type(p, &pp->ret_type);
462 printf("%s:%d:%zd: unhandled return in '%s'\n",
463 hdrfn, hdrfline, (p - protostr) + 1, protostr);
468 if (!strncmp(p, "noreturn ", 9)) {
473 if (!strchr(p, ')')) {
474 p = next_idt(buf, sizeof(buf), p);
476 if (!pp->is_arg && buf[0] == 0) {
477 printf("%s:%d:%zd: var name is missing\n",
478 hdrfn, hdrfline, (p - protostr) + 1);
481 strcpy(pp->name, buf);
486 pp->ret_type.is_array = 1;
498 p = next_word(cconv, sizeof(cconv), p);
501 printf("%s:%d:%zd: cconv missing\n",
502 hdrfn, hdrfline, (p - protostr) + 1);
505 if (IS(cconv, "__cdecl"))
507 else if (IS(cconv, "__stdcall"))
509 else if (IS(cconv, "__fastcall")) {
511 pp->is_stdcall = 1; // sort of..
513 else if (IS(cconv, "__thiscall"))
515 else if (IS(cconv, "__userpurge"))
516 pp->is_stdcall = 1; // IDA
517 else if (IS(cconv, "__usercall"))
518 pp->is_stdcall = 0; // IDA
519 else if (IS(cconv, "__userstack")) {
520 pp->is_stdcall = 0; // custom
521 pp->is_userstack = 1;
523 else if (IS(cconv, "WINAPI") || IS(cconv, "PASCAL"))
526 printf("%s:%d:%zd: unhandled cconv: '%s'\n",
527 hdrfn, hdrfline, (p - protostr) + 1, cconv);
533 printf("%s:%d:%zd: '*' expected\n",
534 hdrfn, hdrfline, (p - protostr) + 1);
538 // XXX: skipping extra asterisks, for now
544 p = next_idt(buf, sizeof(buf), p);
547 //printf("%s:%d:%zd: func name missing\n",
548 // hdrfn, hdrfline, (p - protostr) + 1);
551 strcpy(pp->name, buf);
553 ret = get_regparm(regparm, sizeof(regparm), p, &is_retreg);
555 if (!IS(regparm, "eax") && !IS(regparm, "ax")
556 && !IS(regparm, "al") && !IS(regparm, "edx:eax"))
558 printf("%s:%d:%zd: bad regparm: %s\n",
559 hdrfn, hdrfline, (p - protostr) + 1, regparm);
568 // not really ret_type is array, but ohwell
569 pp->ret_type.is_array = 1;
570 p = strchr(p + 1, ']');
572 printf("%s:%d:%zd: ']' expected\n",
573 hdrfn, hdrfline, (p - protostr) + 1);
579 printf("%s:%d:%zd: ')' expected\n",
580 hdrfn, hdrfline, (p - protostr) + 1);
587 printf("%s:%d:%zd: '(' expected, got '%c'\n",
588 hdrfn, hdrfline, (p - protostr) + 1, *p);
595 if ((!strncmp(p, "void", 4) || !strncmp(p, "VOID", 4))
596 && *sskip(p + 4) == ')')
607 printf("%s:%d:%zd: ',' expected\n",
608 hdrfn, hdrfline, (p - protostr) + 1);
614 if (!strncmp(p, "...", 3)) {
621 printf("%s:%d:%zd: ')' expected\n",
622 hdrfn, hdrfline, (p - protostr) + 1);
626 arg = &pp->arg[xarg];
630 ret = check_type(p, &arg->type);
632 printf("%s:%d:%zd: unhandled type for arg%d\n",
633 hdrfn, hdrfline, (p - protostr) + 1, xarg);
638 if (*p == '(' || arg->type.is_struct) {
639 // func ptr or struct
640 ret = parse_arg(&p1, arg, xarg);
642 printf("%s:%d:%zd: funcarg parse failed\n",
643 hdrfn, hdrfline, p1 - protostr);
649 p = next_idt(buf, sizeof(buf), p);
653 printf("%s:%d:%zd: idt missing for arg%d\n",
654 hdrfn, hdrfline, (p - protostr) + 1, xarg);
660 ret = get_regparm(regparm, sizeof(regparm), p, &is_retreg);
665 arg->reg = strdup(map_reg(regparm));
666 arg->type.is_retreg = is_retreg;
667 pp->has_retreg |= is_retreg;
670 if (!arg->type.is_ptr && (strstr(arg->type.name, "int64")
671 || IS(arg->type.name, "double")))
674 free(arg->type.name);
675 arg->type.name = strdup("int");
676 pp_copy_arg(&pp->arg[xarg], arg);
680 ret = check_struct_arg(arg);
682 pp->has_structarg = 1;
683 arg->type.is_struct = 1;
684 free(arg->type.name);
685 arg->type.name = strdup("int");
686 for (l = 0; l < ret; l++) {
687 pp_copy_arg(&pp->arg[xarg], arg);
693 if (xarg > 0 && (IS(cconv, "__fastcall") || IS(cconv, "__thiscall"))) {
694 if (pp->arg[0].reg != NULL) {
695 printf("%s:%d: %s with arg1 spec %s?\n",
696 hdrfn, hdrfline, cconv, pp->arg[0].reg);
698 pp->arg[0].reg = strdup("ecx");
701 if (xarg > 1 && IS(cconv, "__fastcall")) {
702 if (pp->arg[1].reg != NULL) {
703 printf("%s:%d: %s with arg2 spec %s?\n",
704 hdrfn, hdrfline, cconv, pp->arg[1].reg);
706 pp->arg[1].reg = strdup("edx");
711 for (i = 0; i < pp->argc; i++) {
712 if (pp->arg[i].reg == NULL)
718 if (pp->argc == 1 && pp->arg[0].reg != NULL
719 && IS(pp->arg[0].reg, "ecx"))
723 else if (pp->argc_reg == 2
724 && pp->arg[0].reg != NULL && IS(pp->arg[0].reg, "ecx")
725 && pp->arg[1].reg != NULL && IS(pp->arg[1].reg, "edx"))
730 if (pp->is_vararg && (pp->is_stdcall || pp->is_fastcall)) {
731 printf("%s:%d: vararg %s?\n", hdrfn, hdrfline, cconv);
738 static int pp_name_cmp(const void *p1, const void *p2)
740 const struct parsed_proto *pp1 = p1, *pp2 = p2;
741 return strcmp(pp1->name, pp2->name);
744 static int ps_name_cmp(const void *p1, const void *p2)
746 const struct parsed_struct *ps1 = p1, *ps2 = p2;
747 return strcmp(ps1->name, ps2->name);
750 // parsed struct cache
751 static struct parsed_struct *ps_cache;
752 static int ps_cache_size;
753 static int ps_cache_alloc;
755 static int struct_handler(FILE *fhdr, char *proto, int *line)
757 struct parsed_struct *ps;
763 if (ps_cache_size >= ps_cache_alloc) {
764 ps_cache_alloc = ps_cache_alloc * 2 + 64;
765 ps_cache = realloc(ps_cache, ps_cache_alloc
766 * sizeof(ps_cache[0]));
767 my_assert_not(ps_cache, NULL);
768 memset(ps_cache + ps_cache_size, 0,
769 (ps_cache_alloc - ps_cache_size)
770 * sizeof(ps_cache[0]));
773 ps = &ps_cache[ps_cache_size++];
774 ret = sscanf(proto, "struct %255s {", ps->name);
776 printf("%s:%d: struct parse failed\n", hdrfn, *line);
780 while (fgets(lstr, sizeof(lstr), fhdr))
785 if (p[0] == '/' && p[1] == '/')
790 if (m >= ARRAY_SIZE(ps->members)) {
791 printf("%s:%d: too many struct members\n",
797 ret = parse_protostr(p, &ps->members[m].pp);
799 printf("%s:%d: struct member #%d/%02x "
800 "doesn't parse\n", hdrfn, *line,
804 ps->members[m].offset = offset;
809 ps->member_count = m;
814 // parsed proto cache
815 static struct parsed_proto *pp_cache;
816 static int pp_cache_size;
817 static int pp_cache_alloc;
819 static int b_pp_c_handler(char *proto, const char *fname,
820 int is_include, int is_osinc, int is_cinc)
824 if (pp_cache_size >= pp_cache_alloc) {
825 pp_cache_alloc = pp_cache_alloc * 2 + 64;
826 pp_cache = realloc(pp_cache, pp_cache_alloc
827 * sizeof(pp_cache[0]));
828 my_assert_not(pp_cache, NULL);
829 memset(pp_cache + pp_cache_size, 0,
830 (pp_cache_alloc - pp_cache_size)
831 * sizeof(pp_cache[0]));
834 ret = parse_protostr(proto, &pp_cache[pp_cache_size]);
838 pp_cache[pp_cache_size].is_include = is_include;
839 pp_cache[pp_cache_size].is_osinc = is_osinc;
840 pp_cache[pp_cache_size].is_cinc = is_cinc;
845 static void build_caches(FILE *fhdr)
853 ret = do_protostrs(fhdr, hdrfn, 0);
857 qsort(pp_cache, pp_cache_size, sizeof(pp_cache[0]), pp_name_cmp);
858 qsort(ps_cache, ps_cache_size, sizeof(ps_cache[0]), ps_name_cmp);
859 fseek(fhdr, pos, SEEK_SET);
862 static const struct parsed_proto *proto_parse(FILE *fhdr, const char *sym,
865 const struct parsed_proto *pp_ret;
866 struct parsed_proto pp_search;
869 if (pp_cache == NULL)
873 if (sym[0] == '_' && !IS_START(sym, "__W"))
876 strcpy(pp_search.name, sym);
877 p = strchr(pp_search.name, '@');
881 pp_ret = bsearch(&pp_search, pp_cache, pp_cache_size,
882 sizeof(pp_cache[0]), pp_name_cmp);
883 if (pp_ret == NULL && !quiet)
884 printf("%s: sym '%s' is missing\n", hdrfn, sym);
889 static const struct parsed_proto *proto_lookup_struct(FILE *fhdr,
890 const char *type, int offset)
892 struct parsed_struct ps_search, *ps;
895 if (pp_cache == NULL)
897 if (ps_cache_size == 0)
900 while (my_isblank(*type))
902 if (!strncmp(type, "struct", 6) && my_isblank(type[6]))
905 if (sscanf(type, "%255s", ps_search.name) != 1)
908 ps = bsearch(&ps_search, ps_cache, ps_cache_size,
909 sizeof(ps_cache[0]), ps_name_cmp);
911 printf("%s: struct '%s' is missing\n",
912 hdrfn, ps_search.name);
916 for (m = 0; m < ps->member_count; m++) {
917 if (ps->members[m].offset == offset)
918 return &ps->members[m].pp;
924 static void pp_copy_arg(struct parsed_proto_arg *d,
925 const struct parsed_proto_arg *s)
927 memcpy(d, s, sizeof(*d));
929 if (s->reg != NULL) {
930 d->reg = strdup(s->reg);
931 my_assert_not(d->reg, NULL);
933 if (s->type.name != NULL) {
934 d->type.name = strdup(s->type.name);
935 my_assert_not(d->type.name, NULL);
938 d->pp = malloc(sizeof(*d->pp));
939 my_assert_not(d->pp, NULL);
940 memcpy(d->pp, s->pp, sizeof(*d->pp));
944 struct parsed_proto *proto_clone(const struct parsed_proto *pp_c)
946 struct parsed_proto *pp;
949 pp = malloc(sizeof(*pp));
950 my_assert_not(pp, NULL);
951 memcpy(pp, pp_c, sizeof(*pp)); // lazy..
953 // do the actual deep copy..
954 for (i = 0; i < pp_c->argc; i++)
955 pp_copy_arg(&pp->arg[i], &pp_c->arg[i]);
956 if (pp_c->ret_type.name != NULL)
957 pp->ret_type.name = strdup(pp_c->ret_type.name);
963 static inline int pp_cmp_func(const struct parsed_proto *pp1,
964 const struct parsed_proto *pp2)
968 if (pp1->argc != pp2->argc || pp1->argc_reg != pp2->argc_reg)
971 for (i = 0; i < pp1->argc; i++) {
972 if ((pp1->arg[i].reg != NULL) != (pp2->arg[i].reg != NULL))
975 if ((pp1->arg[i].reg != NULL)
976 && !IS(pp1->arg[i].reg, pp2->arg[i].reg))
986 static inline void pp_print(char *buf, size_t buf_size,
987 const struct parsed_proto *pp)
992 snprintf(buf, buf_size, "%s %s(", pp->ret_type.name, pp->name);
995 for (i = 0; i < pp->argc_reg; i++) {
996 snprintf(buf + l, buf_size - l, "%s%s",
997 i == 0 ? "" : ", ", pp->arg[i].reg);
1000 if (pp->argc_stack > 0) {
1001 snprintf(buf + l, buf_size - l, "%s{%d stack}",
1002 i == 0 ? "" : ", ", pp->argc_stack);
1005 snprintf(buf + l, buf_size - l, ")");
1008 static inline void proto_release(struct parsed_proto *pp)
1012 for (i = 0; i < pp->argc; i++) {
1013 if (pp->arg[i].reg != NULL)
1014 free(pp->arg[i].reg);
1015 if (pp->arg[i].type.name != NULL)
1016 free(pp->arg[i].type.name);
1017 if (pp->arg[i].pp != NULL)
1018 free(pp->arg[i].pp);
1020 if (pp->ret_type.name != NULL)
1021 free(pp->ret_type.name);
1024 (void)proto_lookup_struct;