Skip to content

Commit 0e812ed

Browse files
author
Connor Harris
committed
Okay so the issue was coming from LLVMConstNull. So i made a helper function to try and find the correct type and set it accordinally.
1 parent e05b068 commit 0e812ed

File tree

6 files changed

+45
-40
lines changed

6 files changed

+45
-40
lines changed

src/llvm/expr/defer.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,9 +130,10 @@ void generate_cleanup_blocks(CodeGenContext *ctx) {
130130
if (LLVMGetTypeKind(return_type) == LLVMVoidTypeKind) {
131131
LLVMBuildRetVoid(ctx->builder);
132132
} else {
133-
// Return default value for the type
134-
LLVMValueRef default_val = LLVMConstNull(return_type);
135-
LLVMBuildRet(ctx->builder, default_val);
133+
LLVMValueRef default_val = get_default_value(return_type);
134+
if (default_val) {
135+
LLVMBuildRet(ctx->builder, default_val);
136+
}
136137
}
137138
}
138139

src/llvm/expr/expr.c

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -118,11 +118,7 @@ LLVMValueRef codegen_expr_literal(CodeGenContext *ctx, AstNode *node) {
118118
}
119119

120120
case LITERAL_NULL:
121-
if (!ctx->common_types.i8_ptr) {
122-
fprintf(stderr, "Error: i8_ptr type not initialized\n");
123-
return NULL;
124-
}
125-
return LLVMConstNull(ctx->common_types.i8_ptr);
121+
return get_default_value(ctx->common_types.i8_ptr);
126122

127123
default:
128124
fprintf(stderr, "ERROR: Unknown literal type: %d\n",

src/llvm/llvm.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,7 @@ bool is_float_type(LLVMTypeRef type);
268268
bool pointer_type(LLVMTypeRef type);
269269
bool types_are_equal(LLVMTypeRef a, LLVMTypeRef b);
270270
bool needs_conversion(LLVMTypeRef from, LLVMTypeRef to);
271+
LLVMValueRef get_default_value(LLVMTypeRef type);
271272

272273
LLVMValueRef alloca_and_store(CodeGenContext *ctx, LLVMTypeRef type,
273274
LLVMValueRef value, const char *name);

src/llvm/stmt/stmt.c

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,19 +127,25 @@ LLVMValueRef codegen_stmt_var_decl(CodeGenContext *ctx, AstNode *node) {
127127
} else {
128128
fprintf(stderr,
129129
"Error: Global variable initializer must be constant\n");
130-
LLVMSetInitializer(var_ref, LLVMConstNull(var_type));
130+
LLVMValueRef def = get_default_value(var_type);
131+
if (def)
132+
LLVMSetInitializer(var_ref, def);
131133
}
132134
} else {
133135
LLVMBuildStore(ctx->builder, init_val, var_ref);
134136
}
135137
} else {
136138
if (ctx->current_function == NULL) {
137-
LLVMSetInitializer(var_ref, LLVMConstNull(var_type));
139+
LLVMValueRef def = get_default_value(var_type);
140+
if (def)
141+
LLVMSetInitializer(var_ref, def);
138142
}
139143
}
140144
} else {
141145
if (ctx->current_function == NULL) {
142-
LLVMSetInitializer(var_ref, LLVMConstNull(var_type));
146+
LLVMValueRef def = get_default_value(var_type);
147+
if (def)
148+
LLVMSetInitializer(var_ref, def);
143149
}
144150
}
145151

@@ -356,7 +362,11 @@ generate_body: {
356362
if (LLVMGetTypeKind(return_type) == LLVMVoidTypeKind) {
357363
LLVMBuildRetVoid(ctx->builder);
358364
} else {
359-
LLVMValueRef default_val = LLVMConstNull(return_type);
365+
LLVMValueRef default_val = get_default_value(return_type);
366+
if (!default_val) {
367+
fprintf(stderr, "Error: Cannot create default value for return type\n");
368+
return NULL;
369+
}
360370
LLVMBuildRet(ctx->builder, default_val);
361371
}
362372

src/llvm/struct/struct_helpers.c

Lines changed: 5 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ LLVMValueRef create_struct_zero_initializer(CodeGenContext *ctx, const char *str
4646
return NULL;
4747
}
4848

49-
return LLVMConstNull(struct_info->llvm_type);
49+
return get_default_value(struct_info->llvm_type);
5050
}
5151

5252
// Create a copy of a struct
@@ -235,33 +235,10 @@ LLVMValueRef initialize_struct_with_defaults(CodeGenContext *ctx, const char *st
235235

236236
// Initialize each field with appropriate default
237237
for (size_t i = 0; i < struct_info->field_count; i++) {
238-
LLVMValueRef default_value;
239-
LLVMTypeKind kind = LLVMGetTypeKind(struct_info->field_types[i]);
240-
241-
switch (kind) {
242-
case LLVMIntegerTypeKind:
243-
default_value = LLVMConstInt(struct_info->field_types[i], 0, false);
244-
break;
245-
case LLVMFloatTypeKind:
246-
default_value = LLVMConstReal(struct_info->field_types[i], 0.0);
247-
break;
248-
case LLVMDoubleTypeKind:
249-
default_value = LLVMConstReal(struct_info->field_types[i], 0.0);
250-
break;
251-
case LLVMPointerTypeKind:
252-
if (!struct_info->field_types[i]) {
253-
fprintf(stderr, "Error: field_types[%zu] is NULL\n", i);
254-
return NULL;
255-
}
256-
default_value = LLVMConstNull(struct_info->field_types[i]);
257-
break;
258-
default:
259-
if (!struct_info->field_types[i]) {
260-
fprintf(stderr, "Error: field_types[%zu] is NULL\n", i);
261-
return NULL;
262-
}
263-
default_value = LLVMConstNull(struct_info->field_types[i]);
264-
break;
238+
LLVMValueRef default_value = get_default_value(struct_info->field_types[i]);
239+
if (!default_value) {
240+
fprintf(stderr, "Error: Cannot create default value for field %zu\n", i);
241+
return NULL;
265242
}
266243

267244
LLVMValueRef field_ptr = LLVMBuildStructGEP2(

src/llvm/types/type_cache.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,26 @@ bool pointer_type(LLVMTypeRef type) {
8080

8181
bool types_are_equal(LLVMTypeRef a, LLVMTypeRef b) { return a == b; }
8282

83+
LLVMValueRef get_default_value(LLVMTypeRef type) {
84+
if (!type)
85+
return NULL;
86+
87+
switch (LLVMGetTypeKind(type)) {
88+
case LLVMIntegerTypeKind:
89+
case LLVMFloatTypeKind:
90+
case LLVMDoubleTypeKind:
91+
case LLVMStructTypeKind:
92+
case LLVMArrayTypeKind:
93+
return LLVMConstNull(type);
94+
95+
case LLVMPointerTypeKind:
96+
return LLVMConstPointerNull(type);
97+
98+
default:
99+
return NULL;
100+
}
101+
}
102+
83103
// Check if conversion is needed
84104
bool needs_conversion(LLVMTypeRef from, LLVMTypeRef to) {
85105
if (from == to)

0 commit comments

Comments
 (0)