diff options
| author | Nagi5Yeq <wu@x-nagi.com> | 2020-05-30 18:27:16 +0800 |
|---|---|---|
| committer | Nagi5Yeq <wu@x-nagi.com> | 2020-05-30 18:27:16 +0800 |
| commit | fd21f31e654a97027799f0fdf3518fd639415121 (patch) | |
| tree | a88dd5a89899fd53909772795382c899b4ec014a /node/DeclNode.cc | |
| parent | fb1fed0f275d1673cf6fb6c3887bc8919e1849d7 (diff) | |
add zeroinitializer for global variable
Diffstat (limited to 'node/DeclNode.cc')
| -rw-r--r-- | node/DeclNode.cc | 12 |
1 files changed, 8 insertions, 4 deletions
diff --git a/node/DeclNode.cc b/node/DeclNode.cc index 932191e..929bc70 100644 --- a/node/DeclNode.cc +++ b/node/DeclNode.cc @@ -1,6 +1,7 @@ #include <llvm/IR/IRBuilder.h> #include "DeclNode.hh" +#include "Log.hh" namespace pcc { DeclNode::DeclNode(Context* context, VariableList* scope) @@ -31,9 +32,11 @@ Value VarDeclNode::CodeGen() { // global variable case VariableList* globals = context_->GetGlobals(); for (auto&& decl : decls_) { - llvm::Constant* value = new llvm::GlobalVariable( - *context_->GetModule(), std::get<1>(decl)->GetLLVMType(), false, - llvm::GlobalVariable::PrivateLinkage, nullptr); + std::shared_ptr<Type> type = std::get<1>(decl); + llvm::GlobalValue* value = new llvm::GlobalVariable( + *context_->GetModule(), type->GetLLVMType(), false, + llvm::GlobalVariable::PrivateLinkage, type->GetZeroInitializer()); + value->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global); globals->Add(std::get<0>(decl), {manager->GetPointerType(std::get<1>(decl)), value}); } @@ -54,10 +57,11 @@ Value ConstDeclNode::CodeGen() { std::shared_ptr<ExprNode> ValueNode = std::get<1>(decl); Value value = ValueNode->CodeGen(); std::shared_ptr<Type> type = ValueNode->GetType(); - llvm::Constant* constant = new llvm::GlobalVariable( + llvm::GlobalValue* constant = new llvm::GlobalVariable( *context_->GetModule(), type->GetLLVMType(), true, llvm::GlobalValue::PrivateLinkage, llvm::cast<llvm::Constant>(value)); + constant->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global); scope_->Add(std::get<0>(decl), {manager->GetPointerType(type), constant}); } |
