Ast.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. // LLVM includes
  2. #include "llvm/IR/Verifier.h"
  3. // Local includes
  4. #include "Ast.h"
  5. #include "Parser.h"
  6. #include "JIT.h"
  7. using namespace jit;
  8. namespace ast {
  9. // attributes added from chapter 3 of the tutorial
  10. std::unique_ptr<Module> AstObjects::TheModule =
  11. std::make_unique<Module>("my cool jit", getGlobalContext());
  12. IRBuilder<> AstObjects::Builder(getGlobalContext());
  13. std::map<std::string, AllocaInst*> AstObjects::NamedValues;
  14. Value *ErrorV(const char *Str) {
  15. parser::Error(Str);
  16. return nullptr;
  17. }
  18. Value *NumberExprAST::codegen() {
  19. KSDbgInfo.emitLocation(this);
  20. return ConstantFP::get(getGlobalContext(), APFloat(Val));
  21. }
  22. Value *VariableExprAST::codegen() {
  23. // Look this variable up in the function.
  24. Value *V = AstObjects::NamedValues[Name];
  25. if (!V)
  26. return ErrorV("Unknown variable name");
  27. KSDbgInfo.emitLocation(this);
  28. // Load the value.
  29. return AstObjects::Builder.CreateLoad(V, Name.c_str());
  30. }
  31. Value *UnaryExprAST::codegen() {
  32. Value *OperandV = Operand->codegen();
  33. if (!OperandV)
  34. return nullptr;
  35. Function *F = getFunction(std::string("unary") + Opcode);
  36. if (!F)
  37. return ErrorV("Unknown unary operator");
  38. KSDbgInfo.emitLocation(this);
  39. return AstObjects::Builder.CreateCall(F, OperandV, "unop");
  40. }
  41. Value *BinaryExprAST::codegen() {
  42. KSDbgInfo.emitLocation(this);
  43. // Special case '=' because we don't want to emit the LHS as an expression.
  44. if (Op == '=') {
  45. // Assignment requires the LHS to be an identifier.
  46. // This assume we're building without RTTI because LLVM builds that way by
  47. // default. If you build LLVM with RTTI this can be changed to a
  48. // dynamic_cast for automatic error checking.
  49. VariableExprAST *LHSE = static_cast<VariableExprAST *>(LHS.get());
  50. if (!LHSE)
  51. return ErrorV("destination of '=' must be a variable");
  52. // Codegen the RHS.
  53. Value *Val = RHS->codegen();
  54. if (!Val)
  55. return nullptr;
  56. // Look up the name.
  57. Value *Variable = AstObjects::NamedValues[LHSE->getName()];
  58. if (!Variable)
  59. return ErrorV("Unknown variable name");
  60. AstObjects::Builder.CreateStore(Val, Variable);
  61. return Val;
  62. }
  63. Value *L = LHS->codegen();
  64. Value *R = RHS->codegen();
  65. if (!L || !R)
  66. return nullptr;
  67. switch (Op) {
  68. case '+':
  69. return AstObjects::Builder.CreateFAdd(L, R, "addtmp");
  70. case '-':
  71. return AstObjects::Builder.CreateFSub(L, R, "subtmp");
  72. case '*':
  73. return AstObjects::Builder.CreateFMul(L, R, "multmp");
  74. case '<':
  75. L = AstObjects::Builder.CreateFCmpULT(L, R, "cmptmp");
  76. // Convert bool 0/1 to double 0.0 or 1.0
  77. return AstObjects::Builder.CreateUIToFP(L, Type::getDoubleTy(getGlobalContext()),
  78. "booltmp");
  79. default:
  80. break;
  81. }
  82. // If it wasn't a builtin binary operator, it must be a user defined one. Emit
  83. // a call to it.
  84. Function *F = getFunction(std::string("binary") + Op);
  85. assert(F && "binary operator not found!");
  86. Value *Ops[] = {L, R};
  87. return AstObjects::Builder.CreateCall(F, Ops, "binop");
  88. }
  89. Value *CallExprAST::codegen() {
  90. KSDbgInfo.emitLocation(this);
  91. // Look up the name in the global module table.
  92. Function *CalleeF = getFunction(Callee);
  93. if (!CalleeF)
  94. return ErrorV("Unknown function referenced");
  95. // If argument mismatch error.
  96. if (CalleeF->arg_size() != Args.size())
  97. return ErrorV("Incorrect # arguments passed");
  98. std::vector<Value *> ArgsV;
  99. for (unsigned i = 0, e = Args.size(); i != e; ++i) {
  100. ArgsV.push_back(Args[i]->codegen());
  101. if (!ArgsV.back())
  102. return nullptr;
  103. }
  104. return AstObjects::Builder.CreateCall(CalleeF, ArgsV, "calltmp");
  105. }
  106. Value *IfExprAST::codegen() {
  107. KSDbgInfo.emitLocation(this);
  108. Value *CondV = Cond->codegen();
  109. if (!CondV)
  110. return nullptr;
  111. // Convert condition to a bool by comparing equal to 0.0.
  112. CondV = AstObjects::Builder.CreateFCmpONE(
  113. CondV, ConstantFP::get(getGlobalContext(), APFloat(0.0)), "ifcond");
  114. Function *TheFunction = AstObjects::Builder.GetInsertBlock()->getParent();
  115. // Create blocks for the then and else cases. Insert the 'then' block at the
  116. // end of the function.
  117. BasicBlock *ThenBB =
  118. BasicBlock::Create(getGlobalContext(), "then", TheFunction);
  119. BasicBlock *ElseBB = BasicBlock::Create(getGlobalContext(), "else");
  120. BasicBlock *MergeBB = BasicBlock::Create(getGlobalContext(), "ifcont");
  121. AstObjects::Builder.CreateCondBr(CondV, ThenBB, ElseBB);
  122. // Emit then value.
  123. AstObjects::Builder.SetInsertPoint(ThenBB);
  124. Value *ThenV = Then->codegen();
  125. if (!ThenV)
  126. return nullptr;
  127. AstObjects::Builder.CreateBr(MergeBB);
  128. // Codegen of 'Then' can change the current block, update ThenBB for the PHI.
  129. ThenBB = AstObjects::Builder.GetInsertBlock();
  130. // Emit else block.
  131. TheFunction->getBasicBlockList().push_back(ElseBB);
  132. AstObjects::Builder.SetInsertPoint(ElseBB);
  133. Value *ElseV = Else->codegen();
  134. if (!ElseV)
  135. return nullptr;
  136. AstObjects::Builder.CreateBr(MergeBB);
  137. // codegen of 'Else' can change the current block, update ElseBB for the PHI.
  138. ElseBB = AstObjects::Builder.GetInsertBlock();
  139. // Emit merge block.
  140. TheFunction->getBasicBlockList().push_back(MergeBB);
  141. AstObjects::Builder.SetInsertPoint(MergeBB);
  142. PHINode *PN =
  143. AstObjects::Builder.CreatePHI(Type::getDoubleTy(getGlobalContext()), 2, "iftmp");
  144. PN->addIncoming(ThenV, ThenBB);
  145. PN->addIncoming(ElseV, ElseBB);
  146. return PN;
  147. }
  148. // Output for-loop as:
  149. // var = alloca double
  150. // ...
  151. // start = startexpr
  152. // store start -> var
  153. // goto loop
  154. // loop:
  155. // ...
  156. // bodyexpr
  157. // ...
  158. // loopend:
  159. // step = stepexpr
  160. // endcond = endexpr
  161. //
  162. // curvar = load var
  163. // nextvar = curvar + step
  164. // store nextvar -> var
  165. // br endcond, loop, endloop
  166. // outloop:
  167. Value *ForExprAST::codegen() {
  168. Function *TheFunction = AstObjects::Builder.GetInsertBlock()->getParent();
  169. // Create an alloca for the variable in the entry block.
  170. AllocaInst *Alloca = CreateEntryBlockAlloca(TheFunction, VarName);
  171. KSDbgInfo.emitLocation(this);
  172. // Emit the start code first, without 'variable' in scope.
  173. Value *StartVal = Start->codegen();
  174. if (!StartVal)
  175. return nullptr;
  176. // Store the value into the alloca.
  177. AstObjects::Builder.CreateStore(StartVal, Alloca);
  178. // Make the new basic block for the loop header, inserting after current
  179. // block.
  180. BasicBlock *LoopBB =
  181. BasicBlock::Create(getGlobalContext(), "loop", TheFunction);
  182. // Insert an explicit fall through from the current block to the LoopBB.
  183. AstObjects::Builder.CreateBr(LoopBB);
  184. // Start insertion in LoopBB.
  185. AstObjects::Builder.SetInsertPoint(LoopBB);
  186. // Within the loop, the variable is defined equal to the PHI node. If it
  187. // shadows an existing variable, we have to restore it, so save it now.
  188. AllocaInst *OldVal = AstObjects::NamedValues[VarName];
  189. AstObjects::NamedValues[VarName] = Alloca;
  190. // Emit the body of the loop. This, like any other expr, can change the
  191. // current BB. Note that we ignore the value computed by the body, but don't
  192. // allow an error.
  193. if (!Body->codegen())
  194. return nullptr;
  195. // Emit the step value.
  196. Value *StepVal = nullptr;
  197. if (Step) {
  198. StepVal = Step->codegen();
  199. if (!StepVal)
  200. return nullptr;
  201. } else {
  202. // If not specified, use 1.0.
  203. StepVal = ConstantFP::get(getGlobalContext(), APFloat(1.0));
  204. }
  205. // Compute the end condition.
  206. Value *EndCond = End->codegen();
  207. if (!EndCond)
  208. return nullptr;
  209. // Reload, increment, and restore the alloca. This handles the case where
  210. // the body of the loop mutates the variable.
  211. Value *CurVar = AstObjects::Builder.CreateLoad(Alloca, VarName.c_str());
  212. Value *NextVar = AstObjects::Builder.CreateFAdd(CurVar, StepVal, "nextvar");
  213. AstObjects::Builder.CreateStore(NextVar, Alloca);
  214. // Convert condition to a bool by comparing equal to 0.0.
  215. EndCond = AstObjects::Builder.CreateFCmpONE(
  216. EndCond, ConstantFP::get(getGlobalContext(), APFloat(0.0)), "loopcond");
  217. // Create the "after loop" block and insert it.
  218. BasicBlock *AfterBB =
  219. BasicBlock::Create(getGlobalContext(), "afterloop", TheFunction);
  220. // Insert the conditional branch into the end of LoopEndBB.
  221. AstObjects::Builder.CreateCondBr(EndCond, LoopBB, AfterBB);
  222. // Any new code will be inserted in AfterBB.
  223. AstObjects::Builder.SetInsertPoint(AfterBB);
  224. // Restore the unshadowed variable.
  225. if (OldVal)
  226. AstObjects::NamedValues[VarName] = OldVal;
  227. else
  228. AstObjects::NamedValues.erase(VarName);
  229. // for expr always returns 0.0.
  230. return Constant::getNullValue(Type::getDoubleTy(getGlobalContext()));
  231. }
  232. Value *VarExprAST::codegen() {
  233. std::vector<AllocaInst *> OldBindings;
  234. Function *TheFunction = AstObjects::Builder.GetInsertBlock()->getParent();
  235. // Register all variables and emit their initializer.
  236. for (unsigned i = 0, e = VarNames.size(); i != e; ++i) {
  237. const std::string &VarName = VarNames[i].first;
  238. ExprAST *Init = VarNames[i].second.get();
  239. // Emit the initializer before adding the variable to scope, this prevents
  240. // the initializer from referencing the variable itself, and permits stuff
  241. // like this:
  242. // var a = 1 in
  243. // var a = a in ... # refers to outer 'a'.
  244. Value *InitVal;
  245. if (Init) {
  246. InitVal = Init->codegen();
  247. if (!InitVal)
  248. return nullptr;
  249. } else { // If not specified, use 0.0.
  250. InitVal = ConstantFP::get(getGlobalContext(), APFloat(0.0));
  251. }
  252. AllocaInst *Alloca = CreateEntryBlockAlloca(TheFunction, VarName);
  253. AstObjects::Builder.CreateStore(InitVal, Alloca);
  254. // Remember the old variable binding so that we can restore the binding when
  255. // we unrecurse.
  256. OldBindings.push_back(AstObjects::NamedValues[VarName]);
  257. // Remember this binding.
  258. AstObjects::NamedValues[VarName] = Alloca;
  259. }
  260. KSDbgInfo.emitLocation(this);
  261. // Codegen the body, now that all vars are in scope.
  262. Value *BodyVal = Body->codegen();
  263. if (!BodyVal)
  264. return nullptr;
  265. // Pop all our variables from scope.
  266. for (unsigned i = 0, e = VarNames.size(); i != e; ++i)
  267. AstObjects::NamedValues[VarNames[i].first] = OldBindings[i];
  268. // Return the body computation.
  269. return BodyVal;
  270. }
  271. Function *PrototypeAST::codegen() {
  272. // Make the function type: double(double,double) etc.
  273. std::vector<Type *> Doubles(Args.size(),
  274. Type::getDoubleTy(getGlobalContext()));
  275. FunctionType *FT = FunctionType::get(Type::getDoubleTy(
  276. getGlobalContext()), Doubles, false);
  277. Function *F = Function::Create(FT, Function::ExternalLinkage, Name,
  278. AstObjects::TheModule.get());
  279. // Set names for all arguments.
  280. unsigned Idx = 0;
  281. for (auto &Arg : F->args())
  282. Arg.setName(Args[Idx++]);
  283. return F;
  284. }
  285. Function *FunctionAST::codegen() {
  286. // Transfer ownership of the prototype to the FunctionProtos map, but keep a
  287. // reference to it for use below.
  288. auto &P = *Proto;
  289. JITObjects::FunctionProtos[Proto->getName()] = std::move(Proto);
  290. Function *TheFunction = getFunction(P.getName());
  291. if (!TheFunction)
  292. return nullptr;
  293. // If this is an operator, install it.
  294. if (P.isBinaryOp())
  295. parser::BinopPrecedence[P.getOperatorName()] = P.getBinaryPrecedence();
  296. // Create a new basic block to start insertion into.
  297. BasicBlock *BB = BasicBlock::Create(getGlobalContext(), "entry", TheFunction);
  298. AstObjects::Builder.SetInsertPoint(BB);
  299. // Create a subprogram DIE for this function.
  300. DIFile *Unit = DBuilder->createFile(KSDbgInfo.TheCU->getFilename(),
  301. KSDbgInfo.TheCU->getDirectory());
  302. DIScope *FContext = Unit;
  303. unsigned LineNo = P.getLine();
  304. unsigned ScopeLine = LineNo;
  305. DISubprogram *SP = DBuilder->createFunction(
  306. FContext, P.getName(), StringRef(), Unit, LineNo,
  307. CreateFunctionType(TheFunction->arg_size(), Unit),
  308. false /* internal linkage */, true /* definition */, ScopeLine,
  309. DINode::FlagPrototyped, false);
  310. TheFunction->setSubprogram(SP);
  311. // Push the current scope.
  312. KSDbgInfo.LexicalBlocks.push_back(SP);
  313. // Unset the location for the prologue emission (leading instructions with no
  314. // location in a function are considered part of the prologue and the debugger
  315. // will run past them when breaking on a function)
  316. KSDbgInfo.emitLocation(nullptr);
  317. // Record the function arguments in the NamedValues map.
  318. AstObjects::NamedValues.clear();
  319. unsigned ArgIdx = 0;
  320. for (auto &Arg : TheFunction->args()) {
  321. // Create an alloca for this variable.
  322. AllocaInst *Alloca = CreateEntryBlockAlloca(TheFunction, Arg.getName());
  323. // Create a debug descriptor for the variable.
  324. DILocalVariable *D = DBuilder->createParameterVariable(
  325. SP, Arg.getName(), ++ArgIdx, Unit, LineNo, KSDbgInfo.getDoubleTy(),
  326. true);
  327. DBuilder->insertDeclare(Alloca, D, DBuilder->createExpression(),
  328. DebugLoc::get(LineNo, 0, SP),
  329. Builder.GetInsertBlock());
  330. // Store the initial value into the alloca.
  331. AstObjects::Builder.CreateStore(&Arg, Alloca);
  332. // Add arguments to variable symbol table.
  333. AstObjects::NamedValues[Arg.getName()] = Alloca;
  334. }
  335. KSDbgInfo.emitLocation(Body.get());
  336. if (Value *RetVal = Body->codegen()) {
  337. // Finish off the function.
  338. AstObjects::Builder.CreateRet(RetVal);
  339. // Pop off the lexical block for the function.
  340. KSDbgInfo.LexicalBlocks.pop_back();
  341. // Validate the generated code, checking for consistency.
  342. verifyFunction(*TheFunction);
  343. return TheFunction;
  344. }
  345. // Error reading body, remove function.
  346. TheFunction->eraseFromParent();
  347. if (P.isBinaryOp())
  348. parser::BinopPrecedence.erase(Proto->getOperatorName());
  349. // Pop off the lexical block for the function since we added it
  350. // unconditionally.
  351. KSDbgInfo.LexicalBlocks.pop_back();
  352. return nullptr;
  353. }
  354. /// CreateEntryBlockAlloca - Create an alloca instruction in the entry block of
  355. /// the function. This is used for mutable variables etc.
  356. static AllocaInst *CreateEntryBlockAlloca(Function *TheFunction,
  357. const std::string &VarName) {
  358. IRBuilder<> TmpB(&TheFunction->getEntryBlock(),
  359. TheFunction->getEntryBlock().begin());
  360. return TmpB.CreateAlloca(Type::getDoubleTy(getGlobalContext()), nullptr,
  361. VarName.c_str());
  362. }
  363. // HACK: Libraries not present in the standard llvm installation, but the
  364. // is not actually used.
  365. /*
  366. /// CreateArgumentAllocas - Create an alloca for each argument and register the
  367. /// argument in the symbol table so that references to it will succeed.
  368. void PrototypeAST::CreateArgumentAllocas(Function *F) {
  369. Function::arg_iterator AI = F->arg_begin();
  370. for (unsigned Idx = 0, e = Args.size(); Idx != e; ++Idx, ++AI) {
  371. // Create an alloca for this variable.
  372. AllocaInst *Alloca = CreateEntryBlockAlloca(F, Args[Idx]);
  373. // Store the initial value into the alloca.
  374. AstObjects::Builder.CreateStore(AI, Alloca);
  375. // Add arguments to variable symbol table.
  376. AstObjects::NamedValues[Args[Idx]] = Alloca;
  377. }
  378. }*/
  379. }