#include "ast.hpp" LLVMContext TheContext; IRBuilder<> Builder(TheContext); Module* TheModule; map NamedValues; legacy::FunctionPassManager *TheFPM; extern void yyerror(string s); Value* NumberExprAST::codegen() const { return ConstantFP::get(TheContext, APFloat(Val)); } Value* VariableExprAST::codegen() const { AllocaInst* tmp = NamedValues[Name]; if (tmp == nullptr) yyerror("Promenljiva " + Name + " nije definisana"); return Builder.CreateLoad(tmp, Name); } InnerExprAST::InnerExprAST(ExprAST* e1) { Vec.push_back(e1); } InnerExprAST::InnerExprAST(ExprAST* e1, ExprAST* e2) { Vec.push_back(e1); Vec.push_back(e2); } InnerExprAST::InnerExprAST(ExprAST* e1, ExprAST* e2, ExprAST* e3) { Vec.push_back(e1); Vec.push_back(e2); Vec.push_back(e3); } InnerExprAST::InnerExprAST(ExprAST* e1, ExprAST* e2, ExprAST* e3, ExprAST* e4) { Vec.push_back(e1); Vec.push_back(e2); Vec.push_back(e3); Vec.push_back(e4); } InnerExprAST::~InnerExprAST() { for (auto i : Vec) delete i; } Value* AddExprAST::codegen() const { Value *l = Vec[0]->codegen(); Value *d = Vec[1]->codegen(); if (l == nullptr || d == nullptr) return nullptr; return Builder.CreateFAdd(l, d, "addtmp"); } Value* SubExprAST::codegen() const { Value *l = Vec[0]->codegen(); Value *d = Vec[1]->codegen(); if (l == nullptr || d == nullptr) return nullptr; return Builder.CreateFSub(l, d, "subtmp"); } Value* MulExprAST::codegen() const { Value *l = Vec[0]->codegen(); Value *d = Vec[1]->codegen(); if (l == nullptr || d == nullptr) return nullptr; return Builder.CreateFMul(l, d, "multmp"); } Value* DivExprAST::codegen() const { Value *l = Vec[0]->codegen(); Value *d = Vec[1]->codegen(); if (l == nullptr || d == nullptr) return nullptr; return Builder.CreateFDiv(l, d, "divtmp"); } Value* LtExprAST::codegen() const { Value *l = Vec[0]->codegen(); Value *d = Vec[1]->codegen(); if (l == nullptr || d == nullptr) return nullptr; Value* tmp = Builder.CreateFCmpULT(l, d, "lttmp"); return Builder.CreateUIToFP(tmp, Type::getDoubleTy(TheContext), "booltmp"); } Value* GtExprAST::codegen() const { Value *l = Vec[0]->codegen(); Value *d = Vec[1]->codegen(); if (l == nullptr || d == nullptr) return nullptr; Value* tmp = Builder.CreateFCmpUGT(l, d, "gttmp"); return Builder.CreateUIToFP(tmp, Type::getDoubleTy(TheContext), "booltmp"); } Value* CallExprAST::codegen() const { Function *CalleeF = TheModule->getFunction(Callee); if (CalleeF == nullptr) yyerror("Funkcija " + Callee + " ne postoji"); if (CalleeF->arg_size() != Vec.size()) yyerror("Pogresan broj argumenata fje " + Callee); vector ArgsV; for (unsigned i = 0, e = Vec.size(); i != e; ++i) { ArgsV.push_back(Vec[i]->codegen()); if (!ArgsV.back()) return nullptr; } return Builder.CreateCall(CalleeF, ArgsV, "calltmp"); } Value* IfExprAST::codegen() const { Value* CondV = Vec[0]->codegen(); if (CondV == nullptr) return nullptr; Value *Tmp = Builder.CreateFCmpONE(CondV, ConstantFP::get(TheContext, APFloat(0.0)), "ifcond"); Function* TheFunction = Builder.GetInsertBlock()->getParent(); BasicBlock *ThenBB = BasicBlock::Create(TheContext, "then", TheFunction); BasicBlock *ElseBB = BasicBlock::Create(TheContext, "else"); BasicBlock *MergeBB = BasicBlock::Create(TheContext, "ifcont"); Builder.CreateCondBr(Tmp, ThenBB, ElseBB); Builder.SetInsertPoint(ThenBB); Value* ThenV = Vec[1]->codegen(); if (ThenV == nullptr) return nullptr; Builder.CreateBr(MergeBB); ThenBB = Builder.GetInsertBlock(); TheFunction->getBasicBlockList().push_back(ElseBB); Builder.SetInsertPoint(ElseBB); Value* ElseV = Vec[2]->codegen(); if (ElseV == nullptr) return nullptr; Builder.CreateBr(MergeBB); ElseBB = Builder.GetInsertBlock(); TheFunction->getBasicBlockList().push_back(MergeBB); Builder.SetInsertPoint(MergeBB); PHINode* PHI = Builder.CreatePHI(Type::getDoubleTy(TheContext), 2, "iftmp"); PHI->addIncoming(ThenV, ThenBB); PHI->addIncoming(ElseV, ElseBB); return PHI; } Value* ForExprAST::codegen() const { Function* TheFunction = Builder.GetInsertBlock()->getParent(); BasicBlock *CondBB = BasicBlock::Create(TheContext, "cond"); BasicBlock *LoopBB = BasicBlock::Create(TheContext, "loop"); BasicBlock *AfterLoopBB = BasicBlock::Create(TheContext, "afterloop"); AllocaInst *OldVal = NamedValues[VarName]; AllocaInst *I = CreateEntryBlockAlloca(TheFunction, VarName); NamedValues[VarName] = I; Value* StartV = Vec[0]->codegen(); if (StartV == nullptr) return nullptr; Builder.CreateStore(StartV, I); Builder.CreateBr(CondBB); Builder.SetInsertPoint(CondBB); TheFunction->getBasicBlockList().push_back(CondBB); Value* CondV = Vec[1]->codegen(); if (CondV == nullptr) return nullptr; Value* CondVal = Builder.CreateFCmpONE(CondV, ConstantFP::get(TheContext, APFloat(0.0)), "loopcond"); Builder.CreateCondBr(CondVal, LoopBB, AfterLoopBB); Builder.SetInsertPoint(LoopBB); TheFunction->getBasicBlockList().push_back(LoopBB); Value* BodyV = Vec[3]->codegen(); if (BodyV == nullptr) return nullptr; Value* StepV = Vec[2]->codegen(); if (StepV == nullptr) return nullptr; Value* TmpV = Builder.CreateLoad(I, VarName); Value* NextV = Builder.CreateFAdd(TmpV, StepV, "nextvar"); Builder.CreateStore(NextV, I); Builder.CreateBr(CondBB); Builder.SetInsertPoint(AfterLoopBB); TheFunction->getBasicBlockList().push_back(AfterLoopBB); if (OldVal != nullptr) NamedValues[VarName] = OldVal; else NamedValues.erase(VarName); return ConstantFP::get(TheContext, APFloat(0.0)); } Value* CExprAST::codegen() const { Value *l = Vec[0]->codegen(); Value *d = Vec[1]->codegen(); if (l == nullptr || d == nullptr) return nullptr; return d; } Value* AssignExprAST::codegen() const { Value* Val = Vec[0]->codegen(); if (Val == nullptr) return nullptr; AllocaInst* Alloca = NamedValues[VarName]; if (Alloca == nullptr) yyerror("Promenljiva " + VarName + " ne postoji"); Builder.CreateStore(Val, Alloca); return Val; } Value* VarExprAST::codegen() const { Function* TheFunction = Builder.GetInsertBlock()->getParent(); vector OldValues; for (unsigned i = 0; i < VarNames.size(); i++) { OldValues.push_back(NamedValues[VarNames[i].first]); AllocaInst *alloca = CreateEntryBlockAlloca(TheFunction, VarNames[i].first); NamedValues[VarNames[i].first] = alloca; Value *Val = VarNames[i].second->codegen(); if (Val == nullptr) return nullptr; Builder.CreateStore(Val, alloca); } Value* Body = Vec[0]->codegen(); if (Body == nullptr) return nullptr; for (unsigned i = 0; i < OldValues.size(); i++) if (OldValues[i] == nullptr) NamedValues.erase(VarNames[i].first); else NamedValues[VarNames[i].first] = OldValues[i]; return Body; } Function* PrototypeAST::codegen() const { vector Doubles(Args.size(), Type::getDoubleTy(TheContext)); FunctionType *FT = FunctionType::get(Type::getDoubleTy(TheContext), Doubles, false); Function *F = Function::Create(FT, Function::ExternalLinkage, Name, TheModule); unsigned Idx = 0; for (auto &Arg : F->args()) Arg.setName(Args[Idx++]); return F; } FunctionAST::~FunctionAST() { delete Proto; delete Body; } Function* FunctionAST::codegen() const { Function *TheFunction = TheModule->getFunction(Proto->getName()); if (TheFunction == nullptr) TheFunction = Proto->codegen(); if (!TheFunction) return nullptr; if (!TheFunction->empty()) yyerror("Funkcija " + Proto->getName() + " ne moze da se redefinise"); BasicBlock *BB = BasicBlock::Create(TheContext, "entry", TheFunction); Builder.SetInsertPoint(BB); NamedValues.clear(); for (auto &Arg : TheFunction->args()) { AllocaInst* alloca = CreateEntryBlockAlloca(TheFunction, Arg.getName()); Builder.CreateStore(&Arg, alloca); NamedValues[Arg.getName()] = alloca; } if (Value *RetVal = Body->codegen()) { Builder.CreateRet(RetVal); verifyFunction(*TheFunction); TheFPM->run(*TheFunction); return TheFunction; } TheFunction->eraseFromParent(); return nullptr; } AllocaInst *CreateEntryBlockAlloca(Function *TheFunction, const std::string &VarName) { IRBuilder<> TmpB(&TheFunction->getEntryBlock(), TheFunction->getEntryBlock().begin()); return TmpB.CreateAlloca(Type::getDoubleTy(TheContext), 0, VarName); }