6 unsigned int is_array:1;
8 unsigned int is_struct:1; // split for args
9 unsigned int is_retreg:1; // register to return
12 struct parsed_proto_arg {
14 struct parsed_type type;
15 struct parsed_proto *fptr;
22 struct parsed_type ret_type;
23 struct parsed_type type;
25 struct parsed_proto_arg arg[16];
29 unsigned int is_func:1;
30 unsigned int is_stdcall:1;
31 unsigned int is_fastcall:1;
32 unsigned int is_vararg:1; // vararg func
33 unsigned int is_fptr:1;
34 unsigned int is_noreturn:1;
35 unsigned int is_unresolved:1;
36 unsigned int is_userstack:1;
37 unsigned int is_arg:1; // decl in func arg
38 unsigned int has_structarg:1;
39 unsigned int has_retreg:1;
42 static const char *hdrfn;
43 static int hdrfline = 0;
45 static void pp_copy_arg(struct parsed_proto_arg *d,
46 const struct parsed_proto_arg *s);
48 static int b_pp_c_handler(char *proto, const char *fname);
50 static int do_protostrs(FILE *fhdr, const char *fname)
52 const char *finc_name;
53 const char *hdrfn_saved;
65 while (fgets(protostr, sizeof(protostr), fhdr))
68 if (strncmp(protostr, "//#include ", 11) == 0) {
69 finc_name = protostr + 11;
70 p = strpbrk(finc_name, "\r\n ");
75 p = strrchr(hdrfn_saved, '/');
77 memcpy(path, hdrfn_saved,
79 path[p - hdrfn_saved + 1] = 0;
81 snprintf(fname_inc, sizeof(fname_inc), "%s%s",
83 finc = fopen(fname_inc, "r");
85 printf("%s:%d: can't open '%s'\n",
86 fname_inc, line, finc_name);
89 ret = do_protostrs(finc, finc_name);
95 if (strncmp(sskip(protostr), "//", 2) == 0)
98 p = protostr + strlen(protostr);
99 for (p--; p >= protostr && my_isblank(*p); --p)
106 ret = b_pp_c_handler(protostr, hdrfn);
119 static int get_regparm(char *dst, size_t dlen, char *p, int *retreg)
134 for (o = 0; o < dlen; i++) {
146 static const char *known_type_mod[] = {
156 static const char *known_ptr_types[] = {
171 "HIMC", // DWORD in mingw, ptr in wine..
190 "PMEMORY_BASIC_INFORMATION",
202 static const char *ignored_keywords[] = {
210 // returns ptr to char after type ends
211 static int typecmp(const char *n, const char *t)
213 for (; *t != 0; n++, t++) {
214 while (n[0] == ' ' && (n[1] == ' ' || n[1] == '*'))
216 while (t[0] == ' ' && (t[1] == ' ' || t[1] == '*'))
225 static const char *skip_type_mod(const char *n)
230 for (i = 0; i < ARRAY_SIZE(known_type_mod); i++) {
231 len = strlen(known_type_mod[i]);
232 if (strncmp(n, known_type_mod[i], len) != 0)
234 if (!my_isblank(n[len]))
238 while (my_isblank(*n))
246 static int check_type(const char *name, struct parsed_type *type)
252 n = skip_type_mod(name);
254 for (i = 0; i < ARRAY_SIZE(known_ptr_types); i++) {
255 if (typecmp(n, known_ptr_types[i]))
262 if (n[0] == 'L' && n[1] == 'P' && strncmp(n, "LPARAM", 6))
265 // assume single word
266 while (!my_isblank(*n) && !my_issep(*n))
271 while (my_isblank(*n))
282 type->name = strndup(name, ret);
283 if (IS(type->name, "VOID"))
284 memcpy(type->name, "void", 4);
289 /* args are always expanded to 32bit */
290 static const char *map_reg(const char *reg)
292 const char *regs_f[] = { "eax", "ebx", "ecx", "edx", "esi", "edi" };
293 const char *regs_w[] = { "ax", "bx", "cx", "dx", "si", "di" };
294 const char *regs_b[] = { "al", "bl", "cl", "dl" };
297 for (i = 0; i < ARRAY_SIZE(regs_w); i++)
298 if (IS(reg, regs_w[i]))
301 for (i = 0; i < ARRAY_SIZE(regs_b); i++)
302 if (IS(reg, regs_b[i]))
308 static int check_struct_arg(struct parsed_proto_arg *arg)
310 if (IS(arg->type.name, "POINT"))
316 static int parse_protostr(char *protostr, struct parsed_proto *pp)
318 struct parsed_proto_arg *arg;
329 if (p[0] == '/' && p[1] == '/') {
330 printf("%s:%d: commented out?\n", hdrfn, hdrfline);
334 // strip unneeded stuff
335 for (p1 = p; p1[0] != 0 && p1[1] != 0; p1++) {
336 if ((p1[0] == '/' && p1[1] == '*')
337 || (p1[0] == '*' && p1[1] == '/'))
341 if (!strncmp(p, "DECLSPEC_NORETURN ", 18)) {
346 for (i = 0; i < ARRAY_SIZE(ignored_keywords); i++) {
347 l = strlen(ignored_keywords[i]);
348 if (!strncmp(p, ignored_keywords[i], l) && my_isblank(p[l]))
349 p = sskip(p + l + 1);
352 ret = check_type(p, &pp->ret_type);
354 printf("%s:%d:%zd: unhandled return in '%s'\n",
355 hdrfn, hdrfline, (p - protostr) + 1, protostr);
360 if (!strncmp(p, "noreturn ", 9)) {
365 if (!strchr(p, ')')) {
366 p = next_idt(buf, sizeof(buf), p);
369 printf("%s:%d:%zd: var name missing\n",
370 hdrfn, hdrfline, (p - protostr) + 1);
373 strcpy(pp->name, buf);
378 pp->ret_type.is_array = 1;
390 p = next_word(cconv, sizeof(cconv), p);
393 printf("%s:%d:%zd: cconv missing\n",
394 hdrfn, hdrfline, (p - protostr) + 1);
397 if (IS(cconv, "__cdecl"))
399 else if (IS(cconv, "__stdcall"))
401 else if (IS(cconv, "__fastcall")) {
403 pp->is_stdcall = 1; // sort of..
405 else if (IS(cconv, "__thiscall"))
407 else if (IS(cconv, "__userpurge"))
408 pp->is_stdcall = 1; // IDA
409 else if (IS(cconv, "__usercall"))
410 pp->is_stdcall = 0; // IDA
411 else if (IS(cconv, "__userstack")) {
412 pp->is_stdcall = 0; // custom
413 pp->is_userstack = 1;
415 else if (IS(cconv, "WINAPI"))
418 printf("%s:%d:%zd: unhandled cconv: '%s'\n",
419 hdrfn, hdrfline, (p - protostr) + 1, cconv);
425 printf("%s:%d:%zd: '*' expected\n",
426 hdrfn, hdrfline, (p - protostr) + 1);
430 // XXX: skipping extra asterisks, for now
436 p = next_idt(buf, sizeof(buf), p);
439 //printf("%s:%d:%zd: func name missing\n",
440 // hdrfn, hdrfline, (p - protostr) + 1);
443 strcpy(pp->name, buf);
445 ret = get_regparm(regparm, sizeof(regparm), p, &is_retreg);
447 if (!IS(regparm, "eax") && !IS(regparm, "ax")
448 && !IS(regparm, "al") && !IS(regparm, "edx:eax"))
450 printf("%s:%d:%zd: bad regparm: %s\n",
451 hdrfn, hdrfline, (p - protostr) + 1, regparm);
460 // not really ret_type is array, but ohwell
461 pp->ret_type.is_array = 1;
462 p = strchr(p + 1, ']');
464 printf("%s:%d:%zd: ']' expected\n",
465 hdrfn, hdrfline, (p - protostr) + 1);
471 printf("%s:%d:%zd: ')' expected\n",
472 hdrfn, hdrfline, (p - protostr) + 1);
479 printf("%s:%d:%zd: '(' expected, got '%c'\n",
480 hdrfn, hdrfline, (p - protostr) + 1, *p);
487 if ((!strncmp(p, "void", 4) || !strncmp(p, "VOID", 4))
488 && *sskip(p + 4) == ')')
499 printf("%s:%d:%zd: ',' expected\n",
500 hdrfn, hdrfline, (p - protostr) + 1);
506 if (!strncmp(p, "...", 3)) {
513 printf("%s:%d:%zd: ')' expected\n",
514 hdrfn, hdrfline, (p - protostr) + 1);
518 arg = &pp->arg[xarg];
522 ret = check_type(p, &arg->type);
524 printf("%s:%d:%zd: unhandled type for arg%d\n",
525 hdrfn, hdrfline, (p - protostr) + 1, xarg);
532 arg->fptr = calloc(1, sizeof(*arg->fptr));
533 ret = parse_protostr(p1, arg->fptr);
535 printf("%s:%d:%zd: funcarg parse failed\n",
536 hdrfn, hdrfline, p1 - protostr);
539 arg->fptr->is_arg = 1;
540 // we don't use actual names right now..
541 snprintf(arg->fptr->name,
542 sizeof(arg->fptr->name), "a%d", xarg);
543 // we'll treat it as void * for non-calls
544 arg->type.name = strdup("void *");
545 arg->type.is_ptr = 1;
550 p = next_idt(buf, sizeof(buf), p);
554 printf("%s:%d:%zd: idt missing for arg%d\n",
555 hdrfn, hdrfline, (p - protostr) + 1, xarg);
561 ret = get_regparm(regparm, sizeof(regparm), p, &is_retreg);
566 arg->reg = strdup(map_reg(regparm));
567 arg->type.is_retreg = is_retreg;
568 pp->has_retreg |= is_retreg;
571 if (strstr(arg->type.name, "int64")
572 || IS(arg->type.name, "double"))
575 free(arg->type.name);
576 arg->type.name = strdup("int");
577 pp_copy_arg(&pp->arg[xarg], arg);
581 ret = check_struct_arg(arg);
583 pp->has_structarg = 1;
584 arg->type.is_struct = 1;
585 free(arg->type.name);
586 arg->type.name = strdup("int");
587 for (l = 0; l < ret; l++) {
588 pp_copy_arg(&pp->arg[xarg], arg);
594 if (xarg > 0 && (IS(cconv, "__fastcall") || IS(cconv, "__thiscall"))) {
595 if (pp->arg[0].reg != NULL) {
596 printf("%s:%d: %s with arg1 spec %s?\n",
597 hdrfn, hdrfline, cconv, pp->arg[0].reg);
599 pp->arg[0].reg = strdup("ecx");
602 if (xarg > 1 && IS(cconv, "__fastcall")) {
603 if (pp->arg[1].reg != NULL) {
604 printf("%s:%d: %s with arg2 spec %s?\n",
605 hdrfn, hdrfline, cconv, pp->arg[1].reg);
607 pp->arg[1].reg = strdup("edx");
612 for (i = 0; i < pp->argc; i++) {
613 if (pp->arg[i].reg == NULL)
619 if (pp->argc == 1 && pp->arg[0].reg != NULL
620 && IS(pp->arg[0].reg, "ecx"))
624 else if (pp->argc_reg == 2
625 && pp->arg[0].reg != NULL && IS(pp->arg[0].reg, "ecx")
626 && pp->arg[1].reg != NULL && IS(pp->arg[1].reg, "edx"))
631 if (pp->is_vararg && (pp->is_stdcall || pp->is_fastcall)) {
632 printf("%s:%d: vararg %s?\n", hdrfn, hdrfline, cconv);
639 static int pp_name_cmp(const void *p1, const void *p2)
641 const struct parsed_proto *pp1 = p1, *pp2 = p2;
642 return strcmp(pp1->name, pp2->name);
645 static struct parsed_proto *pp_cache;
646 static int pp_cache_size;
647 static int pp_cache_alloc;
649 static int b_pp_c_handler(char *proto, const char *fname)
653 if (pp_cache_size >= pp_cache_alloc) {
654 pp_cache_alloc = pp_cache_alloc * 2 + 64;
655 pp_cache = realloc(pp_cache, pp_cache_alloc
656 * sizeof(pp_cache[0]));
657 my_assert_not(pp_cache, NULL);
658 memset(pp_cache + pp_cache_size, 0,
659 (pp_cache_alloc - pp_cache_size)
660 * sizeof(pp_cache[0]));
663 ret = parse_protostr(proto, &pp_cache[pp_cache_size]);
671 static void build_pp_cache(FILE *fhdr)
679 ret = do_protostrs(fhdr, hdrfn);
683 qsort(pp_cache, pp_cache_size, sizeof(pp_cache[0]), pp_name_cmp);
684 fseek(fhdr, pos, SEEK_SET);
687 static const struct parsed_proto *proto_parse(FILE *fhdr, const char *sym,
690 const struct parsed_proto *pp_ret;
691 struct parsed_proto pp_search;
694 if (pp_cache == NULL)
695 build_pp_cache(fhdr);
697 if (sym[0] == '_') // && strncmp(fname, "stdc", 4) == 0)
700 strcpy(pp_search.name, sym);
701 p = strchr(pp_search.name, '@');
705 pp_ret = bsearch(&pp_search, pp_cache, pp_cache_size,
706 sizeof(pp_cache[0]), pp_name_cmp);
707 if (pp_ret == NULL && !quiet)
708 printf("%s: sym '%s' is missing\n", hdrfn, sym);
713 static void pp_copy_arg(struct parsed_proto_arg *d,
714 const struct parsed_proto_arg *s)
716 memcpy(d, s, sizeof(*d));
718 if (s->reg != NULL) {
719 d->reg = strdup(s->reg);
720 my_assert_not(d->reg, NULL);
722 if (s->type.name != NULL) {
723 d->type.name = strdup(s->type.name);
724 my_assert_not(d->type.name, NULL);
726 if (s->fptr != NULL) {
727 d->fptr = malloc(sizeof(*d->fptr));
728 my_assert_not(d->fptr, NULL);
729 memcpy(d->fptr, s->fptr, sizeof(*d->fptr));
733 struct parsed_proto *proto_clone(const struct parsed_proto *pp_c)
735 struct parsed_proto *pp;
738 pp = malloc(sizeof(*pp));
739 my_assert_not(pp, NULL);
740 memcpy(pp, pp_c, sizeof(*pp)); // lazy..
742 // do the actual deep copy..
743 for (i = 0; i < pp_c->argc; i++)
744 pp_copy_arg(&pp->arg[i], &pp_c->arg[i]);
745 if (pp_c->ret_type.name != NULL)
746 pp->ret_type.name = strdup(pp_c->ret_type.name);
752 static inline int pp_cmp_func(const struct parsed_proto *pp1,
753 const struct parsed_proto *pp2)
757 if (pp1->argc != pp2->argc || pp1->argc_reg != pp2->argc_reg)
760 for (i = 0; i < pp1->argc; i++) {
761 if ((pp1->arg[i].reg != NULL) != (pp2->arg[i].reg != NULL))
764 if ((pp1->arg[i].reg != NULL)
765 && !IS(pp1->arg[i].reg, pp2->arg[i].reg))
775 static inline void pp_print(char *buf, size_t buf_size,
776 const struct parsed_proto *pp)
781 snprintf(buf, buf_size, "%s %s(", pp->ret_type.name, pp->name);
784 for (i = 0; i < pp->argc_reg; i++) {
785 snprintf(buf + l, buf_size - l, "%s%s",
786 i == 0 ? "" : ", ", pp->arg[i].reg);
789 if (pp->argc_stack > 0) {
790 snprintf(buf + l, buf_size - l, "%s{%d stack}",
791 i == 0 ? "" : ", ", pp->argc_stack);
794 snprintf(buf + l, buf_size - l, ")");
797 static inline void proto_release(struct parsed_proto *pp)
801 for (i = 0; i < pp->argc; i++) {
802 if (pp->arg[i].reg != NULL)
803 free(pp->arg[i].reg);
804 if (pp->arg[i].type.name != NULL)
805 free(pp->arg[i].type.name);
806 if (pp->arg[i].fptr != NULL)
807 free(pp->arg[i].fptr);
809 if (pp->ret_type.name != NULL)
810 free(pp->ret_type.name);