aboutsummaryrefslogtreecommitdiff
path: root/lib/CodeGen
diff options
context:
space:
mode:
Diffstat (limited to 'lib/CodeGen')
-rw-r--r--lib/CodeGen/CodeGen.cc42
-rw-r--r--lib/CodeGen/FunctionGen.cc125
2 files changed, 166 insertions, 1 deletions
diff --git a/lib/CodeGen/CodeGen.cc b/lib/CodeGen/CodeGen.cc
index 9dc88af..6358699 100644
--- a/lib/CodeGen/CodeGen.cc
+++ b/lib/CodeGen/CodeGen.cc
@@ -17,6 +17,8 @@ CodeGen::CodeGen() {
Arrays = std::make_unique<ArrayHandler>(*TheContext, *Builder, *TheModule, NamedValues, *RuntimeChecker);
+ FuncGen = std::make_unique<FunctionGen>(*TheContext, *TheModule, *Builder, NamedValues);
+
SetupExternalFunctions();
}
@@ -105,6 +107,15 @@ Value *CodeGen::emitExpr(ExprAST *Expr) {
return nullptr;
}
}
+
+ if (auto *Call = dynamic_cast<CallExprAST*>(Expr)) {
+ std::vector<Value*> Args;
+ for (const auto &Arg : Call->getArgs()) {
+ Args.push_back(emitExpr(Arg.get()));
+ }
+ return FuncGen->emitCallExpr(Call, Args);
+ }
+
return nullptr;
}
@@ -261,6 +272,36 @@ void CodeGen::emitStmt(StmtAST *Stmt) {
return;
}
+ if (auto *FuncDef = dynamic_cast<FunctionDefAST*>(Stmt)) {
+ BasicBlock *SavedBlock = Builder->GetInsertBlock();
+
+ FuncGen->emitFunctionDef(FuncDef, [this](StmtAST *S) {
+ this->emitStmt(S);
+ });
+
+ if (SavedBlock) Builder->SetInsertPoint(SavedBlock);
+
+ return;
+ }
+
+ else if (auto *Call = dynamic_cast<CallStmtAST*>(Stmt)) {
+ std::vector<Value*> Args;
+ for (const auto &ArgExpr : Call->getArgs()) {
+ Args.push_back(emitExpr(ArgExpr.get()));
+ }
+ FuncGen->emitCallStmt(Call, Args);
+ return;
+ }
+
+ else if (auto *Ret = dynamic_cast<ReturnStmtAST*>(Stmt)) {
+ Value *RetVal = nullptr;
+ if (Ret->getRetVal()) {
+ RetVal = emitExpr(Ret->getRetVal());
+ }
+ FuncGen->emitReturn(Ret, RetVal);
+ return;
+ }
+
if (auto *Decl = dynamic_cast<DeclareStmtAST*>(Stmt)) {
Function *TheFunction = Builder->GetInsertBlock()->getParent();
AllocaInst *Alloca = CreateEntryBlockAlloca(TheFunction, Decl->getName());
@@ -313,4 +354,3 @@ void CodeGen::emitStmt(StmtAST *Stmt) {
emitForStmt(ForStmt);
}
}
-
diff --git a/lib/CodeGen/FunctionGen.cc b/lib/CodeGen/FunctionGen.cc
new file mode 100644
index 0000000..b2adc08
--- /dev/null
+++ b/lib/CodeGen/FunctionGen.cc
@@ -0,0 +1,125 @@
+#include "cps/FunctionGen.h"
+#include "llvm/IR/Verifier.h"
+#include <iostream>
+
+using namespace llvm;
+using namespace cps;
+
+Type *FunctionGen::getLLVMType(const std::string &TypeName) {
+ if (TypeName == "INTEGER") return Type::getInt64Ty(Context);
+ if (TypeName == "BOOLEAN") return Type::getInt1Ty(Context);
+ if (TypeName == "REAL") return Type::getDoubleTy(Context);
+ if (TypeName == "VOID") return Type::getVoidTy(Context);
+ return Type::getInt64Ty(Context);
+}
+
+void FunctionGen::createArgumentAllocas(Function *F, const std::vector<std::pair<std::string, std::string>> &Args) {
+ Function::arg_iterator AI = F->arg_begin();
+ for (unsigned Idx = 0, E = Args.size(); Idx != E; ++Idx, ++AI) {
+ std::string ArgName = Args[Idx].first;
+ std::string ArgTypeStr = Args[Idx].second;
+
+ Type *ArgType = getLLVMType(ArgTypeStr);
+ IRBuilder<> TmpB(&F->getEntryBlock(), F->getEntryBlock().begin());
+ AllocaInst *Alloca = TmpB.CreateAlloca(ArgType, nullptr, ArgName);
+
+ Value *ArgVal = &(*AI);
+ Builder.CreateStore(ArgVal, Alloca);
+
+ NamedValues[ArgName] = Alloca;
+ }
+}
+
+Function *FunctionGen::emitPrototype(PrototypeAST *Proto) {
+ std::vector<Type*> ArgTypes;
+ for (const auto &Arg : Proto->getArgs()) {
+ ArgTypes.push_back(getLLVMType(Arg.second));
+ }
+
+ Type *RetType = getLLVMType(Proto->getReturnType());
+ FunctionType *FT = FunctionType::get(RetType, ArgTypes, false);
+ Function *F = Function::Create(FT, Function::ExternalLinkage, Proto->getName(), &Module);
+
+ unsigned Idx = 0;
+ for (auto &Arg : F->args()) {
+ if (Idx < Proto->getArgs().size())
+ Arg.setName(Proto->getArgs()[Idx++].first);
+ }
+ return F;
+}
+
+Function *FunctionGen::emitFunctionDef(FunctionDefAST *FuncAST,
+ const std::function<void(StmtAST*)> &StmtEmitter) {
+ PrototypeAST *Proto = FuncAST->getProto();
+ Function *TheFunction = Module.getFunction(Proto->getName());
+
+ if (!TheFunction)
+ TheFunction = emitPrototype(Proto);
+
+ if (!TheFunction) return nullptr;
+ if (!TheFunction->empty()) {
+ fprintf(stderr, "Error: Function %s cannot be redefined.\n", Proto->getName().c_str());
+ return nullptr;
+ }
+
+ BasicBlock *BB = BasicBlock::Create(Context, "entry", TheFunction);
+ Builder.SetInsertPoint(BB);
+
+ std::map<std::string, llvm::AllocaInst*> OldNamedValues = NamedValues;
+ NamedValues.clear();
+
+ createArgumentAllocas(TheFunction, Proto->getArgs());
+
+ for (const auto &Stmt : FuncAST->getBody()) {
+ StmtEmitter(Stmt.get());
+ }
+
+ if (!Builder.GetInsertBlock()->getTerminator()) {
+ if (Proto->getReturnType() == "VOID")
+ Builder.CreateRetVoid();
+ else
+ Builder.CreateRet(Constant::getNullValue(TheFunction->getReturnType()));
+ }
+
+ verifyFunction(*TheFunction);
+ NamedValues = OldNamedValues;
+ return TheFunction;
+}
+
+static Value *GenerateCall(llvm::Module &Module, llvm::IRBuilder<> &Builder, llvm::LLVMContext &Context,
+ const std::string &CalleeName, const std::vector<llvm::Value*> &Args) {
+ Function *CalleeF = Module.getFunction(CalleeName);
+ if (!CalleeF) {
+ std::vector<Type*> ArgTypes;
+ for (auto *Val : Args) ArgTypes.push_back(Val->getType());
+ FunctionType *FT = FunctionType::get(Type::getInt64Ty(Context), ArgTypes, false);
+ CalleeF = Function::Create(FT, Function::ExternalLinkage, CalleeName, &Module);
+ }
+
+ if (CalleeF->arg_size() != Args.size()) {
+ fprintf(stderr, "Error: Incorrect # arguments passed to %s\n", CalleeName.c_str());
+ return nullptr;
+ }
+
+ if (CalleeF->getReturnType()->isVoidTy()) {
+ return Builder.CreateCall(CalleeF, Args);
+ }
+
+ return Builder.CreateCall(CalleeF, Args, "calltmp");
+}
+
+llvm::Value *FunctionGen::emitCallExpr(CallExprAST *Call, const std::vector<llvm::Value*> &Args) {
+ return GenerateCall(Module, Builder, Context, Call->getCallee(), Args);
+}
+
+void FunctionGen::emitCallStmt(CallStmtAST *Call, const std::vector<llvm::Value*> &Args) {
+ GenerateCall(Module, Builder, Context, Call->getCallee(), Args);
+}
+
+void FunctionGen::emitReturn(ReturnStmtAST *Ret, llvm::Value *RetVal) {
+ if (RetVal) {
+ Builder.CreateRet(RetVal);
+ } else {
+ Builder.CreateRetVoid();
+ }
+}