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[] = {
203 "HIMC", // DWORD in mingw, ptr in wine..
222 "PMEMORY_BASIC_INFORMATION",
234 static const char *ignored_keywords[] = {
242 static int typecmp(const char *n, const char *t)
244 for (; *t != 0; n++, t++) {
245 while (n[0] == ' ' && (n[1] == ' ' || n[1] == '*'))
247 while (t[0] == ' ' && (t[1] == ' ' || t[1] == '*'))
256 static const char *skip_type_mod(const char *n)
261 for (i = 0; i < ARRAY_SIZE(known_type_mod); i++) {
262 len = strlen(known_type_mod[i]);
263 if (strncmp(n, known_type_mod[i], len) != 0)
265 if (!my_isblank(n[len]))
269 while (my_isblank(*n))
277 static int check_type(const char *name, struct parsed_type *type)
283 n = skip_type_mod(name);
285 if (!strncmp(n, "struct", 6) && my_isblank(n[6])) {
289 while (my_isblank(*n))
293 for (i = 0; i < ARRAY_SIZE(known_ptr_types); i++) {
294 if (typecmp(n, known_ptr_types[i]))
301 if (n[0] == 'L' && n[1] == 'P' && strncmp(n, "LPARAM", 6))
304 // assume single word
305 while (!my_isblank(*n) && !my_issep(*n))
310 while (my_isblank(*n))
321 type->name = strndup(name, ret);
322 if (IS(type->name, "__VALIST") || IS(type->name, "va_list"))
323 type->is_va_list = 1;
324 if (IS(type->name, "VOID"))
325 memcpy(type->name, "void", 4);
330 /* args are always expanded to 32bit */
331 static const char *map_reg(const char *reg)
333 const char *regs_f[] = { "eax", "ebx", "ecx", "edx", "esi", "edi" };
334 const char *regs_w[] = { "ax", "bx", "cx", "dx", "si", "di" };
335 const char *regs_b[] = { "al", "bl", "cl", "dl" };
338 for (i = 0; i < ARRAY_SIZE(regs_w); i++)
339 if (IS(reg, regs_w[i]))
342 for (i = 0; i < ARRAY_SIZE(regs_b); i++)
343 if (IS(reg, regs_b[i]))
349 static int check_struct_arg(struct parsed_proto_arg *arg)
351 if (IS(arg->type.name, "POINT"))
357 static int parse_protostr(char *protostr, struct parsed_proto *pp);
359 static int parse_arg(char **p_, struct parsed_proto_arg *arg, int xarg)
366 arg->pp = calloc(1, sizeof(*arg->pp));
367 my_assert_not(arg->pp, NULL);
372 pe = strpbrk(pe, ",()");
375 if (*pe == ',' || *pe == ')')
377 pe = strchr(pe, ')');
383 if (pe - p > sizeof(buf) - 1)
385 memcpy(buf, p, pe - p);
388 ret = parse_protostr(buf, arg->pp);
392 // we don't use actual names right now...
393 snprintf(arg->pp->name, sizeof(arg->pp->name), "a%d", xarg);
395 if (!arg->type.is_struct)
396 // we'll treat it as void * for non-calls
397 arg->type.name = strdup("void *");
398 arg->type.is_ptr = 1;
405 static int parse_protostr(char *protostr, struct parsed_proto *pp)
407 struct parsed_proto_arg *arg;
418 if (p[0] == '/' && p[1] == '/') {
419 printf("%s:%d: commented out?\n", hdrfn, hdrfline);
423 // allow start of line comment
424 if (p[0] == '/' && p[1] == '*') {
425 p = strstr(p + 2, "*/");
427 printf("%s:%d: multiline comments unsupported\n",
434 // we need remaining hints in comments, so strip / *
435 for (p1 = p; p1[0] != 0 && p1[1] != 0; p1++) {
436 if ((p1[0] == '/' && p1[1] == '*')
437 || (p1[0] == '*' && p1[1] == '/'))
441 if (!strncmp(p, "DECLSPEC_NORETURN ", 18)) {
446 for (i = 0; i < ARRAY_SIZE(ignored_keywords); i++) {
447 l = strlen(ignored_keywords[i]);
448 if (!strncmp(p, ignored_keywords[i], l) && my_isblank(p[l]))
449 p = sskip(p + l + 1);
452 ret = check_type(p, &pp->ret_type);
454 printf("%s:%d:%zd: unhandled return in '%s'\n",
455 hdrfn, hdrfline, (p - protostr) + 1, protostr);
460 if (!strncmp(p, "noreturn ", 9)) {
465 if (!strchr(p, ')')) {
466 p = next_idt(buf, sizeof(buf), p);
468 if (!pp->is_arg && buf[0] == 0) {
469 printf("%s:%d:%zd: var name is missing\n",
470 hdrfn, hdrfline, (p - protostr) + 1);
473 strcpy(pp->name, buf);
478 pp->ret_type.is_array = 1;
490 p = next_word(cconv, sizeof(cconv), p);
493 printf("%s:%d:%zd: cconv missing\n",
494 hdrfn, hdrfline, (p - protostr) + 1);
497 if (IS(cconv, "__cdecl"))
499 else if (IS(cconv, "__stdcall"))
501 else if (IS(cconv, "__fastcall")) {
503 pp->is_stdcall = 1; // sort of..
505 else if (IS(cconv, "__thiscall"))
507 else if (IS(cconv, "__userpurge"))
508 pp->is_stdcall = 1; // IDA
509 else if (IS(cconv, "__usercall"))
510 pp->is_stdcall = 0; // IDA
511 else if (IS(cconv, "__userstack")) {
512 pp->is_stdcall = 0; // custom
513 pp->is_userstack = 1;
515 else if (IS(cconv, "WINAPI"))
518 printf("%s:%d:%zd: unhandled cconv: '%s'\n",
519 hdrfn, hdrfline, (p - protostr) + 1, cconv);
525 printf("%s:%d:%zd: '*' expected\n",
526 hdrfn, hdrfline, (p - protostr) + 1);
530 // XXX: skipping extra asterisks, for now
536 p = next_idt(buf, sizeof(buf), p);
539 //printf("%s:%d:%zd: func name missing\n",
540 // hdrfn, hdrfline, (p - protostr) + 1);
543 strcpy(pp->name, buf);
545 ret = get_regparm(regparm, sizeof(regparm), p, &is_retreg);
547 if (!IS(regparm, "eax") && !IS(regparm, "ax")
548 && !IS(regparm, "al") && !IS(regparm, "edx:eax"))
550 printf("%s:%d:%zd: bad regparm: %s\n",
551 hdrfn, hdrfline, (p - protostr) + 1, regparm);
560 // not really ret_type is array, but ohwell
561 pp->ret_type.is_array = 1;
562 p = strchr(p + 1, ']');
564 printf("%s:%d:%zd: ']' expected\n",
565 hdrfn, hdrfline, (p - protostr) + 1);
571 printf("%s:%d:%zd: ')' expected\n",
572 hdrfn, hdrfline, (p - protostr) + 1);
579 printf("%s:%d:%zd: '(' expected, got '%c'\n",
580 hdrfn, hdrfline, (p - protostr) + 1, *p);
587 if ((!strncmp(p, "void", 4) || !strncmp(p, "VOID", 4))
588 && *sskip(p + 4) == ')')
599 printf("%s:%d:%zd: ',' expected\n",
600 hdrfn, hdrfline, (p - protostr) + 1);
606 if (!strncmp(p, "...", 3)) {
613 printf("%s:%d:%zd: ')' expected\n",
614 hdrfn, hdrfline, (p - protostr) + 1);
618 arg = &pp->arg[xarg];
622 ret = check_type(p, &arg->type);
624 printf("%s:%d:%zd: unhandled type for arg%d\n",
625 hdrfn, hdrfline, (p - protostr) + 1, xarg);
630 if (*p == '(' || arg->type.is_struct) {
631 // func ptr or struct
632 ret = parse_arg(&p1, arg, xarg);
634 printf("%s:%d:%zd: funcarg parse failed\n",
635 hdrfn, hdrfline, p1 - protostr);
641 p = next_idt(buf, sizeof(buf), p);
645 printf("%s:%d:%zd: idt missing for arg%d\n",
646 hdrfn, hdrfline, (p - protostr) + 1, xarg);
652 ret = get_regparm(regparm, sizeof(regparm), p, &is_retreg);
657 arg->reg = strdup(map_reg(regparm));
658 arg->type.is_retreg = is_retreg;
659 pp->has_retreg |= is_retreg;
662 if (strstr(arg->type.name, "int64")
663 || IS(arg->type.name, "double"))
666 free(arg->type.name);
667 arg->type.name = strdup("int");
668 pp_copy_arg(&pp->arg[xarg], arg);
672 ret = check_struct_arg(arg);
674 pp->has_structarg = 1;
675 arg->type.is_struct = 1;
676 free(arg->type.name);
677 arg->type.name = strdup("int");
678 for (l = 0; l < ret; l++) {
679 pp_copy_arg(&pp->arg[xarg], arg);
685 if (xarg > 0 && (IS(cconv, "__fastcall") || IS(cconv, "__thiscall"))) {
686 if (pp->arg[0].reg != NULL) {
687 printf("%s:%d: %s with arg1 spec %s?\n",
688 hdrfn, hdrfline, cconv, pp->arg[0].reg);
690 pp->arg[0].reg = strdup("ecx");
693 if (xarg > 1 && IS(cconv, "__fastcall")) {
694 if (pp->arg[1].reg != NULL) {
695 printf("%s:%d: %s with arg2 spec %s?\n",
696 hdrfn, hdrfline, cconv, pp->arg[1].reg);
698 pp->arg[1].reg = strdup("edx");
703 for (i = 0; i < pp->argc; i++) {
704 if (pp->arg[i].reg == NULL)
710 if (pp->argc == 1 && pp->arg[0].reg != NULL
711 && IS(pp->arg[0].reg, "ecx"))
715 else if (pp->argc_reg == 2
716 && pp->arg[0].reg != NULL && IS(pp->arg[0].reg, "ecx")
717 && pp->arg[1].reg != NULL && IS(pp->arg[1].reg, "edx"))
722 if (pp->is_vararg && (pp->is_stdcall || pp->is_fastcall)) {
723 printf("%s:%d: vararg %s?\n", hdrfn, hdrfline, cconv);
730 static int pp_name_cmp(const void *p1, const void *p2)
732 const struct parsed_proto *pp1 = p1, *pp2 = p2;
733 return strcmp(pp1->name, pp2->name);
736 static int ps_name_cmp(const void *p1, const void *p2)
738 const struct parsed_struct *ps1 = p1, *ps2 = p2;
739 return strcmp(ps1->name, ps2->name);
742 // parsed struct cache
743 static struct parsed_struct *ps_cache;
744 static int ps_cache_size;
745 static int ps_cache_alloc;
747 static int struct_handler(FILE *fhdr, char *proto, int *line)
749 struct parsed_struct *ps;
755 if (ps_cache_size >= ps_cache_alloc) {
756 ps_cache_alloc = ps_cache_alloc * 2 + 64;
757 ps_cache = realloc(ps_cache, ps_cache_alloc
758 * sizeof(ps_cache[0]));
759 my_assert_not(ps_cache, NULL);
760 memset(ps_cache + ps_cache_size, 0,
761 (ps_cache_alloc - ps_cache_size)
762 * sizeof(ps_cache[0]));
765 ps = &ps_cache[ps_cache_size++];
766 ret = sscanf(proto, "struct %255s {", ps->name);
768 printf("%s:%d: struct parse failed\n", hdrfn, *line);
772 while (fgets(lstr, sizeof(lstr), fhdr))
777 if (p[0] == '/' && p[1] == '/')
782 if (m >= ARRAY_SIZE(ps->members)) {
783 printf("%s:%d: too many struct members\n",
789 ret = parse_protostr(p, &ps->members[m].pp);
791 printf("%s:%d: struct member #%d/%02x "
792 "doesn't parse\n", hdrfn, *line,
796 ps->members[m].offset = offset;
801 ps->member_count = m;
806 // parsed proto cache
807 static struct parsed_proto *pp_cache;
808 static int pp_cache_size;
809 static int pp_cache_alloc;
811 static int b_pp_c_handler(char *proto, const char *fname,
812 int is_include, int is_osinc, int is_cinc)
816 if (pp_cache_size >= pp_cache_alloc) {
817 pp_cache_alloc = pp_cache_alloc * 2 + 64;
818 pp_cache = realloc(pp_cache, pp_cache_alloc
819 * sizeof(pp_cache[0]));
820 my_assert_not(pp_cache, NULL);
821 memset(pp_cache + pp_cache_size, 0,
822 (pp_cache_alloc - pp_cache_size)
823 * sizeof(pp_cache[0]));
826 ret = parse_protostr(proto, &pp_cache[pp_cache_size]);
830 pp_cache[pp_cache_size].is_include = is_include;
831 pp_cache[pp_cache_size].is_osinc = is_osinc;
832 pp_cache[pp_cache_size].is_cinc = is_cinc;
837 static void build_caches(FILE *fhdr)
845 ret = do_protostrs(fhdr, hdrfn, 0);
849 qsort(pp_cache, pp_cache_size, sizeof(pp_cache[0]), pp_name_cmp);
850 qsort(ps_cache, ps_cache_size, sizeof(ps_cache[0]), ps_name_cmp);
851 fseek(fhdr, pos, SEEK_SET);
854 static const struct parsed_proto *proto_parse(FILE *fhdr, const char *sym,
857 const struct parsed_proto *pp_ret;
858 struct parsed_proto pp_search;
861 if (pp_cache == NULL)
864 if (sym[0] == '_') // && strncmp(fname, "stdc", 4) == 0)
867 strcpy(pp_search.name, sym);
868 p = strchr(pp_search.name, '@');
872 pp_ret = bsearch(&pp_search, pp_cache, pp_cache_size,
873 sizeof(pp_cache[0]), pp_name_cmp);
874 if (pp_ret == NULL && !quiet)
875 printf("%s: sym '%s' is missing\n", hdrfn, sym);
880 static const struct parsed_proto *proto_lookup_struct(FILE *fhdr,
881 const char *type, int offset)
883 struct parsed_struct ps_search, *ps;
886 if (pp_cache == NULL)
888 if (ps_cache_size == 0)
891 while (my_isblank(*type))
893 if (!strncmp(type, "struct", 6) && my_isblank(type[6]))
896 if (sscanf(type, "%255s", ps_search.name) != 1)
899 ps = bsearch(&ps_search, ps_cache, ps_cache_size,
900 sizeof(ps_cache[0]), ps_name_cmp);
902 printf("%s: struct '%s' is missing\n",
903 hdrfn, ps_search.name);
907 for (m = 0; m < ps->member_count; m++) {
908 if (ps->members[m].offset == offset)
909 return &ps->members[m].pp;
915 static void pp_copy_arg(struct parsed_proto_arg *d,
916 const struct parsed_proto_arg *s)
918 memcpy(d, s, sizeof(*d));
920 if (s->reg != NULL) {
921 d->reg = strdup(s->reg);
922 my_assert_not(d->reg, NULL);
924 if (s->type.name != NULL) {
925 d->type.name = strdup(s->type.name);
926 my_assert_not(d->type.name, NULL);
929 d->pp = malloc(sizeof(*d->pp));
930 my_assert_not(d->pp, NULL);
931 memcpy(d->pp, s->pp, sizeof(*d->pp));
935 struct parsed_proto *proto_clone(const struct parsed_proto *pp_c)
937 struct parsed_proto *pp;
940 pp = malloc(sizeof(*pp));
941 my_assert_not(pp, NULL);
942 memcpy(pp, pp_c, sizeof(*pp)); // lazy..
944 // do the actual deep copy..
945 for (i = 0; i < pp_c->argc; i++)
946 pp_copy_arg(&pp->arg[i], &pp_c->arg[i]);
947 if (pp_c->ret_type.name != NULL)
948 pp->ret_type.name = strdup(pp_c->ret_type.name);
954 static inline int pp_cmp_func(const struct parsed_proto *pp1,
955 const struct parsed_proto *pp2)
959 if (pp1->argc != pp2->argc || pp1->argc_reg != pp2->argc_reg)
962 for (i = 0; i < pp1->argc; i++) {
963 if ((pp1->arg[i].reg != NULL) != (pp2->arg[i].reg != NULL))
966 if ((pp1->arg[i].reg != NULL)
967 && !IS(pp1->arg[i].reg, pp2->arg[i].reg))
977 static inline void pp_print(char *buf, size_t buf_size,
978 const struct parsed_proto *pp)
983 snprintf(buf, buf_size, "%s %s(", pp->ret_type.name, pp->name);
986 for (i = 0; i < pp->argc_reg; i++) {
987 snprintf(buf + l, buf_size - l, "%s%s",
988 i == 0 ? "" : ", ", pp->arg[i].reg);
991 if (pp->argc_stack > 0) {
992 snprintf(buf + l, buf_size - l, "%s{%d stack}",
993 i == 0 ? "" : ", ", pp->argc_stack);
996 snprintf(buf + l, buf_size - l, ")");
999 static inline void proto_release(struct parsed_proto *pp)
1003 for (i = 0; i < pp->argc; i++) {
1004 if (pp->arg[i].reg != NULL)
1005 free(pp->arg[i].reg);
1006 if (pp->arg[i].type.name != NULL)
1007 free(pp->arg[i].type.name);
1008 if (pp->arg[i].pp != NULL)
1009 free(pp->arg[i].pp);
1011 if (pp->ret_type.name != NULL)
1012 free(pp->ret_type.name);
1015 (void)proto_lookup_struct;