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 | |
| parent | fb1fed0f275d1673cf6fb6c3887bc8919e1849d7 (diff) | |
add zeroinitializer for global variable
| -rw-r--r-- | CMakeLists.txt | 1 | ||||
| -rw-r--r-- | Type.cc | 4 | ||||
| -rw-r--r-- | Type.hh | 1 | ||||
| -rw-r--r-- | node/DeclNode.cc | 12 | ||||
| -rw-r--r-- | node/LiteralNode.cc | 3 | ||||
| -rw-r--r-- | node/LiteralNode.hh | 4 | ||||
| -rw-r--r-- | t/trivial.pas | 15 |
7 files changed, 27 insertions, 13 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index b486bc5..74bba18 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,7 @@ cmake_minimum_required(VERSION 3.4) project(pcc) +set(CMAKE_CXX_STANDARD 14) find_package(LLVM REQUIRED CONFIG) include_directories(${LLVM_INCLUDE_DIRS}) add_definitions(${LLVM_DEFINITIONS}) @@ -25,6 +25,10 @@ const char* Type::GetCommonName() { return name_.c_str(); } llvm::Type* Type::GetLLVMType() { return LLVMType_; } +llvm::Constant* Type::GetZeroInitializer() { + return llvm::Constant::getNullValue(LLVMType_); +} + Value Type::NotAllowed(Value v, Context* context) { return nullptr; } Value Type::NotAllowed(Value v0, Value v1, Context* context) { return nullptr; } @@ -56,6 +56,7 @@ class Type { Context* context); virtual Value CreateUnaryOperation(UnaryOperator op, Value v, Context* context); + virtual llvm::Constant* GetZeroInitializer(); protected: Value NotAllowed(Value v0, Value v1, Context* context); 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}); } diff --git a/node/LiteralNode.cc b/node/LiteralNode.cc index c68a219..5523da6 100644 --- a/node/LiteralNode.cc +++ b/node/LiteralNode.cc @@ -40,9 +40,10 @@ Value StringLiteralNode::CodeGen() { std::static_pointer_cast<PointerType>(type_); llvm::Constant* ConstantString = llvm::ConstantDataArray::get(GlobalLLVMContext, value_); - llvm::Constant* GlobalString = new llvm::GlobalVariable( + llvm::GlobalValue* GlobalString = new llvm::GlobalVariable( *context_->GetModule(), ConstantString->getType(), true, llvm::GlobalValue::PrivateLinkage, ConstantString); + GlobalString->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global); Value ZeroIndex = llvm::ConstantInt::get(pointer->GetIndexType()->GetLLVMType(), 0, true); return builder->CreateInBoundsGEP(GlobalString, {ZeroIndex, ZeroIndex}); diff --git a/node/LiteralNode.hh b/node/LiteralNode.hh index ca12131..06f2989 100644 --- a/node/LiteralNode.hh +++ b/node/LiteralNode.hh @@ -10,7 +10,7 @@ namespace pcc { template <typename T> class LiteralNode : public ExprNode { public: LiteralNode(Context* context, std::shared_ptr<Type> type, T value); - Value CodeGen() override; + virtual Value CodeGen() override; private: T value_; @@ -35,7 +35,7 @@ class StringLiteralNode : public ExprNode { public: StringLiteralNode(Context* context, std::shared_ptr<Type> type, std::vector<char> value); - Value CodeGen() override; + virtual Value CodeGen() override; private: std::vector<char> value_; diff --git a/t/trivial.pas b/t/trivial.pas index fb97adc..68e4a53 100644 --- a/t/trivial.pas +++ b/t/trivial.pas @@ -6,7 +6,10 @@ const gr=3.1415; gs='nagi'; var - gv:integer; + gvi:integer; + gvf:real; + gvp:^char; + gva:array[0..4] of integer; function arithmetic_test(a:integer;b:char;c:real):integer; var @@ -138,8 +141,8 @@ end; function name_test_global():integer; begin - gv:=1; - name_test_global:=gv+gb+gc+gi+gr+gs[3]; + gvi:=1; + name_test_global:=gvi+gb+gc+gi+gr+gs[3]; end; function name_test_local():integer; @@ -150,10 +153,10 @@ const gr=2.71828; gs='Hanjie Wu'; var - gv:integer; + gvi:integer; begin - gv:=0; - name_test_local:=gv+gb+gc+gi+gr+gs[3]; + gvi:=0; + name_test_local:=gvi+gb+gc+gi+gr+gs[3]; end; function name_test():boolean; |
