#include #include #include #include "type.h" const type_t builtin_types[] = { { .type = T_NONE }, #define SCALAR(_tid, _, _type) [_tid] = { \ .tid = _tid, \ .size = sizeof(_type), \ .type = T_SCALAR, \ .t = { \ .scalar = { \ .name = #_type, \ }, \ }, \ }, #define POINTER(_tid, _, _base) [_tid] = { \ .tid = _tid, \ .size = sizeof(void *), \ .type = T_POINTER, \ .t = { \ .pointer = { \ .type = _base, \ }, \ }, \ }, BUILTIN_TYPES #undef SCALAR #undef POINTER { .type = T_INVALID } }; const type_t arch_builtin_types[] = { { .type = T_ALIAS, .t = { .alias = { .type = tid_c, .name = "s8" } } }, { .type = T_ALIAS, .t = { .alias = { .type = tid_uc, .name = "u8" } } }, { .type = T_INVALID } }; static types_t types = { .types = NULL, .next = 0, }; type_t *type_info(tid_t tid) { assert(tid < types.next); return &types.types[tid]; } void type_dump(type_t *t, FILE *fp) { switch (t->type){ case T_NONE: fputs("", fp); break; case T_ALIAS: fputs(t->t.alias.name, fp); break; case T_SCALAR: fputs(t->t.scalar.name, fp); break; case T_POINTER: type_dump(type_info(t->t.pointer.type), fp); fputs(" *", fp); break; case T_ARRAY: type_dump(type_info(t->t.array.type), fp); fprintf(fp, "[%zu]", t->t.array.len); break; default: assert(0); } } void type_dump_cdecl(type_t *t, FILE *fp) { switch (t->type) { case T_ALIAS: fputs("typedef ", fp); type_dump(type_info(t->t.alias.type), fp); fprintf(fp, " %s", t->t.alias.name); break; case T_NONE: case T_SCALAR: case T_POINTER: case T_ARRAY: type_dump(t, fp); break; default: assert(0); } } tid_t type_add(const type_t *type) { tid_t tid = types.next++; types.types = realloc(types.types, types.next * sizeof(type_t)); assert(types.types); types.types[tid] = *type; types.types[tid].tid = tid; return tid; } tid_t type_add_list(const type_t *type) { tid_t first = 0; for (; type->type; type++) { if (!first) first = type_add(type); else type_add(type); } return first; } __attribute__((constructor)) static void type_init(void) { type_add_list(builtin_types); type_add_list(arch_builtin_types); } /* int main(void) */ /* { */ /* tid_t tid; */ /* type_init(); */ /* for (tid = 0; tid < types.next; tid++) { */ /* type_t *t = type_info(tid); */ /* printf("tid:%3d sz:%2zx ", t->tid, t->size); */ /* type_dump_cdecl(t, stdout); */ /* putchar('\n'); */ /* } */ /* return 0; */ /* } */