From fb6d80b0f65607a276db73b3a782ecfdb42cc74c Mon Sep 17 00:00:00 2001 From: Christoffer Lerno Date: Wed, 19 May 2021 17:27:59 +0200 Subject: [PATCH] Removed implicitly created modules. Fix classification of arrays in x64. Fix cast with direct-pair. With test cases. --- src/compiler/compiler.c | 19 +------------- src/compiler/compiler_internal.h | 2 +- src/compiler/llvm_codegen_c_abi_x64.c | 2 +- src/compiler/llvm_codegen_expr.c | 2 +- src/compiler/llvm_codegen_type.c | 2 ++ src/compiler/module.c | 13 ---------- test/test_suite/abi/union_x64.c3t | 36 +++++++++++++++++++++++++++ 7 files changed, 42 insertions(+), 34 deletions(-) create mode 100644 test/test_suite/abi/union_x64.c3t diff --git a/src/compiler/compiler.c b/src/compiler/compiler.c index 406838f28..169be79b8 100644 --- a/src/compiler/compiler.c +++ b/src/compiler/compiler.c @@ -419,17 +419,7 @@ Module *global_context_find_module(const char *name) Module *compiler_find_or_create_module(Path *module_name, TokenId *parameters) { Module *module = global_context_find_module(module_name->module); - if (module) - { - // We might have gotten an auto-generated module, if so - // update the path here. - if (module->name->span.loc.index == INVALID_TOKEN_ID.index && module_name->span.loc.index != INVALID_TOKEN_ID.index) - { - module->name = module_name; - module->parameters = parameters; - } - return module; - } + if (module) return module; DEBUG_LOG("Creating module %s.", module_name->module); // Set up the module. @@ -448,13 +438,6 @@ Module *compiler_find_or_create_module(Path *module_name, TokenId *parameters) vec_add(global_context.module_list, module); } - // Now find the possible parent array: - Path *parent_path = path_find_parent_path(NULL, module_name); - if (parent_path) - { - // Get the parent - compiler_find_or_create_module(parent_path, NULL); - } return module; } diff --git a/src/compiler/compiler_internal.h b/src/compiler/compiler_internal.h index 45e3a5f2f..32f6c5c88 100644 --- a/src/compiler/compiler_internal.h +++ b/src/compiler/compiler_internal.h @@ -1616,7 +1616,7 @@ Decl *module_find_symbol(Module *module, const char *symbol); bool parse_file(Context *context); Path *path_create_from_string(const char *string, size_t len, SourceSpan span); -Path *path_find_parent_path(Context *context, Path *path); +Path *path_find_parent_path(Path *path); const char *resolve_status_to_string(ResolveStatus status); diff --git a/src/compiler/llvm_codegen_c_abi_x64.c b/src/compiler/llvm_codegen_c_abi_x64.c index d36a04e03..ca7abc279 100644 --- a/src/compiler/llvm_codegen_c_abi_x64.c +++ b/src/compiler/llvm_codegen_c_abi_x64.c @@ -307,7 +307,7 @@ void x64_classify_array(Type *type, ByteSize offset_base, X64Class *current, X64 X64Class field_lo; X64Class field_hi; x64_classify(element, offset, &field_lo, &field_hi, named_arg); - offset_base += element_size; + offset += element_size; *lo_class = x64_merge(*lo_class, field_lo); *hi_class = x64_merge(*hi_class, field_hi); if (*lo_class == CLASS_MEMORY || *hi_class == CLASS_MEMORY) break; diff --git a/src/compiler/llvm_codegen_expr.c b/src/compiler/llvm_codegen_expr.c index 683fb37cf..7213b3805 100644 --- a/src/compiler/llvm_codegen_expr.c +++ b/src/compiler/llvm_codegen_expr.c @@ -2520,7 +2520,7 @@ void llvm_emit_parameter(GenContext *c, LLVMValueRef **args, ABIArgInfo *info, B LLVMTypeRef lo = llvm_abi_type(c, info->direct_pair.lo); LLVMTypeRef hi = llvm_abi_type(c, info->direct_pair.hi); LLVMTypeRef struct_type = llvm_get_coerce_type(c, info); - LLVMValueRef cast = LLVMBuildBitCast(c->builder, be_value->value, llvm_get_ptr_type(c, type), "casttemp"); + LLVMValueRef cast = LLVMBuildBitCast(c->builder, be_value->value, LLVMPointerType(struct_type, 0), "casttemp"); // Get the lo value. LLVMValueRef lo_ptr = LLVMBuildStructGEP2(c->builder, struct_type, cast, 0, "lo"); vec_add(*args, llvm_emit_load_aligned(c, lo, lo_ptr, llvm_abi_alignment(c, lo), "lo")); diff --git a/src/compiler/llvm_codegen_type.c b/src/compiler/llvm_codegen_type.c index dce37427a..79da7a647 100644 --- a/src/compiler/llvm_codegen_type.c +++ b/src/compiler/llvm_codegen_type.c @@ -174,6 +174,8 @@ static void param_expand(GenContext *context, LLVMTypeRef** params_ref, Type *ty ByteSize largest = 0; Type *largest_type = NULL; Decl **members = type->decl->strukt.members; + // Clang: Unions can be here only in degenerative cases - all the fields are same + // after flattening. Thus we have to use the "largest" field. VECEACH(members, i) { if (type_size(type) > largest) diff --git a/src/compiler/module.c b/src/compiler/module.c index 4433c4e86..fc239fb24 100644 --- a/src/compiler/module.c +++ b/src/compiler/module.c @@ -23,16 +23,3 @@ Path *path_create_from_string(const char *string, size_t len, SourceSpan span) } return path; } - -Path *path_find_parent_path(Context *context, Path *path) -{ - const char *last_scope_chars = strrchr(path->module, ':'); - // No parent - if (!last_scope_chars) return NULL; - - Path *parent_path = path_create_from_string(path->module, last_scope_chars - path->module - 1, INVALID_RANGE); - - assert(parent_path && "Didn't we pass in a TOKEN_IDENT? That's the only reason this could fail."); - - return parent_path; -} \ No newline at end of file diff --git a/test/test_suite/abi/union_x64.c3t b/test/test_suite/abi/union_x64.c3t new file mode 100644 index 000000000..4e0bc1ff5 --- /dev/null +++ b/test/test_suite/abi/union_x64.c3t @@ -0,0 +1,36 @@ +// #target: x64_darwin + +module unionx64; + +union Foo +{ + long a; + char[12] b; +} +extern func void hello2(Foo f); + +func void hello(Foo f) +{ + hello2(f); +} + +// #expect: unionx64.ll + +%unionx64.Foo = type { i64, [8 x i8] } + +declare void @hello2(i64, i64) #0 + +define void @unionx64.hello(i64 %0, i64 %1) + + %f = alloca %unionx64.Foo, align 8 + %pair = bitcast %unionx64.Foo* %f to { i64, i64 }* + %lo = getelementptr inbounds { i64, i64 }, { i64, i64 }* %pair, i32 0, i32 0 + store i64 %0, i64* %lo, align 8 + %hi = getelementptr inbounds { i64, i64 }, { i64, i64 }* %pair, i32 0, i32 1 + store i64 %1, i64* %hi, align 8 + %casttemp = bitcast %unionx64.Foo* %f to { i64, i64 }* + %lo1 = getelementptr inbounds { i64, i64 }, { i64, i64 }* %casttemp, i32 0, i32 0 + %lo2 = load i64, i64* %lo1, align 8 + %hi3 = getelementptr inbounds { i64, i64 }, { i64, i64 }* %casttemp, i32 0, i32 1 + %hi4 = load i64, i64* %hi3, align 8 + call void @hello2(i64 %lo2, i64 %hi4)