Ast.cpp 3.5 KB

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