From e81e91be937a1094512cf65a86e28677dff35986 Mon Sep 17 00:00:00 2001 From: Christoffer Lerno Date: Fri, 13 Oct 2023 12:44:58 +0200 Subject: [PATCH] Fix void* <=> protocol casts. Fix of tests. --- src/compiler/sema_casts.c | 4 +- src/compiler/target.c | 18 ++++----- src/compiler/types.c | 15 ++++---- src/version.h | 2 +- .../abi/darwin_return_boolarray.c3t | 4 +- .../compile_time/ct_switch_top_level.c3t | 4 +- test/test_suite/dynamic/null_and_protocol.c3t | 37 +++++++++++++++++++ .../expressions/optional_ternary.c3t | 4 +- test/test_suite/statements/ranged_switch.c3t | 4 +- test/test_suite/statements/simple_do.c3t | 4 +- test/test_suite/statements/while_switch.c3t | 4 +- .../switch/switch_in_defer_macro.c3t | 4 +- 12 files changed, 69 insertions(+), 35 deletions(-) create mode 100644 test/test_suite/dynamic/null_and_protocol.c3t diff --git a/src/compiler/sema_casts.c b/src/compiler/sema_casts.c index 6d8340881..69fa2dcf7 100644 --- a/src/compiler/sema_casts.c +++ b/src/compiler/sema_casts.c @@ -1968,8 +1968,8 @@ CastRule cast_rules[CONV_LAST + 1][CONV_LAST + 1] = { {REXPL, _NO__, _NO__, _NO__, _NO__, _NO__, _NO__, RARVC, RARBS, RXXDI, RARAR, _NO__, _NO__, _NO__, _NO__, _NO__, _NO__, _NO__, _NO__, _NO__, _NO__, RVAFE}, // ARRAY {REXPL, _NO__, RSTST, RSTST, RSTST, RSTST, RSTST, RSTST, RSTST, RSTDI, RSTST, RSTST, RSTST, RSTST, RSTST, RSTST, RSTST, RSTST, RSTST, RSTST, RSTST, _NO__}, // STRUCT {REXPL, _NO__, _NO__, _NO__, _NO__, _NO__, _NO__, _NO__, _NO__, RXXDI, _NO__, _NO__, _NO__, _NO__, _NO__, _NO__, _NO__, _NO__, _NO__, _NO__, _NO__, _NO__}, // UNION - {REXPL, _NO__, REXPL, _NO__, _NO__, REXPL, _NO__, _NO__, _NO__, RXXDI, _NO__, _NO__, _NO__, _NA__, REXPL, _NO__, _NO__, _NO__, _NO__, REXPL, REXPL, _NO__}, // ANY - {REXPL, _NO__, REXPL, _NO__, _NO__, REXPL, _NO__, _NO__, _NO__, RXXDI, _NO__, _NO__, _NO__, ROKOK, RPCPC, _NO__, _NO__, _NO__, _NO__, REXPL, REXPL, _NO__}, // PROTOCOL + {REXPL, _NO__, REXPL, _NO__, _NO__, REXPL, _NO__, _NO__, _NO__, RXXDI, _NO__, _NO__, _NO__, _NA__, REXPL, _NO__, _NO__, _NO__, _NO__, ROKOK, REXPL, _NO__}, // ANY + {REXPL, _NO__, REXPL, _NO__, _NO__, REXPL, _NO__, _NO__, _NO__, RXXDI, _NO__, _NO__, _NO__, ROKOK, RPCPC, _NO__, _NO__, _NO__, _NO__, ROKOK, REXPL, _NO__}, // PROTOCOL {REXPL, _NO__, REXPL, RPTIN, _NO__, REXPL, _NO__, ROKOK, _NO__, RXXDI, _NO__, _NO__, _NO__, _NO__, _NO__, _NO__, _NO__, _NO__, ROKOK, REXPL, REXPL, _NO__}, // FAULT {REXPL, _NO__, _NO__, REXPL, _NO__, _NO__, _NO__, ROKOK, _NO__, RXXDI, _NO__, _NO__, _NO__, _NO__, _NO__, _NO__, _NO__, _NO__, _NO__, _NO__, _NO__, _NO__}, // ENUM {REXPL, _NO__, REXPL, RPTIN, _NO__, REXPL, _NO__, ROKOK, _NO__, RXXDI, _NO__, _NO__, _NO__, _NO__, _NO__, _NO__, _NO__, _NA__, _NO__, REXPL, REXPL, _NO__}, // TYPEID diff --git a/src/compiler/target.c b/src/compiler/target.c index 99543d196..4a61264ce 100644 --- a/src/compiler/target.c +++ b/src/compiler/target.c @@ -1672,12 +1672,12 @@ static bool arch_os_pic_default_forced(ArchType arch, OsType os) LLVMInitialize ## X ## TargetMC(); \ } while(0) -INLINE const char *llvm_macos_target_triple(void) +INLINE const char *llvm_macos_target_triple(const char *triple) { if (active_target.macos.min_version) { scratch_buffer_clear(); - scratch_buffer_append(platform_target.target_triple); + scratch_buffer_append(triple); scratch_buffer_append(active_target.macos.min_version); return scratch_buffer_to_string(); } @@ -1686,12 +1686,12 @@ INLINE const char *llvm_macos_target_triple(void) if (!mac_sdk) { scratch_buffer_clear(); - scratch_buffer_append(platform_target.target_triple); + scratch_buffer_append(triple); scratch_buffer_append("10.15.0"); return scratch_buffer_to_string(); } scratch_buffer_clear(); - scratch_buffer_append(platform_target.target_triple); + scratch_buffer_append(triple); scratch_buffer_printf("%d.%d.0", mac_sdk->macos_min_deploy_target.major, mac_sdk->macos_min_deploy_target.minor); return scratch_buffer_to_string(); } @@ -1735,12 +1735,7 @@ void *llvm_target_machine_create(void) } DEBUG_LOG("CPU: %s", platform_target.cpu); DEBUG_LOG("Features: %s", platform_target.features); - const char *target_triple = platform_target.target_triple; - if (platform_target.os == OS_TYPE_MACOSX) - { - target_triple = llvm_macos_target_triple(); - } - void *result = LLVMCreateTargetMachine(target, target_triple, + void *result = LLVMCreateTargetMachine(target, platform_target.target_triple, platform_target.cpu ? platform_target.cpu : "", platform_target.features ? platform_target.features : "", (LLVMCodeGenOptLevel)platform_target.llvm_opt_level, reloc_mode, LLVMCodeModelDefault); @@ -1950,10 +1945,11 @@ void target_setup(BuildTarget *target) DEBUG_LOG("Macos SDK: %s", sysroot); active_target.macos.sdk = macos_sysroot_sdk_information(sysroot); } + platform_target.target_triple = strdup(llvm_macos_target_triple(platform_target.target_triple)); + } assert(platform_target.reloc_model != RELOC_DEFAULT); - // TODO remove type_setup(&platform_target); diff --git a/src/compiler/types.c b/src/compiler/types.c index 12f99f2c8..41ca8a2e5 100644 --- a/src/compiler/types.c +++ b/src/compiler/types.c @@ -2059,6 +2059,7 @@ Type *type_find_max_type(Type *type, Type *other) other = temp; } + // The following relies on type kind ordering switch (type->type_kind) { case TYPE_INFERRED_ARRAY: @@ -2070,12 +2071,6 @@ Type *type_find_max_type(Type *type, Type *other) case TYPE_PROTOCOL: case TYPE_ANY: return NULL; - case TYPE_ANYPTR: - // any + protocol => any - return other->type_kind == TYPE_PROPTR ? type : NULL; - case TYPE_PROPTR: - // protocol + protocol => any - return other->type_kind == TYPE_PROPTR ? type_any : NULL; case TYPE_VOID: case TYPE_BOOL: case TYPE_TYPEINFO: @@ -2091,6 +2086,13 @@ Type *type_find_max_type(Type *type, Type *other) if (other->type_kind == TYPE_DISTINCT && type_is_float(other->decl->distinct->type)) return other; if (other->type_kind == TYPE_VECTOR) return other; return type_find_max_num_type(type, other); + case TYPE_ANYPTR: + // any + protocol => any + if (other == type_voidptr) return other; + return other->type_kind == TYPE_PROPTR ? type : NULL; + case TYPE_PROPTR: + // protocol + void* => void* + return other == type_voidptr ? type_voidptr : NULL; case TYPE_POINTER: if (type->pointer->type_kind == TYPE_ARRAY) { @@ -2121,7 +2123,6 @@ Type *type_find_max_type(Type *type, Type *other) type = type_decay_array_pointer(type); // And possibly the other pointer as well if (other->type_kind == TYPE_POINTER) other = type_decay_array_pointer(other); - return type_find_max_ptr_type(type, other); case TYPE_ENUM: // IMPROVE: should there be implicit conversion between one enum and the other in diff --git a/src/version.h b/src/version.h index 3a6ebdcc3..cb63efb5c 100644 --- a/src/version.h +++ b/src/version.h @@ -1 +1 @@ -#define COMPILER_VERSION "0.4.678" \ No newline at end of file +#define COMPILER_VERSION "0.4.679" \ No newline at end of file diff --git a/test/test_suite/abi/darwin_return_boolarray.c3t b/test/test_suite/abi/darwin_return_boolarray.c3t index 434848fd9..dbf7ace3c 100644 --- a/test/test_suite/abi/darwin_return_boolarray.c3t +++ b/test/test_suite/abi/darwin_return_boolarray.c3t @@ -6,8 +6,8 @@ fn bool[2] get() { return { false, false }; } /* #expect: test.ll source_filename = "test" -target datalayout = "e-m:o-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128" -target triple = "x86_64-apple-macosx" +target datalayout = "e-m:o-p270:32:32-p271:32 +target triple = "x86_64-apple @.__const = private unnamed_addr constant [2 x i8] zeroinitializer, align 1 define i16 @test.get() #0 { entry: diff --git a/test/test_suite/compile_time/ct_switch_top_level.c3t b/test/test_suite/compile_time/ct_switch_top_level.c3t index 4912a27b8..922d83d5d 100644 --- a/test/test_suite/compile_time/ct_switch_top_level.c3t +++ b/test/test_suite/compile_time/ct_switch_top_level.c3t @@ -31,8 +31,8 @@ fn int main() /* #expect: test.ll source_filename = "test" -target datalayout = "e-m:o-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128" -target triple = "x86_64-apple-macosx" +target datalayout = "e-m:o-p270:32:32-p271:32 +target triple = "x86_64-apple @test.oeoekgokege = local_unnamed_addr global i32 343432, align 4 @.str = private unnamed_addr constant [7 x i8] c"Hello\0A\00", align 1 diff --git a/test/test_suite/dynamic/null_and_protocol.c3t b/test/test_suite/dynamic/null_and_protocol.c3t new file mode 100644 index 000000000..fc41c4930 --- /dev/null +++ b/test/test_suite/dynamic/null_and_protocol.c3t @@ -0,0 +1,37 @@ +module test; +import std::io; +import std::time; + +protocol Test {} + +fn void main() +{ + Test* a; + bool x = a == null; + a = null; + any* z = a; + z = null; +} + +/* #expect: test.ll + +%"any*" = type { ptr, i64 } +@"$ct.void" = linkonce global %.introspect { i8 0, i64 0, ptr null, i64 1, i64 0, i64 0, [0 x i64] zeroinitializer }, align 8 + +define void @test.main() #0 { +entry: + %a = alloca %"any*", align 8 + %x = alloca i8, align 1 + %z = alloca %"any*", align 8 + store %"any*" zeroinitializer, ptr %a, align 8 + %0 = getelementptr inbounds %"any*", ptr %a, i32 0, i32 0 + %1 = load ptr, ptr %0, align 8 + %eq = icmp eq ptr %1, null + %2 = zext i1 %eq to i8 + store i8 %2, ptr %x, align 1 + store %"any*" { ptr null, i64 ptrtoint (ptr @"$ct.void" to i64) }, ptr %a, align 8 + %3 = load %"any*", ptr %a, align 8 + store %"any*" %3, ptr %z, align 8 + store %"any*" { ptr null, i64 ptrtoint (ptr @"$ct.void" to i64) }, ptr %z, align 8 + ret void +} \ No newline at end of file diff --git a/test/test_suite/expressions/optional_ternary.c3t b/test/test_suite/expressions/optional_ternary.c3t index 497a8fd2e..15823f281 100644 --- a/test/test_suite/expressions/optional_ternary.c3t +++ b/test/test_suite/expressions/optional_ternary.c3t @@ -57,8 +57,8 @@ fn void main() /* expect: test.ll source_filename = "test" -target datalayout = "e-m:o-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128" -target triple = "x86_64-apple-macosx" +target datalayout = "e-m:o-p270:32:32-p271:32 +target triple = "x86_64-apple %.fault = type { i64, %"char[]" } %"char[]" = type { i8*, i64 } diff --git a/test/test_suite/statements/ranged_switch.c3t b/test/test_suite/statements/ranged_switch.c3t index 424a8fbbc..72dc3528e 100644 --- a/test/test_suite/statements/ranged_switch.c3t +++ b/test/test_suite/statements/ranged_switch.c3t @@ -49,8 +49,8 @@ fn void main() /* #expect: foo.ll source_filename = "foo" -target datalayout = "e-m:o-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128" -target triple = "x86_64-apple-macosx" +target datalayout = "e-m:o-p270:32:32-p271:32 +target triple = "x86_64-apple @.str = private unnamed_addr constant [5 x i8] c"1-3\0A\00", align 1 @.str.1 = private unnamed_addr constant [10 x i8] c"7-277 %d\0A\00", align 1 diff --git a/test/test_suite/statements/simple_do.c3t b/test/test_suite/statements/simple_do.c3t index 04befab9f..e1e5345b2 100644 --- a/test/test_suite/statements/simple_do.c3t +++ b/test/test_suite/statements/simple_do.c3t @@ -27,8 +27,8 @@ fn void main() /* #expect: foo.ll source_filename = "foo" -target datalayout = "e-m:o-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128" -target triple = "x86_64-apple-macosx" +target datalayout = "e-m:o-p270:32:32-p271:32 +target triple = "x86_64-apple @test.x = internal unnamed_addr global i32 0, align 4 @.str = private unnamed_addr constant [4 x i8] c"%d\0A\00", align 1 diff --git a/test/test_suite/statements/while_switch.c3t b/test/test_suite/statements/while_switch.c3t index 1996fe24f..80441557f 100644 --- a/test/test_suite/statements/while_switch.c3t +++ b/test/test_suite/statements/while_switch.c3t @@ -31,8 +31,8 @@ fn int main() /* #expect: test.ll source_filename = "test" -target datalayout = "e-m:o-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128" -target triple = "x86_64-apple-macosx" +target datalayout = "e-m:o-p270:32:32-p271:32 +target triple = "x86_64-apple @.str = private unnamed_addr constant [2 x i8] c"3\00", align 1 @.str.1 = private unnamed_addr constant [2 x i8] c"4\00", align 1 diff --git a/test/test_suite/switch/switch_in_defer_macro.c3t b/test/test_suite/switch/switch_in_defer_macro.c3t index fa9dcccf0..3f032e048 100644 --- a/test/test_suite/switch/switch_in_defer_macro.c3t +++ b/test/test_suite/switch/switch_in_defer_macro.c3t @@ -682,8 +682,8 @@ fn void test() ; ModuleID = 'lexer_test' source_filename = "lexer_test" -target datalayout = "e-m:o-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128" -target triple = "x86_64-apple-macosx" +target datalayout = "e-m:o-p270:32:32-p271:32 +target triple = "x86_64-apple %.introspect = type { i8, i64, ptr, i64, i64, i64, [0 x i64] } %"char[]" = type { ptr, i64 } %"UintTest[]" = type { ptr, i64 }