Ast.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // LLVM includes
  2. #include "llvm/IR/Verifier.h"
  3. // Local includes
  4. #include "Ast.h"
  5. #include "Parser.h"
  6. namespace ast{
  7. // attributes added from chapter 3 of the tutorial
  8. std::unique_ptr<Module> AstObjects::TheModule = std::make_unique<Module>("my cool jit", getGlobalContext());
  9. IRBuilder<> AstObjects::Builder(getGlobalContext());
  10. std::map<std::string, Value *> AstObjects::NamedValues;
  11. Value *ErrorV(const char *Str) {
  12. parser::Error(Str);
  13. return nullptr;
  14. }
  15. Value *NumberExprAST::codegen() {
  16. return ConstantFP::get(getGlobalContext(), APFloat(Val));
  17. }
  18. Value *VariableExprAST::codegen() {
  19. // Look this variable up in the function.
  20. Value *V = AstObjects::NamedValues[Name];
  21. if (!V)
  22. ErrorV("Unknown variable name");
  23. return V;
  24. }
  25. Value *BinaryExprAST::codegen() {
  26. Value *L = LHS->codegen();
  27. Value *R = RHS->codegen();
  28. if (!L || !R)
  29. return nullptr;
  30. switch (Op) {
  31. case '+':
  32. return AstObjects::Builder.CreateFAdd(L, R, "addtmp");
  33. case '-':
  34. return AstObjects::Builder.CreateFSub(L, R, "subtmp");
  35. case '*':
  36. return AstObjects::Builder.CreateFMul(L, R, "multmp");
  37. case '<':
  38. L = AstObjects::Builder.CreateFCmpULT(L, R, "cmptmp");
  39. // Convert bool 0/1 to double 0.0 or 1.0
  40. return AstObjects::Builder.CreateUIToFP(L, Type::getDoubleTy(getGlobalContext()),
  41. "booltmp");
  42. default:
  43. return ErrorV("invalid binary operator");
  44. }
  45. }
  46. Value *CallExprAST::codegen() {
  47. // Look up the name in the global module table.
  48. Function *CalleeF = AstObjects::TheModule->getFunction(Callee);
  49. if (!CalleeF)
  50. return ErrorV("Unknown function referenced");
  51. // If argument mismatch error.
  52. if (CalleeF->arg_size() != Args.size())
  53. return ErrorV("Incorrect # arguments passed");
  54. std::vector<Value *> ArgsV;
  55. for (unsigned i = 0, e = Args.size(); i != e; ++i) {
  56. ArgsV.push_back(Args[i]->codegen());
  57. if (!ArgsV.back())
  58. return nullptr;
  59. }
  60. return AstObjects::Builder.CreateCall(CalleeF, ArgsV, "calltmp");
  61. }
  62. Function *PrototypeAST::codegen() {
  63. // Make the function type: double(double,double) etc.
  64. std::vector<Type *> Doubles(Args.size(), Type::getDoubleTy(getGlobalContext()));
  65. FunctionType *FT = FunctionType::get(Type::getDoubleTy(getGlobalContext()), Doubles, false);
  66. Function *F = Function::Create(FT, Function::ExternalLinkage, Name, AstObjects::TheModule.get());
  67. // Set names for all arguments.
  68. unsigned Idx = 0;
  69. for (auto &Arg : F->args())
  70. Arg.setName(Args[Idx++]);
  71. return F;
  72. }
  73. const std::string &PrototypeAST::getName() const { return Name; }
  74. Function *FunctionAST::codegen() {
  75. // First, check for an existing function from a previous 'extern' declaration.
  76. Function *TheFunction = AstObjects::TheModule->getFunction(Proto->getName());
  77. if (!TheFunction)
  78. TheFunction = Proto->codegen();
  79. if (!TheFunction)
  80. return nullptr;
  81. if (!TheFunction->empty())
  82. return (Function*)ErrorV("Function cannot be redefined.");
  83. // Create a new basic block to start insertion into.
  84. BasicBlock *BB = BasicBlock::Create(getGlobalContext(), "entry", TheFunction);
  85. AstObjects::Builder.SetInsertPoint(BB);
  86. // Record the function arguments in the NamedValues map.
  87. AstObjects::NamedValues.clear();
  88. for (auto &Arg : TheFunction->args()) {
  89. AstObjects::NamedValues[Arg.getName()] = &Arg;
  90. }
  91. if (Value *RetVal = Body->codegen()) {
  92. // Finish off the function.
  93. AstObjects::Builder.CreateRet(RetVal);
  94. // Validate the generated code, checking for consistency.
  95. verifyFunction(*TheFunction);
  96. return TheFunction;
  97. }
  98. // Error reading body, remove function.
  99. TheFunction->eraseFromParent();
  100. return nullptr;
  101. }
  102. }