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_oslib:1; // OS/system library func
46 unsigned int is_arg:1; // declared in some func arg
47 unsigned int has_structarg:1;
48 unsigned int has_retreg:1;
51 static const char *hdrfn;
52 static int hdrfline = 0;
54 static void pp_copy_arg(struct parsed_proto_arg *d,
55 const struct parsed_proto_arg *s);
57 static int b_pp_c_handler(char *proto, const char *fname, int is_oslib);
59 static int do_protostrs(FILE *fhdr, const char *fname)
61 const char *finc_name;
62 const char *hdrfn_saved;
75 is_oslib = strstr(fname, "stdc.hlist")
76 || strstr(fname, "win32.hlist");
78 while (fgets(protostr, sizeof(protostr), fhdr))
81 if (strncmp(protostr, "//#include ", 11) == 0) {
82 finc_name = protostr + 11;
83 p = strpbrk(finc_name, "\r\n ");
88 p = strrchr(hdrfn_saved, '/');
90 memcpy(path, hdrfn_saved,
92 path[p - hdrfn_saved + 1] = 0;
94 snprintf(fname_inc, sizeof(fname_inc), "%s%s",
96 finc = fopen(fname_inc, "r");
98 printf("%s:%d: can't open '%s'\n",
99 fname_inc, line, finc_name);
102 ret = do_protostrs(finc, finc_name);
108 if (strncmp(sskip(protostr), "//", 2) == 0)
111 p = protostr + strlen(protostr);
112 for (p--; p >= protostr && my_isblank(*p); --p)
119 ret = b_pp_c_handler(protostr, hdrfn, is_oslib);
132 static int get_regparm(char *dst, size_t dlen, char *p, int *retreg)
147 for (o = 0; o < dlen; i++) {
159 static const char *known_type_mod[] = {
169 static const char *known_ptr_types[] = {
184 "HIMC", // DWORD in mingw, ptr in wine..
203 "PMEMORY_BASIC_INFORMATION",
215 static const char *ignored_keywords[] = {
223 // returns ptr to char after type ends
224 static int typecmp(const char *n, const char *t)
226 for (; *t != 0; n++, t++) {
227 while (n[0] == ' ' && (n[1] == ' ' || n[1] == '*'))
229 while (t[0] == ' ' && (t[1] == ' ' || t[1] == '*'))
238 static const char *skip_type_mod(const char *n)
243 for (i = 0; i < ARRAY_SIZE(known_type_mod); i++) {
244 len = strlen(known_type_mod[i]);
245 if (strncmp(n, known_type_mod[i], len) != 0)
247 if (!my_isblank(n[len]))
251 while (my_isblank(*n))
259 static int check_type(const char *name, struct parsed_type *type)
265 n = skip_type_mod(name);
267 for (i = 0; i < ARRAY_SIZE(known_ptr_types); i++) {
268 if (typecmp(n, known_ptr_types[i]))
275 if (n[0] == 'L' && n[1] == 'P' && strncmp(n, "LPARAM", 6))
278 // assume single word
279 while (!my_isblank(*n) && !my_issep(*n))
284 while (my_isblank(*n))
295 type->name = strndup(name, ret);
296 if (IS(type->name, "__VALIST") || IS(type->name, "va_list"))
297 type->is_va_list = 1;
298 if (IS(type->name, "VOID"))
299 memcpy(type->name, "void", 4);
304 /* args are always expanded to 32bit */
305 static const char *map_reg(const char *reg)
307 const char *regs_f[] = { "eax", "ebx", "ecx", "edx", "esi", "edi" };
308 const char *regs_w[] = { "ax", "bx", "cx", "dx", "si", "di" };
309 const char *regs_b[] = { "al", "bl", "cl", "dl" };
312 for (i = 0; i < ARRAY_SIZE(regs_w); i++)
313 if (IS(reg, regs_w[i]))
316 for (i = 0; i < ARRAY_SIZE(regs_b); i++)
317 if (IS(reg, regs_b[i]))
323 static int check_struct_arg(struct parsed_proto_arg *arg)
325 if (IS(arg->type.name, "POINT"))
331 static int parse_protostr(char *protostr, struct parsed_proto *pp)
333 struct parsed_proto_arg *arg;
344 if (p[0] == '/' && p[1] == '/') {
345 printf("%s:%d: commented out?\n", hdrfn, hdrfline);
349 // strip unneeded stuff
350 for (p1 = p; p1[0] != 0 && p1[1] != 0; p1++) {
351 if ((p1[0] == '/' && p1[1] == '*')
352 || (p1[0] == '*' && p1[1] == '/'))
356 if (!strncmp(p, "DECLSPEC_NORETURN ", 18)) {
361 for (i = 0; i < ARRAY_SIZE(ignored_keywords); i++) {
362 l = strlen(ignored_keywords[i]);
363 if (!strncmp(p, ignored_keywords[i], l) && my_isblank(p[l]))
364 p = sskip(p + l + 1);
367 ret = check_type(p, &pp->ret_type);
369 printf("%s:%d:%zd: unhandled return in '%s'\n",
370 hdrfn, hdrfline, (p - protostr) + 1, protostr);
375 if (!strncmp(p, "noreturn ", 9)) {
380 if (!strchr(p, ')')) {
381 p = next_idt(buf, sizeof(buf), p);
384 printf("%s:%d:%zd: var name missing\n",
385 hdrfn, hdrfline, (p - protostr) + 1);
388 strcpy(pp->name, buf);
393 pp->ret_type.is_array = 1;
405 p = next_word(cconv, sizeof(cconv), p);
408 printf("%s:%d:%zd: cconv missing\n",
409 hdrfn, hdrfline, (p - protostr) + 1);
412 if (IS(cconv, "__cdecl"))
414 else if (IS(cconv, "__stdcall"))
416 else if (IS(cconv, "__fastcall")) {
418 pp->is_stdcall = 1; // sort of..
420 else if (IS(cconv, "__thiscall"))
422 else if (IS(cconv, "__userpurge"))
423 pp->is_stdcall = 1; // IDA
424 else if (IS(cconv, "__usercall"))
425 pp->is_stdcall = 0; // IDA
426 else if (IS(cconv, "__userstack")) {
427 pp->is_stdcall = 0; // custom
428 pp->is_userstack = 1;
430 else if (IS(cconv, "WINAPI"))
433 printf("%s:%d:%zd: unhandled cconv: '%s'\n",
434 hdrfn, hdrfline, (p - protostr) + 1, cconv);
440 printf("%s:%d:%zd: '*' expected\n",
441 hdrfn, hdrfline, (p - protostr) + 1);
445 // XXX: skipping extra asterisks, for now
451 p = next_idt(buf, sizeof(buf), p);
454 //printf("%s:%d:%zd: func name missing\n",
455 // hdrfn, hdrfline, (p - protostr) + 1);
458 strcpy(pp->name, buf);
460 ret = get_regparm(regparm, sizeof(regparm), p, &is_retreg);
462 if (!IS(regparm, "eax") && !IS(regparm, "ax")
463 && !IS(regparm, "al") && !IS(regparm, "edx:eax"))
465 printf("%s:%d:%zd: bad regparm: %s\n",
466 hdrfn, hdrfline, (p - protostr) + 1, regparm);
475 // not really ret_type is array, but ohwell
476 pp->ret_type.is_array = 1;
477 p = strchr(p + 1, ']');
479 printf("%s:%d:%zd: ']' expected\n",
480 hdrfn, hdrfline, (p - protostr) + 1);
486 printf("%s:%d:%zd: ')' expected\n",
487 hdrfn, hdrfline, (p - protostr) + 1);
494 printf("%s:%d:%zd: '(' expected, got '%c'\n",
495 hdrfn, hdrfline, (p - protostr) + 1, *p);
502 if ((!strncmp(p, "void", 4) || !strncmp(p, "VOID", 4))
503 && *sskip(p + 4) == ')')
514 printf("%s:%d:%zd: ',' expected\n",
515 hdrfn, hdrfline, (p - protostr) + 1);
521 if (!strncmp(p, "...", 3)) {
528 printf("%s:%d:%zd: ')' expected\n",
529 hdrfn, hdrfline, (p - protostr) + 1);
533 arg = &pp->arg[xarg];
537 ret = check_type(p, &arg->type);
539 printf("%s:%d:%zd: unhandled type for arg%d\n",
540 hdrfn, hdrfline, (p - protostr) + 1, xarg);
547 arg->fptr = calloc(1, sizeof(*arg->fptr));
548 ret = parse_protostr(p1, arg->fptr);
550 printf("%s:%d:%zd: funcarg parse failed\n",
551 hdrfn, hdrfline, p1 - protostr);
554 arg->fptr->is_arg = 1;
555 // we don't use actual names right now..
556 snprintf(arg->fptr->name,
557 sizeof(arg->fptr->name), "a%d", xarg);
558 // we'll treat it as void * for non-calls
559 arg->type.name = strdup("void *");
560 arg->type.is_ptr = 1;
565 p = next_idt(buf, sizeof(buf), p);
569 printf("%s:%d:%zd: idt missing for arg%d\n",
570 hdrfn, hdrfline, (p - protostr) + 1, xarg);
576 ret = get_regparm(regparm, sizeof(regparm), p, &is_retreg);
581 arg->reg = strdup(map_reg(regparm));
582 arg->type.is_retreg = is_retreg;
583 pp->has_retreg |= is_retreg;
586 if (strstr(arg->type.name, "int64")
587 || IS(arg->type.name, "double"))
590 free(arg->type.name);
591 arg->type.name = strdup("int");
592 pp_copy_arg(&pp->arg[xarg], arg);
596 ret = check_struct_arg(arg);
598 pp->has_structarg = 1;
599 arg->type.is_struct = 1;
600 free(arg->type.name);
601 arg->type.name = strdup("int");
602 for (l = 0; l < ret; l++) {
603 pp_copy_arg(&pp->arg[xarg], arg);
609 if (xarg > 0 && (IS(cconv, "__fastcall") || IS(cconv, "__thiscall"))) {
610 if (pp->arg[0].reg != NULL) {
611 printf("%s:%d: %s with arg1 spec %s?\n",
612 hdrfn, hdrfline, cconv, pp->arg[0].reg);
614 pp->arg[0].reg = strdup("ecx");
617 if (xarg > 1 && IS(cconv, "__fastcall")) {
618 if (pp->arg[1].reg != NULL) {
619 printf("%s:%d: %s with arg2 spec %s?\n",
620 hdrfn, hdrfline, cconv, pp->arg[1].reg);
622 pp->arg[1].reg = strdup("edx");
627 for (i = 0; i < pp->argc; i++) {
628 if (pp->arg[i].reg == NULL)
634 if (pp->argc == 1 && pp->arg[0].reg != NULL
635 && IS(pp->arg[0].reg, "ecx"))
639 else if (pp->argc_reg == 2
640 && pp->arg[0].reg != NULL && IS(pp->arg[0].reg, "ecx")
641 && pp->arg[1].reg != NULL && IS(pp->arg[1].reg, "edx"))
646 if (pp->is_vararg && (pp->is_stdcall || pp->is_fastcall)) {
647 printf("%s:%d: vararg %s?\n", hdrfn, hdrfline, cconv);
654 static int pp_name_cmp(const void *p1, const void *p2)
656 const struct parsed_proto *pp1 = p1, *pp2 = p2;
657 return strcmp(pp1->name, pp2->name);
660 static struct parsed_proto *pp_cache;
661 static int pp_cache_size;
662 static int pp_cache_alloc;
664 static int b_pp_c_handler(char *proto, const char *fname, int is_oslib)
668 if (pp_cache_size >= pp_cache_alloc) {
669 pp_cache_alloc = pp_cache_alloc * 2 + 64;
670 pp_cache = realloc(pp_cache, pp_cache_alloc
671 * sizeof(pp_cache[0]));
672 my_assert_not(pp_cache, NULL);
673 memset(pp_cache + pp_cache_size, 0,
674 (pp_cache_alloc - pp_cache_size)
675 * sizeof(pp_cache[0]));
678 ret = parse_protostr(proto, &pp_cache[pp_cache_size]);
682 pp_cache[pp_cache_size].is_oslib = is_oslib;
687 static void build_pp_cache(FILE *fhdr)
695 ret = do_protostrs(fhdr, hdrfn);
699 qsort(pp_cache, pp_cache_size, sizeof(pp_cache[0]), pp_name_cmp);
700 fseek(fhdr, pos, SEEK_SET);
703 static const struct parsed_proto *proto_parse(FILE *fhdr, const char *sym,
706 const struct parsed_proto *pp_ret;
707 struct parsed_proto pp_search;
710 if (pp_cache == NULL)
711 build_pp_cache(fhdr);
713 if (sym[0] == '_') // && strncmp(fname, "stdc", 4) == 0)
716 strcpy(pp_search.name, sym);
717 p = strchr(pp_search.name, '@');
721 pp_ret = bsearch(&pp_search, pp_cache, pp_cache_size,
722 sizeof(pp_cache[0]), pp_name_cmp);
723 if (pp_ret == NULL && !quiet)
724 printf("%s: sym '%s' is missing\n", hdrfn, sym);
729 static void pp_copy_arg(struct parsed_proto_arg *d,
730 const struct parsed_proto_arg *s)
732 memcpy(d, s, sizeof(*d));
734 if (s->reg != NULL) {
735 d->reg = strdup(s->reg);
736 my_assert_not(d->reg, NULL);
738 if (s->type.name != NULL) {
739 d->type.name = strdup(s->type.name);
740 my_assert_not(d->type.name, NULL);
742 if (s->fptr != NULL) {
743 d->fptr = malloc(sizeof(*d->fptr));
744 my_assert_not(d->fptr, NULL);
745 memcpy(d->fptr, s->fptr, sizeof(*d->fptr));
749 struct parsed_proto *proto_clone(const struct parsed_proto *pp_c)
751 struct parsed_proto *pp;
754 pp = malloc(sizeof(*pp));
755 my_assert_not(pp, NULL);
756 memcpy(pp, pp_c, sizeof(*pp)); // lazy..
758 // do the actual deep copy..
759 for (i = 0; i < pp_c->argc; i++)
760 pp_copy_arg(&pp->arg[i], &pp_c->arg[i]);
761 if (pp_c->ret_type.name != NULL)
762 pp->ret_type.name = strdup(pp_c->ret_type.name);
768 static inline int pp_cmp_func(const struct parsed_proto *pp1,
769 const struct parsed_proto *pp2)
773 if (pp1->argc != pp2->argc || pp1->argc_reg != pp2->argc_reg)
776 for (i = 0; i < pp1->argc; i++) {
777 if ((pp1->arg[i].reg != NULL) != (pp2->arg[i].reg != NULL))
780 if ((pp1->arg[i].reg != NULL)
781 && !IS(pp1->arg[i].reg, pp2->arg[i].reg))
791 static inline void pp_print(char *buf, size_t buf_size,
792 const struct parsed_proto *pp)
797 snprintf(buf, buf_size, "%s %s(", pp->ret_type.name, pp->name);
800 for (i = 0; i < pp->argc_reg; i++) {
801 snprintf(buf + l, buf_size - l, "%s%s",
802 i == 0 ? "" : ", ", pp->arg[i].reg);
805 if (pp->argc_stack > 0) {
806 snprintf(buf + l, buf_size - l, "%s{%d stack}",
807 i == 0 ? "" : ", ", pp->argc_stack);
810 snprintf(buf + l, buf_size - l, ")");
813 static inline void proto_release(struct parsed_proto *pp)
817 for (i = 0; i < pp->argc; i++) {
818 if (pp->arg[i].reg != NULL)
819 free(pp->arg[i].reg);
820 if (pp->arg[i].type.name != NULL)
821 free(pp->arg[i].type.name);
822 if (pp->arg[i].fptr != NULL)
823 free(pp->arg[i].fptr);
825 if (pp->ret_type.name != NULL)
826 free(pp->ret_type.name);