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