Skip to content

Commit

Permalink
added: address of a comptime value is lifted into global data
Browse files Browse the repository at this point in the history
  • Loading branch information
brendanfh committed Jan 18, 2025
1 parent 7ac2670 commit 48f09c7
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 27 deletions.
3 changes: 3 additions & 0 deletions compiler/src/checker.c
Original file line number Diff line number Diff line change
Expand Up @@ -3687,6 +3687,9 @@ CHECK_FUNC(statement, AstNode** pstmt) {

if (typed_stmt->next != NULL && typed_stmt->next->kind == Ast_Kind_Binary_Op) {
AstBinaryOp *next = (AstBinaryOp *) typed_stmt->next;

//
// :BrokenFollowedByInitFlag
if (next->operation == Binary_Op_Assign && next->left == typed_stmt) {
typed_stmt->flags |= Ast_Flag_Decl_Followed_By_Init;
}
Expand Down
74 changes: 47 additions & 27 deletions compiler/src/wasm_emit.c
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,8 @@ EMIT_FUNC_NO_ARGS(leave_structured_block);
static u32 emit_data_entry(OnyxWasmModule *mod, WasmDatum *datum);
static void emit_raw_string(OnyxWasmModule* mod, char *data, i32 len, u64 *out_data_id, u64 *out_len);

static i32 emit_initialized_global_value(OnyxWasmModule *mod, Type *type, AstTyped *expr);

static void emit_constexpr(ConstExprContext *ctx, AstTyped *node, u32 offset);
static b32 emit_constexpr_(ConstExprContext *ctx, AstTyped *node, u32 offset);

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

if (node_is_addressable_literal((AstNode *) aof->expr)) {
aof->expr->flags |= Ast_Flag_Decl_Followed_By_Init;
aof->expr->flags |= Ast_Flag_Address_Taken;
emit_local_allocation(mod, &code, aof->expr);
emit_location(mod, &code, aof->expr);
emit_expression(mod, &code, aof->expr);
emit_store_instruction(mod, &code, aof->expr->type, 0);
if (aof->expr->flags & Ast_Flag_Comptime) {
i32 data_id = emit_initialized_global_value(mod, aof->expr->type, aof->expr);
emit_data_relocation(mod, &code, data_id);

// We need to break early, because we cannot use the
// emit_location below for this kind of address of node.
break;

} else {
aof->expr->flags |= Ast_Flag_Decl_Followed_By_Init;
aof->expr->flags |= Ast_Flag_Address_Taken;
emit_local_allocation(mod, &code, aof->expr);
emit_location(mod, &code, aof->expr);
emit_expression(mod, &code, aof->expr);
emit_store_instruction(mod, &code, aof->expr->type, 0);
}
}

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

static i32 emit_initialized_global_value(OnyxWasmModule *mod, Type *type, AstTyped *expr) {
u64 alignment = type_alignment_of(type);
u64 size = type_size_of(type);

// :ProperLinking
u8* data = NULL;
if (expr != NULL) {
data = bh_alloc(mod->context->gp_alloc, size);
}

WasmDatum datum = {
.alignment = alignment,
.length = size,
.data = data,
};
i32 data_id = emit_data_entry(mod, &datum);

if (expr != NULL) {
ConstExprContext constexpr_ctx;
constexpr_ctx.module = mod;
constexpr_ctx.data = data;
constexpr_ctx.data_id = data_id;
emit_constexpr(&constexpr_ctx, expr, 0);
}

return data_id;
}

static void emit_memory_reservation(OnyxWasmModule* mod, AstMemRes* memres) {
// :ProperLinking
Type* effective_type = memres->type;
Expand Down Expand Up @@ -5155,27 +5195,7 @@ static void emit_memory_reservation(OnyxWasmModule* mod, AstMemRes* memres) {
mod->next_tls_offset = memres->tls_offset + size;

} else {
// :ProperLinking
u8* data = NULL;
if (memres->initial_value != NULL) {
assert(!memres->threadlocal);
data = bh_alloc(mod->context->gp_alloc, size);
}

WasmDatum datum = {
.alignment = alignment,
.length = size,
.data = data,
};
memres->data_id = emit_data_entry(mod, &datum);

if (memres->initial_value != NULL) {
ConstExprContext constexpr_ctx;
constexpr_ctx.module = mod;
constexpr_ctx.data = data;
constexpr_ctx.data_id = memres->data_id;
emit_constexpr(&constexpr_ctx, memres->initial_value, 0);
}
memres->data_id = emit_initialized_global_value(mod, effective_type, memres->initial_value);
}
}

Expand Down

0 comments on commit 48f09c7

Please sign in to comment.