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",
247 static const char *ignored_keywords[] = {
255 static int typecmp(const char *n, const char *t)
257 for (; *t != 0; n++, t++) {
258 while (n[0] == ' ' && (n[1] == ' ' || n[1] == '*'))
260 while (t[0] == ' ' && (t[1] == ' ' || t[1] == '*'))
269 static const char *skip_type_mod(const char *n)
274 for (i = 0; i < ARRAY_SIZE(known_type_mod); i++) {
275 len = strlen(known_type_mod[i]);
276 if (strncmp(n, known_type_mod[i], len) != 0)
278 if (!my_isblank(n[len]))
282 while (my_isblank(*n))
290 static int check_type(const char *name, struct parsed_type *type)
296 n = skip_type_mod(name);
298 if (!strncmp(n, "struct", 6) && my_isblank(n[6])) {
302 while (my_isblank(*n))
306 for (i = 0; i < ARRAY_SIZE(known_ptr_types); i++) {
307 if (typecmp(n, known_ptr_types[i]))
314 if (n[0] == 'L' && n[1] == 'P' && strncmp(n, "LPARAM", 6))
317 // assume single word
318 while (!my_isblank(*n) && !my_issep(*n))
323 while (my_isblank(*n))
334 type->name = strndup(name, ret);
335 if (IS(type->name, "__VALIST") || IS(type->name, "va_list"))
336 type->is_va_list = 1;
337 if (IS(type->name, "VOID"))
338 memcpy(type->name, "void", 4);
343 /* args are always expanded to 32bit */
344 static const char *map_reg(const char *reg)
346 const char *regs_f[] = { "eax", "ebx", "ecx", "edx", "esi", "edi" };
347 const char *regs_w[] = { "ax", "bx", "cx", "dx", "si", "di" };
348 const char *regs_b[] = { "al", "bl", "cl", "dl" };
351 for (i = 0; i < ARRAY_SIZE(regs_w); i++)
352 if (IS(reg, regs_w[i]))
355 for (i = 0; i < ARRAY_SIZE(regs_b); i++)
356 if (IS(reg, regs_b[i]))
362 static int check_struct_arg(struct parsed_proto_arg *arg)
364 if (IS(arg->type.name, "POINT"))
370 static int parse_protostr(char *protostr, struct parsed_proto *pp);
372 static int parse_arg(char **p_, struct parsed_proto_arg *arg, int xarg)
379 arg->pp = calloc(1, sizeof(*arg->pp));
380 my_assert_not(arg->pp, NULL);
385 pe = strpbrk(pe, ",()");
388 if (*pe == ',' || *pe == ')')
390 pe = strchr(pe, ')');
396 if (pe - p > sizeof(buf) - 1)
398 memcpy(buf, p, pe - p);
401 ret = parse_protostr(buf, arg->pp);
405 // we don't use actual names right now...
406 snprintf(arg->pp->name, sizeof(arg->pp->name), "a%d", xarg);
408 if (!arg->type.is_struct)
409 // we'll treat it as void * for non-calls
410 arg->type.name = strdup("void *");
411 arg->type.is_ptr = 1;
418 static int parse_protostr(char *protostr, struct parsed_proto *pp)
420 struct parsed_proto_arg *arg;
431 if (p[0] == '/' && p[1] == '/') {
432 printf("%s:%d: commented out?\n", hdrfn, hdrfline);
436 // allow start of line comment
437 if (p[0] == '/' && p[1] == '*') {
438 p = strstr(p + 2, "*/");
440 printf("%s:%d: multiline comments unsupported\n",
447 // we need remaining hints in comments, so strip / *
448 for (p1 = p; p1[0] != 0 && p1[1] != 0; p1++) {
449 if ((p1[0] == '/' && p1[1] == '*')
450 || (p1[0] == '*' && p1[1] == '/'))
454 if (!strncmp(p, "DECLSPEC_NORETURN ", 18)) {
459 for (i = 0; i < ARRAY_SIZE(ignored_keywords); i++) {
460 l = strlen(ignored_keywords[i]);
461 if (!strncmp(p, ignored_keywords[i], l) && my_isblank(p[l]))
462 p = sskip(p + l + 1);
465 ret = check_type(p, &pp->ret_type);
467 printf("%s:%d:%zd: unhandled return in '%s'\n",
468 hdrfn, hdrfline, (p - protostr) + 1, protostr);
473 if (!strncmp(p, "noreturn ", 9)) {
478 if (!strchr(p, ')')) {
479 p = next_idt(buf, sizeof(buf), p);
481 if (!pp->is_arg && buf[0] == 0) {
482 printf("%s:%d:%zd: var name is missing\n",
483 hdrfn, hdrfline, (p - protostr) + 1);
486 strcpy(pp->name, buf);
491 pp->ret_type.is_array = 1;
503 p = next_word(cconv, sizeof(cconv), p);
506 printf("%s:%d:%zd: cconv missing\n",
507 hdrfn, hdrfline, (p - protostr) + 1);
510 if (IS(cconv, "__cdecl"))
512 else if (IS(cconv, "__stdcall"))
514 else if (IS(cconv, "__fastcall")) {
516 pp->is_stdcall = 1; // sort of..
518 else if (IS(cconv, "__thiscall"))
520 else if (IS(cconv, "__userpurge"))
521 pp->is_stdcall = 1; // IDA
522 else if (IS(cconv, "__usercall"))
523 pp->is_stdcall = 0; // IDA
524 else if (IS(cconv, "__userstack")) {
525 pp->is_stdcall = 0; // custom
526 pp->is_userstack = 1;
528 else if (IS(cconv, "WINAPI") || IS(cconv, "PASCAL"))
531 printf("%s:%d:%zd: unhandled cconv: '%s'\n",
532 hdrfn, hdrfline, (p - protostr) + 1, cconv);
538 printf("%s:%d:%zd: '*' expected\n",
539 hdrfn, hdrfline, (p - protostr) + 1);
543 // XXX: skipping extra asterisks, for now
549 p = next_idt(buf, sizeof(buf), p);
552 //printf("%s:%d:%zd: func name missing\n",
553 // hdrfn, hdrfline, (p - protostr) + 1);
556 strcpy(pp->name, buf);
558 ret = get_regparm(regparm, sizeof(regparm), p, &is_retreg);
560 if (!IS(regparm, "eax") && !IS(regparm, "ax")
561 && !IS(regparm, "al") && !IS(regparm, "edx:eax"))
563 printf("%s:%d:%zd: bad regparm: %s\n",
564 hdrfn, hdrfline, (p - protostr) + 1, regparm);
573 // not really ret_type is array, but ohwell
574 pp->ret_type.is_array = 1;
575 p = strchr(p + 1, ']');
577 printf("%s:%d:%zd: ']' expected\n",
578 hdrfn, hdrfline, (p - protostr) + 1);
584 printf("%s:%d:%zd: ')' expected\n",
585 hdrfn, hdrfline, (p - protostr) + 1);
592 printf("%s:%d:%zd: '(' expected, got '%c'\n",
593 hdrfn, hdrfline, (p - protostr) + 1, *p);
600 if ((!strncmp(p, "void", 4) || !strncmp(p, "VOID", 4))
601 && *sskip(p + 4) == ')')
612 printf("%s:%d:%zd: ',' expected\n",
613 hdrfn, hdrfline, (p - protostr) + 1);
619 if (!strncmp(p, "...", 3)) {
626 printf("%s:%d:%zd: ')' expected\n",
627 hdrfn, hdrfline, (p - protostr) + 1);
631 arg = &pp->arg[xarg];
635 ret = check_type(p, &arg->type);
637 printf("%s:%d:%zd: unhandled type for arg%d\n",
638 hdrfn, hdrfline, (p - protostr) + 1, xarg);
643 if (*p == '(' || arg->type.is_struct) {
644 // func ptr or struct
645 ret = parse_arg(&p1, arg, xarg);
647 printf("%s:%d:%zd: funcarg parse failed\n",
648 hdrfn, hdrfline, p1 - protostr);
654 p = next_idt(buf, sizeof(buf), p);
658 printf("%s:%d:%zd: idt missing for arg%d\n",
659 hdrfn, hdrfline, (p - protostr) + 1, xarg);
665 ret = get_regparm(regparm, sizeof(regparm), p, &is_retreg);
670 arg->reg = strdup(map_reg(regparm));
671 arg->type.is_retreg = is_retreg;
672 pp->has_retreg |= is_retreg;
675 if (IS(arg->type.name, "float")
676 || IS(arg->type.name, "double"))
678 arg->type.is_float = 1;
681 if (!arg->type.is_ptr && (strstr(arg->type.name, "int64")
682 || IS(arg->type.name, "double")))
684 arg->type.is_64bit = 1;
686 pp_copy_arg(&pp->arg[xarg], arg);
687 arg = &pp->arg[xarg];
689 free(arg->type.name);
690 arg->type.name = strdup("dummy");
693 ret = check_struct_arg(arg);
695 pp->has_structarg = 1;
696 arg->type.is_struct = 1;
697 free(arg->type.name);
698 arg->type.name = strdup("int");
699 for (l = 0; l < ret; l++) {
700 pp_copy_arg(&pp->arg[xarg], arg);
706 if (xarg > 0 && (IS(cconv, "__fastcall") || IS(cconv, "__thiscall"))) {
707 if (pp->arg[0].reg != NULL) {
708 printf("%s:%d: %s with arg1 spec %s?\n",
709 hdrfn, hdrfline, cconv, pp->arg[0].reg);
711 pp->arg[0].reg = strdup("ecx");
714 if (xarg > 1 && IS(cconv, "__fastcall")) {
715 if (pp->arg[1].reg != NULL) {
716 printf("%s:%d: %s with arg2 spec %s?\n",
717 hdrfn, hdrfline, cconv, pp->arg[1].reg);
719 pp->arg[1].reg = strdup("edx");
724 for (i = 0; i < pp->argc; i++) {
725 if (pp->arg[i].reg == NULL)
731 if (pp->argc == 1 && pp->arg[0].reg != NULL
732 && IS(pp->arg[0].reg, "ecx"))
736 else if (pp->argc_reg == 2
737 && pp->arg[0].reg != NULL && IS(pp->arg[0].reg, "ecx")
738 && pp->arg[1].reg != NULL && IS(pp->arg[1].reg, "edx"))
743 if (pp->is_vararg && (pp->is_stdcall || pp->is_fastcall)) {
744 printf("%s:%d: vararg %s?\n", hdrfn, hdrfline, cconv);
751 static int pp_name_cmp(const void *p1, const void *p2)
753 const struct parsed_proto *pp1 = p1, *pp2 = p2;
754 return strcmp(pp1->name, pp2->name);
757 static int ps_name_cmp(const void *p1, const void *p2)
759 const struct parsed_struct *ps1 = p1, *ps2 = p2;
760 return strcmp(ps1->name, ps2->name);
763 // parsed struct cache
764 static struct parsed_struct *ps_cache;
765 static int ps_cache_size;
766 static int ps_cache_alloc;
768 static int struct_handler(FILE *fhdr, char *proto, int *line)
770 struct parsed_struct *ps;
776 if (ps_cache_size >= ps_cache_alloc) {
777 ps_cache_alloc = ps_cache_alloc * 2 + 64;
778 ps_cache = realloc(ps_cache, ps_cache_alloc
779 * sizeof(ps_cache[0]));
780 my_assert_not(ps_cache, NULL);
781 memset(ps_cache + ps_cache_size, 0,
782 (ps_cache_alloc - ps_cache_size)
783 * sizeof(ps_cache[0]));
786 ps = &ps_cache[ps_cache_size++];
787 ret = sscanf(proto, "struct %255s {", ps->name);
789 printf("%s:%d: struct parse failed\n", hdrfn, *line);
793 while (fgets(lstr, sizeof(lstr), fhdr))
798 if (p[0] == '/' && p[1] == '/')
803 if (m >= ARRAY_SIZE(ps->members)) {
804 printf("%s:%d: too many struct members\n",
810 ret = parse_protostr(p, &ps->members[m].pp);
812 printf("%s:%d: struct member #%d/%02x "
813 "doesn't parse\n", hdrfn, *line,
817 ps->members[m].offset = offset;
822 ps->member_count = m;
827 // parsed proto cache
828 static struct parsed_proto *pp_cache;
829 static int pp_cache_size;
830 static int pp_cache_alloc;
832 static int b_pp_c_handler(char *proto, const char *fname,
833 int is_include, int is_osinc, int is_cinc)
837 if (pp_cache_size >= pp_cache_alloc) {
838 pp_cache_alloc = pp_cache_alloc * 2 + 64;
839 pp_cache = realloc(pp_cache, pp_cache_alloc
840 * sizeof(pp_cache[0]));
841 my_assert_not(pp_cache, NULL);
842 memset(pp_cache + pp_cache_size, 0,
843 (pp_cache_alloc - pp_cache_size)
844 * sizeof(pp_cache[0]));
847 ret = parse_protostr(proto, &pp_cache[pp_cache_size]);
851 pp_cache[pp_cache_size].is_include = is_include;
852 pp_cache[pp_cache_size].is_osinc = is_osinc;
853 pp_cache[pp_cache_size].is_cinc = is_cinc;
858 static void build_caches(FILE *fhdr)
866 ret = do_protostrs(fhdr, hdrfn, 0);
870 qsort(pp_cache, pp_cache_size, sizeof(pp_cache[0]), pp_name_cmp);
871 qsort(ps_cache, ps_cache_size, sizeof(ps_cache[0]), ps_name_cmp);
872 fseek(fhdr, pos, SEEK_SET);
875 static const struct parsed_proto *proto_parse(FILE *fhdr, const char *sym,
878 const struct parsed_proto *pp_ret;
879 struct parsed_proto pp_search;
882 if (pp_cache == NULL)
886 if (sym[0] == '_' && !IS_START(sym, "__W"))
889 strcpy(pp_search.name, sym);
890 p = strchr(pp_search.name, '@');
894 pp_ret = bsearch(&pp_search, pp_cache, pp_cache_size,
895 sizeof(pp_cache[0]), pp_name_cmp);
896 if (pp_ret == NULL && !quiet)
897 printf("%s: sym '%s' is missing\n", hdrfn, sym);
902 static const struct parsed_proto *proto_lookup_struct(FILE *fhdr,
903 const char *type, int offset)
905 struct parsed_struct ps_search, *ps;
908 if (pp_cache == NULL)
910 if (ps_cache_size == 0)
913 while (my_isblank(*type))
915 if (!strncmp(type, "struct", 6) && my_isblank(type[6]))
918 if (sscanf(type, "%255s", ps_search.name) != 1)
921 ps = bsearch(&ps_search, ps_cache, ps_cache_size,
922 sizeof(ps_cache[0]), ps_name_cmp);
924 printf("%s: struct '%s' is missing\n",
925 hdrfn, ps_search.name);
929 for (m = 0; m < ps->member_count; m++) {
930 if (ps->members[m].offset == offset)
931 return &ps->members[m].pp;
937 static void pp_copy_arg(struct parsed_proto_arg *d,
938 const struct parsed_proto_arg *s)
940 memcpy(d, s, sizeof(*d));
942 if (s->reg != NULL) {
943 d->reg = strdup(s->reg);
944 my_assert_not(d->reg, NULL);
946 if (s->type.name != NULL) {
947 d->type.name = strdup(s->type.name);
948 my_assert_not(d->type.name, NULL);
951 d->pp = malloc(sizeof(*d->pp));
952 my_assert_not(d->pp, NULL);
953 memcpy(d->pp, s->pp, sizeof(*d->pp));
957 struct parsed_proto *proto_clone(const struct parsed_proto *pp_c)
959 struct parsed_proto *pp;
962 pp = malloc(sizeof(*pp));
963 my_assert_not(pp, NULL);
964 memcpy(pp, pp_c, sizeof(*pp)); // lazy..
966 // do the actual deep copy..
967 for (i = 0; i < pp_c->argc; i++)
968 pp_copy_arg(&pp->arg[i], &pp_c->arg[i]);
969 if (pp_c->ret_type.name != NULL)
970 pp->ret_type.name = strdup(pp_c->ret_type.name);
976 static inline int pp_cmp_func(const struct parsed_proto *pp1,
977 const struct parsed_proto *pp2)
981 if (pp1->argc != pp2->argc || pp1->argc_reg != pp2->argc_reg)
984 for (i = 0; i < pp1->argc; i++) {
985 if ((pp1->arg[i].reg != NULL) != (pp2->arg[i].reg != NULL))
988 if ((pp1->arg[i].reg != NULL)
989 && !IS(pp1->arg[i].reg, pp2->arg[i].reg))
999 static inline void pp_print(char *buf, size_t buf_size,
1000 const struct parsed_proto *pp)
1005 snprintf(buf, buf_size, "%s %s(", pp->ret_type.name, pp->name);
1008 for (i = 0; i < pp->argc_reg; i++) {
1009 snprintf(buf + l, buf_size - l, "%s%s",
1010 i == 0 ? "" : ", ", pp->arg[i].reg);
1013 if (pp->argc_stack > 0) {
1014 snprintf(buf + l, buf_size - l, "%s{%d stack}",
1015 i == 0 ? "" : ", ", pp->argc_stack);
1018 snprintf(buf + l, buf_size - l, ")");
1021 static inline void proto_release(struct parsed_proto *pp)
1025 for (i = 0; i < pp->argc; i++) {
1026 if (pp->arg[i].reg != NULL)
1027 free(pp->arg[i].reg);
1028 if (pp->arg[i].type.name != NULL)
1029 free(pp->arg[i].type.name);
1030 if (pp->arg[i].pp != NULL)
1031 free(pp->arg[i].pp);
1033 if (pp->ret_type.name != NULL)
1034 free(pp->ret_type.name);
1037 (void)proto_lookup_struct;