| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149 |
- #include <assert.h>
- #include <stdio.h>
- #include <stdlib.h>
- #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("<NONE>", 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; */
- /* } */
|