Update tilde; procedure type determination from debug types

This commit is contained in:
gingerBill
2023-07-18 13:16:35 +01:00
parent 55733171c1
commit 4d8d3919c0
6 changed files with 41 additions and 279 deletions
+2
View File
@@ -818,6 +818,7 @@ TB_API TB_DebugType* tb_debug_get_integer(TB_Module* m, bool is_signed, int bits
TB_API TB_DebugType* tb_debug_get_float(TB_Module* m, TB_FloatFormat fmt);
TB_API TB_DebugType* tb_debug_create_ptr(TB_Module* m, TB_DebugType* base);
TB_API TB_DebugType* tb_debug_create_array(TB_Module* m, TB_DebugType* base, size_t count);
TB_API TB_DebugType* tb_debug_create_alias(TB_Module* m, TB_DebugType* base, ptrdiff_t len, const char* tag);
TB_API TB_DebugType* tb_debug_create_struct(TB_Module* m, ptrdiff_t len, const char* tag);
TB_API TB_DebugType* tb_debug_create_union(TB_Module* m, ptrdiff_t len, const char* tag);
TB_API TB_DebugType* tb_debug_create_field(TB_Module* m, TB_DebugType* type, ptrdiff_t len, const char* name, TB_CharUnits offset);
@@ -883,6 +884,7 @@ TB_API const char* tb_symbol_get_name(TB_Symbol* s);
//
// returns the parameters
TB_API TB_Node** tb_function_set_prototype_from_dbg(TB_Function* f, TB_DebugType* dbg, TB_Arena* arena, size_t* out_param_count);
TB_API TB_FunctionPrototype* tb_prototype_from_dbg(TB_Module* m, TB_DebugType* dbg);
// if arena is NULL, defaults to module arena which is freed on tb_free_thread_resources
TB_API void tb_function_set_prototype(TB_Function* f, TB_FunctionPrototype* p, TB_Arena* arena);
BIN
View File
Binary file not shown.
+2
View File
@@ -411,6 +411,7 @@ gb_internal cgModule *cg_module_create(Checker *c) {
map_init(&m->debug_type_map);
map_init(&m->proc_debug_type_map);
map_init(&m->proc_proto_map);
for_array(id, global_files) {
@@ -429,6 +430,7 @@ gb_internal void cg_module_destroy(cgModule *m) {
map_destroy(&m->file_id_map);
map_destroy(&m->debug_type_map);
map_destroy(&m->proc_debug_type_map);
map_destroy(&m->proc_proto_map);
tb_module_destroy(m->mod);
}
+4 -2
View File
@@ -8,8 +8,7 @@
#include "tilde/tb.h"
#define TB_TYPE_F16 TB_DataType{ { TB_INT, 0, 16 } }
// #define TB_TYPE_I128 TB_DataType{ { TB_INT, 0, 128 } }
#define TB_TYPE_I128 TB_TYPE_INTN(64)
#define TB_TYPE_I128 TB_DataType{ { TB_INT, 0, 128 } }
#define TB_TYPE_INT TB_TYPE_INTN(cast(u16)(8*build_context.int_size))
#define TB_TYPE_INTPTR TB_TYPE_INTN(cast(u16)(8*build_context.ptr_size))
@@ -191,6 +190,9 @@ struct cgModule {
PtrMap<Type *, TB_DebugType *> debug_type_map;
PtrMap<Type *, TB_DebugType *> proc_debug_type_map; // not pointer to
RecursiveMutex proc_proto_mutex;
PtrMap<Type *, TB_FunctionPrototype *> proc_proto_map;
PtrMap<uintptr, TB_FileID> file_id_map; // Key: AstFile.id (i32 cast to uintptr)
std::atomic<u32> nested_type_name_guid;
+18 -15
View File
@@ -185,13 +185,13 @@ gb_internal TB_DebugType *cg_debug_type_internal(cgModule *m, Type *type) {
case Basic_u32: return tb_debug_get_integer(m->mod, is_signed, bits);
case Basic_i64: return tb_debug_get_integer(m->mod, is_signed, bits);
case Basic_u64: return tb_debug_get_integer(m->mod, is_signed, bits);
case Basic_i128: return tb_debug_get_integer(m->mod, is_signed, 64/*bits*/);
case Basic_u128: return tb_debug_get_integer(m->mod, is_signed, 64/*bits*/);
case Basic_i128: return tb_debug_get_integer(m->mod, is_signed, bits);
case Basic_u128: return tb_debug_get_integer(m->mod, is_signed, bits);
case Basic_rune: return tb_debug_get_integer(m->mod, is_signed, bits);
case Basic_f16: return tb_debug_get_integer(m->mod, false, bits);
case Basic_f32: return tb_debug_get_float(m->mod, TB_FLT_32);
case Basic_f64: return tb_debug_get_float(m->mod,TB_FLT_64);
case Basic_f32: return tb_debug_get_float(m->mod, TB_FLT_32);
case Basic_f64: return tb_debug_get_float(m->mod, TB_FLT_64);
case Basic_complex32:
case Basic_complex64:
@@ -263,7 +263,7 @@ gb_internal TB_DebugType *cg_debug_type_internal(cgModule *m, Type *type) {
tb_debug_record_end(record, size, align);
return record;
}
case Basic_typeid: return tb_debug_get_integer(m->mod, is_signed, bits);
case Basic_typeid: return tb_debug_get_integer(m->mod, false, bits);
case Basic_i16le: return tb_debug_get_integer(m->mod, is_signed, bits);
case Basic_u16le: return tb_debug_get_integer(m->mod, is_signed, bits);
@@ -271,23 +271,23 @@ gb_internal TB_DebugType *cg_debug_type_internal(cgModule *m, Type *type) {
case Basic_u32le: return tb_debug_get_integer(m->mod, is_signed, bits);
case Basic_i64le: return tb_debug_get_integer(m->mod, is_signed, bits);
case Basic_u64le: return tb_debug_get_integer(m->mod, is_signed, bits);
case Basic_i128le: return tb_debug_get_integer(m->mod, is_signed, 64/*bits*/);
case Basic_u128le: return tb_debug_get_integer(m->mod, is_signed, 64/*bits*/);
case Basic_i128le: return tb_debug_get_integer(m->mod, is_signed, bits);
case Basic_u128le: return tb_debug_get_integer(m->mod, is_signed, bits);
case Basic_i16be: return tb_debug_get_integer(m->mod, is_signed, bits);
case Basic_u16be: return tb_debug_get_integer(m->mod, is_signed, bits);
case Basic_i32be: return tb_debug_get_integer(m->mod, is_signed, bits);
case Basic_u32be: return tb_debug_get_integer(m->mod, is_signed, bits);
case Basic_i64be: return tb_debug_get_integer(m->mod, is_signed, bits);
case Basic_u64be: return tb_debug_get_integer(m->mod, is_signed, bits);
case Basic_i128be: return tb_debug_get_integer(m->mod, is_signed, 64/*bits*/);
case Basic_u128be: return tb_debug_get_integer(m->mod, is_signed, 64/*bits*/);
case Basic_i128be: return tb_debug_get_integer(m->mod, is_signed, bits);
case Basic_u128be: return tb_debug_get_integer(m->mod, is_signed, bits);
case Basic_f16le: return tb_debug_get_integer(m->mod, false, bits);
case Basic_f32le: return tb_debug_get_float(m->mod, TB_FLT_32);
case Basic_f64le: return tb_debug_get_float(m->mod,TB_FLT_64);
case Basic_f32le: return tb_debug_get_float(m->mod, TB_FLT_32);
case Basic_f64le: return tb_debug_get_float(m->mod, TB_FLT_64);
case Basic_f16be: return tb_debug_get_integer(m->mod, false, bits);
case Basic_f32be: return tb_debug_get_float(m->mod, TB_FLT_32);
case Basic_f64be: return tb_debug_get_float(m->mod,TB_FLT_64);
case Basic_f32be: return tb_debug_get_float(m->mod, TB_FLT_32);
case Basic_f64be: return tb_debug_get_float(m->mod, TB_FLT_64);
}
break;
case Type_Generic:
@@ -350,8 +350,9 @@ gb_internal TB_DebugType *cg_debug_type_internal(cgModule *m, Type *type) {
}
}
if (pt->results) {
if (pt->result_count > 0) {
if (is_odin_cc) {
// Split returns
param_count += pt->result_count-1;
return_count = 1;
} else {
@@ -392,8 +393,10 @@ gb_internal TB_DebugType *cg_debug_type_internal(cgModule *m, Type *type) {
}
}
if (pt->results) {
if (pt->result_count) {
GB_ASSERT(pt->results);
if (is_odin_cc) {
// Split Returns
for (isize i = 0; i < pt->results->Tuple.variables.count-1; i++) {
Entity *e = pt->results->Tuple.variables[i];
GB_ASSERT(e->kind == Entity_Variable);
+15 -262
View File
@@ -1,260 +1,21 @@
gb_internal TB_FunctionPrototype *cg_procedure_type_as_prototype(cgModule *m, Type *type) {
// TODO(bill): cache the procedure type generation
GB_ASSERT(build_context.metrics.os == TargetOs_windows);
GB_ASSERT(is_type_proc(type));
mutex_lock(&m->proc_proto_mutex);
defer (mutex_unlock(&m->proc_proto_mutex));
GB_ASSERT(type != nullptr);
type = base_type(type);
GB_ASSERT(type->kind == Type_Proc);
TypeProc *pt = &type->Proc;
auto params = array_make<TB_PrototypeParam>(heap_allocator(), 0, pt->param_count);
if (pt->params) for (Entity *e : pt->params->Tuple.variables) {
TB_PrototypeParam param = {};
Type *t = core_type(e->type);
i64 sz = type_size_of(t);
switch (t->kind) {
case Type_Basic:
switch (t->Basic.kind) {
case Basic_bool:
case Basic_b8:
case Basic_b16:
case Basic_b32:
case Basic_b64:
case Basic_i8:
case Basic_u8:
case Basic_i16:
case Basic_u16:
case Basic_i32:
case Basic_u32:
case Basic_i64:
case Basic_u64:
case Basic_i128:
case Basic_u128:
case Basic_rune:
case Basic_int:
case Basic_uint:
case Basic_uintptr:
param.dt = TB_TYPE_INTN(cast(u16)gb_min(64, 8*sz));
break;
case Basic_f16: param.dt = TB_TYPE_F16; break;
case Basic_f32: param.dt = TB_TYPE_F32; break;
case Basic_f64: param.dt = TB_TYPE_F64; break;
case Basic_complex32:
case Basic_complex64:
case Basic_complex128:
case Basic_quaternion64:
case Basic_quaternion128:
case Basic_quaternion256:
param.dt = TB_TYPE_PTR;
break;
case Basic_rawptr:
param.dt = TB_TYPE_PTR;
break;
case Basic_string: // ^u8 + int
param.dt = TB_TYPE_PTR;
break;
case Basic_cstring: // ^u8
param.dt = TB_TYPE_PTR;
break;
case Basic_any: // rawptr + ^Type_Info
param.dt = TB_TYPE_PTR;
break;
case Basic_typeid:
param.dt = TB_TYPE_INTN(cast(u16)gb_min(64, 8*sz));
break;
// Endian Specific Types
case Basic_i16le:
case Basic_u16le:
case Basic_i32le:
case Basic_u32le:
case Basic_i64le:
case Basic_u64le:
case Basic_i128le:
case Basic_u128le:
case Basic_i16be:
case Basic_u16be:
case Basic_i32be:
case Basic_u32be:
case Basic_i64be:
case Basic_u64be:
case Basic_i128be:
case Basic_u128be:
param.dt = TB_TYPE_INTN(cast(u16)gb_min(64, 8*sz));
break;
case Basic_f16le: param.dt = TB_TYPE_F16; break;
case Basic_f32le: param.dt = TB_TYPE_F32; break;
case Basic_f64le: param.dt = TB_TYPE_F64; break;
case Basic_f16be: param.dt = TB_TYPE_F16; break;
case Basic_f32be: param.dt = TB_TYPE_F32; break;
case Basic_f64be: param.dt = TB_TYPE_F64; break;
}
case Type_Pointer:
case Type_MultiPointer:
case Type_Proc:
param.dt = TB_TYPE_PTR;
break;
default:
switch (sz) {
case 1: param.dt = TB_TYPE_I8; break;
case 2: param.dt = TB_TYPE_I16; break;
case 4: param.dt = TB_TYPE_I32; break;
case 8: param.dt = TB_TYPE_I64; break;
default:
param.dt = TB_TYPE_PTR;
break;
}
}
if (param.dt.raw != 0) {
if (is_blank_ident(e->token)) {
param.name = alloc_cstring(temporary_allocator(), e->token.string);
}
param.debug_type = cg_debug_type(m, e->type);
array_add(&params, param);
}
if (type->kind == Type_Named) {
type = base_type(type);
}
TB_FunctionPrototype **found = map_get(&m->proc_proto_map, type);
if (found) {
return *found;
}
auto results = array_make<TB_PrototypeParam>(heap_allocator(), 0, 1);
TB_DebugType *dbg = cg_debug_type_for_proc(m, type);
TB_FunctionPrototype *proto = tb_prototype_from_dbg(m->mod, dbg);
Type *result_type = reduce_tuple_to_single_type(pt->results);
if (result_type) {
bool return_is_tuple = result_type->kind == Type_Tuple && is_calling_convention_odin(pt->calling_convention);
if (return_is_tuple) {
for (isize i = 0; i < result_type->Tuple.variables.count-1; i++) {
Entity *e = result_type->Tuple.variables[i];
TB_PrototypeParam param = {};
param.dt = TB_TYPE_PTR;
param.debug_type = cg_debug_type(m, alloc_type_pointer(e->type));
array_add(&params, param);
}
result_type = result_type->Tuple.variables[result_type->Tuple.variables.count-1]->type;
}
Type *rt = core_type(result_type);
i64 sz = type_size_of(rt);
TB_PrototypeParam result = {};
switch (rt->kind) {
case Type_Basic:
switch (rt->Basic.kind) {
case Basic_bool:
case Basic_b8:
case Basic_b16:
case Basic_b32:
case Basic_b64:
case Basic_i8:
case Basic_u8:
case Basic_i16:
case Basic_u16:
case Basic_i32:
case Basic_u32:
case Basic_i64:
case Basic_u64:
case Basic_i128:
case Basic_u128:
case Basic_rune:
case Basic_int:
case Basic_uint:
case Basic_uintptr:
result.dt = TB_TYPE_INTN(cast(u16)gb_min(64, 8*sz));
break;
case Basic_f16: result.dt = TB_TYPE_I16; break;
case Basic_f32: result.dt = TB_TYPE_F32; break;
case Basic_f64: result.dt = TB_TYPE_F64; break;
case Basic_rawptr:
result.dt = TB_TYPE_PTR;
break;
case Basic_cstring: // ^u8
result.dt = TB_TYPE_PTR;
break;
case Basic_typeid:
result.dt = TB_TYPE_INTN(cast(u16)gb_min(64, 8*sz));
break;
// Endian Specific Types
case Basic_i16le:
case Basic_u16le:
case Basic_i32le:
case Basic_u32le:
case Basic_i64le:
case Basic_u64le:
case Basic_i128le:
case Basic_u128le:
case Basic_i16be:
case Basic_u16be:
case Basic_i32be:
case Basic_u32be:
case Basic_i64be:
case Basic_u64be:
case Basic_i128be:
case Basic_u128be:
result.dt = TB_TYPE_INTN(cast(u16)gb_min(64, 8*sz));
break;
case Basic_f16le: result.dt = TB_TYPE_I16; break;
case Basic_f32le: result.dt = TB_TYPE_F32; break;
case Basic_f64le: result.dt = TB_TYPE_F64; break;
case Basic_f16be: result.dt = TB_TYPE_I16; break;
case Basic_f32be: result.dt = TB_TYPE_F32; break;
case Basic_f64be: result.dt = TB_TYPE_F64; break;
}
case Type_Pointer:
case Type_MultiPointer:
case Type_Proc:
result.dt = TB_TYPE_PTR;
break;
default:
switch (sz) {
case 1: result.dt = TB_TYPE_I8; break;
case 2: result.dt = TB_TYPE_I16; break;
case 4: result.dt = TB_TYPE_I32; break;
case 8: result.dt = TB_TYPE_I64; break;
}
}
if (result.dt.raw != 0) {
result.debug_type = cg_debug_type(m, result_type);
array_add(&results, result);
} else {
result.debug_type = cg_debug_type(m, alloc_type_pointer(result_type));
result.dt = TB_TYPE_PTR;
array_resize(&params, params.count+1);
array_copy(&params, params, 1);
params[0] = result;
}
}
if (pt->calling_convention == ProcCC_Odin) {
TB_PrototypeParam param = {};
param.dt = TB_TYPE_PTR;
param.debug_type = cg_debug_type(m, t_context_ptr);
param.name = "__.context_ptr";
array_add(&params, param);
}
return tb_prototype_create(m->mod, TB_CDECL, params.count, params.data, results.count, results.data, pt->c_vararg);
map_set(&m->proc_proto_map, type, proto);
return proto;
}
gb_internal cgProcedure *cg_procedure_create(cgModule *m, Entity *entity, bool ignore_body=false) {
@@ -327,9 +88,6 @@ gb_internal cgProcedure *cg_procedure_create(cgModule *m, Entity *entity, bool i
TB_Arena *arena = tb_default_arena();
p->func = tb_function_create(m->mod, link_name.len, cast(char const *)link_name.text, linkage, TB_COMDAT_NONE);
// p->proto = cg_procedure_type_as_prototype(m, p->type);
// tb_function_set_prototype(p->func, p->proto, arena);
size_t out_param_count = 0;
p->debug_type = cg_debug_type_for_proc(m, p->type);
TB_Node **params = tb_function_set_prototype_from_dbg(p->func, p->debug_type, arena, &out_param_count);
@@ -382,8 +140,6 @@ gb_internal cgProcedure *cg_procedure_create_dummy(cgModule *m, String const &li
TB_Arena *arena = tb_default_arena();
p->func = tb_function_create(m->mod, link_name.len, cast(char const *)link_name.text, linkage, TB_COMDAT_NONE);
// p->proto = cg_procedure_type_as_prototype(m, p->type);
// tb_function_set_prototype(p->func, p->proto, arena);
size_t out_param_count = 0;
p->debug_type = cg_debug_type_for_proc(m, p->type);
TB_Node **params = tb_function_set_prototype_from_dbg(p->func, p->debug_type, arena, &out_param_count);
@@ -425,16 +181,13 @@ gb_internal void cg_procedure_begin(cgProcedure *p) {
}
TB_Node *param = p->param_nodes[param_index++];
TB_Node *ptr = tb_inst_local(p->func, cast(TB_CharUnits)type_size_of(e->type), cast(TB_CharUnits)type_align_of(e->type));
TB_DataType dt = cg_data_type(e->type);
tb_inst_store(p->func, dt, ptr, param, cast(TB_CharUnits)type_align_of(e->type), false);
cgValue local = cg_value(ptr, alloc_type_pointer(e->type));
cgValue local = cg_value(param, alloc_type_pointer(e->type));
if (e != nullptr && e->token.string.len > 0 && e->token.string != "_") {
// NOTE(bill): for debugging purposes only
String name = e->token.string;
TB_DebugType *debug_type = cg_debug_type(p->module, e->type);
tb_node_append_attrib(ptr, tb_function_attrib_variable(p->func, name.len, cast(char const *)name.text, debug_type));
tb_node_append_attrib(param, tb_function_attrib_variable(p->func, name.len, cast(char const *)name.text, debug_type));
}
cgAddr addr = cg_addr(local);