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;
21 struct parsed_proto_arg {
23 struct parsed_type type;
24 struct parsed_proto *pp; // fptr or struct
26 unsigned int is_saved:1; // not set here, for tool use
32 struct parsed_type ret_type;
33 struct parsed_type type;
35 struct parsed_proto_arg arg[16];
39 unsigned int is_func:1;
40 unsigned int is_stdcall:1;
41 unsigned int is_fastcall:1;
42 unsigned int is_vararg:1; // vararg func
43 unsigned int is_fptr:1;
44 unsigned int is_noreturn:1;
45 unsigned int is_unresolved:1;
46 unsigned int is_userstack:1;
47 unsigned int is_include:1; // not from top-level header
48 unsigned int is_osinc:1; // OS/system library func
49 unsigned int is_cinc:1; // crt library func
50 unsigned int is_arg:1; // declared in some func arg
51 unsigned int has_structarg:1;
52 unsigned int has_retreg:1;
55 struct parsed_struct {
59 struct parsed_proto pp;
64 static const char *hdrfn;
65 static int hdrfline = 0;
67 static void pp_copy_arg(struct parsed_proto_arg *d,
68 const struct parsed_proto_arg *s);
70 static int b_pp_c_handler(char *proto, const char *fname,
71 int is_include, int is_osinc, int is_cinc);
72 static int struct_handler(FILE *fhdr, char *proto, int *line);
74 static int do_protostrs(FILE *fhdr, const char *fname, int is_include)
76 const char *finc_name;
77 const char *hdrfn_saved;
91 is_cinc = strstr(fname, "stdc.hlist") != NULL;
92 is_osinc = is_cinc || strstr(fname, "win32.hlist") != NULL;
94 while (fgets(protostr, sizeof(protostr), fhdr))
97 if (strncmp(protostr, "//#include ", 11) == 0) {
98 finc_name = protostr + 11;
99 p = strpbrk(finc_name, "\r\n ");
104 p = strrchr(hdrfn_saved, '/');
106 memcpy(path, hdrfn_saved,
107 p - hdrfn_saved + 1);
108 path[p - hdrfn_saved + 1] = 0;
110 snprintf(fname_inc, sizeof(fname_inc), "%s%s",
112 finc = fopen(fname_inc, "r");
114 printf("%s:%d: can't open '%s'\n",
115 fname_inc, line, finc_name);
118 ret = do_protostrs(finc, finc_name, 1);
124 if (strncmp(sskip(protostr), "//", 2) == 0)
127 p = protostr + strlen(protostr);
128 for (p--; p >= protostr && my_isblank(*p); --p)
135 if (!strncmp(protostr, "struct", 6)
136 && strchr(protostr, '{') != NULL)
137 ret = struct_handler(fhdr, protostr, &line);
139 ret = b_pp_c_handler(protostr, hdrfn,
140 is_include, is_osinc, is_cinc);
153 static int get_regparm(char *dst, size_t dlen, char *p, int *retreg)
168 for (o = 0; o < dlen; i++) {
180 static const char *known_type_mod[] = {
189 static const char *known_ptr_types[] = {
206 "HIMC", // DWORD in mingw, ptr in wine..
226 "PMEMORY_BASIC_INFORMATION",
243 static const char *ignored_keywords[] = {
251 static int typecmp(const char *n, const char *t)
253 for (; *t != 0; n++, t++) {
254 while (n[0] == ' ' && (n[1] == ' ' || n[1] == '*'))
256 while (t[0] == ' ' && (t[1] == ' ' || t[1] == '*'))
265 static const char *skip_type_mod(const char *n)
270 for (i = 0; i < ARRAY_SIZE(known_type_mod); i++) {
271 len = strlen(known_type_mod[i]);
272 if (strncmp(n, known_type_mod[i], len) != 0)
274 if (!my_isblank(n[len]))
278 while (my_isblank(*n))
286 static int check_type(const char *name, struct parsed_type *type)
292 n = skip_type_mod(name);
294 if (!strncmp(n, "struct", 6) && my_isblank(n[6])) {
298 while (my_isblank(*n))
302 for (i = 0; i < ARRAY_SIZE(known_ptr_types); i++) {
303 if (typecmp(n, known_ptr_types[i]))
310 if (n[0] == 'L' && n[1] == 'P' && strncmp(n, "LPARAM", 6))
313 // assume single word
314 while (!my_isblank(*n) && !my_issep(*n))
319 while (my_isblank(*n))
330 type->name = strndup(name, ret);
331 if (IS(type->name, "__VALIST") || IS(type->name, "va_list"))
332 type->is_va_list = 1;
333 if (IS(type->name, "VOID"))
334 memcpy(type->name, "void", 4);
339 /* args are always expanded to 32bit */
340 static const char *map_reg(const char *reg)
342 const char *regs_f[] = { "eax", "ebx", "ecx", "edx", "esi", "edi" };
343 const char *regs_w[] = { "ax", "bx", "cx", "dx", "si", "di" };
344 const char *regs_b[] = { "al", "bl", "cl", "dl" };
347 for (i = 0; i < ARRAY_SIZE(regs_w); i++)
348 if (IS(reg, regs_w[i]))
351 for (i = 0; i < ARRAY_SIZE(regs_b); i++)
352 if (IS(reg, regs_b[i]))
358 static int check_struct_arg(struct parsed_proto_arg *arg)
360 if (IS(arg->type.name, "POINT"))
366 static int parse_protostr(char *protostr, struct parsed_proto *pp);
368 static int parse_arg(char **p_, struct parsed_proto_arg *arg, int xarg)
375 arg->pp = calloc(1, sizeof(*arg->pp));
376 my_assert_not(arg->pp, NULL);
381 pe = strpbrk(pe, ",()");
384 if (*pe == ',' || *pe == ')')
386 pe = strchr(pe, ')');
392 if (pe - p > sizeof(buf) - 1)
394 memcpy(buf, p, pe - p);
397 ret = parse_protostr(buf, arg->pp);
401 // we don't use actual names right now...
402 snprintf(arg->pp->name, sizeof(arg->pp->name), "a%d", xarg);
404 if (!arg->type.is_struct)
405 // we'll treat it as void * for non-calls
406 arg->type.name = strdup("void *");
407 arg->type.is_ptr = 1;
414 static int parse_protostr(char *protostr, struct parsed_proto *pp)
416 struct parsed_proto_arg *arg;
427 if (p[0] == '/' && p[1] == '/') {
428 printf("%s:%d: commented out?\n", hdrfn, hdrfline);
432 // allow start of line comment
433 if (p[0] == '/' && p[1] == '*') {
434 p = strstr(p + 2, "*/");
436 printf("%s:%d: multiline comments unsupported\n",
443 // we need remaining hints in comments, so strip / *
444 for (p1 = p; p1[0] != 0 && p1[1] != 0; p1++) {
445 if ((p1[0] == '/' && p1[1] == '*')
446 || (p1[0] == '*' && p1[1] == '/'))
450 if (!strncmp(p, "DECLSPEC_NORETURN ", 18)) {
455 for (i = 0; i < ARRAY_SIZE(ignored_keywords); i++) {
456 l = strlen(ignored_keywords[i]);
457 if (!strncmp(p, ignored_keywords[i], l) && my_isblank(p[l]))
458 p = sskip(p + l + 1);
461 ret = check_type(p, &pp->ret_type);
463 printf("%s:%d:%zd: unhandled return in '%s'\n",
464 hdrfn, hdrfline, (p - protostr) + 1, protostr);
469 if (!strncmp(p, "noreturn ", 9)) {
474 if (!strchr(p, ')')) {
475 p = next_idt(buf, sizeof(buf), p);
477 if (!pp->is_arg && buf[0] == 0) {
478 printf("%s:%d:%zd: var name is missing\n",
479 hdrfn, hdrfline, (p - protostr) + 1);
482 strcpy(pp->name, buf);
487 pp->ret_type.is_array = 1;
499 p = next_word(cconv, sizeof(cconv), p);
502 printf("%s:%d:%zd: cconv missing\n",
503 hdrfn, hdrfline, (p - protostr) + 1);
506 if (IS(cconv, "__cdecl"))
508 else if (IS(cconv, "__stdcall"))
510 else if (IS(cconv, "__fastcall")) {
512 pp->is_stdcall = 1; // sort of..
514 else if (IS(cconv, "__thiscall"))
516 else if (IS(cconv, "__userpurge"))
517 pp->is_stdcall = 1; // IDA
518 else if (IS(cconv, "__usercall"))
519 pp->is_stdcall = 0; // IDA
520 else if (IS(cconv, "__userstack")) {
521 pp->is_stdcall = 0; // custom
522 pp->is_userstack = 1;
524 else if (IS(cconv, "WINAPI") || IS(cconv, "PASCAL"))
527 printf("%s:%d:%zd: unhandled cconv: '%s'\n",
528 hdrfn, hdrfline, (p - protostr) + 1, cconv);
534 printf("%s:%d:%zd: '*' expected\n",
535 hdrfn, hdrfline, (p - protostr) + 1);
539 // XXX: skipping extra asterisks, for now
545 p = next_idt(buf, sizeof(buf), p);
548 //printf("%s:%d:%zd: func name missing\n",
549 // hdrfn, hdrfline, (p - protostr) + 1);
552 strcpy(pp->name, buf);
554 ret = get_regparm(regparm, sizeof(regparm), p, &is_retreg);
556 if (!IS(regparm, "eax") && !IS(regparm, "ax")
557 && !IS(regparm, "al") && !IS(regparm, "edx:eax"))
559 printf("%s:%d:%zd: bad regparm: %s\n",
560 hdrfn, hdrfline, (p - protostr) + 1, regparm);
569 // not really ret_type is array, but ohwell
570 pp->ret_type.is_array = 1;
571 p = strchr(p + 1, ']');
573 printf("%s:%d:%zd: ']' expected\n",
574 hdrfn, hdrfline, (p - protostr) + 1);
580 printf("%s:%d:%zd: ')' expected\n",
581 hdrfn, hdrfline, (p - protostr) + 1);
588 printf("%s:%d:%zd: '(' expected, got '%c'\n",
589 hdrfn, hdrfline, (p - protostr) + 1, *p);
596 if ((!strncmp(p, "void", 4) || !strncmp(p, "VOID", 4))
597 && *sskip(p + 4) == ')')
608 printf("%s:%d:%zd: ',' expected\n",
609 hdrfn, hdrfline, (p - protostr) + 1);
615 if (!strncmp(p, "...", 3)) {
622 printf("%s:%d:%zd: ')' expected\n",
623 hdrfn, hdrfline, (p - protostr) + 1);
627 arg = &pp->arg[xarg];
631 ret = check_type(p, &arg->type);
633 printf("%s:%d:%zd: unhandled type for arg%d\n",
634 hdrfn, hdrfline, (p - protostr) + 1, xarg);
639 if (*p == '(' || arg->type.is_struct) {
640 // func ptr or struct
641 ret = parse_arg(&p1, arg, xarg);
643 printf("%s:%d:%zd: funcarg parse failed\n",
644 hdrfn, hdrfline, p1 - protostr);
650 p = next_idt(buf, sizeof(buf), p);
654 printf("%s:%d:%zd: idt missing for arg%d\n",
655 hdrfn, hdrfline, (p - protostr) + 1, xarg);
661 ret = get_regparm(regparm, sizeof(regparm), p, &is_retreg);
666 arg->reg = strdup(map_reg(regparm));
667 arg->type.is_retreg = is_retreg;
668 pp->has_retreg |= is_retreg;
671 if (!arg->type.is_ptr && (strstr(arg->type.name, "int64")
672 || IS(arg->type.name, "double")))
674 arg->type.is_64bit = 1;
676 pp_copy_arg(&pp->arg[xarg], arg);
677 arg = &pp->arg[xarg];
679 free(arg->type.name);
680 arg->type.name = strdup("dummy");
683 ret = check_struct_arg(arg);
685 pp->has_structarg = 1;
686 arg->type.is_struct = 1;
687 free(arg->type.name);
688 arg->type.name = strdup("int");
689 for (l = 0; l < ret; l++) {
690 pp_copy_arg(&pp->arg[xarg], arg);
696 if (xarg > 0 && (IS(cconv, "__fastcall") || IS(cconv, "__thiscall"))) {
697 if (pp->arg[0].reg != NULL) {
698 printf("%s:%d: %s with arg1 spec %s?\n",
699 hdrfn, hdrfline, cconv, pp->arg[0].reg);
701 pp->arg[0].reg = strdup("ecx");
704 if (xarg > 1 && IS(cconv, "__fastcall")) {
705 if (pp->arg[1].reg != NULL) {
706 printf("%s:%d: %s with arg2 spec %s?\n",
707 hdrfn, hdrfline, cconv, pp->arg[1].reg);
709 pp->arg[1].reg = strdup("edx");
714 for (i = 0; i < pp->argc; i++) {
715 if (pp->arg[i].reg == NULL)
721 if (pp->argc == 1 && pp->arg[0].reg != NULL
722 && IS(pp->arg[0].reg, "ecx"))
726 else if (pp->argc_reg == 2
727 && pp->arg[0].reg != NULL && IS(pp->arg[0].reg, "ecx")
728 && pp->arg[1].reg != NULL && IS(pp->arg[1].reg, "edx"))
733 if (pp->is_vararg && (pp->is_stdcall || pp->is_fastcall)) {
734 printf("%s:%d: vararg %s?\n", hdrfn, hdrfline, cconv);
741 static int pp_name_cmp(const void *p1, const void *p2)
743 const struct parsed_proto *pp1 = p1, *pp2 = p2;
744 return strcmp(pp1->name, pp2->name);
747 static int ps_name_cmp(const void *p1, const void *p2)
749 const struct parsed_struct *ps1 = p1, *ps2 = p2;
750 return strcmp(ps1->name, ps2->name);
753 // parsed struct cache
754 static struct parsed_struct *ps_cache;
755 static int ps_cache_size;
756 static int ps_cache_alloc;
758 static int struct_handler(FILE *fhdr, char *proto, int *line)
760 struct parsed_struct *ps;
766 if (ps_cache_size >= ps_cache_alloc) {
767 ps_cache_alloc = ps_cache_alloc * 2 + 64;
768 ps_cache = realloc(ps_cache, ps_cache_alloc
769 * sizeof(ps_cache[0]));
770 my_assert_not(ps_cache, NULL);
771 memset(ps_cache + ps_cache_size, 0,
772 (ps_cache_alloc - ps_cache_size)
773 * sizeof(ps_cache[0]));
776 ps = &ps_cache[ps_cache_size++];
777 ret = sscanf(proto, "struct %255s {", ps->name);
779 printf("%s:%d: struct parse failed\n", hdrfn, *line);
783 while (fgets(lstr, sizeof(lstr), fhdr))
788 if (p[0] == '/' && p[1] == '/')
793 if (m >= ARRAY_SIZE(ps->members)) {
794 printf("%s:%d: too many struct members\n",
800 ret = parse_protostr(p, &ps->members[m].pp);
802 printf("%s:%d: struct member #%d/%02x "
803 "doesn't parse\n", hdrfn, *line,
807 ps->members[m].offset = offset;
812 ps->member_count = m;
817 // parsed proto cache
818 static struct parsed_proto *pp_cache;
819 static int pp_cache_size;
820 static int pp_cache_alloc;
822 static int b_pp_c_handler(char *proto, const char *fname,
823 int is_include, int is_osinc, int is_cinc)
827 if (pp_cache_size >= pp_cache_alloc) {
828 pp_cache_alloc = pp_cache_alloc * 2 + 64;
829 pp_cache = realloc(pp_cache, pp_cache_alloc
830 * sizeof(pp_cache[0]));
831 my_assert_not(pp_cache, NULL);
832 memset(pp_cache + pp_cache_size, 0,
833 (pp_cache_alloc - pp_cache_size)
834 * sizeof(pp_cache[0]));
837 ret = parse_protostr(proto, &pp_cache[pp_cache_size]);
841 pp_cache[pp_cache_size].is_include = is_include;
842 pp_cache[pp_cache_size].is_osinc = is_osinc;
843 pp_cache[pp_cache_size].is_cinc = is_cinc;
848 static void build_caches(FILE *fhdr)
856 ret = do_protostrs(fhdr, hdrfn, 0);
860 qsort(pp_cache, pp_cache_size, sizeof(pp_cache[0]), pp_name_cmp);
861 qsort(ps_cache, ps_cache_size, sizeof(ps_cache[0]), ps_name_cmp);
862 fseek(fhdr, pos, SEEK_SET);
865 static const struct parsed_proto *proto_parse(FILE *fhdr, const char *sym,
868 const struct parsed_proto *pp_ret;
869 struct parsed_proto pp_search;
872 if (pp_cache == NULL)
876 if (sym[0] == '_' && !IS_START(sym, "__W"))
879 strcpy(pp_search.name, sym);
880 p = strchr(pp_search.name, '@');
884 pp_ret = bsearch(&pp_search, pp_cache, pp_cache_size,
885 sizeof(pp_cache[0]), pp_name_cmp);
886 if (pp_ret == NULL && !quiet)
887 printf("%s: sym '%s' is missing\n", hdrfn, sym);
892 static const struct parsed_proto *proto_lookup_struct(FILE *fhdr,
893 const char *type, int offset)
895 struct parsed_struct ps_search, *ps;
898 if (pp_cache == NULL)
900 if (ps_cache_size == 0)
903 while (my_isblank(*type))
905 if (!strncmp(type, "struct", 6) && my_isblank(type[6]))
908 if (sscanf(type, "%255s", ps_search.name) != 1)
911 ps = bsearch(&ps_search, ps_cache, ps_cache_size,
912 sizeof(ps_cache[0]), ps_name_cmp);
914 printf("%s: struct '%s' is missing\n",
915 hdrfn, ps_search.name);
919 for (m = 0; m < ps->member_count; m++) {
920 if (ps->members[m].offset == offset)
921 return &ps->members[m].pp;
927 static void pp_copy_arg(struct parsed_proto_arg *d,
928 const struct parsed_proto_arg *s)
930 memcpy(d, s, sizeof(*d));
932 if (s->reg != NULL) {
933 d->reg = strdup(s->reg);
934 my_assert_not(d->reg, NULL);
936 if (s->type.name != NULL) {
937 d->type.name = strdup(s->type.name);
938 my_assert_not(d->type.name, NULL);
941 d->pp = malloc(sizeof(*d->pp));
942 my_assert_not(d->pp, NULL);
943 memcpy(d->pp, s->pp, sizeof(*d->pp));
947 struct parsed_proto *proto_clone(const struct parsed_proto *pp_c)
949 struct parsed_proto *pp;
952 pp = malloc(sizeof(*pp));
953 my_assert_not(pp, NULL);
954 memcpy(pp, pp_c, sizeof(*pp)); // lazy..
956 // do the actual deep copy..
957 for (i = 0; i < pp_c->argc; i++)
958 pp_copy_arg(&pp->arg[i], &pp_c->arg[i]);
959 if (pp_c->ret_type.name != NULL)
960 pp->ret_type.name = strdup(pp_c->ret_type.name);
966 static inline int pp_cmp_func(const struct parsed_proto *pp1,
967 const struct parsed_proto *pp2)
971 if (pp1->argc != pp2->argc || pp1->argc_reg != pp2->argc_reg)
974 for (i = 0; i < pp1->argc; i++) {
975 if ((pp1->arg[i].reg != NULL) != (pp2->arg[i].reg != NULL))
978 if ((pp1->arg[i].reg != NULL)
979 && !IS(pp1->arg[i].reg, pp2->arg[i].reg))
989 static inline void pp_print(char *buf, size_t buf_size,
990 const struct parsed_proto *pp)
995 snprintf(buf, buf_size, "%s %s(", pp->ret_type.name, pp->name);
998 for (i = 0; i < pp->argc_reg; i++) {
999 snprintf(buf + l, buf_size - l, "%s%s",
1000 i == 0 ? "" : ", ", pp->arg[i].reg);
1003 if (pp->argc_stack > 0) {
1004 snprintf(buf + l, buf_size - l, "%s{%d stack}",
1005 i == 0 ? "" : ", ", pp->argc_stack);
1008 snprintf(buf + l, buf_size - l, ")");
1011 static inline void proto_release(struct parsed_proto *pp)
1015 for (i = 0; i < pp->argc; i++) {
1016 if (pp->arg[i].reg != NULL)
1017 free(pp->arg[i].reg);
1018 if (pp->arg[i].type.name != NULL)
1019 free(pp->arg[i].type.name);
1020 if (pp->arg[i].pp != NULL)
1021 free(pp->arg[i].pp);
1023 if (pp->ret_type.name != NULL)
1024 free(pp->ret_type.name);
1027 (void)proto_lookup_struct;