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