Chapter5.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945
  1. #include "llvm/ADT/STLExtras.h"
  2. #include "llvm/Analysis/Passes.h"
  3. #include "llvm/IR/IRBuilder.h"
  4. #include "llvm/IR/LLVMContext.h"
  5. #include "llvm/IR/LegacyPassManager.h"
  6. #include "llvm/IR/Module.h"
  7. #include "llvm/IR/Verifier.h"
  8. #include "llvm/Support/TargetSelect.h"
  9. #include "llvm/Transforms/Scalar.h"
  10. #include <cctype>
  11. #include <cstdio>
  12. #include <map>
  13. #include <string>
  14. #include <vector>
  15. #include "../KaleidoscopeJIT.h"
  16. using namespace llvm;
  17. using namespace llvm::orc;
  18. //===----------------------------------------------------------------------===//
  19. // Lexer
  20. //===----------------------------------------------------------------------===//
  21. // The lexer returns tokens [0-255] if it is an unknown character, otherwise one
  22. // of these for known things.
  23. enum Token {
  24. tok_eof = -1,
  25. // commands
  26. tok_def = -2,
  27. tok_extern = -3,
  28. // primary
  29. tok_identifier = -4,
  30. tok_number = -5,
  31. // control
  32. tok_if = -6,
  33. tok_then = -7,
  34. tok_else = -8,
  35. tok_for = -9,
  36. tok_in = -10
  37. };
  38. static std::string IdentifierStr; // Filled in if tok_identifier
  39. static double NumVal; // Filled in if tok_number
  40. /// gettok - Return the next token from standard input.
  41. static int gettok() {
  42. static int LastChar = ' ';
  43. // Skip any whitespace.
  44. while (isspace(LastChar))
  45. LastChar = getchar();
  46. if (isalpha(LastChar)) { // identifier: [a-zA-Z][a-zA-Z0-9]*
  47. IdentifierStr = LastChar;
  48. while (isalnum((LastChar = getchar())))
  49. IdentifierStr += LastChar;
  50. if (IdentifierStr == "def")
  51. return tok_def;
  52. if (IdentifierStr == "extern")
  53. return tok_extern;
  54. if (IdentifierStr == "if")
  55. return tok_if;
  56. if (IdentifierStr == "then")
  57. return tok_then;
  58. if (IdentifierStr == "else")
  59. return tok_else;
  60. if (IdentifierStr == "for")
  61. return tok_for;
  62. if (IdentifierStr == "in")
  63. return tok_in;
  64. return tok_identifier;
  65. }
  66. if (isdigit(LastChar) || LastChar == '.') { // Number: [0-9.]+
  67. std::string NumStr;
  68. do {
  69. NumStr += LastChar;
  70. LastChar = getchar();
  71. } while (isdigit(LastChar) || LastChar == '.');
  72. NumVal = strtod(NumStr.c_str(), nullptr);
  73. return tok_number;
  74. }
  75. if (LastChar == '#') {
  76. // Comment until end of line.
  77. do
  78. LastChar = getchar();
  79. while (LastChar != EOF && LastChar != '\n' && LastChar != '\r');
  80. if (LastChar != EOF)
  81. return gettok();
  82. }
  83. // Check for end of file. Don't eat the EOF.
  84. if (LastChar == EOF)
  85. return tok_eof;
  86. // Otherwise, just return the character as its ascii value.
  87. int ThisChar = LastChar;
  88. LastChar = getchar();
  89. return ThisChar;
  90. }
  91. //===----------------------------------------------------------------------===//
  92. // Abstract Syntax Tree (aka Parse Tree)
  93. //===----------------------------------------------------------------------===//
  94. namespace {
  95. /// ExprAST - Base class for all expression nodes.
  96. class ExprAST {
  97. public:
  98. virtual ~ExprAST() {}
  99. virtual Value *codegen() = 0;
  100. };
  101. /// NumberExprAST - Expression class for numeric literals like "1.0".
  102. class NumberExprAST : public ExprAST {
  103. double Val;
  104. public:
  105. NumberExprAST(double Val) : Val(Val) {}
  106. Value *codegen() override;
  107. };
  108. /// VariableExprAST - Expression class for referencing a variable, like "a".
  109. class VariableExprAST : public ExprAST {
  110. std::string Name;
  111. public:
  112. VariableExprAST(const std::string &Name) : Name(Name) {}
  113. Value *codegen() override;
  114. };
  115. /// BinaryExprAST - Expression class for a binary operator.
  116. class BinaryExprAST : public ExprAST {
  117. char Op;
  118. std::unique_ptr<ExprAST> LHS, RHS;
  119. public:
  120. BinaryExprAST(char Op, std::unique_ptr<ExprAST> LHS,
  121. std::unique_ptr<ExprAST> RHS)
  122. : Op(Op), LHS(std::move(LHS)), RHS(std::move(RHS)) {}
  123. Value *codegen() override;
  124. };
  125. /// CallExprAST - Expression class for function calls.
  126. class CallExprAST : public ExprAST {
  127. std::string Callee;
  128. std::vector<std::unique_ptr<ExprAST>> Args;
  129. public:
  130. CallExprAST(const std::string &Callee,
  131. std::vector<std::unique_ptr<ExprAST>> Args)
  132. : Callee(Callee), Args(std::move(Args)) {}
  133. Value *codegen() override;
  134. };
  135. /// IfExprAST - Expression class for if/then/else.
  136. class IfExprAST : public ExprAST {
  137. std::unique_ptr<ExprAST> Cond, Then, Else;
  138. public:
  139. IfExprAST(std::unique_ptr<ExprAST> Cond, std::unique_ptr<ExprAST> Then,
  140. std::unique_ptr<ExprAST> Else)
  141. : Cond(std::move(Cond)), Then(std::move(Then)), Else(std::move(Else)) {}
  142. Value *codegen() override;
  143. };
  144. /// ForExprAST - Expression class for for/in.
  145. class ForExprAST : public ExprAST {
  146. std::string VarName;
  147. std::unique_ptr<ExprAST> Start, End, Step, Body;
  148. public:
  149. ForExprAST(const std::string &VarName, std::unique_ptr<ExprAST> Start,
  150. std::unique_ptr<ExprAST> End, std::unique_ptr<ExprAST> Step,
  151. std::unique_ptr<ExprAST> Body)
  152. : VarName(VarName), Start(std::move(Start)), End(std::move(End)),
  153. Step(std::move(Step)), Body(std::move(Body)) {}
  154. Value *codegen() override;
  155. };
  156. /// PrototypeAST - This class represents the "prototype" for a function,
  157. /// which captures its name, and its argument names (thus implicitly the number
  158. /// of arguments the function takes).
  159. class PrototypeAST {
  160. std::string Name;
  161. std::vector<std::string> Args;
  162. public:
  163. PrototypeAST(const std::string &Name, std::vector<std::string> Args)
  164. : Name(Name), Args(std::move(Args)) {}
  165. Function *codegen();
  166. const std::string &getName() const { return Name; }
  167. };
  168. /// FunctionAST - This class represents a function definition itself.
  169. class FunctionAST {
  170. std::unique_ptr<PrototypeAST> Proto;
  171. std::unique_ptr<ExprAST> Body;
  172. public:
  173. FunctionAST(std::unique_ptr<PrototypeAST> Proto,
  174. std::unique_ptr<ExprAST> Body)
  175. : Proto(std::move(Proto)), Body(std::move(Body)) {}
  176. Function *codegen();
  177. };
  178. } // end anonymous namespace
  179. //===----------------------------------------------------------------------===//
  180. // Parser
  181. //===----------------------------------------------------------------------===//
  182. /// CurTok/getNextToken - Provide a simple token buffer. CurTok is the current
  183. /// token the parser is looking at. getNextToken reads another token from the
  184. /// lexer and updates CurTok with its results.
  185. static int CurTok;
  186. static int getNextToken() { return CurTok = gettok(); }
  187. /// BinopPrecedence - This holds the precedence for each binary operator that is
  188. /// defined.
  189. static std::map<char, int> BinopPrecedence;
  190. /// GetTokPrecedence - Get the precedence of the pending binary operator token.
  191. static int GetTokPrecedence() {
  192. if (!isascii(CurTok))
  193. return -1;
  194. // Make sure it's a declared binop.
  195. int TokPrec = BinopPrecedence[CurTok];
  196. if (TokPrec <= 0)
  197. return -1;
  198. return TokPrec;
  199. }
  200. /// Error* - These are little helper functions for error handling.
  201. std::unique_ptr<ExprAST> Error(const char *Str) {
  202. fprintf(stderr, "Error: %s\n", Str);
  203. return nullptr;
  204. }
  205. std::unique_ptr<PrototypeAST> ErrorP(const char *Str) {
  206. Error(Str);
  207. return nullptr;
  208. }
  209. static std::unique_ptr<ExprAST> ParseExpression();
  210. /// numberexpr ::= number
  211. static std::unique_ptr<ExprAST> ParseNumberExpr() {
  212. auto Result = llvm::make_unique<NumberExprAST>(NumVal);
  213. getNextToken(); // consume the number
  214. return std::move(Result);
  215. }
  216. /// parenexpr ::= '(' expression ')'
  217. static std::unique_ptr<ExprAST> ParseParenExpr() {
  218. getNextToken(); // eat (.
  219. auto V = ParseExpression();
  220. if (!V)
  221. return nullptr;
  222. if (CurTok != ')')
  223. return Error("expected ')'");
  224. getNextToken(); // eat ).
  225. return V;
  226. }
  227. /// identifierexpr
  228. /// ::= identifier
  229. /// ::= identifier '(' expression* ')'
  230. static std::unique_ptr<ExprAST> ParseIdentifierExpr() {
  231. std::string IdName = IdentifierStr;
  232. getNextToken(); // eat identifier.
  233. if (CurTok != '(') // Simple variable ref.
  234. return llvm::make_unique<VariableExprAST>(IdName);
  235. // Call.
  236. getNextToken(); // eat (
  237. std::vector<std::unique_ptr<ExprAST>> Args;
  238. if (CurTok != ')') {
  239. while (1) {
  240. if (auto Arg = ParseExpression())
  241. Args.push_back(std::move(Arg));
  242. else
  243. return nullptr;
  244. if (CurTok == ')')
  245. break;
  246. if (CurTok != ',')
  247. return Error("Expected ')' or ',' in argument list");
  248. getNextToken();
  249. }
  250. }
  251. // Eat the ')'.
  252. getNextToken();
  253. return llvm::make_unique<CallExprAST>(IdName, std::move(Args));
  254. }
  255. /// ifexpr ::= 'if' expression 'then' expression 'else' expression
  256. static std::unique_ptr<ExprAST> ParseIfExpr() {
  257. getNextToken(); // eat the if.
  258. // condition.
  259. auto Cond = ParseExpression();
  260. if (!Cond)
  261. return nullptr;
  262. if (CurTok != tok_then)
  263. return Error("expected then");
  264. getNextToken(); // eat the then
  265. auto Then = ParseExpression();
  266. if (!Then)
  267. return nullptr;
  268. if (CurTok != tok_else)
  269. return Error("expected else");
  270. getNextToken();
  271. auto Else = ParseExpression();
  272. if (!Else)
  273. return nullptr;
  274. return llvm::make_unique<IfExprAST>(std::move(Cond), std::move(Then),
  275. std::move(Else));
  276. }
  277. /// forexpr ::= 'for' identifier '=' expr ',' expr (',' expr)? 'in' expression
  278. static std::unique_ptr<ExprAST> ParseForExpr() {
  279. getNextToken(); // eat the for.
  280. if (CurTok != tok_identifier)
  281. return Error("expected identifier after for");
  282. std::string IdName = IdentifierStr;
  283. getNextToken(); // eat identifier.
  284. if (CurTok != '=')
  285. return Error("expected '=' after for");
  286. getNextToken(); // eat '='.
  287. auto Start = ParseExpression();
  288. if (!Start)
  289. return nullptr;
  290. if (CurTok != ',')
  291. return Error("expected ',' after for start value");
  292. getNextToken();
  293. auto End = ParseExpression();
  294. if (!End)
  295. return nullptr;
  296. // The step value is optional.
  297. std::unique_ptr<ExprAST> Step;
  298. if (CurTok == ',') {
  299. getNextToken();
  300. Step = ParseExpression();
  301. if (!Step)
  302. return nullptr;
  303. }
  304. if (CurTok != tok_in)
  305. return Error("expected 'in' after for");
  306. getNextToken(); // eat 'in'.
  307. auto Body = ParseExpression();
  308. if (!Body)
  309. return nullptr;
  310. return llvm::make_unique<ForExprAST>(IdName, std::move(Start), std::move(End),
  311. std::move(Step), std::move(Body));
  312. }
  313. /// primary
  314. /// ::= identifierexpr
  315. /// ::= numberexpr
  316. /// ::= parenexpr
  317. /// ::= ifexpr
  318. /// ::= forexpr
  319. static std::unique_ptr<ExprAST> ParsePrimary() {
  320. switch (CurTok) {
  321. default:
  322. return Error("unknown token when expecting an expression");
  323. case tok_identifier:
  324. return ParseIdentifierExpr();
  325. case tok_number:
  326. return ParseNumberExpr();
  327. case '(':
  328. return ParseParenExpr();
  329. case tok_if:
  330. return ParseIfExpr();
  331. case tok_for:
  332. return ParseForExpr();
  333. }
  334. }
  335. /// binoprhs
  336. /// ::= ('+' primary)*
  337. static std::unique_ptr<ExprAST> ParseBinOpRHS(int ExprPrec,
  338. std::unique_ptr<ExprAST> LHS) {
  339. // If this is a binop, find its precedence.
  340. while (1) {
  341. int TokPrec = GetTokPrecedence();
  342. // If this is a binop that binds at least as tightly as the current binop,
  343. // consume it, otherwise we are done.
  344. if (TokPrec < ExprPrec)
  345. return LHS;
  346. // Okay, we know this is a binop.
  347. int BinOp = CurTok;
  348. getNextToken(); // eat binop
  349. // Parse the primary expression after the binary operator.
  350. auto RHS = ParsePrimary();
  351. if (!RHS)
  352. return nullptr;
  353. // If BinOp binds less tightly with RHS than the operator after RHS, let
  354. // the pending operator take RHS as its LHS.
  355. int NextPrec = GetTokPrecedence();
  356. if (TokPrec < NextPrec) {
  357. RHS = ParseBinOpRHS(TokPrec + 1, std::move(RHS));
  358. if (!RHS)
  359. return nullptr;
  360. }
  361. // Merge LHS/RHS.
  362. LHS =
  363. llvm::make_unique<BinaryExprAST>(BinOp, std::move(LHS), std::move(RHS));
  364. }
  365. }
  366. /// expression
  367. /// ::= primary binoprhs
  368. ///
  369. static std::unique_ptr<ExprAST> ParseExpression() {
  370. auto LHS = ParsePrimary();
  371. if (!LHS)
  372. return nullptr;
  373. return ParseBinOpRHS(0, std::move(LHS));
  374. }
  375. /// prototype
  376. /// ::= id '(' id* ')'
  377. static std::unique_ptr<PrototypeAST> ParsePrototype() {
  378. if (CurTok != tok_identifier)
  379. return ErrorP("Expected function name in prototype");
  380. std::string FnName = IdentifierStr;
  381. getNextToken();
  382. if (CurTok != '(')
  383. return ErrorP("Expected '(' in prototype");
  384. std::vector<std::string> ArgNames;
  385. while (getNextToken() == tok_identifier)
  386. ArgNames.push_back(IdentifierStr);
  387. if (CurTok != ')')
  388. return ErrorP("Expected ')' in prototype");
  389. // success.
  390. getNextToken(); // eat ')'.
  391. return llvm::make_unique<PrototypeAST>(FnName, std::move(ArgNames));
  392. }
  393. /// definition ::= 'def' prototype expression
  394. static std::unique_ptr<FunctionAST> ParseDefinition() {
  395. getNextToken(); // eat def.
  396. auto Proto = ParsePrototype();
  397. if (!Proto)
  398. return nullptr;
  399. if (auto E = ParseExpression())
  400. return llvm::make_unique<FunctionAST>(std::move(Proto), std::move(E));
  401. return nullptr;
  402. }
  403. /// toplevelexpr ::= expression
  404. static std::unique_ptr<FunctionAST> ParseTopLevelExpr() {
  405. if (auto E = ParseExpression()) {
  406. // Make an anonymous proto.
  407. auto Proto = llvm::make_unique<PrototypeAST>("__anon_expr",
  408. std::vector<std::string>());
  409. return llvm::make_unique<FunctionAST>(std::move(Proto), std::move(E));
  410. }
  411. return nullptr;
  412. }
  413. /// external ::= 'extern' prototype
  414. static std::unique_ptr<PrototypeAST> ParseExtern() {
  415. getNextToken(); // eat extern.
  416. return ParsePrototype();
  417. }
  418. //===----------------------------------------------------------------------===//
  419. // Code Generation
  420. //===----------------------------------------------------------------------===//
  421. static std::unique_ptr<Module> TheModule;
  422. static IRBuilder<> Builder(getGlobalContext());
  423. static std::map<std::string, Value *> NamedValues;
  424. static std::unique_ptr<legacy::FunctionPassManager> TheFPM;
  425. static std::unique_ptr<KaleidoscopeJIT> TheJIT;
  426. static std::map<std::string, std::unique_ptr<PrototypeAST>> FunctionProtos;
  427. Value *ErrorV(const char *Str) {
  428. Error(Str);
  429. return nullptr;
  430. }
  431. Function *getFunction(std::string Name) {
  432. // First, see if the function has already been added to the current module.
  433. if (auto *F = TheModule->getFunction(Name))
  434. return F;
  435. // If not, check whether we can codegen the declaration from some existing
  436. // prototype.
  437. auto FI = FunctionProtos.find(Name);
  438. if (FI != FunctionProtos.end())
  439. return FI->second->codegen();
  440. // If no existing prototype exists, return null.
  441. return nullptr;
  442. }
  443. Value *NumberExprAST::codegen() {
  444. return ConstantFP::get(getGlobalContext(), APFloat(Val));
  445. }
  446. Value *VariableExprAST::codegen() {
  447. // Look this variable up in the function.
  448. Value *V = NamedValues[Name];
  449. if (!V)
  450. return ErrorV("Unknown variable name");
  451. return V;
  452. }
  453. Value *BinaryExprAST::codegen() {
  454. Value *L = LHS->codegen();
  455. Value *R = RHS->codegen();
  456. if (!L || !R)
  457. return nullptr;
  458. switch (Op) {
  459. case '+':
  460. return Builder.CreateFAdd(L, R, "addtmp");
  461. case '-':
  462. return Builder.CreateFSub(L, R, "subtmp");
  463. case '*':
  464. return Builder.CreateFMul(L, R, "multmp");
  465. case '<':
  466. L = Builder.CreateFCmpULT(L, R, "cmptmp");
  467. // Convert bool 0/1 to double 0.0 or 1.0
  468. return Builder.CreateUIToFP(L, Type::getDoubleTy(getGlobalContext()),
  469. "booltmp");
  470. default:
  471. return ErrorV("invalid binary operator");
  472. }
  473. }
  474. Value *CallExprAST::codegen() {
  475. // Look up the name in the global module table.
  476. Function *CalleeF = getFunction(Callee);
  477. if (!CalleeF)
  478. return ErrorV("Unknown function referenced");
  479. // If argument mismatch error.
  480. if (CalleeF->arg_size() != Args.size())
  481. return ErrorV("Incorrect # arguments passed");
  482. std::vector<Value *> ArgsV;
  483. for (unsigned i = 0, e = Args.size(); i != e; ++i) {
  484. ArgsV.push_back(Args[i]->codegen());
  485. if (!ArgsV.back())
  486. return nullptr;
  487. }
  488. return Builder.CreateCall(CalleeF, ArgsV, "calltmp");
  489. }
  490. Value *IfExprAST::codegen() {
  491. Value *CondV = Cond->codegen();
  492. if (!CondV)
  493. return nullptr;
  494. // Convert condition to a bool by comparing equal to 0.0.
  495. CondV = Builder.CreateFCmpONE(
  496. CondV, ConstantFP::get(getGlobalContext(), APFloat(0.0)), "ifcond");
  497. Function *TheFunction = Builder.GetInsertBlock()->getParent();
  498. // Create blocks for the then and else cases. Insert the 'then' block at the
  499. // end of the function.
  500. BasicBlock *ThenBB =
  501. BasicBlock::Create(getGlobalContext(), "then", TheFunction);
  502. BasicBlock *ElseBB = BasicBlock::Create(getGlobalContext(), "else");
  503. BasicBlock *MergeBB = BasicBlock::Create(getGlobalContext(), "ifcont");
  504. Builder.CreateCondBr(CondV, ThenBB, ElseBB);
  505. // Emit then value.
  506. Builder.SetInsertPoint(ThenBB);
  507. Value *ThenV = Then->codegen();
  508. if (!ThenV)
  509. return nullptr;
  510. Builder.CreateBr(MergeBB);
  511. // Codegen of 'Then' can change the current block, update ThenBB for the PHI.
  512. ThenBB = Builder.GetInsertBlock();
  513. // Emit else block.
  514. TheFunction->getBasicBlockList().push_back(ElseBB);
  515. Builder.SetInsertPoint(ElseBB);
  516. Value *ElseV = Else->codegen();
  517. if (!ElseV)
  518. return nullptr;
  519. Builder.CreateBr(MergeBB);
  520. // Codegen of 'Else' can change the current block, update ElseBB for the PHI.
  521. ElseBB = Builder.GetInsertBlock();
  522. // Emit merge block.
  523. TheFunction->getBasicBlockList().push_back(MergeBB);
  524. Builder.SetInsertPoint(MergeBB);
  525. PHINode *PN =
  526. Builder.CreatePHI(Type::getDoubleTy(getGlobalContext()), 2, "iftmp");
  527. PN->addIncoming(ThenV, ThenBB);
  528. PN->addIncoming(ElseV, ElseBB);
  529. return PN;
  530. }
  531. // Output for-loop as:
  532. // ...
  533. // start = startexpr
  534. // goto loop
  535. // loop:
  536. // variable = phi [start, loopheader], [nextvariable, loopend]
  537. // ...
  538. // bodyexpr
  539. // ...
  540. // loopend:
  541. // step = stepexpr
  542. // nextvariable = variable + step
  543. // endcond = endexpr
  544. // br endcond, loop, endloop
  545. // outloop:
  546. Value *ForExprAST::codegen() {
  547. // Emit the start code first, without 'variable' in scope.
  548. Value *StartVal = Start->codegen();
  549. if (!StartVal)
  550. return nullptr;
  551. // Make the new basic block for the loop header, inserting after current
  552. // block.
  553. Function *TheFunction = Builder.GetInsertBlock()->getParent();
  554. BasicBlock *PreheaderBB = Builder.GetInsertBlock();
  555. BasicBlock *LoopBB =
  556. BasicBlock::Create(getGlobalContext(), "loop", TheFunction);
  557. // Insert an explicit fall through from the current block to the LoopBB.
  558. Builder.CreateBr(LoopBB);
  559. // Start insertion in LoopBB.
  560. Builder.SetInsertPoint(LoopBB);
  561. // Start the PHI node with an entry for Start.
  562. PHINode *Variable = Builder.CreatePHI(Type::getDoubleTy(getGlobalContext()),
  563. 2, VarName.c_str());
  564. Variable->addIncoming(StartVal, PreheaderBB);
  565. // Within the loop, the variable is defined equal to the PHI node. If it
  566. // shadows an existing variable, we have to restore it, so save it now.
  567. Value *OldVal = NamedValues[VarName];
  568. NamedValues[VarName] = Variable;
  569. // Emit the body of the loop. This, like any other expr, can change the
  570. // current BB. Note that we ignore the value computed by the body, but don't
  571. // allow an error.
  572. if (!Body->codegen())
  573. return nullptr;
  574. // Emit the step value.
  575. Value *StepVal = nullptr;
  576. if (Step) {
  577. StepVal = Step->codegen();
  578. if (!StepVal)
  579. return nullptr;
  580. } else {
  581. // If not specified, use 1.0.
  582. StepVal = ConstantFP::get(getGlobalContext(), APFloat(1.0));
  583. }
  584. Value *NextVar = Builder.CreateFAdd(Variable, StepVal, "nextvar");
  585. // Compute the end condition.
  586. Value *EndCond = End->codegen();
  587. if (!EndCond)
  588. return nullptr;
  589. // Convert condition to a bool by comparing equal to 0.0.
  590. EndCond = Builder.CreateFCmpONE(
  591. EndCond, ConstantFP::get(getGlobalContext(), APFloat(0.0)), "loopcond");
  592. // Create the "after loop" block and insert it.
  593. BasicBlock *LoopEndBB = Builder.GetInsertBlock();
  594. BasicBlock *AfterBB =
  595. BasicBlock::Create(getGlobalContext(), "afterloop", TheFunction);
  596. // Insert the conditional branch into the end of LoopEndBB.
  597. Builder.CreateCondBr(EndCond, LoopBB, AfterBB);
  598. // Any new code will be inserted in AfterBB.
  599. Builder.SetInsertPoint(AfterBB);
  600. // Add a new entry to the PHI node for the backedge.
  601. Variable->addIncoming(NextVar, LoopEndBB);
  602. // Restore the unshadowed variable.
  603. if (OldVal)
  604. NamedValues[VarName] = OldVal;
  605. else
  606. NamedValues.erase(VarName);
  607. // for expr always returns 0.0.
  608. return Constant::getNullValue(Type::getDoubleTy(getGlobalContext()));
  609. }
  610. Function *PrototypeAST::codegen() {
  611. // Make the function type: double(double,double) etc.
  612. std::vector<Type *> Doubles(Args.size(),
  613. Type::getDoubleTy(getGlobalContext()));
  614. FunctionType *FT =
  615. FunctionType::get(Type::getDoubleTy(getGlobalContext()), Doubles, false);
  616. Function *F =
  617. Function::Create(FT, Function::ExternalLinkage, Name, TheModule.get());
  618. // Set names for all arguments.
  619. unsigned Idx = 0;
  620. for (auto &Arg : F->args())
  621. Arg.setName(Args[Idx++]);
  622. return F;
  623. }
  624. Function *FunctionAST::codegen() {
  625. // Transfer ownership of the prototype to the FunctionProtos map, but keep a
  626. // reference to it for use below.
  627. auto &P = *Proto;
  628. FunctionProtos[Proto->getName()] = std::move(Proto);
  629. Function *TheFunction = getFunction(P.getName());
  630. if (!TheFunction)
  631. return nullptr;
  632. // Create a new basic block to start insertion into.
  633. BasicBlock *BB = BasicBlock::Create(getGlobalContext(), "entry", TheFunction);
  634. Builder.SetInsertPoint(BB);
  635. // Record the function arguments in the NamedValues map.
  636. NamedValues.clear();
  637. for (auto &Arg : TheFunction->args())
  638. NamedValues[Arg.getName()] = &Arg;
  639. if (Value *RetVal = Body->codegen()) {
  640. // Finish off the function.
  641. Builder.CreateRet(RetVal);
  642. // Validate the generated code, checking for consistency.
  643. verifyFunction(*TheFunction);
  644. // Run the optimizer on the function.
  645. TheFPM->run(*TheFunction);
  646. return TheFunction;
  647. }
  648. // Error reading body, remove function.
  649. TheFunction->eraseFromParent();
  650. return nullptr;
  651. }
  652. //===----------------------------------------------------------------------===//
  653. // Top-Level parsing and JIT Driver
  654. //===----------------------------------------------------------------------===//
  655. static void InitializeModuleAndPassManager() {
  656. // Open a new module.
  657. TheModule = llvm::make_unique<Module>("my cool jit", getGlobalContext());
  658. TheModule->setDataLayout(TheJIT->getTargetMachine().createDataLayout());
  659. // Create a new pass manager attached to it.
  660. TheFPM = llvm::make_unique<legacy::FunctionPassManager>(TheModule.get());
  661. // Do simple "peephole" optimizations and bit-twiddling optzns.
  662. TheFPM->add(createInstructionCombiningPass());
  663. // Reassociate expressions.
  664. TheFPM->add(createReassociatePass());
  665. // Eliminate Common SubExpressions.
  666. TheFPM->add(createGVNPass());
  667. // Simplify the control flow graph (deleting unreachable blocks, etc).
  668. TheFPM->add(createCFGSimplificationPass());
  669. TheFPM->doInitialization();
  670. }
  671. static void HandleDefinition() {
  672. if (auto FnAST = ParseDefinition()) {
  673. if (auto *FnIR = FnAST->codegen()) {
  674. fprintf(stderr, "Read function definition:");
  675. FnIR->dump();
  676. TheJIT->addModule(std::move(TheModule));
  677. InitializeModuleAndPassManager();
  678. }
  679. } else {
  680. // Skip token for error recovery.
  681. getNextToken();
  682. }
  683. }
  684. static void HandleExtern() {
  685. if (auto ProtoAST = ParseExtern()) {
  686. if (auto *FnIR = ProtoAST->codegen()) {
  687. fprintf(stderr, "Read extern: ");
  688. FnIR->dump();
  689. FunctionProtos[ProtoAST->getName()] = std::move(ProtoAST);
  690. }
  691. } else {
  692. // Skip token for error recovery.
  693. getNextToken();
  694. }
  695. }
  696. static void HandleTopLevelExpression() {
  697. // Evaluate a top-level expression into an anonymous function.
  698. if (auto FnAST = ParseTopLevelExpr()) {
  699. if (FnAST->codegen()) {
  700. // JIT the module containing the anonymous expression, keeping a handle so
  701. // we can free it later.
  702. auto H = TheJIT->addModule(std::move(TheModule));
  703. InitializeModuleAndPassManager();
  704. // Search the JIT for the __anon_expr symbol.
  705. auto ExprSymbol = TheJIT->findSymbol("__anon_expr");
  706. assert(ExprSymbol && "Function not found");
  707. // Get the symbol's address and cast it to the right type (takes no
  708. // arguments, returns a double) so we can call it as a native function.
  709. double (*FP)() = (double (*)())(intptr_t)ExprSymbol.getAddress();
  710. fprintf(stderr, "Evaluated to %f\n", FP());
  711. // Delete the anonymous expression module from the JIT.
  712. TheJIT->removeModule(H);
  713. }
  714. } else {
  715. // Skip token for error recovery.
  716. getNextToken();
  717. }
  718. }
  719. /// top ::= definition | external | expression | ';'
  720. static void MainLoop() {
  721. while (1) {
  722. fprintf(stderr, "ready> ");
  723. switch (CurTok) {
  724. case tok_eof:
  725. return;
  726. case ';': // ignore top-level semicolons.
  727. getNextToken();
  728. break;
  729. case tok_def:
  730. HandleDefinition();
  731. break;
  732. case tok_extern:
  733. HandleExtern();
  734. break;
  735. default:
  736. HandleTopLevelExpression();
  737. break;
  738. }
  739. }
  740. }
  741. //===----------------------------------------------------------------------===//
  742. // "Library" functions that can be "extern'd" from user code.
  743. //===----------------------------------------------------------------------===//
  744. /// putchard - putchar that takes a double and returns 0.
  745. extern "C" double putchard(double X) {
  746. fputc((char)X, stderr);
  747. return 0;
  748. }
  749. /// printd - printf that takes a double prints it as "%f\n", returning 0.
  750. extern "C" double printd(double X) {
  751. fprintf(stderr, "%f\n", X);
  752. return 0;
  753. }
  754. //===----------------------------------------------------------------------===//
  755. // Main driver code.
  756. //===----------------------------------------------------------------------===//
  757. int main() {
  758. InitializeNativeTarget();
  759. InitializeNativeTargetAsmPrinter();
  760. InitializeNativeTargetAsmParser();
  761. // Install standard binary operators.
  762. // 1 is lowest precedence.
  763. BinopPrecedence['<'] = 10;
  764. BinopPrecedence['+'] = 20;
  765. BinopPrecedence['-'] = 20;
  766. BinopPrecedence['*'] = 40; // highest.
  767. // Prime the first token.
  768. fprintf(stderr, "ready> ");
  769. getNextToken();
  770. TheJIT = llvm::make_unique<KaleidoscopeJIT>();
  771. InitializeModuleAndPassManager();
  772. // Run the main "interpreter loop" now.
  773. MainLoop();
  774. return 0;
  775. }