mirror of
https://github.com/Ed94/Odin.git
synced 2026-08-05 15:18:49 +00:00
Just get the value directly and store it in another global variable
// global
x := &Foo{}
This commit is contained in:
+22
-3
@@ -758,9 +758,29 @@ lbProcedure *lb_create_startup_runtime(lbModule *main_module, lbProcedure *start
|
|||||||
GB_ASSERT(e->kind == Entity_Variable);
|
GB_ASSERT(e->kind == Entity_Variable);
|
||||||
e->code_gen_module = entity_module;
|
e->code_gen_module = entity_module;
|
||||||
|
|
||||||
Ast *init_expr = var->decl->init_expr;
|
Ast *init_expr = unparen_expr(var->decl->init_expr);
|
||||||
if (init_expr != nullptr) {
|
if (init_expr != nullptr) {
|
||||||
lbValue init = lb_build_expr(p, init_expr);
|
lbValue init = {};
|
||||||
|
|
||||||
|
if (!(is_type_any(e->type) || is_type_union(e->type))) {
|
||||||
|
if (init_expr->kind == Ast_UnaryExpr && init_expr->UnaryExpr.op.kind == Token_And) {
|
||||||
|
// NOTE(bill): Just get the value directly and store it in another global variable
|
||||||
|
Ast *expr = unparen_expr(init_expr->UnaryExpr.expr);
|
||||||
|
if (expr->tav.value.kind != ExactValue_Invalid) {
|
||||||
|
init = lb_build_expr(p, expr);
|
||||||
|
GB_ASSERT(init.value != nullptr);
|
||||||
|
GB_ASSERT(LLVMIsConstant(init.value));
|
||||||
|
|
||||||
|
lbAddr addr = lb_add_global_generated(p->module, init.type, init, nullptr);
|
||||||
|
LLVMValueRef global_val = lb_make_global_truly_private(addr);
|
||||||
|
LLVMSetInitializer(var->var.value, global_val);
|
||||||
|
var->is_initialized = true;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
init = lb_build_expr(p, init_expr);
|
||||||
if (init.value == nullptr) {
|
if (init.value == nullptr) {
|
||||||
LLVMTypeRef global_type = llvm_addr_type(p->module, var->var);
|
LLVMTypeRef global_type = llvm_addr_type(p->module, var->var);
|
||||||
if (is_type_untyped_undef(init.type)) {
|
if (is_type_untyped_undef(init.type)) {
|
||||||
@@ -1588,7 +1608,6 @@ void lb_generate_code(lbGenerator *gen) {
|
|||||||
bool is_foreign = e->Variable.is_foreign;
|
bool is_foreign = e->Variable.is_foreign;
|
||||||
bool is_export = e->Variable.is_export;
|
bool is_export = e->Variable.is_export;
|
||||||
|
|
||||||
|
|
||||||
lbModule *m = &gen->default_module;
|
lbModule *m = &gen->default_module;
|
||||||
String name = lb_get_entity_name(m, e);
|
String name = lb_get_entity_name(m, e);
|
||||||
|
|
||||||
|
|||||||
@@ -395,6 +395,7 @@ lbContextData *lb_push_context_onto_stack_from_implicit_parameter(lbProcedure *p
|
|||||||
|
|
||||||
lbAddr lb_add_global_generated(lbModule *m, Type *type, lbValue value={}, Entity **entity_=nullptr);
|
lbAddr lb_add_global_generated(lbModule *m, Type *type, lbValue value={}, Entity **entity_=nullptr);
|
||||||
lbAddr lb_add_local(lbProcedure *p, Type *type, Entity *e=nullptr, bool zero_init=true, i32 param_index=0, bool force_no_init=false);
|
lbAddr lb_add_local(lbProcedure *p, Type *type, Entity *e=nullptr, bool zero_init=true, i32 param_index=0, bool force_no_init=false);
|
||||||
|
LLVMValueRef lb_make_global_truly_private(lbAddr const &addr);
|
||||||
|
|
||||||
void lb_add_foreign_library_path(lbModule *m, Entity *e);
|
void lb_add_foreign_library_path(lbModule *m, Entity *e);
|
||||||
|
|
||||||
|
|||||||
@@ -926,11 +926,7 @@ void lb_emit_store(lbProcedure *p, lbValue ptr, lbValue value) {
|
|||||||
return;
|
return;
|
||||||
} else if (LLVMIsConstant(value.value)) {
|
} else if (LLVMIsConstant(value.value)) {
|
||||||
lbAddr addr = lb_add_global_generated(p->module, value.type, value, nullptr);
|
lbAddr addr = lb_add_global_generated(p->module, value.type, value, nullptr);
|
||||||
LLVMValueRef global_data = addr.addr.value;
|
LLVMValueRef global_data = lb_make_global_truly_private(addr);
|
||||||
// make it truly private data
|
|
||||||
LLVMSetLinkage(global_data, LLVMPrivateLinkage);
|
|
||||||
LLVMSetUnnamedAddress(global_data, LLVMGlobalUnnamedAddr);
|
|
||||||
LLVMSetGlobalConstant(global_data, true);
|
|
||||||
|
|
||||||
LLVMValueRef dst_ptr = ptr.value;
|
LLVMValueRef dst_ptr = ptr.value;
|
||||||
LLVMValueRef src_ptr = global_data;
|
LLVMValueRef src_ptr = global_data;
|
||||||
@@ -2669,6 +2665,15 @@ lbValue lb_find_procedure_value_from_entity(lbModule *m, Entity *e) {
|
|||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
LLVMValueRef lb_make_global_truly_private(lbAddr const &addr) {
|
||||||
|
LLVMValueRef global_data = addr.addr.value;
|
||||||
|
// make it truly private data
|
||||||
|
LLVMSetLinkage(global_data, LLVMPrivateLinkage);
|
||||||
|
LLVMSetUnnamedAddress(global_data, LLVMGlobalUnnamedAddr);
|
||||||
|
LLVMSetGlobalConstant(global_data, true);
|
||||||
|
return global_data;
|
||||||
|
}
|
||||||
|
|
||||||
lbAddr lb_add_global_generated(lbModule *m, Type *type, lbValue value, Entity **entity_) {
|
lbAddr lb_add_global_generated(lbModule *m, Type *type, lbValue value, Entity **entity_) {
|
||||||
GB_ASSERT(type != nullptr);
|
GB_ASSERT(type != nullptr);
|
||||||
type = default_type(type);
|
type = default_type(type);
|
||||||
|
|||||||
Reference in New Issue
Block a user