From 63d2a1c552b0d190a0131c2db2045d154b9ec138 Mon Sep 17 00:00:00 2001 From: Nagi5Yeq Date: Sat, 30 May 2020 02:33:00 +0800 Subject: support for break, continue and void type --- node/FunctionNode.cc | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) (limited to 'node/FunctionNode.cc') diff --git a/node/FunctionNode.cc b/node/FunctionNode.cc index a812ee1..f6810fd 100644 --- a/node/FunctionNode.cc +++ b/node/FunctionNode.cc @@ -28,11 +28,12 @@ FunctionNode::FunctionNode( Value FunctionNode::CodeGen() { llvm::Module* module = context_->GetModule(); llvm::IRBuilder<>* builder = context_->GetBuilder(); + TypeManager* manager = context_->GetTypeManager(); std::vector> params(arguments_.size()); std::transform(arguments_.cbegin(), arguments_.cend(), params.begin(), [](const Declaration& decl) { return std::get<1>(decl); }); std::shared_ptr type = - context_->GetTypeManager()->CreateFunctionType(type_, params); + manager->CreateFunctionType(type_, params); context_->AddFunctionType(name_, type); llvm::FunctionType* LLVMType = llvm::cast(type->GetLLVMType()); @@ -55,13 +56,21 @@ Value FunctionNode::CodeGen() { for (auto&& decl : LocalDeclarations_) { decl->CodeGen(); } - // create a variable with the same name as the function for return value - Value rv = builder->CreateAlloca(type_->GetLLVMType()); - locals_.Add(name_, {type_, rv}); + Value rv = nullptr; + bool IsVoid = type_ == manager->GetBuiltinType(BuiltinType::VOID); + if (IsVoid == false) { + // create a variable with the same name as the function for return value + rv = builder->CreateAlloca(type_->GetLLVMType()); + locals_.Add(name_, {type_, rv}); + } // push the function scope variables context_->PushScope(&locals_); body_->CodeGen(); - builder->CreateRet(builder->CreateLoad(type_->GetLLVMType(), rv)); + if (IsVoid == false) { + builder->CreateRet(builder->CreateLoad(type_->GetLLVMType(), rv)); + } else { + builder->CreateRetVoid(); + } context_->PopScope(); std::string msg; llvm::raw_string_ostream MsgStream(msg); -- cgit