unsigned int is_array:1;
unsigned int is_ptr:1;
unsigned int is_struct:1; // split for args
- unsigned int is_retreg:1; // register to return
+ unsigned int is_retreg:1; // register to return to caller
+ unsigned int is_va_list:1;
};
struct parsed_proto_arg {
unsigned int is_noreturn:1;
unsigned int is_unresolved:1;
unsigned int is_userstack:1;
- unsigned int is_oslib:1; // OS/system library func
+ unsigned int is_include:1; // not from top-level header
+ unsigned int is_osinc:1; // OS/system library func
+ unsigned int is_cinc:1; // crt library func
unsigned int is_arg:1; // declared in some func arg
unsigned int has_structarg:1;
unsigned int has_retreg:1;
};
+struct parsed_struct {
+ char name[256];
+ struct {
+ int offset;
+ struct parsed_proto pp;
+ } members[64];
+ int member_count;
+};
+
static const char *hdrfn;
static int hdrfline = 0;
static void pp_copy_arg(struct parsed_proto_arg *d,
const struct parsed_proto_arg *s);
-static int b_pp_c_handler(char *proto, const char *fname, int is_oslib);
+static int b_pp_c_handler(char *proto, const char *fname,
+ int is_include, int is_osinc, int is_cinc);
+static int struct_handler(FILE *fhdr, char *proto, int *line);
-static int do_protostrs(FILE *fhdr, const char *fname)
+static int do_protostrs(FILE *fhdr, const char *fname, int is_include)
{
const char *finc_name;
const char *hdrfn_saved;
char protostr[256];
char path[256];
char fname_inc[256];
- int is_oslib;
+ int is_osinc;
+ int is_cinc;
FILE *finc;
int line = 0;
int ret;
hdrfn_saved = hdrfn;
hdrfn = fname;
- is_oslib = strstr(fname, "stdc.hlist")
- || strstr(fname, "win32.hlist");
+ is_cinc = strstr(fname, "stdc.hlist") != NULL;
+ is_osinc = is_cinc || strstr(fname, "win32.hlist") != NULL;
while (fgets(protostr, sizeof(protostr), fhdr))
{
fname_inc, line, finc_name);
continue;
}
- ret = do_protostrs(finc, finc_name);
+ ret = do_protostrs(finc, finc_name, 1);
fclose(finc);
if (ret < 0)
break;
hdrfline = line;
- ret = b_pp_c_handler(protostr, hdrfn, is_oslib);
+ if (!strncmp(protostr, "struct", 6)
+ && strchr(protostr, '{') != NULL)
+ ret = struct_handler(fhdr, protostr, &line);
+ else
+ ret = b_pp_c_handler(protostr, hdrfn,
+ is_include, is_osinc, is_cinc);
if (ret < 0)
break;
}
"const",
"signed",
"unsigned",
- "struct",
"enum",
"CONST",
"volatile",
"WINADVAPI",
};
-// returns ptr to char after type ends
static int typecmp(const char *n, const char *t)
{
for (; *t != 0; n++, t++) {
n = skip_type_mod(name);
+ if (!strncmp(n, "struct", 6) && my_isblank(n[6])) {
+ type->is_struct = 1;
+
+ n += 6;
+ while (my_isblank(*n))
+ n++;
+ }
+
for (i = 0; i < ARRAY_SIZE(known_ptr_types); i++) {
if (typecmp(n, known_ptr_types[i]))
continue;
ret = n1 - name;
type->name = strndup(name, ret);
+ if (IS(type->name, "__VALIST") || IS(type->name, "va_list"))
+ type->is_va_list = 1;
if (IS(type->name, "VOID"))
memcpy(type->name, "void", 4);
p = sskip(p + 2);
}
- // strip unneeded stuff
+ // allow start of line comment
+ if (p[0] == '/' && p[1] == '*') {
+ p = strstr(p + 2, "*/");
+ if (p == NULL) {
+ printf("%s:%d: multiline comments unsupported\n",
+ hdrfn, hdrfline);
+ return -1;
+ }
+ p = sskip(p + 2);
+ }
+
+ // we need remaining hints in comments, so strip / *
for (p1 = p; p1[0] != 0 && p1[1] != 0; p1++) {
if ((p1[0] == '/' && p1[1] == '*')
|| (p1[0] == '*' && p1[1] == '/'))
return strcmp(pp1->name, pp2->name);
}
+static int ps_name_cmp(const void *p1, const void *p2)
+{
+ const struct parsed_struct *ps1 = p1, *ps2 = p2;
+ return strcmp(ps1->name, ps2->name);
+}
+
+// parsed struct cache
+static struct parsed_struct *ps_cache;
+static int ps_cache_size;
+static int ps_cache_alloc;
+
+static int struct_handler(FILE *fhdr, char *proto, int *line)
+{
+ struct parsed_struct *ps;
+ char lstr[256], *p;
+ int offset = 0;
+ int m = 0;
+ int ret;
+
+ if (ps_cache_size >= ps_cache_alloc) {
+ ps_cache_alloc = ps_cache_alloc * 2 + 64;
+ ps_cache = realloc(ps_cache, ps_cache_alloc
+ * sizeof(ps_cache[0]));
+ my_assert_not(ps_cache, NULL);
+ memset(ps_cache + ps_cache_size, 0,
+ (ps_cache_alloc - ps_cache_size)
+ * sizeof(ps_cache[0]));
+ }
+
+ ps = &ps_cache[ps_cache_size++];
+ ret = sscanf(proto, "struct %255s {", ps->name);
+ if (ret != 1) {
+ printf("%s:%d: struct parse failed\n", hdrfn, *line);
+ return -1;
+ }
+
+ while (fgets(lstr, sizeof(lstr), fhdr))
+ {
+ (*line)++;
+
+ p = sskip(lstr);
+ if (p[0] == '/' && p[1] == '/')
+ continue;
+ if (p[0] == '}')
+ break;
+
+ if (m >= ARRAY_SIZE(ps->members)) {
+ printf("%s:%d: too many struct members\n",
+ hdrfn, *line);
+ return -1;
+ }
+
+ hdrfline = *line;
+ ret = parse_protostr(p, &ps->members[m].pp);
+ if (ret < 0) {
+ printf("%s:%d: struct member #%d/%02x "
+ "doesn't parse\n", hdrfn, *line,
+ m, offset);
+ return -1;
+ }
+ ps->members[m].offset = offset;
+ offset += 4;
+ m++;
+ }
+
+ ps->member_count = m;
+
+ return 0;
+}
+
+// parsed proto cache
static struct parsed_proto *pp_cache;
static int pp_cache_size;
static int pp_cache_alloc;
-static int b_pp_c_handler(char *proto, const char *fname, int is_oslib)
+static int b_pp_c_handler(char *proto, const char *fname,
+ int is_include, int is_osinc, int is_cinc)
{
int ret;
if (ret < 0)
return -1;
- pp_cache[pp_cache_size].is_oslib = is_oslib;
+ pp_cache[pp_cache_size].is_include = is_include;
+ pp_cache[pp_cache_size].is_osinc = is_osinc;
+ pp_cache[pp_cache_size].is_cinc = is_cinc;
pp_cache_size++;
return 0;
}
-static void build_pp_cache(FILE *fhdr)
+static void build_caches(FILE *fhdr)
{
long pos;
int ret;
pos = ftell(fhdr);
rewind(fhdr);
- ret = do_protostrs(fhdr, hdrfn);
+ ret = do_protostrs(fhdr, hdrfn, 0);
if (ret < 0)
exit(1);
qsort(pp_cache, pp_cache_size, sizeof(pp_cache[0]), pp_name_cmp);
+ qsort(ps_cache, ps_cache_size, sizeof(ps_cache[0]), ps_name_cmp);
fseek(fhdr, pos, SEEK_SET);
}
char *p;
if (pp_cache == NULL)
- build_pp_cache(fhdr);
+ build_caches(fhdr);
if (sym[0] == '_') // && strncmp(fname, "stdc", 4) == 0)
sym++;
return pp_ret;
}
+static const struct parsed_proto *proto_lookup_struct(FILE *fhdr,
+ const char *type, int offset)
+{
+ struct parsed_struct ps_search, *ps;
+ int m;
+
+ if (pp_cache == NULL)
+ build_caches(fhdr);
+ if (ps_cache_size == 0)
+ return NULL;
+
+ while (my_isblank(*type))
+ type++;
+ if (!strncmp(type, "struct", 6) && my_isblank(type[6]))
+ type += 7;
+
+ if (sscanf(type, "%255s", ps_search.name) != 1)
+ return NULL;
+
+ ps = bsearch(&ps_search, ps_cache, ps_cache_size,
+ sizeof(ps_cache[0]), ps_name_cmp);
+ if (ps == NULL) {
+ printf("%s: struct '%s' is missing\n",
+ hdrfn, ps_search.name);
+ return NULL;
+ }
+
+ for (m = 0; m < ps->member_count; m++) {
+ if (ps->members[m].offset == offset)
+ return &ps->members[m].pp;
+ }
+
+ return NULL;
+}
+
static void pp_copy_arg(struct parsed_proto_arg *d,
const struct parsed_proto_arg *s)
{
if (pp->ret_type.name != NULL)
free(pp->ret_type.name);
free(pp);
+
+ (void)proto_lookup_struct;
}