14 static const char *hdrfn;
15 static int hdrfline = 0;
17 static int find_protostr(char *dst, size_t dlen, FILE *fhdr, const char *sym)
24 while (fgets(dst, dlen, fhdr))
27 if (strstr(dst, sym) != NULL)
35 p = dst + strlen(dst);
36 for (p--; p > dst && my_isblank(*p); --p)
42 static int get_regparm(char *dst, size_t dlen, char *p)
49 for (o = 0, i = 1; o < dlen; i++) {
61 static const char *known_types[] = {
86 static const char *check_type(const char *name)
90 for (i = 0; i < ARRAY_SIZE(known_types); i++) {
91 l = strlen(known_types[i]);
92 if (strncmp(known_types[i], name, l) == 0)
93 return known_types[i];
99 /* args are always expanded to 32bit */
100 static const char *map_reg(const char *reg)
102 const char *regs_f[] = { "eax", "ebx", "ecx", "edx", "esi", "edi" };
103 const char *regs_w[] = { "ax", "bx", "cx", "dx", "si", "di" };
104 const char *regs_b[] = { "al", "bl", "cl", "dl" };
107 for (i = 0; i < ARRAY_SIZE(regs_w); i++)
108 if (IS(reg, regs_w[i]))
111 for (i = 0; i < ARRAY_SIZE(regs_b); i++)
112 if (IS(reg, regs_b[i]))
118 static int parse_protostr(char *protostr, struct parsed_proto *pp)
130 if (p[0] == '/' && p[1] == '/') {
131 //printf("warning: decl for sym '%s' is commented out\n", sym);
137 printf("%s:%d:%ld: unhandled return in '%s'\n",
138 hdrfn, hdrfline, (p - protostr) + 1, protostr);
145 p = next_word(cconv, sizeof(cconv), p);
148 printf("%s:%d:%ld: cconv missing\n",
149 hdrfn, hdrfline, (p - protostr) + 1);
152 if (IS(cconv, "__cdecl"))
154 else if (IS(cconv, "__stdcall"))
156 else if (IS(cconv, "__fastcall"))
158 else if (IS(cconv, "__thiscall"))
160 else if (IS(cconv, "__userpurge"))
161 pp->is_stdcall = 1; // in all cases seen..
162 else if (IS(cconv, "__usercall"))
163 pp->is_stdcall = 0; // ..or is it?
165 printf("%s:%d:%ld: unhandled cconv: '%s'\n",
166 hdrfn, hdrfline, (p - protostr) + 1, cconv);
170 p = next_idt(buf, sizeof(buf), p);
173 printf("%s:%d:%ld: func name missing\n",
174 hdrfn, hdrfline, (p - protostr) + 1);
178 ret = get_regparm(regparm, sizeof(regparm), p);
180 if (!IS(regparm, "eax") && !IS(regparm, "ax")
181 && !IS(regparm, "al"))
183 printf("%s:%d:%ld: bad regparm: %s\n",
184 hdrfn, hdrfline, (p - protostr) + 1, regparm);
192 printf("%s:%d:%ld: '(' expected, got '%c'\n",
193 hdrfn, hdrfline, (p - protostr) + 1, *p);
209 printf("%s:%d:%ld: unhandled type for arg%d\n",
210 hdrfn, hdrfline, (p - protostr) + 1, xarg);
213 pp->arg[xarg - 1].type = kt;
217 p = next_idt(buf, sizeof(buf), p);
221 printf("%s:%d:%ld: idt missing for arg%d\n",
222 hdrfn, hdrfline, (p - protostr) + 1, xarg);
226 pp->arg[xarg - 1].reg = NULL;
228 ret = get_regparm(regparm, sizeof(regparm), p);
233 pp->arg[xarg - 1].reg = strdup(map_reg(regparm));
237 if (xarg > 0 && (IS(cconv, "__fastcall") || IS(cconv, "__thiscall"))) {
238 if (pp->arg[0].reg != NULL) {
239 printf("%s:%d: %s with arg1 spec %s?\n",
240 hdrfn, hdrfline, cconv, pp->arg[0].reg);
242 pp->arg[0].reg = strdup("ecx");
245 if (xarg > 1 && IS(cconv, "__fastcall")) {
246 if (pp->arg[1].reg != NULL) {
247 printf("%s:%d: %s with arg2 spec %s?\n",
248 hdrfn, hdrfline, cconv, pp->arg[1].reg);
250 pp->arg[1].reg = strdup("edx");
255 for (i = 0; i < pp->argc; i++) {
256 if (pp->arg[i].reg == NULL)
265 static int proto_parse(FILE *fhdr, const char *sym, struct parsed_proto *pp)
270 memset(pp, 0, sizeof(*pp));
272 ret = find_protostr(protostr, sizeof(protostr), fhdr, sym);
274 printf("%s: sym '%s' is missing\n", hdrfn, sym);
278 return parse_protostr(protostr, pp);
281 static void proto_release(struct parsed_proto *pp)
285 for (i = 0; i < pp->argc; i++) {
286 if (pp->arg[i].reg == NULL)
287 free(pp->arg[i].reg);