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