Browse Source

Default struct field values

Ginger Bill 8 years ago
parent
commit
193c7c82c8
4 changed files with 276 additions and 68 deletions
  1. 23 4
      code/demo.odin
  2. 77 20
      src/check_expr.cpp
  3. 75 8
      src/ir.cpp
  4. 101 36
      src/ir_print.cpp

+ 23 - 4
code/demo.odin

@@ -110,7 +110,7 @@ get_hash :: proc(s: string) -> u32 {
 }
 }
 
 
 
 
-
+/*
 Vector :: struct(N: int, T: type) {
 Vector :: struct(N: int, T: type) {
 	using _: raw_union {
 	using _: raw_union {
 		using e: [N]T;
 		using e: [N]T;
@@ -140,11 +140,29 @@ foo1 :: proc(a: type/Vector)         { fmt.println("foo1", a{}); }
 foo3 :: proc(a: type/Vector(3, $T))  {fmt.println("foo3", a{}); }
 foo3 :: proc(a: type/Vector(3, $T))  {fmt.println("foo3", a{}); }
 // foo4 :: proc(a: type/Vector3)        {}
 // foo4 :: proc(a: type/Vector3)        {}
 
 
+*/
+
 
 
+foo :: proc() -> (f32, f32) {
+	return 1, 2;
+}
 
 
 
 
 main :: proc() {
 main :: proc() {
-	foo1(Vector(3, f32));
+
+	Vector3 :: struct {
+		x: f32 = 1;
+		y: f32 = 4;
+		z: f32 = 9;
+	}
+
+	v := make([dynamic]Vector3, 3);
+
+	array: [100]Vector3;
+	v2 := array[50];
+	fmt.println(v2);
+
+/*	foo1(Vector(3, f32));
 	foo1(Vector3);
 	foo1(Vector3);
 	foo3(Vector(3, f32));
 	foo3(Vector(3, f32));
 	foo3(Vector3);
 	foo3(Vector3);
@@ -161,8 +179,9 @@ main :: proc() {
 
 
 	v := add(a, b);
 	v := add(a, b);
 	fmt.println(v.v);
 	fmt.println(v.v);
+*/
 
 
-
+/*
 	table: Table(string, int);
 	table: Table(string, int);
 
 
 	for i in 0..36 do put(&table, "Hellope", i);
 	for i in 0..36 do put(&table, "Hellope", i);
@@ -174,5 +193,5 @@ main :: proc() {
 
 
 	found, _ = find(&table, "World!");
 	found, _ = find(&table, "World!");
 	fmt.printf("found is %v\n", found);
 	fmt.printf("found is %v\n", found);
-
+*/
 }
 }

+ 77 - 20
src/check_expr.cpp

