Skip to content

Commit 48f09c7

Browse files
committed
added: address of a comptime value is lifted into global data
1 parent 7ac2670 commit 48f09c7

File tree

2 files changed

+50
-27
lines changed

2 files changed

+50
-27
lines changed

compiler/src/checker.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3687,6 +3687,9 @@ CHECK_FUNC(statement, AstNode** pstmt) {
36873687

36883688
if (typed_stmt->next != NULL && typed_stmt->next->kind == Ast_Kind_Binary_Op) {
36893689
AstBinaryOp *next = (AstBinaryOp *) typed_stmt->next;
3690+
3691+
//
3692+
// :BrokenFollowedByInitFlag
36903693
if (next->operation == Binary_Op_Assign && next->left == typed_stmt) {
36913694
typed_stmt->flags |= Ast_Flag_Decl_Followed_By_Init;
36923695
}

compiler/src/wasm_emit.c

Lines changed: 47 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -552,6 +552,8 @@ EMIT_FUNC_NO_ARGS(leave_structured_block);
552552
static u32 emit_data_entry(OnyxWasmModule *mod, WasmDatum *datum);
553553
static void emit_raw_string(OnyxWasmModule* mod, char *data, i32 len, u64 *out_data_id, u64 *out_len);
554554

555+
static i32 emit_initialized_global_value(OnyxWasmModule *mod, Type *type, AstTyped *expr);
556+
555557
static void emit_constexpr(ConstExprContext *ctx, AstTyped *node, u32 offset);
556558
static b32 emit_constexpr_(ConstExprContext *ctx, AstTyped *node, u32 offset);
557559

@@ -3764,12 +3766,22 @@ EMIT_FUNC(expression, AstTyped* expr) {
37643766
AstAddressOf* aof = (AstAddressOf *) expr;
37653767

37663768
if (node_is_addressable_literal((AstNode *) aof->expr)) {
3767-
aof->expr->flags |= Ast_Flag_Decl_Followed_By_Init;
3768-
aof->expr->flags |= Ast_Flag_Address_Taken;
3769-
emit_local_allocation(mod, &code, aof->expr);
3770-
emit_location(mod, &code, aof->expr);
3771-
emit_expression(mod, &code, aof->expr);
3772-
emit_store_instruction(mod, &code, aof->expr->type, 0);
3769+
if (aof->expr->flags & Ast_Flag_Comptime) {
3770+
i32 data_id = emit_initialized_global_value(mod, aof->expr->type, aof->expr);
3771+
emit_data_relocation(mod, &code, data_id);
3772+
3773+
// We need to break early, because we cannot use the
3774+
// emit_location below for this kind of address of node.
3775+
break;
3776+
3777+
} else {
3778+
aof->expr->flags |= Ast_Flag_Decl_Followed_By_Init;
3779+
aof->expr->flags |= Ast_Flag_Address_Taken;
3780+
emit_local_allocation(mod, &code, aof->expr);
3781+
emit_location(mod, &code, aof->expr);
3782+
emit_expression(mod, &code, aof->expr);
3783+
emit_store_instruction(mod, &code, aof->expr->type, 0);
3784+
}
37733785
}
37743786

37753787
emit_location(mod, &code, aof->expr);
@@ -5116,6 +5128,34 @@ static b32 emit_constexpr_(ConstExprContext *ctx, AstTyped *node, u32 offset) {
51165128
#undef CE
51175129
}
51185130

5131+
static i32 emit_initialized_global_value(OnyxWasmModule *mod, Type *type, AstTyped *expr) {
5132+
u64 alignment = type_alignment_of(type);
5133+
u64 size = type_size_of(type);
5134+
5135+
// :ProperLinking
5136+
u8* data = NULL;
5137+
if (expr != NULL) {
5138+
data = bh_alloc(mod->context->gp_alloc, size);
5139+
}
5140+
5141+
WasmDatum datum = {
5142+
.alignment = alignment,
5143+
.length = size,
5144+
.data = data,
5145+
};
5146+
i32 data_id = emit_data_entry(mod, &datum);
5147+
5148+
if (expr != NULL) {
5149+
ConstExprContext constexpr_ctx;
5150+
constexpr_ctx.module = mod;
5151+
constexpr_ctx.data = data;
5152+
constexpr_ctx.data_id = data_id;
5153+
emit_constexpr(&constexpr_ctx, expr, 0);
5154+
}
5155+
5156+
return data_id;
5157+
}
5158+
51195159
static void emit_memory_reservation(OnyxWasmModule* mod, AstMemRes* memres) {
51205160
// :ProperLinking
51215161
Type* effective_type = memres->type;
@@ -5155,27 +5195,7 @@ static void emit_memory_reservation(OnyxWasmModule* mod, AstMemRes* memres) {
51555195
mod->next_tls_offset = memres->tls_offset + size;
51565196

51575197
} else {
5158-
// :ProperLinking
5159-
u8* data = NULL;
5160-
if (memres->initial_value != NULL) {
5161-
assert(!memres->threadlocal);
5162-
data = bh_alloc(mod->context->gp_alloc, size);
5163-
}
5164-
5165-
WasmDatum datum = {
5166-
.alignment = alignment,
5167-
.length = size,
5168-
.data = data,
5169-
};
5170-
memres->data_id = emit_data_entry(mod, &datum);
5171-
5172-
if (memres->initial_value != NULL) {
5173-
ConstExprContext constexpr_ctx;
5174-
constexpr_ctx.module = mod;
5175-
constexpr_ctx.data = data;
5176-
constexpr_ctx.data_id = memres->data_id;
5177-
emit_constexpr(&constexpr_ctx, memres->initial_value, 0);
5178-
}
5198+
memres->data_id = emit_initialized_global_value(mod, effective_type, memres->initial_value);
51795199
}
51805200
}
51815201

0 commit comments

Comments
 (0)