1#include "llvm/DerivedTypes.h"
2#include "llvm/ExecutionEngine/ExecutionEngine.h"
3#include "llvm/ExecutionEngine/JIT.h"
4#include "llvm/IRBuilder.h"
5#include "llvm/LLVMContext.h"
6#include "llvm/Module.h"
7#include "llvm/PassManager.h"
8#include "llvm/Analysis/Verifier.h"
9#include "llvm/Analysis/Passes.h"
10#include "llvm/Target/TargetData.h"
11#include "llvm/Transforms/Scalar.h"
12#include "llvm/Support/TargetSelect.h"
13#include <cstdio>
14#include <string>
15#include <map>
16#include <vector>
17using 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.
25enum 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
39static std::string IdentifierStr;  // Filled in if tok_identifier
40static double NumVal;              // Filled in if tok_number
41
42/// gettok - Return the next token from standard input.
43static 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.
100class ExprAST {
101public:
102  virtual ~ExprAST() {}
103  virtual Value *Codegen() = 0;
104};
105
106/// NumberExprAST - Expression class for numeric literals like "1.0".
107class NumberExprAST : public ExprAST {
108  double Val;
109public:
110  NumberExprAST(double val) : Val(val) {}
111  virtual Value *Codegen();
112};
113
114/// VariableExprAST - Expression class for referencing a variable, like "a".
115class VariableExprAST : public ExprAST {
116  std::string Name;
117public:
118  VariableExprAST(const std::string &name) : Name(name) {}
119  virtual Value *Codegen();
120};
121
122/// BinaryExprAST - Expression class for a binary operator.
123class BinaryExprAST : public ExprAST {
124  char Op;
125  ExprAST *LHS, *RHS;
126public:
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.
133class CallExprAST : public ExprAST {
134  std::string Callee;
135  std::vector<ExprAST*> Args;
136public:
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.
143class IfExprAST : public ExprAST {
144  ExprAST *Cond, *Then, *Else;
145public:
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.
152class ForExprAST : public ExprAST {
153  std::string VarName;
154  ExprAST *Start, *End, *Step, *Body;
155public:
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).
165class PrototypeAST {
166  std::string Name;
167  std::vector<std::string> Args;
168public:
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.
176class FunctionAST {
177  PrototypeAST *Proto;
178  ExprAST *Body;
179public:
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.
193static int CurTok;
194static int getNextToken() {
195  return CurTok = gettok();
196}
197
198/// BinopPrecedence - This holds the precedence for each binary operator that is
199/// defined.
200static std::map<char, int> BinopPrecedence;
201
202/// GetTokPrecedence - Get the precedence of the pending binary operator token.
203static 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.
214ExprAST *Error(const char *Str) { fprintf(stderr, "Error: %s\n", Str);return 0;}
215PrototypeAST *ErrorP(const char *Str) { Error(Str); return 0; }
216FunctionAST *ErrorF(const char *Str) { Error(Str); return 0; }
217
218static ExprAST *ParseExpression();
219
220/// identifierexpr
221///   ::= identifier
222///   ::= identifier '(' expression* ')'
223static 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
255static ExprAST *ParseNumberExpr() {
256  ExprAST *Result = new NumberExprAST(NumVal);
257  getNextToken(); // consume the number
258  return Result;
259}
260
261/// parenexpr ::= '(' expression ')'
262static 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
274static 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
300static 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
347static 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)*
360static 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///
394static ExprAST *ParseExpression() {
395  ExprAST *LHS = ParsePrimary();
396  if (!LHS) return 0;
397
398  return ParseBinOpRHS(0, LHS);
399}
400
401/// prototype
402///   ::= id '(' id* ')'
403static 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
426static 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
437static 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
447static PrototypeAST *ParseExtern() {
448  getNextToken();  // eat extern.
449  return ParsePrototype();
450}
451
452//===----------------------------------------------------------------------===//
453// Code Generation
454//===----------------------------------------------------------------------===//
455
456static Module *TheModule;
457static IRBuilder<> Builder(getGlobalContext());
458static std::map<std::string, Value*> NamedValues;
459static FunctionPassManager *TheFPM;
460
461Value *ErrorV(const char *Str) { Error(Str); return 0; }
462
463Value *NumberExprAST::Codegen() {
464  return ConstantFP::get(getGlobalContext(), APFloat(Val));
465}
466
467Value *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
473Value *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
491Value *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, "calltmp");
508}
509
510Value *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()), 2,
554                                  "iftmp");
555
556  PN->addIncoming(ThenV, ThenBB);
557  PN->addIncoming(ElseV, ElseBB);
558  return PN;
559}
560
561Value *ForExprAST::Codegen() {
562  // Output this as:
563  //   ...
564  //   start = startexpr
565  //   goto loop
566  // loop:
567  //   variable = phi [start, loopheader], [nextvariable, loopend]
568  //   ...
569  //   bodyexpr
570  //   ...
571  // loopend:
572  //   step = stepexpr
573  //   nextvariable = variable + step
574  //   endcond = endexpr
575  //   br endcond, loop, endloop
576  // outloop:
577
578  // Emit the start code first, without 'variable' in scope.
579  Value *StartVal = Start->Codegen();
580  if (StartVal == 0) return 0;
581
582  // Make the new basic block for the loop header, inserting after current
583  // block.
584  Function *TheFunction = Builder.GetInsertBlock()->getParent();
585  BasicBlock *PreheaderBB = Builder.GetInsertBlock();
586  BasicBlock *LoopBB = BasicBlock::Create(getGlobalContext(), "loop", TheFunction);
587
588  // Insert an explicit fall through from the current block to the LoopBB.
589  Builder.CreateBr(LoopBB);
590
591  // Start insertion in LoopBB.
592  Builder.SetInsertPoint(LoopBB);
593
594  // Start the PHI node with an entry for Start.
595  PHINode *Variable = Builder.CreatePHI(Type::getDoubleTy(getGlobalContext()), 2, VarName.c_str());
596  Variable->addIncoming(StartVal, PreheaderBB);
597
598  // Within the loop, the variable is defined equal to the PHI node.  If it
599  // shadows an existing variable, we have to restore it, so save it now.
600  Value *OldVal = NamedValues[VarName];
601  NamedValues[VarName] = Variable;
602
603  // Emit the body of the loop.  This, like any other expr, can change the
604  // current BB.  Note that we ignore the value computed by the body, but don't
605  // allow an error.
606  if (Body->Codegen() == 0)
607    return 0;
608
609  // Emit the step value.
610  Value *StepVal;
611  if (Step) {
612    StepVal = Step->Codegen();
613    if (StepVal == 0) return 0;
614  } else {
615    // If not specified, use 1.0.
616    StepVal = ConstantFP::get(getGlobalContext(), APFloat(1.0));
617  }
618
619  Value *NextVar = Builder.CreateFAdd(Variable, StepVal, "nextvar");
620
621  // Compute the end condition.
622  Value *EndCond = End->Codegen();
623  if (EndCond == 0) return EndCond;
624
625  // Convert condition to a bool by comparing equal to 0.0.
626  EndCond = Builder.CreateFCmpONE(EndCond,
627                              ConstantFP::get(getGlobalContext(), APFloat(0.0)),
628                                  "loopcond");
629
630  // Create the "after loop" block and insert it.
631  BasicBlock *LoopEndBB = Builder.GetInsertBlock();
632  BasicBlock *AfterBB = BasicBlock::Create(getGlobalContext(), "afterloop", TheFunction);
633
634  // Insert the conditional branch into the end of LoopEndBB.
635  Builder.CreateCondBr(EndCond, LoopBB, AfterBB);
636
637  // Any new code will be inserted in AfterBB.
638  Builder.SetInsertPoint(AfterBB);
639
640  // Add a new entry to the PHI node for the backedge.
641  Variable->addIncoming(NextVar, LoopEndBB);
642
643  // Restore the unshadowed variable.
644  if (OldVal)
645    NamedValues[VarName] = OldVal;
646  else
647    NamedValues.erase(VarName);
648
649
650  // for expr always returns 0.0.
651  return Constant::getNullValue(Type::getDoubleTy(getGlobalContext()));
652}
653
654Function *PrototypeAST::Codegen() {
655  // Make the function type:  double(double,double) etc.
656  std::vector<Type*> Doubles(Args.size(),
657                             Type::getDoubleTy(getGlobalContext()));
658  FunctionType *FT = FunctionType::get(Type::getDoubleTy(getGlobalContext()),
659                                       Doubles, false);
660
661  Function *F = Function::Create(FT, Function::ExternalLinkage, Name, TheModule);
662
663  // If F conflicted, there was already something named 'Name'.  If it has a
664  // body, don't allow redefinition or reextern.
665  if (F->getName() != Name) {
666    // Delete the one we just made and get the existing one.
667    F->eraseFromParent();
668    F = TheModule->getFunction(Name);
669
670    // If F already has a body, reject this.
671    if (!F->empty()) {
672      ErrorF("redefinition of function");
673      return 0;
674    }
675
676    // If F took a different number of args, reject.
677    if (F->arg_size() != Args.size()) {
678      ErrorF("redefinition of function with different # args");
679      return 0;
680    }
681  }
682
683  // Set names for all arguments.
684  unsigned Idx = 0;
685  for (Function::arg_iterator AI = F->arg_begin(); Idx != Args.size();
686       ++AI, ++Idx) {
687    AI->setName(Args[Idx]);
688
689    // Add arguments to variable symbol table.
690    NamedValues[Args[Idx]] = AI;
691  }
692
693  return F;
694}
695
696Function *FunctionAST::Codegen() {
697  NamedValues.clear();
698
699  Function *TheFunction = Proto->Codegen();
700  if (TheFunction == 0)
701    return 0;
702
703  // Create a new basic block to start insertion into.
704  BasicBlock *BB = BasicBlock::Create(getGlobalContext(), "entry", TheFunction);
705  Builder.SetInsertPoint(BB);
706
707  if (Value *RetVal = Body->Codegen()) {
708    // Finish off the function.
709    Builder.CreateRet(RetVal);
710
711    // Validate the generated code, checking for consistency.
712    verifyFunction(*TheFunction);
713
714    // Optimize the function.
715    TheFPM->run(*TheFunction);
716
717    return TheFunction;
718  }
719
720  // Error reading body, remove function.
721  TheFunction->eraseFromParent();
722  return 0;
723}
724
725//===----------------------------------------------------------------------===//
726// Top-Level parsing and JIT Driver
727//===----------------------------------------------------------------------===//
728
729static ExecutionEngine *TheExecutionEngine;
730
731static void HandleDefinition() {
732  if (FunctionAST *F = ParseDefinition()) {
733    if (Function *LF = F->Codegen()) {
734      fprintf(stderr, "Read function definition:");
735      LF->dump();
736    }
737  } else {
738    // Skip token for error recovery.
739    getNextToken();
740  }
741}
742
743static void HandleExtern() {
744  if (PrototypeAST *P = ParseExtern()) {
745    if (Function *F = P->Codegen()) {
746      fprintf(stderr, "Read extern: ");
747      F->dump();
748    }
749  } else {
750    // Skip token for error recovery.
751    getNextToken();
752  }
753}
754
755static void HandleTopLevelExpression() {
756  // Evaluate a top-level expression into an anonymous function.
757  if (FunctionAST *F = ParseTopLevelExpr()) {
758    if (Function *LF = F->Codegen()) {
759      // JIT the function, returning a function pointer.
760      void *FPtr = TheExecutionEngine->getPointerToFunction(LF);
761
762      // Cast it to the right type (takes no arguments, returns a double) so we
763      // can call it as a native function.
764      double (*FP)() = (double (*)())(intptr_t)FPtr;
765      fprintf(stderr, "Evaluated to %f\n", FP());
766    }
767  } else {
768    // Skip token for error recovery.
769    getNextToken();
770  }
771}
772
773/// top ::= definition | external | expression | ';'
774static void MainLoop() {
775  while (1) {
776    fprintf(stderr, "ready> ");
777    switch (CurTok) {
778    case tok_eof:    return;
779    case ';':        getNextToken(); break;  // ignore top-level semicolons.
780    case tok_def:    HandleDefinition(); break;
781    case tok_extern: HandleExtern(); break;
782    default:         HandleTopLevelExpression(); break;
783    }
784  }
785}
786
787//===----------------------------------------------------------------------===//
788// "Library" functions that can be "extern'd" from user code.
789//===----------------------------------------------------------------------===//
790
791/// putchard - putchar that takes a double and returns 0.
792extern "C"
793double putchard(double X) {
794  putchar((char)X);
795  return 0;
796}
797
798//===----------------------------------------------------------------------===//
799// Main driver code.
800//===----------------------------------------------------------------------===//
801
802int main() {
803  InitializeNativeTarget();
804  LLVMContext &Context = getGlobalContext();
805
806  // Install standard binary operators.
807  // 1 is lowest precedence.
808  BinopPrecedence['<'] = 10;
809  BinopPrecedence['+'] = 20;
810  BinopPrecedence['-'] = 20;
811  BinopPrecedence['*'] = 40;  // highest.
812
813  // Prime the first token.
814  fprintf(stderr, "ready> ");
815  getNextToken();
816
817  // Make the module, which holds all the code.
818  TheModule = new Module("my cool jit", Context);
819
820  // Create the JIT.  This takes ownership of the module.
821  std::string ErrStr;
822  TheExecutionEngine = EngineBuilder(TheModule).setErrorStr(&ErrStr).create();
823  if (!TheExecutionEngine) {
824    fprintf(stderr, "Could not create ExecutionEngine: %s\n", ErrStr.c_str());
825    exit(1);
826  }
827
828  FunctionPassManager OurFPM(TheModule);
829
830  // Set up the optimizer pipeline.  Start with registering info about how the
831  // target lays out data structures.
832  OurFPM.add(new TargetData(*TheExecutionEngine->getTargetData()));
833  // Provide basic AliasAnalysis support for GVN.
834  OurFPM.add(createBasicAliasAnalysisPass());
835  // Do simple "peephole" optimizations and bit-twiddling optzns.
836  OurFPM.add(createInstructionCombiningPass());
837  // Reassociate expressions.
838  OurFPM.add(createReassociatePass());
839  // Eliminate Common SubExpressions.
840  OurFPM.add(createGVNPass());
841  // Simplify the control flow graph (deleting unreachable blocks, etc).
842  OurFPM.add(createCFGSimplificationPass());
843
844  OurFPM.doInitialization();
845
846  // Set the global so the code gen can use this.
847  TheFPM = &OurFPM;
848
849  // Run the main "interpreter loop" now.
850  MainLoop();
851
852  TheFPM = 0;
853
854  // Print out all of the generated code.
855  TheModule->dump();
856
857  return 0;
858}
859