@@ -61,6 +61,7 @@ void     check_init_constant            (Checker *c, Entity *e, Operand *operand
 bool     check_representable_as_constant(Checker *c, ExactValue in_value, Type *type, ExactValue *out_value);
 bool     check_representable_as_constant(Checker *c, ExactValue in_value, Type *type, ExactValue *out_value);
 bool     check_procedure_type           (Checker *c, Type *type, AstNode *proc_type_node, Array<Operand> *operands = nullptr);
 bool     check_procedure_type           (Checker *c, Type *type, AstNode *proc_type_node, Array<Operand> *operands = nullptr);
 CallArgumentData check_call_arguments   (Checker *c, Operand *operand, Type *proc_type, AstNode *call);
 CallArgumentData check_call_arguments   (Checker *c, Operand *operand, Type *proc_type, AstNode *call);
+Type *           check_init_variable    (Checker *c, Entity *e, Operand *operand, String context_name);
 
 
 void error_operand_not_expression(Operand *o) {
 void error_operand_not_expression(Operand *o) {
 	if (o->mode == Addressing_Type) {
 	if (o->mode == Addressing_Type) {
@@ -759,7 +760,8 @@ void populate_using_entity_map(Checker *c, AstNode *node, Type *t, Map<Entity *>
 }
 }
 
 
 
 
-void check_record_field_decl(Checker *c, AstNode *decl, Array<Entity *> *fields, Map<Entity *> *entity_map, AstNode *record_node, String context) {
+void check_record_field_decl(Checker *c, AstNode *decl, Array<Entity *> *fields, Map<Entity *> *entity_map, AstNode *record_node, String context, bool allow_default_values) {
+	GB_ASSERT(fields != nullptr);
 	if (decl->kind == AstNode_WhenStmt) {
 	if (decl->kind == AstNode_WhenStmt) {
 		ast_node(ws, WhenStmt, decl);
 		ast_node(ws, WhenStmt, decl);
 		Operand operand = {Addressing_Invalid};
 		Operand operand = {Addressing_Invalid};
@@ -776,18 +778,18 @@ void check_record_field_decl(Checker *c, AstNode *decl, Array<Entity *> *fields,
 		    operand.value.value_bool) {
 		    operand.value.value_bool) {
 			for_array(i, ws->body->BlockStmt.stmts) {
 			for_array(i, ws->body->BlockStmt.stmts) {
 				AstNode *stmt = ws->body->BlockStmt.stmts[i];
 				AstNode *stmt = ws->body->BlockStmt.stmts[i];
-				check_record_field_decl(c, stmt, fields, entity_map, record_node, context);
+				check_record_field_decl(c, stmt, fields, entity_map, record_node, context, allow_default_values);
 			}
 			}
 		} else if (ws->else_stmt) {
 		} else if (ws->else_stmt) {
 			switch (ws->else_stmt->kind) {
 			switch (ws->else_stmt->kind) {
 			case AstNode_BlockStmt:
 			case AstNode_BlockStmt:
 				for_array(i, ws->else_stmt->BlockStmt.stmts) {
 				for_array(i, ws->else_stmt->BlockStmt.stmts) {
 					AstNode *stmt = ws->else_stmt->BlockStmt.stmts[i];
 					AstNode *stmt = ws->else_stmt->BlockStmt.stmts[i];
-					check_record_field_decl(c, stmt, fields, entity_map, record_node, context);
+					check_record_field_decl(c, stmt, fields, entity_map, record_node, context, allow_default_values);
 				}
 				}
 				break;
 				break;
 			case AstNode_WhenStmt:
 			case AstNode_WhenStmt:
-				check_record_field_decl(c, ws->else_stmt, fields, entity_map, record_node, context);
+				check_record_field_decl(c, ws->else_stmt, fields, entity_map, record_node, context, allow_default_values);
 				break;
 				break;
 			default:
 			default:
 				error(ws->else_stmt, "Invalid `else` statement in `when` statement");
 				error(ws->else_stmt, "Invalid `else` statement in `when` statement");
@@ -805,13 +807,6 @@ void check_record_field_decl(Checker *c, AstNode *decl, Array<Entity *> *fields,
 
 
 	if (!vd->is_mutable) return;
 	if (!vd->is_mutable) return;
 
 
-	Type *type = nullptr;
-	if (vd->type != nullptr) {
-		type = check_type(c, vd->type);
-	} else {
-		error(vd->names[0], "Expected a type for this field");
-		type = t_invalid;
-	}
 	bool is_using = (vd->flags&VarDeclFlag_using) != 0;
 	bool is_using = (vd->flags&VarDeclFlag_using) != 0;
 
 
 	if (is_using && vd->names.count > 1) {
 	if (is_using && vd->names.count > 1) {
@@ -819,15 +814,59 @@ void check_record_field_decl(Checker *c, AstNode *decl, Array<Entity *> *fields,
 		is_using = false;
 		is_using = false;
 	}
 	}
 
 
-	if (!vd->is_mutable) {
-		error(vd->names[0], "Immutable values in a %.*s are not yet supported", LIT(context));
-		return;
-	}
+	bool arity_ok = check_arity_match(c, vd);
 
 
-	if (vd->values.count) {
+	if (vd->values.count > 0 && !allow_default_values) {
 		error(vd->values[0], "Default values are not allowed within a %.*s", LIT(context));
 		error(vd->values[0], "Default values are not allowed within a %.*s", LIT(context));
 	}
 	}
 
 
+
+	Type *type = nullptr;
+	if (vd->type != nullptr) {
+		type = check_type(c, vd->type);
+	} else if (!allow_default_values) {
+		error(vd->names[0], "Expected a type for this field");
+		type = t_invalid;
+	}
+
+	Array<Operand> default_values = {};
+	defer (array_free(&default_values));
+	if (vd->values.count > 0 && allow_default_values) {
+		array_init(&default_values, heap_allocator(), 2*vd->values.count);
+
+		Type *type_hint = nullptr;
+		if (type != t_invalid && type != nullptr) {
+			type_hint = type;
+		}
+
+		for_array(i, vd->values) {
+			AstNode *v = vd->values[i];
+			Operand o = {};
+
+			check_expr_base(c, &o, v, type_hint);
+			check_not_tuple(c, &o);
+
+			if (o.mode == Addressing_NoValue) {
+				error_operand_no_value(&o);
+			} else {
+				if (o.mode == Addressing_Value && o.type->kind == Type_Tuple) {
+					// NOTE(bill): Tuples are not first class thus never named
+					isize count = o.type->Tuple.variable_count;
+					for (isize index = 0; index < count; index++) {
+						Operand single = {Addressing_Value};
+						single.type    = o.type->Tuple.variables[index]->type;
+						single.expr    = v;
+						array_add(&default_values, single);
+					}
+				} else {
+					array_add(&default_values, o);
+				}
+			}
+		}
+	}
+
+
+	isize name_field_index = 0;
 	for_array(name_index, vd->names) {
 	for_array(name_index, vd->names) {
 		AstNode *name = vd->names[name_index];
 		AstNode *name = vd->names[name_index];
 		if (!ast_node_expect(name, AstNode_Ident)) {
 		if (!ast_node_expect(name, AstNode_Ident)) {
@@ -838,6 +877,22 @@ void check_record_field_decl(Checker *c, AstNode *decl, Array<Entity *> *fields,
 
 
 		Entity *e = make_entity_field(c->allocator, c->context.scope, name_token, type, is_using, cast(i32)fields->count);
 		Entity *e = make_entity_field(c->allocator, c->context.scope, name_token, type, is_using, cast(i32)fields->count);
 		e->identifier = name;
 		e->identifier = name;
+
+		if (name_field_index < default_values.count) {
+			Operand op = default_values[name_field_index++];
+			check_init_variable(c, e, &op, str_lit("struct field assignment"));
+			if (is_operand_nil(op)) {
+				e->Variable.default_is_nil = true;
+			} else if (op.mode != Addressing_Constant) {
+				error(op.expr, "Default field parameter must be a constant");
+			} else {
+				e->Variable.default_value = op.value;
+			}
+		} else {
+			GB_ASSERT(type != nullptr);
+		}
+
+
 		if (is_blank_ident(name_token)) {
 		if (is_blank_ident(name_token)) {
 			array_add(fields, e);
 			array_add(fields, e);
 		} else if (name_token.string == "__tag") {
 		} else if (name_token.string == "__tag") {
@@ -858,12 +913,14 @@ void check_record_field_decl(Checker *c, AstNode *decl, Array<Entity *> *fields,
 			}
 			}
 			add_entity_use(c, name, e);
 			add_entity_use(c, name, e);
 		}
 		}
+
 	}
 	}
 
 
 	Entity *using_index_expr = nullptr;
 	Entity *using_index_expr = nullptr;
 
 
-	if (is_using) {
-		Type *t = base_type(type_deref(type));
+	if (is_using && fields->count > 0) {
+		Type *first_type = (*fields)[fields->count-1]->type;
+		Type *t = base_type(type_deref(first_type));
 		if (!is_type_struct(t) && !is_type_raw_union(t) && !is_type_bit_field(t) &&
 		if (!is_type_struct(t) && !is_type_raw_union(t) && !is_type_bit_field(t) &&
 		    vd->names.count >= 1 &&
 		    vd->names.count >= 1 &&
 		    vd->names[0]->kind == AstNode_Ident) {
 		    vd->names[0]->kind == AstNode_Ident) {
@@ -889,7 +946,7 @@ void check_record_field_decl(Checker *c, AstNode *decl, Array<Entity *> *fields,
 					error(name_token, "Previous `using` for an index expression `%.*s`", LIT(name_token.string));
 					error(name_token, "Previous `using` for an index expression `%.*s`", LIT(name_token.string));
 				}
 				}
 			} else {
 			} else {
-				gbString type_str = type_to_string(type);
+				gbString type_str = type_to_string(first_type);
 				error(name_token, "`using` cannot be applied to the field `%.*s` of type `%s`", LIT(name_token.string), type_str);
 				error(name_token, "`using` cannot be applied to the field `%.*s` of type `%s`", LIT(name_token.string), type_str);
 				gb_string_free(type_str);
 				gb_string_free(type_str);
 				return;
 				return;
@@ -934,7 +991,7 @@ Array<Entity *> check_fields(Checker *c, AstNode *node, Array<AstNode *> decls,
 	}
 	}
 
 
 	for_array(decl_index, decls) {
 	for_array(decl_index, decls) {
-		check_record_field_decl(c, decls[decl_index], &fields, &entity_map, node, str_lit("struct"));
+		check_record_field_decl(c, decls[decl_index], &fields, &entity_map, node, context, context == "struct");
 	}
 	}
 
 
 
 

+ 75 - 8
src/ir.cpp

@@ -653,6 +653,32 @@ Type *ir_type(irValue *value) {
 }
 }
 
 
 
 
+bool ir_type_has_default_values(Type *t) {
+	switch (t->kind) {
+	case Type_Named:
+		return ir_type_has_default_values(t->Named.base);
+
+	case Type_Array:
+		return ir_type_has_default_values(t->Array.elem);
+
+	case Type_Record:
+		if (t->Record.kind == TypeRecord_Struct) {
+			for (isize i = 0; i < t->Record.field_count; i++) {
+				Entity *f = t->Record.fields_in_src_order[i];
+				if (f->kind != Entity_Variable) continue;
+				if (f->Variable.default_is_nil) {
+					// NOTE(bill): This is technically zero
+					continue;
+				} else if (f->Variable.default_value.kind != ExactValue_Invalid) {
+					return true;
+				}
+			}
+		}
+		break;
+	}
+
+	return false;
+}
 
 
 
 
 irInstr *ir_get_last_instr(irBlock *block) {
 irInstr *ir_get_last_instr(irBlock *block) {
@@ -3791,6 +3817,45 @@ irValue *ir_emit_source_code_location(irProcedure *proc, String procedure, Token
 	return ir_emit_global_call(proc, "make_source_code_location", args, 4);
 	return ir_emit_global_call(proc, "make_source_code_location", args, 4);
 }
 }
 
 
+void ir_emit_increment(irProcedure *proc, irValue *addr) {
+	GB_ASSERT(is_type_pointer(ir_type(addr)));
+	Type *type = type_deref(ir_type(addr));
+	ir_emit_store(proc, addr, ir_emit_arith(proc, Token_Add, ir_emit_load(proc, addr), v_one, type));
+
+}
+
+void ir_init_data_with_defaults(irProcedure *proc, irValue *ptr, irValue *count) {
+	Type *elem_type = type_deref(ir_type(ptr));
+	GB_ASSERT(is_type_struct(elem_type) || is_type_array(elem_type));
+
+	irValue *index = ir_add_local_generated(proc, t_int);
+	ir_emit_store(proc, index, ir_const_int(proc->module->allocator, 0));
+
+	irBlock *loop = nullptr;
+	irBlock *done = nullptr;
+	irBlock *body = nullptr;
+
+	loop = ir_new_block(proc, nullptr, "make.init.loop");
+	ir_emit_jump(proc, loop);
+	ir_start_block(proc, loop);
+
+	body = ir_new_block(proc, nullptr, "make.init.body");
+	done = ir_new_block(proc, nullptr, "make.init.done");
+
+	irValue *cond = ir_emit_comp(proc, Token_Lt, ir_emit_load(proc, index), count);
+	ir_emit_if(proc, cond, body, done);
+	ir_start_block(proc, body);
+
+	irValue *offset_ptr = ir_emit_ptr_offset(proc, ptr, ir_emit_load(proc, index));
+	ir_emit_zero_init(proc, offset_ptr);
+
+	ir_emit_increment(proc, index);
+
+	ir_emit_jump(proc, loop);
+	ir_start_block(proc, done);
+}
+
+
 irValue *ir_build_builtin_proc(irProcedure *proc, AstNode *expr, TypeAndValue tv, BuiltinProcId id) {
 irValue *ir_build_builtin_proc(irProcedure *proc, AstNode *expr, TypeAndValue tv, BuiltinProcId id) {
 	ast_node(ce, CallExpr, expr);
 	ast_node(ce, CallExpr, expr);
 
 
@@ -3952,8 +4017,12 @@ irValue *ir_build_builtin_proc(irProcedure *proc, AstNode *expr, TypeAndValue tv
 			irValue *call = ir_emit_global_call(proc, "alloc", args, 2);
 			irValue *call = ir_emit_global_call(proc, "alloc", args, 2);
 
 
 			irValue *ptr = ir_emit_conv(proc, call, elem_ptr_type);
 			irValue *ptr = ir_emit_conv(proc, call, elem_ptr_type);
-			irValue *slice = ir_add_local_generated(proc, type);
 
 
+			if (ir_type_has_default_values(elem_type)) {
+				ir_init_data_with_defaults(proc, ptr, count);
+			}
+
+			irValue *slice = ir_add_local_generated(proc, type);
 			ir_fill_slice(proc, slice, ptr, count, capacity);
 			ir_fill_slice(proc, slice, ptr, count, capacity);
 			return ir_emit_load(proc, slice);
 			return ir_emit_load(proc, slice);
 		} else if (is_type_dynamic_map(type)) {
 		} else if (is_type_dynamic_map(type)) {
@@ -3991,11 +4060,15 @@ irValue *ir_build_builtin_proc(irProcedure *proc, AstNode *expr, TypeAndValue tv
 			irValue **args = gb_alloc_array(a, irValue *, 5);
 			irValue **args = gb_alloc_array(a, irValue *, 5);
 			args[0] = ir_emit_conv(proc, array, t_rawptr);
 			args[0] = ir_emit_conv(proc, array, t_rawptr);
 			args[1] = ir_const_int(a, type_size_of(a, elem_type));
 			args[1] = ir_const_int(a, type_size_of(a, elem_type));
-			args[2] = ir_const_int(a, type_align_of(a, elem_type));;
+			args[2] = ir_const_int(a, type_align_of(a, elem_type));
 			args[3] = len;
 			args[3] = len;
 			args[4] = cap;
 			args[4] = cap;
 			ir_emit_global_call(proc, "__dynamic_array_make", args, 5);
 			ir_emit_global_call(proc, "__dynamic_array_make", args, 5);
 
 
+			if (ir_type_has_default_values(elem_type)) {
+				ir_init_data_with_defaults(proc, ir_dynamic_array_elem(proc, ir_emit_load(proc, array)), len);
+			}
+
 			return ir_emit_load(proc, array);
 			return ir_emit_load(proc, array);
 		}
 		}
 	} break;
 	} break;
@@ -5901,12 +5974,6 @@ void ir_build_when_stmt(irProcedure *proc, AstNodeWhenStmt *ws) {
 	}
 	}
 }
 }
 
 
-void ir_emit_increment(irProcedure *proc, irValue *addr) {
-	GB_ASSERT(is_type_pointer(ir_type(addr)));
-	Type *type = type_deref(ir_type(addr));
-	ir_emit_store(proc, addr, ir_emit_arith(proc, Token_Add, ir_emit_load(proc, addr), v_one, type));
-
-}
 
 
 
 
 void ir_build_range_indexed(irProcedure *proc, irValue *expr, Type *val_type, irValue *count_ptr,
 void ir_build_range_indexed(irProcedure *proc, irValue *expr, Type *val_type, irValue *count_ptr,

+ 101 - 36
src/ir_print.cpp

@@ -506,14 +506,22 @@ void ir_print_exact_value(irFileBuffer *f, irModule *m, ExactValue value, Type *
 		type = base_type(type);
 		type = base_type(type);
 		if (is_type_array(type)) {
 		if (is_type_array(type)) {
 			ast_node(cl, CompoundLit, value.value_compound);
 			ast_node(cl, CompoundLit, value.value_compound);
+
+			Type *elem_type = type->Array.elem;
 			isize elem_count = cl->elems.count;
 			isize elem_count = cl->elems.count;
+			bool has_defaults = ir_type_has_default_values(type);
 			if (elem_count == 0) {
 			if (elem_count == 0) {
-				ir_fprintf(f, "zeroinitializer");
+				if (!has_defaults) {
+					ir_fprintf(f, "zeroinitializer");
+				} else {
+					ir_fprintf(f, "[");
+
+					ir_fprintf(f, "]");
+				}
 				break;
 				break;
 			}
 			}
 
 
 			ir_fprintf(f, "[");
 			ir_fprintf(f, "[");
-			Type *elem_type = type->Array.elem;
 
 
 			for (isize i = 0; i < elem_count; i++) {
 			for (isize i = 0; i < elem_count; i++) {
 				if (i > 0) {
 				if (i > 0) {
@@ -575,7 +583,8 @@ void ir_print_exact_value(irFileBuffer *f, irModule *m, ExactValue value, Type *
 
 
 			ast_node(cl, CompoundLit, value.value_compound);
 			ast_node(cl, CompoundLit, value.value_compound);
 
 
-			if (cl->elems.count == 0) {
+			bool has_defaults = ir_type_has_default_values(type);
+			if (cl->elems.count == 0 && !has_defaults) {
 				ir_fprintf(f, "zeroinitializer");
 				ir_fprintf(f, "zeroinitializer");
 				break;
 				break;
 			}
 			}
@@ -583,39 +592,51 @@ void ir_print_exact_value(irFileBuffer *f, irModule *m, ExactValue value, Type *
 
 
 			isize value_count = type->Record.field_count;
 			isize value_count = type->Record.field_count;
 			ExactValue *values = gb_alloc_array(m->tmp_allocator, ExactValue, value_count);
 			ExactValue *values = gb_alloc_array(m->tmp_allocator, ExactValue, value_count);
+			bool *visited = gb_alloc_array(m->tmp_allocator, bool, value_count);
 
 
+			if (cl->elems.count > 0) {
+				if (cl->elems[0]->kind == AstNode_FieldValue) {
+					isize elem_count = cl->elems.count;
+					for (isize i = 0; i < elem_count; i++) {
+						ast_node(fv, FieldValue, cl->elems[i]);
+						String name = fv->field->Ident.token.string;
 
 
-			if (cl->elems[0]->kind == AstNode_FieldValue) {
-				isize elem_count = cl->elems.count;
-				for (isize i = 0; i < elem_count; i++) {
-					ast_node(fv, FieldValue, cl->elems[i]);
-					String name = fv->field->Ident.token.string;
-
-					TypeAndValue tav = type_and_value_of_expr(m->info, fv->value);
-					GB_ASSERT(tav.mode != Addressing_Invalid);
+						TypeAndValue tav = type_and_value_of_expr(m->info, fv->value);
+						GB_ASSERT(tav.mode != Addressing_Invalid);
 
 
-					Selection sel = lookup_field(m->allocator, type, name, false);
-					Entity *f = type->Record.fields[sel.index[0]];
+						Selection sel = lookup_field(m->allocator, type, name, false);
+						Entity *f = type->Record.fields[sel.index[0]];
 
 
-					values[f->Variable.field_index] = tav.value;
-				}
-			} else {
-				for (isize i = 0; i < value_count; i++) {
-					Entity *f = type->Record.fields_in_src_order[i];
-					TypeAndValue tav = type_and_value_of_expr(m->info, cl->elems[i]);
-					ExactValue val = {};
-					if (tav.mode != Addressing_Invalid) {
-						val = tav.value;
+						values[f->Variable.field_index] = tav.value;
+						visited[f->Variable.field_index] = true;
+					}
+				} else {
+					for (isize i = 0; i < value_count; i++) {
+						Entity *f = type->Record.fields[i];
+						TypeAndValue tav = type_and_value_of_expr(m->info, cl->elems[i]);
+						ExactValue val = {};
+						if (tav.mode != Addressing_Invalid) {
+							val = tav.value;
+						}
+						values[f->Variable.field_index] = val;
+						visited[f->Variable.field_index] = true;
 					}
 					}
-					values[f->Variable.field_index] = val;
 				}
 				}
 			}
 			}
 
 
+			for (isize i = 0; i < value_count; i++) {
+				if (visited[i]) continue;
+				Entity *f = type->Record.fields[i];
+				ExactValue v = {};
+				if (!f->Variable.default_is_nil) {
+					v = f->Variable.default_value;
+				}
+				values[i] = v;
+			}
 
 
 
 
-			if (type->Record.is_packed) {
-				ir_fprintf(f, "<");
-			}
+
+			if (type->Record.is_packed) ir_fprintf(f, "<");
 			ir_fprintf(f, "{");
 			ir_fprintf(f, "{");
 			if (type->Record.custom_align > 0) {
 			if (type->Record.custom_align > 0) {
 				ir_fprintf(f, "[0 x <%lld x i8>] zeroinitializer", cast(i64)type->Record.custom_align);
 				ir_fprintf(f, "[0 x <%lld x i8>] zeroinitializer", cast(i64)type->Record.custom_align);
@@ -626,9 +647,7 @@ void ir_print_exact_value(irFileBuffer *f, irModule *m, ExactValue value, Type *
 
 
 
 
 			for (isize i = 0; i < value_count; i++) {
 			for (isize i = 0; i < value_count; i++) {
-				if (i > 0) {
-					ir_fprintf(f, ", ");
-				}
+				if (i > 0) ir_fprintf(f, ", ");
 				Type *elem_type = type->Record.fields[i]->type;
 				Type *elem_type = type->Record.fields[i]->type;
 
 
 				ir_print_compound_element(f, m, values[i], elem_type);
 				ir_print_compound_element(f, m, values[i], elem_type);
@@ -636,9 +655,7 @@ void ir_print_exact_value(irFileBuffer *f, irModule *m, ExactValue value, Type *
 
 
 
 
 			ir_fprintf(f, "}");
 			ir_fprintf(f, "}");
-			if (type->Record.is_packed) {
-				ir_fprintf(f, ">");
-			}
+			if (type->Record.is_packed) ir_fprintf(f, ">");
 
 
 			gb_temp_arena_memory_end(tmp);
 			gb_temp_arena_memory_end(tmp);
 		} else {
 		} else {
@@ -647,10 +664,56 @@ void ir_print_exact_value(irFileBuffer *f, irModule *m, ExactValue value, Type *
 
 
 	} break;
 	} break;
 
 
-	default:
-		ir_fprintf(f, "zeroinitializer");
+	default: {
+		bool has_defaults = ir_type_has_default_values(type);
+		if (!has_defaults) {
+			ir_fprintf(f, "zeroinitializer");
+		} else {
+			if (is_type_struct(type)) {
+				i32 value_count = type->Record.field_count;
+				if (type->Record.is_packed) ir_fprintf(f, "<");
+				ir_fprintf(f, "{");
+				if (type->Record.custom_align > 0) {
+					ir_fprintf(f, "[0 x <%lld x i8>] zeroinitializer", cast(i64)type->Record.custom_align);
+					if (value_count > 0) {
+						ir_fprintf(f, ", ");
+					}
+				}
+
+				for (isize i = 0; i < value_count; i++) {
+					if (i > 0) ir_fprintf(f, ", ");
+					Entity *field = type->Record.fields[i];
+					ExactValue value = {};
+					if (!field->Variable.default_is_nil) {
+						value = field->Variable.default_value;
+					}
+					ir_print_compound_element(f, m, value, field->type);
+				}
+
+				ir_fprintf(f, "}");
+				if (type->Record.is_packed) ir_fprintf(f, ">");
+
+			} else if (is_type_array(type)) {
+				i64 count = type->Array.count;
+				if (count == 0) {
+					ir_fprintf(f, "zeroinitializer");
+				} else {
+					Type *elem = type->Array.elem;
+					ir_fprintf(f, "[");
+					for (i64 i = 0; i < count; i++) {
+						if (i > 0) ir_fprintf(f, ", ");
+						ir_print_type(f, m, elem);
+						ir_fprintf(f, " ");
+						ir_print_exact_value(f, m, empty_exact_value, elem);
+					}
+					ir_fprintf(f, "]");
+				}
+			} else {
+				GB_PANIC("Unknown type for default values");
+			}
+		}
 		// GB_PANIC("Invalid ExactValue: %d", value.kind);
 		// GB_PANIC("Invalid ExactValue: %d", value.kind);
-		break;
+	} break;
 	}
 	}
 }
 }
 
 
@@ -796,7 +859,9 @@ void ir_print_instr(irFileBuffer *f, irModule *m, irValue *value) {
 		Type *type = type_deref(ir_type(instr->ZeroInit.address));
 		Type *type = type_deref(ir_type(instr->ZeroInit.address));
 		ir_fprintf(f, "store ");
 		ir_fprintf(f, "store ");
 		ir_print_type(f, m, type);
 		ir_print_type(f, m, type);
-		ir_fprintf(f, " zeroinitializer, ");
+		ir_fprintf(f, " ");
+		ir_print_exact_value(f, m, empty_exact_value, type);
+		ir_fprintf(f, ", ");
 		ir_print_type(f, m, type);
 		ir_print_type(f, m, type);
 		ir_fprintf(f, "* %%%d\n", instr->ZeroInit.address->index);
 		ir_fprintf(f, "* %%%d\n", instr->ZeroInit.address->index);
 	} break;
 	} break;