From bd799e42b40613983c6e3cb9aedf58866ac68605 Mon Sep 17 00:00:00 2001 From: _ov4 Date: Fri, 30 Jan 2026 13:11:20 +0000 Subject: Added FUNCTION and PROCEDURE --- lib/CodeGen/CodeGen.cc | 42 ++++++++++++++- lib/CodeGen/FunctionGen.cc | 125 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 166 insertions(+), 1 deletion(-) create mode 100644 lib/CodeGen/FunctionGen.cc (limited to 'lib/CodeGen') 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(*TheContext, *Builder, *TheModule, NamedValues, *RuntimeChecker); + FuncGen = std::make_unique(*TheContext, *TheModule, *Builder, NamedValues); + SetupExternalFunctions(); } @@ -105,6 +107,15 @@ Value *CodeGen::emitExpr(ExprAST *Expr) { return nullptr; } } + + if (auto *Call = dynamic_cast(Expr)) { + std::vector 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(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(Stmt)) { + std::vector Args; + for (const auto &ArgExpr : Call->getArgs()) { + Args.push_back(emitExpr(ArgExpr.get())); + } + FuncGen->emitCallStmt(Call, Args); + return; + } + + else if (auto *Ret = dynamic_cast(Stmt)) { + Value *RetVal = nullptr; + if (Ret->getRetVal()) { + RetVal = emitExpr(Ret->getRetVal()); + } + FuncGen->emitReturn(Ret, RetVal); + return; + } + if (auto *Decl = dynamic_cast(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 + +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> &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 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 &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 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 &Args) { + Function *CalleeF = Module.getFunction(CalleeName); + if (!CalleeF) { + std::vector 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 &Args) { + return GenerateCall(Module, Builder, Context, Call->getCallee(), Args); +} + +void FunctionGen::emitCallStmt(CallStmtAST *Call, const std::vector &Args) { + GenerateCall(Module, Builder, Context, Call->getCallee(), Args); +} + +void FunctionGen::emitReturn(ReturnStmtAST *Ret, llvm::Value *RetVal) { + if (RetVal) { + Builder.CreateRet(RetVal); + } else { + Builder.CreateRetVoid(); + } +} -- cgit v1.3.1-4-g156e