kit

kit
git clone https://git.ryansepassi.com/git/kit.git
Log | Files | Refs | README

debug.c (3561B)


      1 #include "cg/internal.h"
      2 
      3 DebugTypeId api_debug_type(KitCg* g, KitCgTypeId id) {
      4   const CgType* ty;
      5   if (!g || !g->debug) return DEBUG_TYPE_NONE;
      6   ty = cg_type_get(g->c, id);
      7   if (!ty) return DEBUG_TYPE_NONE;
      8   switch (ty->kind) {
      9     case KIT_CG_TYPE_VOID:
     10       return debug_type_void(g->debug);
     11     case KIT_CG_TYPE_BOOL:
     12       return debug_type_base(
     13           g->debug, pool_intern_slice(g->c->global, SLICE_LIT("_Bool")),
     14           DEBUG_BE_BOOL, 1);
     15     case KIT_CG_TYPE_INT: {
     16       const char* name = "long long";
     17       if (ty->integer.width <= 8)
     18         name = "char";
     19       else if (ty->integer.width <= 16)
     20         name = "short";
     21       else if (ty->integer.width <= 32)
     22         name = "int";
     23       return debug_type_base(
     24           g->debug, pool_intern_slice(g->c->global, slice_from_cstr(name)),
     25           DEBUG_BE_SIGNED, (u32)((ty->integer.width + 7u) / 8u));
     26     }
     27     case KIT_CG_TYPE_FLOAT: {
     28       const char* name = ty->fp.width <= 32 ? "float" : "double";
     29       return debug_type_base(
     30           g->debug, pool_intern_slice(g->c->global, slice_from_cstr(name)),
     31           DEBUG_BE_FLOAT, (u32)((ty->fp.width + 7u) / 8u));
     32     }
     33     case KIT_CG_TYPE_PTR: {
     34       DebugTypeId pointee = api_debug_type(g, ty->ptr.pointee);
     35       if (pointee == DEBUG_TYPE_NONE) pointee = debug_type_void(g->debug);
     36       return debug_type_ptr(g->debug, pointee);
     37     }
     38     case KIT_CG_TYPE_ARRAY: {
     39       DebugTypeId elem = api_debug_type(g, ty->array.elem);
     40       u32 count = ty->array.count > UINT32_MAX ? 0u : (u32)ty->array.count;
     41       if (elem == DEBUG_TYPE_NONE) elem = debug_type_void(g->debug);
     42       return debug_type_array(g->debug, elem, count);
     43     }
     44     case KIT_CG_TYPE_FUNC: {
     45       Heap* h = (Heap*)g->c->ctx->heap;
     46       DebugTypeId ret = ty->func.result.type
     47                             ? api_debug_type(g, ty->func.result.type)
     48                             : DEBUG_TYPE_NONE;
     49       DebugTypeId* params = NULL;
     50       DebugTypeId fn;
     51       if (ret == DEBUG_TYPE_NONE) ret = debug_type_void(g->debug);
     52       if (ty->func.nparams) {
     53         params = (DebugTypeId*)h->alloc(h, sizeof(*params) * ty->func.nparams,
     54                                         _Alignof(DebugTypeId));
     55         if (!params) return DEBUG_TYPE_NONE;
     56         for (u32 i = 0; i < ty->func.nparams; ++i) {
     57           params[i] = api_debug_type(g, ty->func.params[i].type);
     58           if (params[i] == DEBUG_TYPE_NONE)
     59             params[i] = debug_type_void(g->debug);
     60         }
     61       }
     62       fn = debug_type_func(g->debug, ret, params, ty->func.nparams,
     63                            ty->func.abi_variadic);
     64       if (params) h->free(h, params, sizeof(*params) * ty->func.nparams);
     65       return fn;
     66     }
     67     case KIT_CG_TYPE_RECORD: {
     68       DebugTypeBuilder* b = debug_type_record_begin(
     69           g->debug, (Sym)ty->record.tag, ty->record.is_union, (u32)ty->size,
     70           ty->align);
     71       if (!b) return DEBUG_TYPE_NONE;
     72       return debug_type_record_end(b);
     73     }
     74     case KIT_CG_TYPE_ENUM:
     75       return debug_type_base(g->debug,
     76                              pool_intern_slice(g->c->global, SLICE_LIT("int")),
     77                              DEBUG_BE_SIGNED, ty->size ? (u32)ty->size : 4u);
     78     case KIT_CG_TYPE_ALIAS: {
     79       DebugTypeId base = api_debug_type(g, ty->alias.base);
     80       if (base == DEBUG_TYPE_NONE) base = debug_type_void(g->debug);
     81       return debug_type_typedef(g->debug, (Sym)ty->alias.name, base);
     82     }
     83     case KIT_CG_TYPE_VARARG_STATE:
     84       return debug_type_void(g->debug);
     85   }
     86   return DEBUG_TYPE_NONE;
     87 }
     88 
     89 /* ---- value stack helpers ---- */