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 *fptr;
30 struct parsed_type ret_type;
31 struct parsed_type type;
33 struct parsed_proto_arg arg[16];
37 unsigned int is_func:1;
38 unsigned int is_stdcall:1;
39 unsigned int is_fastcall:1;
40 unsigned int is_vararg:1; // vararg func
41 unsigned int is_fptr:1;
42 unsigned int is_noreturn:1;
43 unsigned int is_unresolved:1;
44 unsigned int is_userstack:1;
45 unsigned int is_include:1; // not from top-level header
46 unsigned int is_osinc:1; // OS/system library func
47 unsigned int is_arg:1; // declared in some func arg
48 unsigned int has_structarg:1;
49 unsigned int has_retreg:1;
52 static const char *hdrfn;
53 static int hdrfline = 0;
55 static void pp_copy_arg(struct parsed_proto_arg *d,
56 const struct parsed_proto_arg *s);
58 static int b_pp_c_handler(char *proto, const char *fname,
59 int is_include, int is_osinc);
61 static int do_protostrs(FILE *fhdr, const char *fname, int is_include)
63 const char *finc_name;
64 const char *hdrfn_saved;
77 is_osinc = strstr(fname, "stdc.hlist")
78 || strstr(fname, "win32.hlist");
80 while (fgets(protostr, sizeof(protostr), fhdr))
83 if (strncmp(protostr, "//#include ", 11) == 0) {
84 finc_name = protostr + 11;
85 p = strpbrk(finc_name, "\r\n ");
90 p = strrchr(hdrfn_saved, '/');
92 memcpy(path, hdrfn_saved,
94 path[p - hdrfn_saved + 1] = 0;
96 snprintf(fname_inc, sizeof(fname_inc), "%s%s",
98 finc = fopen(fname_inc, "r");
100 printf("%s:%d: can't open '%s'\n",
101 fname_inc, line, finc_name);
104 ret = do_protostrs(finc, finc_name, 1);
110 if (strncmp(sskip(protostr), "//", 2) == 0)
113 p = protostr + strlen(protostr);
114 for (p--; p >= protostr && my_isblank(*p); --p)
121 ret = b_pp_c_handler(protostr, hdrfn, is_include,
135 static int get_regparm(char *dst, size_t dlen, char *p, int *retreg)
150 for (o = 0; o < dlen; i++) {
162 static const char *known_type_mod[] = {
172 static const char *known_ptr_types[] = {
187 "HIMC", // DWORD in mingw, ptr in wine..
206 "PMEMORY_BASIC_INFORMATION",
218 static const char *ignored_keywords[] = {
226 // returns ptr to char after type ends
227 static int typecmp(const char *n, const char *t)
229 for (; *t != 0; n++, t++) {
230 while (n[0] == ' ' && (n[1] == ' ' || n[1] == '*'))
232 while (t[0] == ' ' && (t[1] == ' ' || t[1] == '*'))
241 static const char *skip_type_mod(const char *n)
246 for (i = 0; i < ARRAY_SIZE(known_type_mod); i++) {
247 len = strlen(known_type_mod[i]);
248 if (strncmp(n, known_type_mod[i], len) != 0)
250 if (!my_isblank(n[len]))
254 while (my_isblank(*n))
262 static int check_type(const char *name, struct parsed_type *type)
268 n = skip_type_mod(name);
270 for (i = 0; i < ARRAY_SIZE(known_ptr_types); i++) {
271 if (typecmp(n, known_ptr_types[i]))
278 if (n[0] == 'L' && n[1] == 'P' && strncmp(n, "LPARAM", 6))
281 // assume single word
282 while (!my_isblank(*n) && !my_issep(*n))
287 while (my_isblank(*n))
298 type->name = strndup(name, ret);
299 if (IS(type->name, "__VALIST") || IS(type->name, "va_list"))
300 type->is_va_list = 1;
301 if (IS(type->name, "VOID"))
302 memcpy(type->name, "void", 4);
307 /* args are always expanded to 32bit */
308 static const char *map_reg(const char *reg)
310 const char *regs_f[] = { "eax", "ebx", "ecx", "edx", "esi", "edi" };
311 const char *regs_w[] = { "ax", "bx", "cx", "dx", "si", "di" };
312 const char *regs_b[] = { "al", "bl", "cl", "dl" };
315 for (i = 0; i < ARRAY_SIZE(regs_w); i++)
316 if (IS(reg, regs_w[i]))
319 for (i = 0; i < ARRAY_SIZE(regs_b); i++)
320 if (IS(reg, regs_b[i]))
326 static int check_struct_arg(struct parsed_proto_arg *arg)
328 if (IS(arg->type.name, "POINT"))
334 static int parse_protostr(char *protostr, struct parsed_proto *pp)
336 struct parsed_proto_arg *arg;
347 if (p[0] == '/' && p[1] == '/') {
348 printf("%s:%d: commented out?\n", hdrfn, hdrfline);
352 // strip unneeded stuff
353 for (p1 = p; p1[0] != 0 && p1[1] != 0; p1++) {
354 if ((p1[0] == '/' && p1[1] == '*')
355 || (p1[0] == '*' && p1[1] == '/'))
359 if (!strncmp(p, "DECLSPEC_NORETURN ", 18)) {
364 for (i = 0; i < ARRAY_SIZE(ignored_keywords); i++) {
365 l = strlen(ignored_keywords[i]);
366 if (!strncmp(p, ignored_keywords[i], l) && my_isblank(p[l]))
367 p = sskip(p + l + 1);
370 ret = check_type(p, &pp->ret_type);
372 printf("%s:%d:%zd: unhandled return in '%s'\n",
373 hdrfn, hdrfline, (p - protostr) + 1, protostr);
378 if (!strncmp(p, "noreturn ", 9)) {
383 if (!strchr(p, ')')) {
384 p = next_idt(buf, sizeof(buf), p);
387 printf("%s:%d:%zd: var name missing\n",
388 hdrfn, hdrfline, (p - protostr) + 1);
391 strcpy(pp->name, buf);
396 pp->ret_type.is_array = 1;
408 p = next_word(cconv, sizeof(cconv), p);
411 printf("%s:%d:%zd: cconv missing\n",
412 hdrfn, hdrfline, (p - protostr) + 1);
415 if (IS(cconv, "__cdecl"))
417 else if (IS(cconv, "__stdcall"))
419 else if (IS(cconv, "__fastcall")) {
421 pp->is_stdcall = 1; // sort of..
423 else if (IS(cconv, "__thiscall"))
425 else if (IS(cconv, "__userpurge"))
426 pp->is_stdcall = 1; // IDA
427 else if (IS(cconv, "__usercall"))
428 pp->is_stdcall = 0; // IDA
429 else if (IS(cconv, "__userstack")) {
430 pp->is_stdcall = 0; // custom
431 pp->is_userstack = 1;
433 else if (IS(cconv, "WINAPI"))
436 printf("%s:%d:%zd: unhandled cconv: '%s'\n",
437 hdrfn, hdrfline, (p - protostr) + 1, cconv);
443 printf("%s:%d:%zd: '*' expected\n",
444 hdrfn, hdrfline, (p - protostr) + 1);
448 // XXX: skipping extra asterisks, for now
454 p = next_idt(buf, sizeof(buf), p);
457 //printf("%s:%d:%zd: func name missing\n",
458 // hdrfn, hdrfline, (p - protostr) + 1);
461 strcpy(pp->name, buf);
463 ret = get_regparm(regparm, sizeof(regparm), p, &is_retreg);
465 if (!IS(regparm, "eax") && !IS(regparm, "ax")
466 && !IS(regparm, "al") && !IS(regparm, "edx:eax"))
468 printf("%s:%d:%zd: bad regparm: %s\n",
469 hdrfn, hdrfline, (p - protostr) + 1, regparm);
478 // not really ret_type is array, but ohwell
479 pp->ret_type.is_array = 1;
480 p = strchr(p + 1, ']');
482 printf("%s:%d:%zd: ']' expected\n",
483 hdrfn, hdrfline, (p - protostr) + 1);
489 printf("%s:%d:%zd: ')' expected\n",
490 hdrfn, hdrfline, (p - protostr) + 1);
497 printf("%s:%d:%zd: '(' expected, got '%c'\n",
498 hdrfn, hdrfline, (p - protostr) + 1, *p);
505 if ((!strncmp(p, "void", 4) || !strncmp(p, "VOID", 4))
506 && *sskip(p + 4) == ')')
517 printf("%s:%d:%zd: ',' expected\n",
518 hdrfn, hdrfline, (p - protostr) + 1);
524 if (!strncmp(p, "...", 3)) {
531 printf("%s:%d:%zd: ')' expected\n",
532 hdrfn, hdrfline, (p - protostr) + 1);
536 arg = &pp->arg[xarg];
540 ret = check_type(p, &arg->type);
542 printf("%s:%d:%zd: unhandled type for arg%d\n",
543 hdrfn, hdrfline, (p - protostr) + 1, xarg);
550 arg->fptr = calloc(1, sizeof(*arg->fptr));
551 ret = parse_protostr(p1, arg->fptr);
553 printf("%s:%d:%zd: funcarg parse failed\n",
554 hdrfn, hdrfline, p1 - protostr);
557 arg->fptr->is_arg = 1;
558 // we don't use actual names right now..
559 snprintf(arg->fptr->name,
560 sizeof(arg->fptr->name), "a%d", xarg);
561 // we'll treat it as void * for non-calls
562 arg->type.name = strdup("void *");
563 arg->type.is_ptr = 1;
568 p = next_idt(buf, sizeof(buf), p);
572 printf("%s:%d:%zd: idt missing for arg%d\n",
573 hdrfn, hdrfline, (p - protostr) + 1, xarg);
579 ret = get_regparm(regparm, sizeof(regparm), p, &is_retreg);
584 arg->reg = strdup(map_reg(regparm));
585 arg->type.is_retreg = is_retreg;
586 pp->has_retreg |= is_retreg;
589 if (strstr(arg->type.name, "int64")
590 || IS(arg->type.name, "double"))
593 free(arg->type.name);
594 arg->type.name = strdup("int");
595 pp_copy_arg(&pp->arg[xarg], arg);
599 ret = check_struct_arg(arg);
601 pp->has_structarg = 1;
602 arg->type.is_struct = 1;
603 free(arg->type.name);
604 arg->type.name = strdup("int");
605 for (l = 0; l < ret; l++) {
606 pp_copy_arg(&pp->arg[xarg], arg);
612 if (xarg > 0 && (IS(cconv, "__fastcall") || IS(cconv, "__thiscall"))) {
613 if (pp->arg[0].reg != NULL) {
614 printf("%s:%d: %s with arg1 spec %s?\n",
615 hdrfn, hdrfline, cconv, pp->arg[0].reg);
617 pp->arg[0].reg = strdup("ecx");
620 if (xarg > 1 && IS(cconv, "__fastcall")) {
621 if (pp->arg[1].reg != NULL) {
622 printf("%s:%d: %s with arg2 spec %s?\n",
623 hdrfn, hdrfline, cconv, pp->arg[1].reg);
625 pp->arg[1].reg = strdup("edx");
630 for (i = 0; i < pp->argc; i++) {
631 if (pp->arg[i].reg == NULL)
637 if (pp->argc == 1 && pp->arg[0].reg != NULL
638 && IS(pp->arg[0].reg, "ecx"))
642 else if (pp->argc_reg == 2
643 && pp->arg[0].reg != NULL && IS(pp->arg[0].reg, "ecx")
644 && pp->arg[1].reg != NULL && IS(pp->arg[1].reg, "edx"))
649 if (pp->is_vararg && (pp->is_stdcall || pp->is_fastcall)) {
650 printf("%s:%d: vararg %s?\n", hdrfn, hdrfline, cconv);
657 static int pp_name_cmp(const void *p1, const void *p2)
659 const struct parsed_proto *pp1 = p1, *pp2 = p2;
660 return strcmp(pp1->name, pp2->name);
663 static struct parsed_proto *pp_cache;
664 static int pp_cache_size;
665 static int pp_cache_alloc;
667 static int b_pp_c_handler(char *proto, const char *fname,
668 int is_include, int is_osinc)
672 if (pp_cache_size >= pp_cache_alloc) {
673 pp_cache_alloc = pp_cache_alloc * 2 + 64;
674 pp_cache = realloc(pp_cache, pp_cache_alloc
675 * sizeof(pp_cache[0]));
676 my_assert_not(pp_cache, NULL);
677 memset(pp_cache + pp_cache_size, 0,
678 (pp_cache_alloc - pp_cache_size)
679 * sizeof(pp_cache[0]));
682 ret = parse_protostr(proto, &pp_cache[pp_cache_size]);
686 pp_cache[pp_cache_size].is_include = is_include;
687 pp_cache[pp_cache_size].is_osinc = is_osinc;
692 static void build_pp_cache(FILE *fhdr)
700 ret = do_protostrs(fhdr, hdrfn, 0);
704 qsort(pp_cache, pp_cache_size, sizeof(pp_cache[0]), pp_name_cmp);
705 fseek(fhdr, pos, SEEK_SET);
708 static const struct parsed_proto *proto_parse(FILE *fhdr, const char *sym,
711 const struct parsed_proto *pp_ret;
712 struct parsed_proto pp_search;
715 if (pp_cache == NULL)
716 build_pp_cache(fhdr);
718 if (sym[0] == '_') // && strncmp(fname, "stdc", 4) == 0)
721 strcpy(pp_search.name, sym);
722 p = strchr(pp_search.name, '@');
726 pp_ret = bsearch(&pp_search, pp_cache, pp_cache_size,
727 sizeof(pp_cache[0]), pp_name_cmp);
728 if (pp_ret == NULL && !quiet)
729 printf("%s: sym '%s' is missing\n", hdrfn, sym);
734 static void pp_copy_arg(struct parsed_proto_arg *d,
735 const struct parsed_proto_arg *s)
737 memcpy(d, s, sizeof(*d));
739 if (s->reg != NULL) {
740 d->reg = strdup(s->reg);
741 my_assert_not(d->reg, NULL);
743 if (s->type.name != NULL) {
744 d->type.name = strdup(s->type.name);
745 my_assert_not(d->type.name, NULL);
747 if (s->fptr != NULL) {
748 d->fptr = malloc(sizeof(*d->fptr));
749 my_assert_not(d->fptr, NULL);
750 memcpy(d->fptr, s->fptr, sizeof(*d->fptr));
754 struct parsed_proto *proto_clone(const struct parsed_proto *pp_c)
756 struct parsed_proto *pp;
759 pp = malloc(sizeof(*pp));
760 my_assert_not(pp, NULL);
761 memcpy(pp, pp_c, sizeof(*pp)); // lazy..
763 // do the actual deep copy..
764 for (i = 0; i < pp_c->argc; i++)
765 pp_copy_arg(&pp->arg[i], &pp_c->arg[i]);
766 if (pp_c->ret_type.name != NULL)
767 pp->ret_type.name = strdup(pp_c->ret_type.name);
773 static inline int pp_cmp_func(const struct parsed_proto *pp1,
774 const struct parsed_proto *pp2)
778 if (pp1->argc != pp2->argc || pp1->argc_reg != pp2->argc_reg)
781 for (i = 0; i < pp1->argc; i++) {
782 if ((pp1->arg[i].reg != NULL) != (pp2->arg[i].reg != NULL))
785 if ((pp1->arg[i].reg != NULL)
786 && !IS(pp1->arg[i].reg, pp2->arg[i].reg))
796 static inline void pp_print(char *buf, size_t buf_size,
797 const struct parsed_proto *pp)
802 snprintf(buf, buf_size, "%s %s(", pp->ret_type.name, pp->name);
805 for (i = 0; i < pp->argc_reg; i++) {
806 snprintf(buf + l, buf_size - l, "%s%s",
807 i == 0 ? "" : ", ", pp->arg[i].reg);
810 if (pp->argc_stack > 0) {
811 snprintf(buf + l, buf_size - l, "%s{%d stack}",
812 i == 0 ? "" : ", ", pp->argc_stack);
815 snprintf(buf + l, buf_size - l, ")");
818 static inline void proto_release(struct parsed_proto *pp)
822 for (i = 0; i < pp->argc; i++) {
823 if (pp->arg[i].reg != NULL)
824 free(pp->arg[i].reg);
825 if (pp->arg[i].type.name != NULL)
826 free(pp->arg[i].type.name);
827 if (pp->arg[i].fptr != NULL)
828 free(pp->arg[i].fptr);
830 if (pp->ret_type.name != NULL)
831 free(pp->ret_type.name);