2
0
Эх сурвалжийг харах

Add proper procedure type support (return types and param proc signature)

lachsinc 7 жил өмнө
parent
commit
7dcad45e0d
2 өөрчлөгдсөн 43 нэмэгдсэн , 16 устгасан
  1. 30 8
      src/ir.cpp
  2. 13 8
      src/ir_print.cpp

+ 30 - 8
src/ir.cpp

@@ -566,7 +566,7 @@ struct irDebugInfo {
 			String               name;
 			irDebugInfo *        file;
 			TokenPos             pos;
-			Array<irDebugInfo *> return_types;
+			Array<irDebugInfo *> types; // !{return, return, param, param, param.. etc.}
 			// TODO(lachsinc): variables / retainedNodes ?
 		} Proc;
 		struct {
@@ -1794,19 +1794,41 @@ irDebugInfo *ir_add_debug_info_proc(irProcedure *proc, Entity *entity, String na
 	di->Proc.file = file;
 	di->Proc.pos = entity->token.pos;
 
-	isize return_count = proc->type->Proc.result_count;
-	array_init(&di->Proc.return_types, ir_allocator(), 0, return_count); // TODO(lachsinc): ir_allocator() safe to use?
-	if (return_count >= 1) {
-		TypeTuple *tuple  = &proc->type->Proc.results->Tuple;
-		for_array(i, tuple->variables) {
-			Entity *e = tuple->variables[i];
+	isize result_count = proc->type->Proc.result_count;
+	isize param_count = proc->type->Proc.param_count;
+	array_init(&di->Proc.types, ir_allocator(), 0, gb_max(result_count, 1) + param_count); // TODO(lachsinc): ir_allocator() safe to use?
+
+	// Result/return types
+	if (result_count >= 1) {
+		TypeTuple *results_tuple = &proc->type->Proc.results->Tuple;
+		for_array(i, results_tuple->variables) {
+			Entity *e = results_tuple->variables[i];
 			if (e->kind != Entity_Variable) {
 				continue; // TODO(lachsinc): Confirm correct?
 			}
 
 			irDebugInfo *type_di = ir_add_debug_info_type(proc->module, di, e, e->type, file);
 			GB_ASSERT_NOT_NULL(type_di);
-			array_add(&di->Proc.return_types, type_di);
+			array_add(&di->Proc.types, type_di);
+		}
+	} else {
+		// llvm expects "!{null}" for a function without return type, use nullptr to represent it.
+		// TODO(lachsinc): Is there a specific "void" type we should refer to?
+		array_add(&di->Proc.types, (irDebugInfo*)nullptr);
+	}
+
+	// Param types
+	if (param_count >= 1) {
+		TypeTuple *params_tuple = &proc->type->Proc.params->Tuple;
+		for_array(i, params_tuple->variables) {
+			Entity *e = params_tuple->variables[i];
+			if (e->kind != Entity_Variable) {
+				continue; // TODO(lachsinc): Confirm correct?
+			}
+			// TODO(lach): Could technically be a local?
+			irDebugInfo *type_di = ir_add_debug_info_type(proc->module, di, e, e->type, file);
+			GB_ASSERT_NOT_NULL(type_di);
+			array_add(&di->Proc.types, type_di);
 		}
 	}
 	

+ 13 - 8
src/ir_print.cpp

@@ -1982,16 +1982,21 @@ void print_llvm_ir(irGen *ir) {
 				            di->Proc.pos.line,
 				            di->Proc.pos.line, // NOTE(lachsinc): Assume scopeLine always same as line.
 				            m->debug_compile_unit->id);
-				if (di->Proc.return_types.count == 0) {
-					ir_fprintf(f, "null})");
-				} else {
-					for_array(return_type_index, di->Proc.return_types) {
-						ir_fprintf(f, "%s!%d",
-						           return_type_index > 0 ? ", " : "",
-						           di->Proc.return_types[return_type_index]->id);
+				if (di->Proc.types.count > 0) {
+					for_array(type_index, di->Proc.types) {
+						if (type_index > 0) {
+							ir_write_byte(f, ',');
+						}
+						if (di->Proc.types[type_index]) {
+							ir_fprintf(f, "!%d", di->Proc.types[type_index]->id);
+						} else {
+							ir_write_str_lit(f, "null");
+						}
 					}
-					ir_write_str_lit(f, "})");
+				} else {
+					ir_write_str_lit(f, "null");
 				}
+				ir_write_str_lit(f, "})"); // !DISubroutineTypes close
 				ir_write_byte(f, ')');
 				break;
 			case irDebugInfo_LocalVariable: {