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;
18 unsigned int is_64bit:1;
19 unsigned int is_float:1; // float, double
22 struct parsed_proto_arg {
24 struct parsed_type type;
25 struct parsed_proto *pp; // fptr or struct
27 unsigned int is_saved:1; // not set here, for tool use
33 struct parsed_type ret_type;
34 struct parsed_type type;
36 struct parsed_proto_arg arg[16];
40 unsigned int is_func:1;
41 unsigned int is_stdcall:1;
42 unsigned int is_fastcall:1;
43 unsigned int is_vararg:1; // vararg func
44 unsigned int is_fptr:1;
45 unsigned int is_noreturn:1;
46 unsigned int is_unresolved:1;
47 unsigned int is_userstack:1;
48 unsigned int is_include:1; // not from top-level header
49 unsigned int is_osinc:1; // OS/system library func
50 unsigned int is_cinc:1; // crt library func
51 unsigned int is_arg:1; // declared in some func arg
52 unsigned int has_structarg:1;
53 unsigned int has_retreg:1;
56 struct parsed_struct {
60 struct parsed_proto pp;
65 static const char *hdrfn;
66 static int hdrfline = 0;
68 static void pp_copy_arg(struct parsed_proto_arg *d,
69 const struct parsed_proto_arg *s);
71 static int b_pp_c_handler(char *proto, const char *fname,
72 int is_include, int is_osinc, int is_cinc);
73 static int struct_handler(FILE *fhdr, char *proto, int *line);
75 static int do_protostrs(FILE *fhdr, const char *fname, int is_include)
77 const char *finc_name;
78 const char *hdrfn_saved;
92 is_cinc = strstr(fname, "stdc.hlist") != NULL;
93 is_osinc = is_cinc || strstr(fname, "win32.hlist") != NULL;
95 while (fgets(protostr, sizeof(protostr), fhdr))
98 if (strncmp(protostr, "//#include ", 11) == 0) {
99 finc_name = protostr + 11;
100 p = strpbrk(finc_name, "\r\n ");
105 p = strrchr(hdrfn_saved, '/');
107 memcpy(path, hdrfn_saved,
108 p - hdrfn_saved + 1);
109 path[p - hdrfn_saved + 1] = 0;
111 snprintf(fname_inc, sizeof(fname_inc), "%s%s",
113 finc = fopen(fname_inc, "r");
115 printf("%s:%d: can't open '%s'\n",
116 fname_inc, line, finc_name);
119 ret = do_protostrs(finc, finc_name, 1);
125 if (strncmp(sskip(protostr), "//", 2) == 0)
128 p = protostr + strlen(protostr);
129 for (p--; p >= protostr && my_isblank(*p); --p)
136 if (!strncmp(protostr, "struct", 6)
137 && strchr(protostr, '{') != NULL)
138 ret = struct_handler(fhdr, protostr, &line);
140 ret = b_pp_c_handler(protostr, hdrfn,
141 is_include, is_osinc, is_cinc);
154 static int get_regparm(char *dst, size_t dlen, char *p, int *retreg)
169 for (o = 0; o < dlen; i++) {
181 static const char *known_type_mod[] = {
190 static const char *known_ptr_types[] = {
207 "HIMC", // DWORD in mingw, ptr in wine..
229 "PMEMORY_BASIC_INFORMATION",
246 static const char *ignored_keywords[] = {
254 static int typecmp(const char *n, const char *t)
256 for (; *t != 0; n++, t++) {
257 while (n[0] == ' ' && (n[1] == ' ' || n[1] == '*'))
259 while (t[0] == ' ' && (t[1] == ' ' || t[1] == '*'))
268 static const char *skip_type_mod(const char *n)
273 for (i = 0; i < ARRAY_SIZE(known_type_mod); i++) {
274 len = strlen(known_type_mod[i]);
275 if (strncmp(n, known_type_mod[i], len) != 0)
277 if (!my_isblank(n[len]))
281 while (my_isblank(*n))
289 static int check_type(const char *name, struct parsed_type *type)
295 n = skip_type_mod(name);
297 if (!strncmp(n, "struct", 6) && my_isblank(n[6])) {
301 while (my_isblank(*n))
305 for (i = 0; i < ARRAY_SIZE(known_ptr_types); i++) {
306 if (typecmp(n, known_ptr_types[i]))
313 if (n[0] == 'L' && n[1] == 'P' && strncmp(n, "LPARAM", 6))
316 // assume single word
317 while (!my_isblank(*n) && !my_issep(*n))
322 while (my_isblank(*n))
333 type->name = strndup(name, ret);
334 if (IS(type->name, "__VALIST") || IS(type->name, "va_list"))
335 type->is_va_list = 1;
336 if (IS(type->name, "VOID"))
337 memcpy(type->name, "void", 4);
342 /* args are always expanded to 32bit */
343 static const char *map_reg(const char *reg)
345 const char *regs_f[] = { "eax", "ebx", "ecx", "edx", "esi", "edi" };
346 const char *regs_w[] = { "ax", "bx", "cx", "dx", "si", "di" };
347 const char *regs_b[] = { "al", "bl", "cl", "dl" };
350 for (i = 0; i < ARRAY_SIZE(regs_w); i++)
351 if (IS(reg, regs_w[i]))
354 for (i = 0; i < ARRAY_SIZE(regs_b); i++)
355 if (IS(reg, regs_b[i]))
361 static int check_struct_arg(struct parsed_proto_arg *arg)
363 if (IS(arg->type.name, "POINT"))
369 static int parse_protostr(char *protostr, struct parsed_proto *pp);
371 static int parse_arg(char **p_, struct parsed_proto_arg *arg, int xarg)
378 arg->pp = calloc(1, sizeof(*arg->pp));
379 my_assert_not(arg->pp, NULL);
384 pe = strpbrk(pe, ",()");
387 if (*pe == ',' || *pe == ')')
389 pe = strchr(pe, ')');
395 if (pe - p > sizeof(buf) - 1)
397 memcpy(buf, p, pe - p);
400 ret = parse_protostr(buf, arg->pp);
404 // we don't use actual names right now...
405 snprintf(arg->pp->name, sizeof(arg->pp->name), "a%d", xarg);
407 if (!arg->type.is_struct)
408 // we'll treat it as void * for non-calls
409 arg->type.name = strdup("void *");
410 arg->type.is_ptr = 1;
417 static int parse_protostr(char *protostr, struct parsed_proto *pp)
419 struct parsed_proto_arg *arg;
430 if (p[0] == '/' && p[1] == '/') {
431 printf("%s:%d: commented out?\n", hdrfn, hdrfline);
435 // allow start of line comment
436 if (p[0] == '/' && p[1] == '*') {
437 p = strstr(p + 2, "*/");
439 printf("%s:%d: multiline comments unsupported\n",
446 // we need remaining hints in comments, so strip / *
447 for (p1 = p; p1[0] != 0 && p1[1] != 0; p1++) {
448 if ((p1[0] == '/' && p1[1] == '*')
449 || (p1[0] == '*' && p1[1] == '/'))
453 if (!strncmp(p, "DECLSPEC_NORETURN ", 18)) {
458 for (i = 0; i < ARRAY_SIZE(ignored_keywords); i++) {
459 l = strlen(ignored_keywords[i]);
460 if (!strncmp(p, ignored_keywords[i], l) && my_isblank(p[l]))
461 p = sskip(p + l + 1);
464 ret = check_type(p, &pp->ret_type);
466 printf("%s:%d:%zd: unhandled return in '%s'\n",
467 hdrfn, hdrfline, (p - protostr) + 1, protostr);
472 if (!strncmp(p, "noreturn ", 9)) {
477 if (!strchr(p, ')')) {
478 p = next_idt(buf, sizeof(buf), p);
480 if (!pp->is_arg && buf[0] == 0) {
481 printf("%s:%d:%zd: var name is missing\n",
482 hdrfn, hdrfline, (p - protostr) + 1);
485 strcpy(pp->name, buf);
490 pp->ret_type.is_array = 1;
502 p = next_word(cconv, sizeof(cconv), p);
505 printf("%s:%d:%zd: cconv missing\n",
506 hdrfn, hdrfline, (p - protostr) + 1);
509 if (IS(cconv, "__cdecl"))
511 else if (IS(cconv, "__stdcall"))
513 else if (IS(cconv, "__fastcall")) {
515 pp->is_stdcall = 1; // sort of..
517 else if (IS(cconv, "__thiscall"))
519 else if (IS(cconv, "__userpurge"))
520 pp->is_stdcall = 1; // IDA
521 else if (IS(cconv, "__usercall"))
522 pp->is_stdcall = 0; // IDA
523 else if (IS(cconv, "__userstack")) {
524 pp->is_stdcall = 0; // custom
525 pp->is_userstack = 1;
527 else if (IS(cconv, "WINAPI") || IS(cconv, "PASCAL"))
530 printf("%s:%d:%zd: unhandled cconv: '%s'\n",
531 hdrfn, hdrfline, (p - protostr) + 1, cconv);
537 printf("%s:%d:%zd: '*' expected\n",
538 hdrfn, hdrfline, (p - protostr) + 1);
542 // XXX: skipping extra asterisks, for now
548 p = next_idt(buf, sizeof(buf), p);
551 //printf("%s:%d:%zd: func name missing\n",
552 // hdrfn, hdrfline, (p - protostr) + 1);
555 strcpy(pp->name, buf);
557 ret = get_regparm(regparm, sizeof(regparm), p, &is_retreg);
559 if (!IS(regparm, "eax") && !IS(regparm, "ax")
560 && !IS(regparm, "al") && !IS(regparm, "edx:eax"))
562 printf("%s:%d:%zd: bad regparm: %s\n",
563 hdrfn, hdrfline, (p - protostr) + 1, regparm);
572 // not really ret_type is array, but ohwell
573 pp->ret_type.is_array = 1;
574 p = strchr(p + 1, ']');
576 printf("%s:%d:%zd: ']' expected\n",
577 hdrfn, hdrfline, (p - protostr) + 1);
583 printf("%s:%d:%zd: ')' expected\n",
584 hdrfn, hdrfline, (p - protostr) + 1);
591 printf("%s:%d:%zd: '(' expected, got '%c'\n",
592 hdrfn, hdrfline, (p - protostr) + 1, *p);
599 if ((!strncmp(p, "void", 4) || !strncmp(p, "VOID", 4))
600 && *sskip(p + 4) == ')')
611 printf("%s:%d:%zd: ',' expected\n",
612 hdrfn, hdrfline, (p - protostr) + 1);
618 if (!strncmp(p, "...", 3)) {
625 printf("%s:%d:%zd: ')' expected\n",
626 hdrfn, hdrfline, (p - protostr) + 1);
630 arg = &pp->arg[xarg];
634 ret = check_type(p, &arg->type);
636 printf("%s:%d:%zd: unhandled type for arg%d\n",
637 hdrfn, hdrfline, (p - protostr) + 1, xarg);
642 if (*p == '(' || arg->type.is_struct) {
643 // func ptr or struct
644 ret = parse_arg(&p1, arg, xarg);
646 printf("%s:%d:%zd: funcarg parse failed\n",
647 hdrfn, hdrfline, p1 - protostr);
653 p = next_idt(buf, sizeof(buf), p);
657 printf("%s:%d:%zd: idt missing for arg%d\n",
658 hdrfn, hdrfline, (p - protostr) + 1, xarg);
664 ret = get_regparm(regparm, sizeof(regparm), p, &is_retreg);
669 arg->reg = strdup(map_reg(regparm));
670 arg->type.is_retreg = is_retreg;
671 pp->has_retreg |= is_retreg;
674 if (IS(arg->type.name, "float")
675 || IS(arg->type.name, "double"))
677 arg->type.is_float = 1;
680 if (!arg->type.is_ptr && (strstr(arg->type.name, "int64")
681 || IS(arg->type.name, "double")))
683 arg->type.is_64bit = 1;
685 pp_copy_arg(&pp->arg[xarg], arg);
686 arg = &pp->arg[xarg];
688 free(arg->type.name);
689 arg->type.name = strdup("dummy");
692 ret = check_struct_arg(arg);
694 pp->has_structarg = 1;
695 arg->type.is_struct = 1;
696 free(arg->type.name);
697 arg->type.name = strdup("int");
698 for (l = 0; l < ret; l++) {
699 pp_copy_arg(&pp->arg[xarg], arg);
705 if (xarg > 0 && (IS(cconv, "__fastcall") || IS(cconv, "__thiscall"))) {
706 if (pp->arg[0].reg != NULL) {
707 printf("%s:%d: %s with arg1 spec %s?\n",
708 hdrfn, hdrfline, cconv, pp->arg[0].reg);
710 pp->arg[0].reg = strdup("ecx");
713 if (xarg > 1 && IS(cconv, "__fastcall")) {
714 if (pp->arg[1].reg != NULL) {
715 printf("%s:%d: %s with arg2 spec %s?\n",
716 hdrfn, hdrfline, cconv, pp->arg[1].reg);
718 pp->arg[1].reg = strdup("edx");
723 for (i = 0; i < pp->argc; i++) {
724 if (pp->arg[i].reg == NULL)
730 if (pp->argc == 1 && pp->arg[0].reg != NULL
731 && IS(pp->arg[0].reg, "ecx"))
735 else if (pp->argc_reg == 2
736 && pp->arg[0].reg != NULL && IS(pp->arg[0].reg, "ecx")
737 && pp->arg[1].reg != NULL && IS(pp->arg[1].reg, "edx"))
742 if (pp->is_vararg && (pp->is_stdcall || pp->is_fastcall)) {
743 printf("%s:%d: vararg %s?\n", hdrfn, hdrfline, cconv);
750 static int pp_name_cmp(const void *p1, const void *p2)
752 const struct parsed_proto *pp1 = p1, *pp2 = p2;
753 return strcmp(pp1->name, pp2->name);
756 static int ps_name_cmp(const void *p1, const void *p2)
758 const struct parsed_struct *ps1 = p1, *ps2 = p2;
759 return strcmp(ps1->name, ps2->name);
762 // parsed struct cache
763 static struct parsed_struct *ps_cache;
764 static int ps_cache_size;
765 static int ps_cache_alloc;
767 static int struct_handler(FILE *fhdr, char *proto, int *line)
769 struct parsed_struct *ps;
775 if (ps_cache_size >= ps_cache_alloc) {
776 ps_cache_alloc = ps_cache_alloc * 2 + 64;
777 ps_cache = realloc(ps_cache, ps_cache_alloc
778 * sizeof(ps_cache[0]));
779 my_assert_not(ps_cache, NULL);
780 memset(ps_cache + ps_cache_size, 0,
781 (ps_cache_alloc - ps_cache_size)
782 * sizeof(ps_cache[0]));
785 ps = &ps_cache[ps_cache_size++];
786 ret = sscanf(proto, "struct %255s {", ps->name);
788 printf("%s:%d: struct parse failed\n", hdrfn, *line);
792 while (fgets(lstr, sizeof(lstr), fhdr))
797 if (p[0] == '/' && p[1] == '/')
802 if (m >= ARRAY_SIZE(ps->members)) {
803 printf("%s:%d: too many struct members\n",
809 ret = parse_protostr(p, &ps->members[m].pp);
811 printf("%s:%d: struct member #%d/%02x "
812 "doesn't parse\n", hdrfn, *line,
816 ps->members[m].offset = offset;
821 ps->member_count = m;
826 // parsed proto cache
827 static struct parsed_proto *pp_cache;
828 static int pp_cache_size;
829 static int pp_cache_alloc;
831 static int b_pp_c_handler(char *proto, const char *fname,
832 int is_include, int is_osinc, int is_cinc)
836 if (pp_cache_size >= pp_cache_alloc) {
837 pp_cache_alloc = pp_cache_alloc * 2 + 64;
838 pp_cache = realloc(pp_cache, pp_cache_alloc
839 * sizeof(pp_cache[0]));
840 my_assert_not(pp_cache, NULL);
841 memset(pp_cache + pp_cache_size, 0,
842 (pp_cache_alloc - pp_cache_size)
843 * sizeof(pp_cache[0]));
846 ret = parse_protostr(proto, &pp_cache[pp_cache_size]);
850 pp_cache[pp_cache_size].is_include = is_include;
851 pp_cache[pp_cache_size].is_osinc = is_osinc;
852 pp_cache[pp_cache_size].is_cinc = is_cinc;
857 static void build_caches(FILE *fhdr)
865 ret = do_protostrs(fhdr, hdrfn, 0);
869 qsort(pp_cache, pp_cache_size, sizeof(pp_cache[0]), pp_name_cmp);
870 qsort(ps_cache, ps_cache_size, sizeof(ps_cache[0]), ps_name_cmp);
871 fseek(fhdr, pos, SEEK_SET);
874 static const struct parsed_proto *proto_parse(FILE *fhdr, const char *sym,
877 const struct parsed_proto *pp_ret;
878 struct parsed_proto pp_search;
881 if (pp_cache == NULL)
885 if (sym[0] == '_' && !IS_START(sym, "__W"))
888 strcpy(pp_search.name, sym);
889 p = strchr(pp_search.name, '@');
893 pp_ret = bsearch(&pp_search, pp_cache, pp_cache_size,
894 sizeof(pp_cache[0]), pp_name_cmp);
895 if (pp_ret == NULL && !quiet)
896 printf("%s: sym '%s' is missing\n", hdrfn, sym);
901 static const struct parsed_proto *proto_lookup_struct(FILE *fhdr,
902 const char *type, int offset)
904 struct parsed_struct ps_search, *ps;
907 if (pp_cache == NULL)
909 if (ps_cache_size == 0)
912 while (my_isblank(*type))
914 if (!strncmp(type, "struct", 6) && my_isblank(type[6]))
917 if (sscanf(type, "%255s", ps_search.name) != 1)
920 ps = bsearch(&ps_search, ps_cache, ps_cache_size,
921 sizeof(ps_cache[0]), ps_name_cmp);
923 printf("%s: struct '%s' is missing\n",
924 hdrfn, ps_search.name);
928 for (m = 0; m < ps->member_count; m++) {
929 if (ps->members[m].offset == offset)
930 return &ps->members[m].pp;
936 static void pp_copy_arg(struct parsed_proto_arg *d,
937 const struct parsed_proto_arg *s)
939 memcpy(d, s, sizeof(*d));
941 if (s->reg != NULL) {
942 d->reg = strdup(s->reg);
943 my_assert_not(d->reg, NULL);
945 if (s->type.name != NULL) {
946 d->type.name = strdup(s->type.name);
947 my_assert_not(d->type.name, NULL);
950 d->pp = malloc(sizeof(*d->pp));
951 my_assert_not(d->pp, NULL);
952 memcpy(d->pp, s->pp, sizeof(*d->pp));
956 struct parsed_proto *proto_clone(const struct parsed_proto *pp_c)
958 struct parsed_proto *pp;
961 pp = malloc(sizeof(*pp));
962 my_assert_not(pp, NULL);
963 memcpy(pp, pp_c, sizeof(*pp)); // lazy..
965 // do the actual deep copy..
966 for (i = 0; i < pp_c->argc; i++)
967 pp_copy_arg(&pp->arg[i], &pp_c->arg[i]);
968 if (pp_c->ret_type.name != NULL)
969 pp->ret_type.name = strdup(pp_c->ret_type.name);
975 static inline int pp_cmp_func(const struct parsed_proto *pp1,
976 const struct parsed_proto *pp2)
980 if (pp1->argc != pp2->argc || pp1->argc_reg != pp2->argc_reg)
983 for (i = 0; i < pp1->argc; i++) {
984 if ((pp1->arg[i].reg != NULL) != (pp2->arg[i].reg != NULL))
987 if ((pp1->arg[i].reg != NULL)
988 && !IS(pp1->arg[i].reg, pp2->arg[i].reg))
998 static inline void pp_print(char *buf, size_t buf_size,
999 const struct parsed_proto *pp)
1004 snprintf(buf, buf_size, "%s %s(", pp->ret_type.name, pp->name);
1007 for (i = 0; i < pp->argc_reg; i++) {
1008 snprintf(buf + l, buf_size - l, "%s%s",
1009 i == 0 ? "" : ", ", pp->arg[i].reg);
1012 if (pp->argc_stack > 0) {
1013 snprintf(buf + l, buf_size - l, "%s{%d stack}",
1014 i == 0 ? "" : ", ", pp->argc_stack);
1017 snprintf(buf + l, buf_size - l, ")");
1020 static inline void proto_release(struct parsed_proto *pp)
1024 for (i = 0; i < pp->argc; i++) {
1025 if (pp->arg[i].reg != NULL)
1026 free(pp->arg[i].reg);
1027 if (pp->arg[i].type.name != NULL)
1028 free(pp->arg[i].type.name);
1029 if (pp->arg[i].pp != NULL)
1030 free(pp->arg[i].pp);
1032 if (pp->ret_type.name != NULL)
1033 free(pp->ret_type.name);
1036 (void)proto_lookup_struct;