| ... | ... | @@ -2533,26 +2533,34 @@ static void do_code_gen(CodeGen *g) { |
| 2533 | 2533 | continue; |
| 2534 | 2534 | } |
| 2535 | 2535 | |
| 2536 | | // TODO if the global is exported, set external linkage |
| 2537 | | LLVMValueRef init_val; |
| 2538 | | |
| 2539 | 2536 | assert(var->decl_node); |
| 2540 | 2537 | assert(var->decl_node->type == NodeTypeVariableDeclaration); |
| 2541 | | AstNode *expr_node = var->decl_node->data.variable_declaration.expr; |
| 2542 | | if (expr_node) { |
| 2543 | | Expr *expr = get_resolved_expr(expr_node); |
| 2544 | | ConstExprValue *const_val = &expr->const_val; |
| 2545 | | assert(const_val->ok); |
| 2546 | | TypeTableEntry *type_entry = expr->type_entry; |
| 2547 | | init_val = gen_const_val(g, type_entry, const_val); |
| 2538 | |
| 2539 | LLVMValueRef global_value; |
| 2540 | if (var->decl_node->data.variable_declaration.is_extern) { |
| 2541 | global_value = LLVMAddGlobal(g->module, var->type->type_ref, buf_ptr(&var->name)); |
| 2542 | |
| 2543 | LLVMSetLinkage(global_value, LLVMExternalLinkage); |
| 2548 | 2544 | } else { |
| 2549 | | init_val = LLVMConstNull(var->type->type_ref); |
| 2545 | AstNode *expr_node = var->decl_node->data.variable_declaration.expr; |
| 2546 | LLVMValueRef init_val; |
| 2547 | if (expr_node) { |
| 2548 | Expr *expr = get_resolved_expr(expr_node); |
| 2549 | ConstExprValue *const_val = &expr->const_val; |
| 2550 | assert(const_val->ok); |
| 2551 | TypeTableEntry *type_entry = expr->type_entry; |
| 2552 | init_val = gen_const_val(g, type_entry, const_val); |
| 2553 | } else { |
| 2554 | init_val = LLVMConstNull(var->type->type_ref); |
| 2555 | } |
| 2556 | |
| 2557 | global_value = LLVMAddGlobal(g->module, LLVMTypeOf(init_val), buf_ptr(&var->name)); |
| 2558 | LLVMSetInitializer(global_value, init_val); |
| 2559 | LLVMSetLinkage(global_value, LLVMInternalLinkage); |
| 2560 | LLVMSetUnnamedAddr(global_value, true); |
| 2550 | 2561 | } |
| 2551 | | LLVMValueRef global_value = LLVMAddGlobal(g->module, LLVMTypeOf(init_val), buf_ptr(&var->name)); |
| 2552 | | LLVMSetInitializer(global_value, init_val); |
| 2562 | |
| 2553 | 2563 | LLVMSetGlobalConstant(global_value, var->is_const); |
| 2554 | | LLVMSetUnnamedAddr(global_value, true); |
| 2555 | | LLVMSetLinkage(global_value, LLVMInternalLinkage); |
| 2556 | 2564 | |
| 2557 | 2565 | var->value_ref = global_value; |
| 2558 | 2566 | } |