From 8008fb2c184d569b18672850ccc143d466207c9b Mon Sep 17 00:00:00 2001 From: Christoffer Lerno Date: Thu, 15 Dec 2022 23:36:52 +0100 Subject: [PATCH] Do not auto-deref subscripting. --- CMakeLists.txt | 23 +++---- src/compiler/linker.c | 2 +- src/compiler/sema_expr.c | 1 - src/version.h | 2 +- .../pointers/array_pointer_decay.c3t | 43 ++++++------ .../pointers/array_pointer_decay.c3t | 66 +++++++++---------- 6 files changed, 69 insertions(+), 68 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 379316dca..27ca68f2c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -84,6 +84,7 @@ message(STATUS "Found LLVM ${LLVM_PACKAGE_VERSION}") message(STATUS "Using LLVMConfig.cmake in: ${LLVM_DIR}") message(STATUS "Libraries located in: ${LLVM_LIBRARY_DIRS}") include_directories(${LLVM_INCLUDE_DIRS}) +link_directories(${LLVM_LIBRARY_DIRS}) add_definitions(${LLVM_DEFINITIONS}) set(LLVM_LINK_COMPONENTS @@ -129,22 +130,22 @@ message(STATUS "using find_library") if(C3_USE_TB) find_library(TB_LIB NAMES tildebackend.a tildebackend.lib PATHS ${CMAKE_SOURCE_DIR}/tb/) endif() -find_library(LLD_COFF NAMES lldCOFF.lib lldCOFF.a liblldCOFF.a PATHS ${LLVM_LIBRARY_DIRS}) -find_library(LLD_COMMON NAMES lldCommon.lib lldCommon.a liblldCommon.a PATHS ${LLVM_LIBRARY_DIRS}) -find_library(LLD_ELF NAMES lldELF.lib lldELF.a liblldELF.a PATHS ${LLVM_LIBRARY_DIRS}) +find_library(LLD_COFF NAMES lldCOFF.lib lldCOFF.a liblldCOFF.a PATHS ${LLVM_LIBRARY_DIRS} NO_DEFAULT_PATH) +find_library(LLD_COMMON NAMES lldCommon.lib lldCommon.a liblldCommon.a PATHS ${LLVM_LIBRARY_DIRS} NO_DEFAULT_PATH) +find_library(LLD_ELF NAMES lldELF.lib lldELF.a liblldELF.a PATHS ${LLVM_LIBRARY_DIRS} NO_DEFAULT_PATH) if (${LLVM_PACKAGE_VERSION} VERSION_LESS 14) - find_library(LLD_MACHO NAMES lldMachO2.lib lldMachO2.a liblldMachO2.a PATHS ${LLVM_LIBRARY_DIRS}) + find_library(LLD_MACHO NAMES lldMachO2.lib lldMachO2.a liblldMachO2.a PATHS ${LLVM_LIBRARY_DIRS} NO_DEFAULT_PATH) else () - find_library(LLD_MACHO NAMES lldMachO.lib lldMachO.a liblldMachO.a PATHS ${LLVM_LIBRARY_DIRS}) + find_library(LLD_MACHO NAMES lldMachO.lib lldMachO.a liblldMachO.a PATHS ${LLVM_LIBRARY_DIRS} NO_DEFAULT_PATH) endif () -find_library(LLD_MINGW NAMES lldMinGW.lib lldMinGW.a liblldMinGW.a PATHS ${LLVM_LIBRARY_DIRS}) -find_library(LLD_WASM NAMES lldWasm.lib lldWasm.a liblldWasm.a PATHS ${LLVM_LIBRARY_DIRS}) +find_library(LLD_MINGW NAMES lldMinGW.lib lldMinGW.a liblldMinGW.a PATHS ${LLVM_LIBRARY_DIRS} NO_DEFAULT_PATH) +find_library(LLD_WASM NAMES lldWasm.lib lldWasm.a liblldWasm.a PATHS ${LLVM_LIBRARY_DIRS} NO_DEFAULT_PATH) if (${LLVM_PACKAGE_VERSION} VERSION_LESS 14) - find_library(LLD_CORE NAMES lldCore.lib lldCore.a liblldCore.a PATHS ${LLVM_LIBRARY_DIRS}) - find_library(LLD_DRIVER NAMES lldDriver.lib lldDriver.a liblldDriver.a PATHS ${LLVM_LIBRARY_DIRS}) - find_library(LLD_READER_WRITER NAMES lldReaderWriter.lib lldReaderWriter.a liblldReaderWriter.a PATHS ${LLVM_LIBRARY_DIRS}) - find_library(LLD_YAML NAMES lldYAML.lib lldYAML.a liblldYAML.a PATHS ${LLVM_LIBRARY_DIRS}) + find_library(LLD_CORE NAMES lldCore.lib lldCore.a liblldCore.a PATHS ${LLVM_LIBRARY_DIRS} NO_DEFAULT_PATH) + find_library(LLD_DRIVER NAMES lldDriver.lib lldDriver.a liblldDriver.a PATHS ${LLVM_LIBRARY_DIRS} NO_DEFAULT_PATH) + find_library(LLD_READER_WRITER NAMES lldReaderWriter.lib lldReaderWriter.a liblldReaderWriter.a PATHS ${LLVM_LIBRARY_DIRS} NO_DEFAULT_PATH) + find_library(LLD_YAML NAMES lldYAML.lib lldYAML.a liblldYAML.a PATHS ${LLVM_LIBRARY_DIRS} NO_DEFAULT_PATH) endif () set(lld_libs diff --git a/src/compiler/linker.c b/src/compiler/linker.c index 33d4e7404..7af747a1e 100644 --- a/src/compiler/linker.c +++ b/src/compiler/linker.c @@ -301,7 +301,7 @@ static void linker_setup_macos(const char ***args_ref, LinkerType linker_type) if (is_pie(platform_target.reloc_model)) add_arg("-pie"); add_arg("-platform_version"); add_arg("macos"); - add_arg(str_printf("%d.0.0", mac_sdk->macos_deploy_target.major)); + add_arg(str_printf("%d.%d.0", mac_sdk->macos_min_deploy_target.major, mac_sdk->macos_min_deploy_target.minor)); add_arg(str_printf("%d.%d", mac_sdk->macos_deploy_target.major, mac_sdk->macos_deploy_target.minor)); } diff --git a/src/compiler/sema_expr.c b/src/compiler/sema_expr.c index efc53fa42..23867741e 100644 --- a/src/compiler/sema_expr.c +++ b/src/compiler/sema_expr.c @@ -2278,7 +2278,6 @@ static inline bool sema_expr_analyse_subscript(SemaContext *context, Expr *expr, // 1. Evaluate the expression to index. Expr *subscripted = exprptr(expr->subscript_expr.expr); if (!sema_analyse_expr_lvalue_fold_const(context, subscripted)) return false; - sema_subscript_deref_array_pointers(subscripted); // 2. Evaluate the index. Expr *index = exprptr(expr->subscript_expr.range.start); diff --git a/src/version.h b/src/version.h index 93a40ed90..2a2d89181 100644 --- a/src/version.h +++ b/src/version.h @@ -1 +1 @@ -#define COMPILER_VERSION "0.3.120" \ No newline at end of file +#define COMPILER_VERSION "0.3.121" \ No newline at end of file diff --git a/test/test_suite/pointers/array_pointer_decay.c3t b/test/test_suite/pointers/array_pointer_decay.c3t index b0000123a..377c1c4b3 100644 --- a/test/test_suite/pointers/array_pointer_decay.c3t +++ b/test/test_suite/pointers/array_pointer_decay.c3t @@ -8,14 +8,14 @@ fn void main() int[3]* y = &x; int* z = y; int[] sub = y; - int y1 = y[1]; + int[3] y1 = y[1]; int z1 = z[1]; int* xx = &x + 1; int[3]* yy = (int[3]*)(xx); int* zz = yy - 1; - printf("%p = %p = %p, %p = %p\n", y, z, zz, &y[1], xx); + printf("%p = %p = %p, %p = %p\n", y, z, zz, &(*y)[1], xx); x[1] = 123; - printf("%d = %d\n", (&x)[1], y[1]); + printf("%d = %d\n", x[1], z[1]); } /* #expect: foo.ll @@ -26,7 +26,7 @@ entry: %y = alloca [3 x i32]*, align 8 %z = alloca i32*, align 8 %sub = alloca %"int[]", align 8 - %y1 = alloca i32, align 4 + %y1 = alloca [3 x i32], align 4 %z1 = alloca i32, align 4 %xx = alloca i32*, align 8 %yy = alloca [3 x i32]*, align 8 @@ -47,23 +47,24 @@ entry: %7 = insertvalue %"int[]" %6, i64 3, 1 store %"int[]" %7, %"int[]"* %sub, align 8 %8 = load [3 x i32]*, [3 x i32]** %y, align 8 - %9 = getelementptr inbounds [3 x i32], [3 x i32]* %8, i64 0, i64 1 - %10 = load i32, i32* %9, align 4 - store i32 %10, i32* %y1, align 4 + %ptroffset = getelementptr inbounds [3 x i32], [3 x i32]* %8, i64 1 + %9 = bitcast [3 x i32]* %y1 to i8* + %10 = bitcast [3 x i32]* %ptroffset to i8* + call void @llvm.memcpy.p0i8.p0i8.i32(i8* align 4 %9, i8* align 4 %10, i32 12, i1 false) %11 = load i32*, i32** %z, align 8 - %ptroffset = getelementptr inbounds i32, i32* %11, i64 1 - %12 = load i32, i32* %ptroffset, align 4 + %ptroffset1 = getelementptr inbounds i32, i32* %11, i64 1 + %12 = load i32, i32* %ptroffset1, align 4 store i32 %12, i32* %z1, align 4 - %ptrptr1 = bitcast [3 x i32]* %x to i32* - %ptroffset2 = getelementptr i32, i32* %ptrptr1, i64 1 - store i32* %ptroffset2, i32** %xx, align 8 + %ptrptr2 = bitcast [3 x i32]* %x to i32* + %ptroffset3 = getelementptr i32, i32* %ptrptr2, i64 1 + store i32* %ptroffset3, i32** %xx, align 8 %13 = load i32*, i32** %xx, align 8 - %ptrptr3 = bitcast i32* %13 to [3 x i32]* - store [3 x i32]* %ptrptr3, [3 x i32]** %yy, align 8 + %ptrptr4 = bitcast i32* %13 to [3 x i32]* + store [3 x i32]* %ptrptr4, [3 x i32]** %yy, align 8 %14 = load [3 x i32]*, [3 x i32]** %yy, align 8 - %ptrptr4 = bitcast [3 x i32]* %14 to i32* - %ptroffset5 = getelementptr i32, i32* %ptrptr4, i64 -1 - store i32* %ptroffset5, i32** %zz, align 8 + %ptrptr5 = bitcast [3 x i32]* %14 to i32* + %ptroffset6 = getelementptr i32, i32* %ptrptr5, i64 -1 + store i32* %ptroffset6, i32** %zz, align 8 %15 = load [3 x i32]*, [3 x i32]** %y, align 8 %16 = load i32*, i32** %z, align 8 %17 = load i32*, i32** %zz, align 8 @@ -75,9 +76,9 @@ entry: store i32 123, i32* %21, align 4 %22 = getelementptr inbounds [3 x i32], [3 x i32]* %x, i64 0, i64 1 %23 = load i32, i32* %22, align 4 - %24 = load [3 x i32]*, [3 x i32]** %y, align 8 - %25 = getelementptr inbounds [3 x i32], [3 x i32]* %24, i64 0, i64 1 - %26 = load i32, i32* %25, align 4 - call void (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i32 0, i32 0), i32 %23, i32 %26) + %24 = load i32*, i32** %z, align 8 + %ptroffset7 = getelementptr inbounds i32, i32* %24, i64 1 + %25 = load i32, i32* %ptroffset7, align 4 + call void (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i32 0, i32 0), i32 %23, i32 %25) ret void } \ No newline at end of file diff --git a/test/test_suite2/pointers/array_pointer_decay.c3t b/test/test_suite2/pointers/array_pointer_decay.c3t index 50ae346ba..2cf06ed23 100644 --- a/test/test_suite2/pointers/array_pointer_decay.c3t +++ b/test/test_suite2/pointers/array_pointer_decay.c3t @@ -8,16 +8,17 @@ fn void main() int[3]* y = &x; int* z = y; int[] sub = y; - int y1 = y[1]; + int[3] y1 = y[1]; int z1 = z[1]; int* xx = &x + 1; int[3]* yy = (int[3]*)(xx); int* zz = yy - 1; - printf("%p = %p = %p, %p = %p\n", y, z, zz, &y[1], xx); + printf("%p = %p = %p, %p = %p\n", y, z, zz, &(*y)[1], xx); x[1] = 123; - printf("%d = %d\n", (&x)[1], y[1]); + printf("%d = %d\n", x[1], z[1]); } + /* #expect: foo.ll define void @foo_main() #0 { @@ -26,7 +27,7 @@ entry: %y = alloca ptr, align 8 %z = alloca ptr, align 8 %sub = alloca %"int[]", align 8 - %y1 = alloca i32, align 4 + %y1 = alloca [3 x i32], align 4 %z1 = alloca i32, align 4 %xx = alloca ptr, align 8 %yy = alloca ptr, align 8 @@ -45,34 +46,33 @@ entry: %6 = insertvalue %"int[]" %5, i64 3, 1 store %"int[]" %6, ptr %sub, align 8 %7 = load ptr, ptr %y, align 8 - %8 = getelementptr inbounds [3 x i32], ptr %7, i64 0, i64 1 - %9 = load i32, ptr %8, align 4 - store i32 %9, ptr %y1, align 4 - %10 = load ptr, ptr %z, align 8 - %ptroffset = getelementptr inbounds i32, ptr %10, i64 1 - %11 = load i32, ptr %ptroffset, align 4 - store i32 %11, ptr %z1, align 4 - %ptroffset1 = getelementptr i32, ptr %x, i64 1 - store ptr %ptroffset1, ptr %xx, align 8 - %12 = load ptr, ptr %xx, align 8 - store ptr %12, ptr %yy, align 8 - %13 = load ptr, ptr %yy, align 8 - %ptroffset2 = getelementptr i32, ptr %13, i64 -1 - store ptr %ptroffset2, ptr %zz, align 8 - %14 = load ptr, ptr %y, align 8 - %15 = load ptr, ptr %z, align 8 - %16 = load ptr, ptr %zz, align 8 - %17 = load ptr, ptr %y, align 8 - %18 = getelementptr inbounds [3 x i32], ptr %17, i64 0, i64 1 - %19 = load ptr, ptr %xx, align 8 - call void (ptr, ...) @printf(ptr @.str, ptr %14, ptr %15, ptr %16, ptr %18, ptr %19) - %20 = getelementptr inbounds [3 x i32], ptr %x, i64 0, i64 1 - store i32 123, ptr %20, align 4 - %21 = getelementptr inbounds [3 x i32], ptr %x, i64 0, i64 1 - %22 = load i32, ptr %21, align 4 - %23 = load ptr, ptr %y, align 8 - %24 = getelementptr inbounds [3 x i32], ptr %23, i64 0, i64 1 - %25 = load i32, ptr %24, align 4 - call void (ptr, ...) @printf(ptr @.str.1, i32 %22, i32 %25) + %ptroffset = getelementptr inbounds [3 x i32], ptr %7, i64 1 + call void @llvm.memcpy.p0.p0.i32(ptr align 4 %y1, ptr align 4 %ptroffset, i32 12, i1 false) + %8 = load ptr, ptr %z, align 8 + %ptroffset1 = getelementptr inbounds i32, ptr %8, i64 1 + %9 = load i32, ptr %ptroffset1, align 4 + store i32 %9, ptr %z1, align 4 + %ptroffset2 = getelementptr i32, ptr %x, i64 1 + store ptr %ptroffset2, ptr %xx, align 8 + %10 = load ptr, ptr %xx, align 8 + store ptr %10, ptr %yy, align 8 + %11 = load ptr, ptr %yy, align 8 + %ptroffset3 = getelementptr i32, ptr %11, i64 -1 + store ptr %ptroffset3, ptr %zz, align 8 + %12 = load ptr, ptr %y, align 8 + %13 = load ptr, ptr %z, align 8 + %14 = load ptr, ptr %zz, align 8 + %15 = load ptr, ptr %y, align 8 + %16 = getelementptr inbounds [3 x i32], ptr %15, i64 0, i64 1 + %17 = load ptr, ptr %xx, align 8 + call void (ptr, ...) @printf(ptr @.str, ptr %12, ptr %13, ptr %14, ptr %16, ptr %17) + %18 = getelementptr inbounds [3 x i32], ptr %x, i64 0, i64 1 + store i32 123, ptr %18, align 4 + %19 = getelementptr inbounds [3 x i32], ptr %x, i64 0, i64 1 + %20 = load i32, ptr %19, align 4 + %21 = load ptr, ptr %z, align 8 + %ptroffset4 = getelementptr inbounds i32, ptr %21, i64 1 + %22 = load i32, ptr %ptroffset4, align 4 + call void (ptr, ...) @printf(ptr @.str.1, i32 %20, i32 %22) ret void } \ No newline at end of file