Browse Source

Flesh out debug procedure type

gingerBill 2 years ago
parent
commit
988926b59d
4 changed files with 46 additions and 8 deletions
  1. 1 1
      src/tilde/tb.h
  2. 2 0
      src/tilde_backend.hpp
  3. 23 3
      src/tilde_debug.cpp
  4. 20 4
      src/tilde_proc.cpp

+ 1 - 1
src/tilde/tb.h

@@ -941,7 +941,7 @@ TB_API void tb_inst_memcpy(TB_Function* f, TB_Node* dst, TB_Node* src, TB_Node*
 // result = base + (index * stride)
 TB_API TB_Node* tb_inst_array_access(TB_Function* f, TB_Node* base, TB_Node* index, int64_t stride);
 
-// result = base +
+// result = base + offset
 // where base is a pointer
 TB_API TB_Node* tb_inst_member_access(TB_Function* f, TB_Node* base, int64_t offset);
 

+ 2 - 0
src/tilde_backend.hpp

@@ -145,6 +145,8 @@ struct cgProcedure {
 	bool         is_entry_point;
 	bool         is_startup;
 
+	TB_DebugType *debug_type;
+
 	cgValue value;
 
 	Ast *curr_stmt;

+ 23 - 3
src/tilde_debug.cpp

@@ -12,6 +12,20 @@ gb_internal TB_DebugType *cg_debug_type(cgModule *m, Type *type) {
 	return res;
 }
 
+gb_internal TB_DebugType *cg_debug_type_for_proc(cgModule *m, Type *type) {
+	GB_ASSERT(is_type_proc(type));
+	TB_DebugType **func_found = nullptr;
+	TB_DebugType *func_ptr = cg_debug_type(m, type);
+	GB_ASSERT(func_ptr != nullptr);
+
+	mutex_lock(&m->debug_type_mutex);
+	func_found = map_get(&m->proc_debug_type_map, type);
+	mutex_unlock(&m->debug_type_mutex);
+	GB_ASSERT(func_found != nullptr);
+	return *func_found;
+}
+
+
 gb_internal TB_DebugType *cg_debug_type_internal_record(cgModule *m, Type *type, String const &record_name) {
 	Type *bt = base_type(type);
 	switch (bt->kind) {
@@ -345,7 +359,7 @@ gb_internal TB_DebugType *cg_debug_type_internal(cgModule *m, Type *type) {
 				}
 			}
 
-			if (is_odin_cc) {
+			if (pt->calling_convention == ProcCC_Odin) {
 				// `context` ptr
 				param_count += 1;
 			}
@@ -406,8 +420,14 @@ gb_internal TB_DebugType *cg_debug_type_internal(cgModule *m, Type *type) {
 				}
 			}
 
-			GB_ASSERT(param_index  == param_count);
-			GB_ASSERT(return_index == return_count);
+			if (pt->calling_convention == ProcCC_Odin) {
+				Type *type = t_context_ptr;
+				String name = str_lit("__.context_ptr");
+				params[param_index++] = tb_debug_create_field(m->mod, cg_debug_type(m, type), name.len, cast(char const *)name.text, 0);
+			}
+
+			GB_ASSERT_MSG(param_index  == param_count,  "%td vs %td for %s", param_index,  param_count, type_to_string(type));
+			GB_ASSERT_MSG(return_index == return_count, "%td vs %td for %s", return_index, return_count, type_to_string(type));
 
 			return func_ptr;
 		}

+ 20 - 4
src/tilde_proc.cpp

@@ -326,8 +326,16 @@ gb_internal cgProcedure *cg_procedure_create(cgModule *m, Entity *entity, bool i
 	if (p->symbol == nullptr)  {
 		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);
+
+		// 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);
+		gb_unused(params);
+		p->proto = tb_function_get_prototype(p->func);
+
 		p->symbol = cast(TB_Symbol *)p->func;
 	}
 
@@ -373,8 +381,16 @@ 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);
+
+	// 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);
+	gb_unused(params);
+	p->proto = tb_function_get_prototype(p->func);
+
+
 	p->symbol = cast(TB_Symbol *)p->func;
 
 	cgValue proc_value = cg_value(p->symbol, p->type);