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