summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--CMakeLists.txt1
-rw-r--r--Type.cc4
-rw-r--r--Type.hh1
-rw-r--r--node/DeclNode.cc12
-rw-r--r--node/LiteralNode.cc3
-rw-r--r--node/LiteralNode.hh4
-rw-r--r--t/trivial.pas15
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})
diff --git a/Type.cc b/Type.cc
index 9c7fd6f..95e529d 100644
--- a/Type.cc
+++ b/Type.cc
@@ -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; }
diff --git a/Type.hh b/Type.hh
index c83c974..e11a9ea 100644
--- a/Type.hh
+++ b/Type.hh
@@ -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;