Skip to content

Commit

Permalink
Improved alignment handling.
Browse files Browse the repository at this point in the history
  • Loading branch information
lerno committed Feb 27, 2024
1 parent 117f09a commit 284f976
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 36 deletions.
1 change: 1 addition & 0 deletions releasenotes.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
- `inline` on function parameters for implicit conversions.
- 'rgba' also available for swizzling.
- The name "subarray" has been replaced by the more well known name "slice' across the codebase.
- Improved alignment handling.

### Fixes
- Fixed issue in safe mode when converting enums.
Expand Down
4 changes: 2 additions & 2 deletions src/compiler/enums.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,8 @@ typedef enum
CAST_PTRINT,
CAST_SLBOOL,
CAST_SAPTR,
CAST_SASA,
CAST_SAARR,
CAST_SLSL,
CAST_SLARR,
CAST_STRPTR,
CAST_STINLINE,
CAST_VECARR,
Expand Down
4 changes: 2 additions & 2 deletions src/compiler/expr.c
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ static inline bool expr_cast_is_constant_eval(Expr *expr, ConstantEvalKind eval_
case CAST_PTRPTR:
case CAST_APTSA:
case CAST_SAPTR:
case CAST_SASA:
case CAST_SLSL:
case CAST_VOID:
case CAST_ANYBOOL:
case CAST_ERPTR:
Expand All @@ -386,7 +386,7 @@ static inline bool expr_cast_is_constant_eval(Expr *expr, ConstantEvalKind eval_
case CAST_IDINT:
case CAST_INTARRBS:
case CAST_BSINTARR:
case CAST_SAARR:
case CAST_SLARR:
if (eval_kind == CONSTANT_EVAL_CONSTANT_VALUE) return false;
return exprid_is_constant_eval(expr->cast_expr.expr, eval_kind);

Expand Down
77 changes: 48 additions & 29 deletions src/compiler/llvm_codegen_expr.c
Original file line number Diff line number Diff line change
Expand Up @@ -658,20 +658,14 @@ static inline void llvm_emit_vector_subscript(GenContext *c, BEValue *value, Exp


/**
* Expand foo[123] or someCall()[n] or some such.
* Expand &foo[123] or &someCall()[n] or some such.
* Evaluation order is left to right.
*/
static inline void gencontext_emit_subscript(GenContext *c, BEValue *value, Expr *expr)
static inline void llvm_emit_subscript_addr(GenContext *c, BEValue *value, Expr *expr)
{
bool is_value = expr->expr_kind == EXPR_SUBSCRIPT;
Expr *parent_expr = exprptr(expr->subscript_expr.expr);
Expr *index_expr = exprptr(expr->subscript_expr.range.start);
Type *parent_type = type_lowering(parent_expr->type);
if (is_value && parent_type->type_kind == TYPE_VECTOR)
{
llvm_emit_vector_subscript(c, value, expr);
return;
}
BEValue ref;
// First, get thing being subscripted.
llvm_emit_expr(c, value, parent_expr);
Expand Down Expand Up @@ -714,13 +708,24 @@ static inline void gencontext_emit_subscript(GenContext *c, BEValue *value, Expr
llvm_emit_array_bounds_check(c, &index, len.value, index_expr->span);
}
llvm_emit_subscript_addr_with_base(c, value, value, &index, index_expr->span);
if (!is_value)
assert(llvm_value_is_addr(value));
llvm_value_fold_optional(c, value);
}

/**
* Expand foo[123] or someCall()[n] or some such.
* Evaluation order is left to right.
*/
static inline void llvm_emit_subscript(GenContext *c, BEValue *value, Expr *expr)
{
Expr *parent_expr = exprptr(expr->subscript_expr.expr);
Type *parent_type = type_lowering(parent_expr->type);
if (parent_type->type_kind == TYPE_VECTOR)
{
assert(llvm_value_is_addr(value));
llvm_value_fold_optional(c, value);
value->kind = BE_VALUE;
value->type = type_get_ptr(value->type);
llvm_emit_vector_subscript(c, value, expr);
return;
}
llvm_emit_subscript_addr(c, value, expr);
}

static inline void llvm_emit_pointer_offset(GenContext *c, BEValue *value, Expr *expr)
Expand Down Expand Up @@ -1388,7 +1393,7 @@ void llvm_emit_cast(GenContext *c, CastKind cast_kind, Expr *expr, BEValue *valu

switch (cast_kind)
{
case CAST_SAARR:
case CAST_SLARR:
llvm_emit_slice_to_vec_array_cast(c, value, to_type, from_type);
return;
case CAST_EXPVEC:
Expand Down Expand Up @@ -1541,8 +1546,9 @@ void llvm_emit_cast(GenContext *c, CastKind cast_kind, Expr *expr, BEValue *valu
llvm_value_rvalue(c, value);
value->value = LLVMBuildIntToPtr(c->builder, value->value, llvm_get_type(c, to_type), "intptr");
break;
case CAST_SASA:
case CAST_SLSL:
// Improve this
break;
case CAST_STINLINE:
llvm_value_addr(c, value);
value->type = to_type;
Expand Down Expand Up @@ -2513,20 +2519,27 @@ static inline void llvm_emit_pre_inc_dec(GenContext *c, BEValue *value, Expr *ex

static inline void llvm_emit_deref(GenContext *c, BEValue *value, Expr *inner, Type *type)
{
if (inner->expr_kind == EXPR_UNARY)
switch (inner->expr_kind)
{
switch (inner->unary_expr.operator)
{
case UNARYOP_ADDR:
llvm_emit_expr(c, value, inner->unary_expr.expr);
return;
case UNARYOP_TADDR:
llvm_emit_expr(c, value, inner->unary_expr.expr);
llvm_value_addr(c, value);
return;
default:
break;
}
case EXPR_UNARY:
switch (inner->unary_expr.operator)
{
case UNARYOP_ADDR:
llvm_emit_expr(c, value, inner->unary_expr.expr);
return;
case UNARYOP_TADDR:
llvm_emit_expr(c, value, inner->unary_expr.expr);
llvm_value_addr(c, value);
return;
default:
break;
}
break;
case EXPR_SUBSCRIPT_ADDR:
llvm_emit_subscript_addr(c, value, inner);
return;
default:
break;
}
llvm_emit_expr(c, value, inner);
llvm_value_rvalue(c, value);
Expand Down Expand Up @@ -6974,8 +6987,14 @@ void llvm_emit_expr(GenContext *c, BEValue *value, Expr *expr)
llvm_value_set_decl(c, value, expr->identifier_expr.decl);
return;
case EXPR_SUBSCRIPT:
llvm_emit_subscript(c, value, expr);
return;
case EXPR_SUBSCRIPT_ADDR:
gencontext_emit_subscript(c, value, expr);
llvm_emit_subscript_addr(c, value, expr);
assert(llvm_value_is_addr(value));
llvm_value_fold_optional(c, value);
value->kind = BE_VALUE;
value->type = type_get_ptr(value->type);
return;
case EXPR_ACCESS:
llvm_emit_access_addr(c, value, expr);
Expand Down
4 changes: 2 additions & 2 deletions src/compiler/sema_casts.c
Original file line number Diff line number Diff line change
Expand Up @@ -1856,7 +1856,7 @@ static void cast_slice_to_slice(SemaContext *context, Expr *expr, Type *to_type)
expr->type = to_type;
return;
}
insert_runtime_cast(expr, CAST_SASA, to_type);
insert_runtime_cast(expr, CAST_SLSL, to_type);
}

static void cast_slice_to_vecarr(SemaContext *context, Expr *expr, Type *to_type)
Expand All @@ -1874,7 +1874,7 @@ static void cast_slice_to_vecarr(SemaContext *context, Expr *expr, Type *to_type
}
case EXPR_SLICE:
{
insert_runtime_cast(expr, CAST_SAARR, to_type);
insert_runtime_cast(expr, CAST_SLARR, to_type);
return;
}
default:
Expand Down
22 changes: 22 additions & 0 deletions test/test_suite/assignment/alignment_index.c3t
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// #target: windows-x64
module test;
int[<2>] foo @align(1);

fn void main()
{
foo[1] = 2;
foo.y = 4;
}

/* #expect: test.ll

@test.foo = local_unnamed_addr global <2 x i32> zeroinitializer, align 1

define void @test.main() #0 {
entry:
%0 = load <2 x i32>, ptr @test.foo, align 1
%elemset = insertelement <2 x i32> %0, i32 2, i64 1
store <2 x i32> %elemset, ptr @test.foo, align 1
store i32 4, ptr getelementptr inbounds (i8, ptr @test.foo, i64 4), align 1
ret void
}
1 change: 1 addition & 0 deletions test/test_suite/assignment/int_assign.c3t
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// #target: windows-x64
module test;

fn int foo()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
module test;
import std::io;


macro @foo(;@body)
{
var i = 1.0;
Expand Down

0 comments on commit 284f976

Please sign in to comment.