From 4bc47a195bedcd72a1bf921e5ad3676b2dd7aaf8 Mon Sep 17 00:00:00 2001 From: Christoffer Lerno Date: Thu, 9 Dec 2021 00:49:50 +0100 Subject: [PATCH] Introduce array pointer decay. --- src/compiler/sema_expr.c | 47 +++++++++++ src/version.h | 2 +- .../pointers/array_pointer_decay.c3t | 83 +++++++++++++++++++ 3 files changed, 131 insertions(+), 1 deletion(-) create mode 100644 test/test_suite/pointers/array_pointer_decay.c3t diff --git a/src/compiler/sema_expr.c b/src/compiler/sema_expr.c index 07d221e27..9605903b1 100644 --- a/src/compiler/sema_expr.c +++ b/src/compiler/sema_expr.c @@ -24,6 +24,21 @@ static inline void expr_set_as_const_list(Expr *expr, ConstInitializer *list) expr->const_expr.list = list; } +static bool sema_decay_array_pointers(Expr *expr) +{ + Type *expr_type = expr->type->canonical; + if (expr_type->type_kind != TYPE_POINTER) return true; + switch (expr_type->pointer->type_kind) + { + case TYPE_ARRAY: + return cast_implicit(expr, type_get_ptr(expr_type->pointer->array.base)); + case TYPE_VECTOR: + return cast_implicit(expr, type_get_ptr(expr_type->pointer->vector.base)); + default: + return true; + } +} + int BINOP_PREC_REQ[BINARYOP_LAST + 1] = { // bitwise operations @@ -2245,6 +2260,22 @@ static inline bool sema_expr_analyse_call(Context *context, Expr *expr) return sema_expr_analyse_general_call(context, expr, decl, struct_var, macro, failable); } +static void sema_deref_array_pointers(Expr *expr) +{ + Type *expr_type = expr->type->canonical; + if (expr_type->type_kind == TYPE_POINTER) + { + switch (expr_type->pointer->type_kind) + { + case TYPE_ARRAY: + case TYPE_VECTOR: + expr_insert_deref(expr); + break; + default: + break; + } + } +} static bool expr_check_index_in_range(Context *context, Type *type, Expr *index_expr, bool end_index, bool from_end) { @@ -2342,6 +2373,7 @@ static inline bool sema_expr_analyse_subscript(Context *context, Expr *expr, boo // 1. Evaluate the expression to index. Expr *subscripted = expr->subscript_expr.expr; if (!sema_analyse_expr_lvalue(context, subscripted)) return false; + sema_deref_array_pointers(subscripted); // 2. Evaluate the index. Expr *index = expr->subscript_expr.index; @@ -2351,6 +2383,7 @@ static inline bool sema_expr_analyse_subscript(Context *context, Expr *expr, boo bool failable = IS_FAILABLE(subscripted); Type *underlying_type = type_flatten(subscripted->type); + Type *current_type = underlying_type; Expr *current_expr = subscripted; @@ -4314,6 +4347,9 @@ static bool sema_expr_analyse_add_sub_assign(Context *context, Expr *expr, Expr if (left_type_canonical->type_kind == TYPE_POINTER) { + if (!sema_decay_array_pointers(left)) return false; + expr->type = left->type; + // 7. Finally, check that the right side is indeed an integer. if (!type_is_integer(right->type->canonical)) { @@ -4430,9 +4466,15 @@ static bool sema_expr_analyse_sub(Context *context, Expr *expr, Expr *left, Expr // 2. Handle the ptr - x and ptr - other_pointer if (left_type->type_kind == TYPE_POINTER) { + if (!sema_decay_array_pointers(left)) return false; + left_type = type_no_fail(left->type)->canonical; + // 3. ptr - other pointer if (right_type->type_kind == TYPE_POINTER) { + if (!sema_decay_array_pointers(right)) return false; + right_type = type_no_fail(right->type)->canonical; + // 3a. Require that both types are the same. unify_voidptr(left, right, &left_type, &right_type); if (left_type != right_type) @@ -4544,6 +4586,8 @@ static bool sema_expr_analyse_add(Context *context, Expr *expr, Expr *left, Expr // so check if we want to do the normal pointer add special handling. if (left_type->type_kind == TYPE_POINTER) { + if (!sema_decay_array_pointers(left)) return false; + // 3a. Check that the other side is an integer of some sort. if (!type_is_integer(right_type)) { @@ -5045,6 +5089,7 @@ static bool sema_expr_analyse_comp(Context *context, Expr *expr, Expr *left, Exp } if (type_flatten(max)->type_kind == TYPE_POINTER) { + // Only comparisons between the same type is allowed. Subtypes not allowed. if (left_type != right_type && left_type != type_voidptr && right_type != type_voidptr) { @@ -5475,6 +5520,8 @@ static inline bool sema_expr_analyse_incdec(Context *context, Expr *expr) return false; } + if (!sema_decay_array_pointers(inner)) return false; + // 6. Done, the result is same as the inner type. expr->type = inner->type; return true; diff --git a/src/version.h b/src/version.h index fdca69187..16c426931 100644 --- a/src/version.h +++ b/src/version.h @@ -1 +1 @@ -#define COMPILER_VERSION "PRE.4" \ No newline at end of file +#define COMPILER_VERSION "PRE.5" \ 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 new file mode 100644 index 000000000..adae1464c --- /dev/null +++ b/test/test_suite/pointers/array_pointer_decay.c3t @@ -0,0 +1,83 @@ +// #target: x64-darwin +module foo; +extern fn void printf(char*, ...); + +fn void main() +{ + int[3] x; + int[3]* y = &x; + int* z = y; + int[] sub = y; + int 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); + x[1] = 123; + printf("%d = %d\n", (&x)[1], y[1]); +} + +/* #expect: foo.ll + +define void @main() #0 { +entry: + %x = alloca [3 x i32], align 4 + %y = alloca [3 x i32]*, align 8 + %z = alloca i32*, align 8 + %sub = alloca %"int[]", align 8 + %y1 = alloca i32, align 4 + %z1 = alloca i32, align 4 + %xx = alloca i32*, align 8 + %yy = alloca [3 x i32]*, align 8 + %zz = alloca i32*, align 8 + %0 = getelementptr inbounds [3 x i32], [3 x i32]* %x, i64 0, i64 0 + store i32 0, i32* %0, align 4 + %1 = getelementptr inbounds [3 x i32], [3 x i32]* %x, i64 0, i64 1 + store i32 0, i32* %1, align 4 + %2 = getelementptr inbounds [3 x i32], [3 x i32]* %x, i64 0, i64 2 + store i32 0, i32* %2, align 4 + store [3 x i32]* %x, [3 x i32]** %y, align 8 + %3 = load [3 x i32]*, [3 x i32]** %y, align 8 + %ptrptr = bitcast [3 x i32]* %3 to i32* + store i32* %ptrptr, i32** %z, align 8 + %4 = load [3 x i32]*, [3 x i32]** %y, align 8 + %5 = bitcast [3 x i32]* %4 to i32* + %6 = insertvalue %"int[]" undef, i32* %5, 0 + %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 + %11 = load i32*, i32** %z, align 8 + %ptroffset = getelementptr inbounds i32, i32* %11, i64 1 + %12 = load i32, i32* %ptroffset, 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 + %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 + %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 + %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 + %18 = load [3 x i32]*, [3 x i32]** %y, align 8 + %19 = getelementptr inbounds [3 x i32], [3 x i32]* %18, i64 0, i64 1 + %20 = load i32*, i32** %xx, align 8 + call void (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i32 0, i32 0), [3 x i32]* %15, i32* %16, i32* %17, i32* %19, i32* %20) + %21 = getelementptr inbounds [3 x i32], [3 x i32]* %x, i64 0, i64 1 + 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) + ret void +} \ No newline at end of file