Skip to content

Commit

Permalink
Sometimes generating introspection info would not be in the global sc…
Browse files Browse the repository at this point in the history
…ope causing a crash #1586.
  • Loading branch information
lerno committed Oct 31, 2024
1 parent 475972a commit cd4fd02
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
1 change: 1 addition & 0 deletions releasenotes.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
- Named vector component access would not fold at compile time. #1574
- `$define` would occasionally not properly evaluate declarations it encountered.
- Fixes with error handling recursive `@tag` #1583.
- Sometimes generating introspection info would not be in the global scope causing a crash #1586.

### Stdlib changes
- Remove unintended print of `char[]` as String
Expand Down
18 changes: 18 additions & 0 deletions src/compiler/llvm_codegen_type.c
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,10 @@ static inline LLVMValueRef llvm_generate_temp_introspection_global(GenContext *c
static inline LLVMValueRef llvm_generate_introspection_global(GenContext *c, LLVMValueRef original_global, Type *type, IntrospectType introspect_type,
Type *inner, size_t len, LLVMValueRef additional, bool is_external)
{
// Push the builder
void *builder = c->builder;
c->builder = c->global_builder;

if (original_global)
{
assert(type->backend_typeid);
Expand Down Expand Up @@ -478,6 +482,7 @@ static inline LLVMValueRef llvm_generate_introspection_global(GenContext *c, LLV
{
type->backend_typeid = LLVMBuildPtrToInt(c->builder, global_name, c->typeid_type, "");
}
c->builder = builder;
return type->backend_typeid;
}
static LLVMValueRef llvm_get_introspection_for_builtin_type(GenContext *c, Type *type, IntrospectType introspect_type, int bits)
Expand All @@ -488,6 +493,10 @@ static LLVMValueRef llvm_get_introspection_for_builtin_type(GenContext *c, Type

static LLVMValueRef llvm_get_introspection_for_enum(GenContext *c, Type *type)
{

void *builder = c->builder;
c->builder = c->global_builder;

Decl *decl = type->decl;
bool is_external = decl->unit->module != c->code_module;
bool is_dynamic = decl->is_dynamic;
Expand Down Expand Up @@ -563,11 +572,15 @@ static LLVMValueRef llvm_get_introspection_for_enum(GenContext *c, Type *type)
LLVMSetGlobalConstant(global_ref, true);
associated_value->backend_ref = global_ref;
}
c->builder = builder;
return val;
}

static LLVMValueRef llvm_get_introspection_for_struct_union(GenContext *c, Type *type)
{
void *builder = c->builder;
c->builder = c->global_builder;

Decl *decl = type->decl;
Decl **decls = decl->strukt.members;
LLVMValueRef ref = llvm_generate_temp_introspection_global(c, type);
Expand All @@ -579,13 +592,17 @@ static LLVMValueRef llvm_get_introspection_for_struct_union(GenContext *c, Type
}
}

c->builder = builder;
return llvm_generate_introspection_global(c, ref, type, decl->decl_kind == DECL_UNION ? INTROSPECT_TYPE_UNION : INTROSPECT_TYPE_STRUCT,
NULL,
vec_size(decls), NULL, false);
}

static LLVMValueRef llvm_get_introspection_for_fault(GenContext *c, Type *type)
{
void *builder = c->builder;
c->builder = c->global_builder;

Decl *decl = type->decl;
Decl **fault_vals = decl->enums.values;
unsigned elements = vec_size(fault_vals);
Expand Down Expand Up @@ -613,6 +630,7 @@ static LLVMValueRef llvm_get_introspection_for_fault(GenContext *c, Type *type)
{
values[i] = fault_vals[i]->backend_ref;
}
c->builder = builder;
return llvm_generate_introspection_global(c, ref, type, INTROSPECT_TYPE_FAULT, NULL, elements, NULL, false);
}

Expand Down
24 changes: 24 additions & 0 deletions test/test_suite/enumerations/introspection_data_error.c3t
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// #target: macos-x64
module boom;
enum Boom: int (String a) {
BOOM = {0}
}

module app;
import std::io, boom;
fn void! main(String[] args) {
io::printn(Boom.BOOM);
}
/* #expect: boom.ll




---------------------------------------------------> boom.ll

@.enum.BOOM = internal constant [5 x i8] c"BOOM\00", align 1
@"$ct.int" = linkonce global %.introspect { i8 2, i64 0, ptr null, i64 4, i64 0, i64 0, [0 x i64] zeroinitializer }, align 8
@"$ct.boom.Boom" = linkonce global { i8, i64, ptr, i64, i64, i64, [1 x %"char[]"] } { i8 8, i64 0, ptr null, i64 4, i64 ptrtoint (ptr @"$ct.int" to i64), i64 1, [1 x %"char[]"] [%"char[]" { ptr @.enum.BOOM, i64 4 }] }, align 8
@.__const_slice = private unnamed_addr global [1 x i8] zeroinitializer, align 1
@"boom.Boom$a" = linkonce constant [1 x %"char[]"] [%"char[]" { ptr @.__const_slice, i64 1 }], align 8

0 comments on commit cd4fd02

Please sign in to comment.