diff options
| author | Nagi5Yeq <wu@x-nagi.com> | 2020-05-26 00:26:57 +0800 |
|---|---|---|
| committer | Nagi5Yeq <wu@x-nagi.com> | 2020-05-26 00:26:57 +0800 |
| commit | e83990754f49ec6eba7b9e1a5ff80a6add5de3a1 (patch) | |
| tree | 6308b4d0afa5bde2de82e7aa034058302cb1d193 /node | |
| parent | 958a811b18aee04e4795cf003fd8e3db00312c91 (diff) | |
bugfix & test
Diffstat (limited to 'node')
| -rw-r--r-- | node/BaseNode.cc | 19 | ||||
| -rw-r--r-- | node/BaseNode.hh | 15 | ||||
| -rw-r--r-- | node/ExprNode.cc | 1 | ||||
| -rw-r--r-- | node/FunctionNode.cc | 10 |
4 files changed, 43 insertions, 2 deletions
diff --git a/node/BaseNode.cc b/node/BaseNode.cc index 8fe2c5d..4b1166b 100644 --- a/node/BaseNode.cc +++ b/node/BaseNode.cc @@ -3,4 +3,23 @@ namespace pcc { BaseNode::BaseNode(Context* context) : context_(context) {} + +ProgramNode::ProgramNode( + Context* context, const std::string& name, + std::list<std::shared_ptr<BaseNode>> GlobalDeclarations, + std::list<std::shared_ptr<FunctionNode>> functions) + : BaseNode(context) + , name_(name) + , GlobalDeclarations_(GlobalDeclarations) + , functions_(functions) {} + +Value ProgramNode::CodeGen() { + for (auto decl : GlobalDeclarations_) { + decl->CodeGen(); + } + for (auto function : functions_) { + function->CodeGen(); + } + return nullptr; +} } // namespace pcc diff --git a/node/BaseNode.hh b/node/BaseNode.hh index 16ee82d..71b0925 100644 --- a/node/BaseNode.hh +++ b/node/BaseNode.hh @@ -25,6 +25,21 @@ template <typename T, class Base = BaseNode> class ListNode : public Base { private: std::list<T> childs_; }; + +class FunctionNode; + +class ProgramNode : public BaseNode { + public: + ProgramNode(Context* context, const std::string& name, + std::list<std::shared_ptr<BaseNode>> GlobalDeclarations, + std::list<std::shared_ptr<FunctionNode>> functions); + virtual Value CodeGen() override; + + protected: + std::string name_; + std::list<std::shared_ptr<BaseNode>> GlobalDeclarations_; + std::list<std::shared_ptr<FunctionNode>> functions_; +}; } // namespace pcc #endif diff --git a/node/ExprNode.cc b/node/ExprNode.cc index e7ef2aa..6127f6f 100644 --- a/node/ExprNode.cc +++ b/node/ExprNode.cc @@ -51,6 +51,7 @@ Value UnaryExprNode::CodeGen() { if (result == nullptr) { Log(LogLevel::PCC_ERROR, "failed to generate a unary operation"); } + type_ = value_->GetType(); return result; } diff --git a/node/FunctionNode.cc b/node/FunctionNode.cc index dfb7eff..e22c8b6 100644 --- a/node/FunctionNode.cc +++ b/node/FunctionNode.cc @@ -2,9 +2,11 @@ #include <vector> #include <llvm/IR/IRBuilder.h> +#include <llvm/IR/Verifier.h> #include "DeclNode.hh" #include "FunctionNode.hh" +#include "Log.hh" namespace pcc { FunctionNode::FunctionNode( @@ -34,7 +36,7 @@ Value FunctionNode::CodeGen() { std::vector<std::shared_ptr<Type>> params(arguments_.size()); std::transform(arguments_.cbegin(), arguments_.cend(), params.begin(), [](const Declaration& decl) { return std::get<1>(decl); }); - std::shared_ptr<Type> type = + std::shared_ptr<FunctionType> type = context_->GetTypeManager()->CreateFunctionType(type_, params); context_->AddFunctionType(name_, type); llvm::FunctionType* LLVMType = @@ -46,7 +48,7 @@ Value FunctionNode::CodeGen() { llvm::BasicBlock::Create(GlobalLLVMContext, name_, function); builder->SetInsertPoint(block); // alloc arguments - llvm::Function::arg_iterator ArgIt; + llvm::Function::arg_iterator ArgIt = function->arg_begin(); for (auto& arg : arguments_) { Value ArgValue = builder->CreateAlloca(std::get<1>(arg)->GetLLVMType()); builder->CreateStore(ArgIt++, ArgValue); @@ -64,6 +66,10 @@ Value FunctionNode::CodeGen() { body_->CodeGen(); builder->CreateRet(rv); context_->PopScope(); + std::string msg; + llvm::raw_string_ostream MsgStream(msg); + llvm::verifyFunction(*function, &MsgStream); + Log(LogLevel::PCC_INFO, "%s", msg.c_str()); return nullptr; } |
