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