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
19 struct parsed_proto_arg {
21 struct parsed_type type;
22 struct parsed_proto *fptr;
29 struct parsed_type ret_type;
30 struct parsed_type type;
32 struct parsed_proto_arg arg[16];
36 unsigned int is_func:1;
37 unsigned int is_stdcall:1;
38 unsigned int is_fastcall:1;
39 unsigned int is_vararg:1; // vararg func
40 unsigned int is_fptr:1;
41 unsigned int is_noreturn:1;
42 unsigned int is_unresolved:1;
43 unsigned int is_userstack:1;
44 unsigned int is_arg:1; // decl in func arg
45 unsigned int has_structarg:1;
46 unsigned int has_retreg:1;
49 static const char *hdrfn;
50 static int hdrfline = 0;
52 static void pp_copy_arg(struct parsed_proto_arg *d,
53 const struct parsed_proto_arg *s);
55 static int b_pp_c_handler(char *proto, const char *fname);
57 static int do_protostrs(FILE *fhdr, const char *fname)
59 const char *finc_name;
60 const char *hdrfn_saved;
72 while (fgets(protostr, sizeof(protostr), fhdr))
75 if (strncmp(protostr, "//#include ", 11) == 0) {
76 finc_name = protostr + 11;
77 p = strpbrk(finc_name, "\r\n ");
82 p = strrchr(hdrfn_saved, '/');
84 memcpy(path, hdrfn_saved,
86 path[p - hdrfn_saved + 1] = 0;
88 snprintf(fname_inc, sizeof(fname_inc), "%s%s",
90 finc = fopen(fname_inc, "r");
92 printf("%s:%d: can't open '%s'\n",
93 fname_inc, line, finc_name);
96 ret = do_protostrs(finc, finc_name);
102 if (strncmp(sskip(protostr), "//", 2) == 0)
105 p = protostr + strlen(protostr);
106 for (p--; p >= protostr && my_isblank(*p); --p)
113 ret = b_pp_c_handler(protostr, hdrfn);
126 static int get_regparm(char *dst, size_t dlen, char *p, int *retreg)
141 for (o = 0; o < dlen; i++) {
153 static const char *known_type_mod[] = {
163 static const char *known_ptr_types[] = {
178 "HIMC", // DWORD in mingw, ptr in wine..
197 "PMEMORY_BASIC_INFORMATION",
209 static const char *ignored_keywords[] = {
217 // returns ptr to char after type ends
218 static int typecmp(const char *n, const char *t)
220 for (; *t != 0; n++, t++) {
221 while (n[0] == ' ' && (n[1] == ' ' || n[1] == '*'))
223 while (t[0] == ' ' && (t[1] == ' ' || t[1] == '*'))
232 static const char *skip_type_mod(const char *n)
237 for (i = 0; i < ARRAY_SIZE(known_type_mod); i++) {
238 len = strlen(known_type_mod[i]);
239 if (strncmp(n, known_type_mod[i], len) != 0)
241 if (!my_isblank(n[len]))
245 while (my_isblank(*n))
253 static int check_type(const char *name, struct parsed_type *type)
259 n = skip_type_mod(name);
261 for (i = 0; i < ARRAY_SIZE(known_ptr_types); i++) {
262 if (typecmp(n, known_ptr_types[i]))
269 if (n[0] == 'L' && n[1] == 'P' && strncmp(n, "LPARAM", 6))
272 // assume single word
273 while (!my_isblank(*n) && !my_issep(*n))
278 while (my_isblank(*n))
289 type->name = strndup(name, ret);
290 if (IS(type->name, "VOID"))
291 memcpy(type->name, "void", 4);
296 /* args are always expanded to 32bit */
297 static const char *map_reg(const char *reg)
299 const char *regs_f[] = { "eax", "ebx", "ecx", "edx", "esi", "edi" };
300 const char *regs_w[] = { "ax", "bx", "cx", "dx", "si", "di" };
301 const char *regs_b[] = { "al", "bl", "cl", "dl" };
304 for (i = 0; i < ARRAY_SIZE(regs_w); i++)
305 if (IS(reg, regs_w[i]))
308 for (i = 0; i < ARRAY_SIZE(regs_b); i++)
309 if (IS(reg, regs_b[i]))
315 static int check_struct_arg(struct parsed_proto_arg *arg)
317 if (IS(arg->type.name, "POINT"))
323 static int parse_protostr(char *protostr, struct parsed_proto *pp)
325 struct parsed_proto_arg *arg;
336 if (p[0] == '/' && p[1] == '/') {
337 printf("%s:%d: commented out?\n", hdrfn, hdrfline);
341 // strip unneeded stuff
342 for (p1 = p; p1[0] != 0 && p1[1] != 0; p1++) {
343 if ((p1[0] == '/' && p1[1] == '*')
344 || (p1[0] == '*' && p1[1] == '/'))
348 if (!strncmp(p, "DECLSPEC_NORETURN ", 18)) {
353 for (i = 0; i < ARRAY_SIZE(ignored_keywords); i++) {
354 l = strlen(ignored_keywords[i]);
355 if (!strncmp(p, ignored_keywords[i], l) && my_isblank(p[l]))
356 p = sskip(p + l + 1);
359 ret = check_type(p, &pp->ret_type);
361 printf("%s:%d:%zd: unhandled return in '%s'\n",
362 hdrfn, hdrfline, (p - protostr) + 1, protostr);
367 if (!strncmp(p, "noreturn ", 9)) {
372 if (!strchr(p, ')')) {
373 p = next_idt(buf, sizeof(buf), p);
376 printf("%s:%d:%zd: var name missing\n",
377 hdrfn, hdrfline, (p - protostr) + 1);
380 strcpy(pp->name, buf);
385 pp->ret_type.is_array = 1;
397 p = next_word(cconv, sizeof(cconv), p);
400 printf("%s:%d:%zd: cconv missing\n",
401 hdrfn, hdrfline, (p - protostr) + 1);
404 if (IS(cconv, "__cdecl"))
406 else if (IS(cconv, "__stdcall"))
408 else if (IS(cconv, "__fastcall")) {
410 pp->is_stdcall = 1; // sort of..
412 else if (IS(cconv, "__thiscall"))
414 else if (IS(cconv, "__userpurge"))
415 pp->is_stdcall = 1; // IDA
416 else if (IS(cconv, "__usercall"))
417 pp->is_stdcall = 0; // IDA
418 else if (IS(cconv, "__userstack")) {
419 pp->is_stdcall = 0; // custom
420 pp->is_userstack = 1;
422 else if (IS(cconv, "WINAPI"))
425 printf("%s:%d:%zd: unhandled cconv: '%s'\n",
426 hdrfn, hdrfline, (p - protostr) + 1, cconv);
432 printf("%s:%d:%zd: '*' expected\n",
433 hdrfn, hdrfline, (p - protostr) + 1);
437 // XXX: skipping extra asterisks, for now
443 p = next_idt(buf, sizeof(buf), p);
446 //printf("%s:%d:%zd: func name missing\n",
447 // hdrfn, hdrfline, (p - protostr) + 1);
450 strcpy(pp->name, buf);
452 ret = get_regparm(regparm, sizeof(regparm), p, &is_retreg);
454 if (!IS(regparm, "eax") && !IS(regparm, "ax")
455 && !IS(regparm, "al") && !IS(regparm, "edx:eax"))
457 printf("%s:%d:%zd: bad regparm: %s\n",
458 hdrfn, hdrfline, (p - protostr) + 1, regparm);
467 // not really ret_type is array, but ohwell
468 pp->ret_type.is_array = 1;
469 p = strchr(p + 1, ']');
471 printf("%s:%d:%zd: ']' expected\n",
472 hdrfn, hdrfline, (p - protostr) + 1);
478 printf("%s:%d:%zd: ')' expected\n",
479 hdrfn, hdrfline, (p - protostr) + 1);
486 printf("%s:%d:%zd: '(' expected, got '%c'\n",
487 hdrfn, hdrfline, (p - protostr) + 1, *p);
494 if ((!strncmp(p, "void", 4) || !strncmp(p, "VOID", 4))
495 && *sskip(p + 4) == ')')
506 printf("%s:%d:%zd: ',' expected\n",
507 hdrfn, hdrfline, (p - protostr) + 1);
513 if (!strncmp(p, "...", 3)) {
520 printf("%s:%d:%zd: ')' expected\n",
521 hdrfn, hdrfline, (p - protostr) + 1);
525 arg = &pp->arg[xarg];
529 ret = check_type(p, &arg->type);
531 printf("%s:%d:%zd: unhandled type for arg%d\n",
532 hdrfn, hdrfline, (p - protostr) + 1, xarg);
539 arg->fptr = calloc(1, sizeof(*arg->fptr));
540 ret = parse_protostr(p1, arg->fptr);
542 printf("%s:%d:%zd: funcarg parse failed\n",
543 hdrfn, hdrfline, p1 - protostr);
546 arg->fptr->is_arg = 1;
547 // we don't use actual names right now..
548 snprintf(arg->fptr->name,
549 sizeof(arg->fptr->name), "a%d", xarg);
550 // we'll treat it as void * for non-calls
551 arg->type.name = strdup("void *");
552 arg->type.is_ptr = 1;
557 p = next_idt(buf, sizeof(buf), p);
561 printf("%s:%d:%zd: idt missing for arg%d\n",
562 hdrfn, hdrfline, (p - protostr) + 1, xarg);
568 ret = get_regparm(regparm, sizeof(regparm), p, &is_retreg);
573 arg->reg = strdup(map_reg(regparm));
574 arg->type.is_retreg = is_retreg;
575 pp->has_retreg |= is_retreg;
578 if (strstr(arg->type.name, "int64")
579 || IS(arg->type.name, "double"))
582 free(arg->type.name);
583 arg->type.name = strdup("int");
584 pp_copy_arg(&pp->arg[xarg], arg);
588 ret = check_struct_arg(arg);
590 pp->has_structarg = 1;
591 arg->type.is_struct = 1;
592 free(arg->type.name);
593 arg->type.name = strdup("int");
594 for (l = 0; l < ret; l++) {
595 pp_copy_arg(&pp->arg[xarg], arg);
601 if (xarg > 0 && (IS(cconv, "__fastcall") || IS(cconv, "__thiscall"))) {
602 if (pp->arg[0].reg != NULL) {
603 printf("%s:%d: %s with arg1 spec %s?\n",
604 hdrfn, hdrfline, cconv, pp->arg[0].reg);
606 pp->arg[0].reg = strdup("ecx");
609 if (xarg > 1 && IS(cconv, "__fastcall")) {
610 if (pp->arg[1].reg != NULL) {
611 printf("%s:%d: %s with arg2 spec %s?\n",
612 hdrfn, hdrfline, cconv, pp->arg[1].reg);
614 pp->arg[1].reg = strdup("edx");
619 for (i = 0; i < pp->argc; i++) {
620 if (pp->arg[i].reg == NULL)
626 if (pp->argc == 1 && pp->arg[0].reg != NULL
627 && IS(pp->arg[0].reg, "ecx"))
631 else if (pp->argc_reg == 2
632 && pp->arg[0].reg != NULL && IS(pp->arg[0].reg, "ecx")
633 && pp->arg[1].reg != NULL && IS(pp->arg[1].reg, "edx"))
638 if (pp->is_vararg && (pp->is_stdcall || pp->is_fastcall)) {
639 printf("%s:%d: vararg %s?\n", hdrfn, hdrfline, cconv);
646 static int pp_name_cmp(const void *p1, const void *p2)
648 const struct parsed_proto *pp1 = p1, *pp2 = p2;
649 return strcmp(pp1->name, pp2->name);
652 static struct parsed_proto *pp_cache;
653 static int pp_cache_size;
654 static int pp_cache_alloc;
656 static int b_pp_c_handler(char *proto, const char *fname)
660 if (pp_cache_size >= pp_cache_alloc) {
661 pp_cache_alloc = pp_cache_alloc * 2 + 64;
662 pp_cache = realloc(pp_cache, pp_cache_alloc
663 * sizeof(pp_cache[0]));
664 my_assert_not(pp_cache, NULL);
665 memset(pp_cache + pp_cache_size, 0,
666 (pp_cache_alloc - pp_cache_size)
667 * sizeof(pp_cache[0]));
670 ret = parse_protostr(proto, &pp_cache[pp_cache_size]);
678 static void build_pp_cache(FILE *fhdr)
686 ret = do_protostrs(fhdr, hdrfn);
690 qsort(pp_cache, pp_cache_size, sizeof(pp_cache[0]), pp_name_cmp);
691 fseek(fhdr, pos, SEEK_SET);
694 static const struct parsed_proto *proto_parse(FILE *fhdr, const char *sym,
697 const struct parsed_proto *pp_ret;
698 struct parsed_proto pp_search;
701 if (pp_cache == NULL)
702 build_pp_cache(fhdr);
704 if (sym[0] == '_') // && strncmp(fname, "stdc", 4) == 0)
707 strcpy(pp_search.name, sym);
708 p = strchr(pp_search.name, '@');
712 pp_ret = bsearch(&pp_search, pp_cache, pp_cache_size,
713 sizeof(pp_cache[0]), pp_name_cmp);
714 if (pp_ret == NULL && !quiet)
715 printf("%s: sym '%s' is missing\n", hdrfn, sym);
720 static void pp_copy_arg(struct parsed_proto_arg *d,
721 const struct parsed_proto_arg *s)
723 memcpy(d, s, sizeof(*d));
725 if (s->reg != NULL) {
726 d->reg = strdup(s->reg);
727 my_assert_not(d->reg, NULL);
729 if (s->type.name != NULL) {
730 d->type.name = strdup(s->type.name);
731 my_assert_not(d->type.name, NULL);
733 if (s->fptr != NULL) {
734 d->fptr = malloc(sizeof(*d->fptr));
735 my_assert_not(d->fptr, NULL);
736 memcpy(d->fptr, s->fptr, sizeof(*d->fptr));
740 struct parsed_proto *proto_clone(const struct parsed_proto *pp_c)
742 struct parsed_proto *pp;
745 pp = malloc(sizeof(*pp));
746 my_assert_not(pp, NULL);
747 memcpy(pp, pp_c, sizeof(*pp)); // lazy..
749 // do the actual deep copy..
750 for (i = 0; i < pp_c->argc; i++)
751 pp_copy_arg(&pp->arg[i], &pp_c->arg[i]);
752 if (pp_c->ret_type.name != NULL)
753 pp->ret_type.name = strdup(pp_c->ret_type.name);
759 static inline int pp_cmp_func(const struct parsed_proto *pp1,
760 const struct parsed_proto *pp2)
764 if (pp1->argc != pp2->argc || pp1->argc_reg != pp2->argc_reg)
767 for (i = 0; i < pp1->argc; i++) {
768 if ((pp1->arg[i].reg != NULL) != (pp2->arg[i].reg != NULL))
771 if ((pp1->arg[i].reg != NULL)
772 && !IS(pp1->arg[i].reg, pp2->arg[i].reg))
782 static inline void pp_print(char *buf, size_t buf_size,
783 const struct parsed_proto *pp)
788 snprintf(buf, buf_size, "%s %s(", pp->ret_type.name, pp->name);
791 for (i = 0; i < pp->argc_reg; i++) {
792 snprintf(buf + l, buf_size - l, "%s%s",
793 i == 0 ? "" : ", ", pp->arg[i].reg);
796 if (pp->argc_stack > 0) {
797 snprintf(buf + l, buf_size - l, "%s{%d stack}",
798 i == 0 ? "" : ", ", pp->argc_stack);
801 snprintf(buf + l, buf_size - l, ")");
804 static inline void proto_release(struct parsed_proto *pp)
808 for (i = 0; i < pp->argc; i++) {
809 if (pp->arg[i].reg != NULL)
810 free(pp->arg[i].reg);
811 if (pp->arg[i].type.name != NULL)
812 free(pp->arg[i].type.name);
813 if (pp->arg[i].fptr != NULL)
814 free(pp->arg[i].fptr);
816 if (pp->ret_type.name != NULL)
817 free(pp->ret_type.name);