@@ -552,6 +552,8 @@ EMIT_FUNC_NO_ARGS(leave_structured_block);
552
552
static u32 emit_data_entry (OnyxWasmModule * mod , WasmDatum * datum );
553
553
static void emit_raw_string (OnyxWasmModule * mod , char * data , i32 len , u64 * out_data_id , u64 * out_len );
554
554
555
+ static i32 emit_initialized_global_value (OnyxWasmModule * mod , Type * type , AstTyped * expr );
556
+
555
557
static void emit_constexpr (ConstExprContext * ctx , AstTyped * node , u32 offset );
556
558
static b32 emit_constexpr_ (ConstExprContext * ctx , AstTyped * node , u32 offset );
557
559
@@ -3764,12 +3766,22 @@ EMIT_FUNC(expression, AstTyped* expr) {
3764
3766
AstAddressOf * aof = (AstAddressOf * ) expr ;
3765
3767
3766
3768
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
+ }
3773
3785
}
3774
3786
3775
3787
emit_location (mod , & code , aof -> expr );
@@ -5116,6 +5128,34 @@ static b32 emit_constexpr_(ConstExprContext *ctx, AstTyped *node, u32 offset) {
5116
5128
#undef CE
5117
5129
}
5118
5130
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
+
5119
5159
static void emit_memory_reservation (OnyxWasmModule * mod , AstMemRes * memres ) {
5120
5160
// :ProperLinking
5121
5161
Type * effective_type = memres -> type ;
@@ -5155,27 +5195,7 @@ static void emit_memory_reservation(OnyxWasmModule* mod, AstMemRes* memres) {
5155
5195
mod -> next_tls_offset = memres -> tls_offset + size ;
5156
5196
5157
5197
} 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 );
5179
5199
}
5180
5200
}
5181
5201
0 commit comments