1#define MINIMAL_STDERR_OUTPUT
2
3#include "llvm/Analysis/Passes.h"
4#include "llvm/ExecutionEngine/ExecutionEngine.h"
5#include "llvm/ExecutionEngine/MCJIT.h"
6#include "llvm/ExecutionEngine/SectionMemoryManager.h"
7#include "llvm/IR/DataLayout.h"
8#include "llvm/IR/DerivedTypes.h"
9#include "llvm/IR/IRBuilder.h"
10#include "llvm/IR/LLVMContext.h"
11#include "llvm/IR/Module.h"
12#include "llvm/IR/Verifier.h"
13#include "llvm/PassManager.h"
14#include "llvm/Support/TargetSelect.h"
15#include "llvm/Transforms/Scalar.h"
16#include <cctype>
17#include <cstdio>
18#include <map>
19#include <string>
20#include <vector>
21using namespace llvm;
22
23//===----------------------------------------------------------------------===//
24// Lexer
25//===----------------------------------------------------------------------===//
26
27// The lexer returns tokens [0-255] if it is an unknown character, otherwise one
28// of these for known things.
29enum Token {
30  tok_eof = -1,
31
32  // commands
33  tok_def = -2, tok_extern = -3,
34
35  // primary
36  tok_identifier = -4, tok_number = -5,
37
38  // control
39  tok_if = -6, tok_then = -7, tok_else = -8,
40  tok_for = -9, tok_in = -10,
41
42  // operators
43  tok_binary = -11, tok_unary = -12,
44
45  // var definition
46  tok_var = -13
47};
48
49static std::string IdentifierStr;  // Filled in if tok_identifier
50static double NumVal;              // Filled in if tok_number
51
52/// gettok - Return the next token from standard input.
53static int gettok() {
54  static int LastChar = ' ';
55
56  // Skip any whitespace.
57  while (isspace(LastChar))
58    LastChar = getchar();
59
60  if (isalpha(LastChar)) { // identifier: [a-zA-Z][a-zA-Z0-9]*
61    IdentifierStr = LastChar;
62    while (isalnum((LastChar = getchar())))
63      IdentifierStr += LastChar;
64
65    if (IdentifierStr == "def") return tok_def;
66    if (IdentifierStr == "extern") return tok_extern;
67    if (IdentifierStr == "if") return tok_if;
68    if (IdentifierStr == "then") return tok_then;
69    if (IdentifierStr == "else") return tok_else;
70    if (IdentifierStr == "for") return tok_for;
71    if (IdentifierStr == "in") return tok_in;
72    if (IdentifierStr == "binary") return tok_binary;
73    if (IdentifierStr == "unary") return tok_unary;
74    if (IdentifierStr == "var") return tok_var;
75    return tok_identifier;
76  }
77
78  if (isdigit(LastChar) || LastChar == '.') {   // Number: [0-9.]+
79    std::string NumStr;
80    do {
81      NumStr += LastChar;
82      LastChar = getchar();
83    } while (isdigit(LastChar) || LastChar == '.');
84
85    NumVal = strtod(NumStr.c_str(), 0);
86    return tok_number;
87  }
88
89  if (LastChar == '#') {
90    // Comment until end of line.
91    do LastChar = getchar();
92    while (LastChar != EOF && LastChar != '\n' && LastChar != '\r');
93
94    if (LastChar != EOF)
95      return gettok();
96  }
97
98  // Check for end of file.  Don't eat the EOF.
99  if (LastChar == EOF)
100    return tok_eof;
101
102  // Otherwise, just return the character as its ascii value.
103  int ThisChar = LastChar;
104  LastChar = getchar();
105  return ThisChar;
106}
107
108//===----------------------------------------------------------------------===//
109// Abstract Syntax Tree (aka Parse Tree)
110//===----------------------------------------------------------------------===//
111
112/// ExprAST - Base class for all expression nodes.
113class ExprAST {
114public:
115  virtual ~ExprAST() {}
116  virtual Value *Codegen() = 0;
117};
118
119/// NumberExprAST - Expression class for numeric literals like "1.0".
120class NumberExprAST : public ExprAST {
121  double Val;
122public:
123  NumberExprAST(double val) : Val(val) {}
124  virtual Value *Codegen();
125};
126
127/// VariableExprAST - Expression class for referencing a variable, like "a".
128class VariableExprAST : public ExprAST {
129  std::string Name;
130public:
131  VariableExprAST(const std::string &name) : Name(name) {}
132  const std::string &getName() const { return Name; }
133  virtual Value *Codegen();
134};
135
136/// UnaryExprAST - Expression class for a unary operator.
137class UnaryExprAST : public ExprAST {
138  char Opcode;
139  ExprAST *Operand;
140public:
141  UnaryExprAST(char opcode, ExprAST *operand)
142    : Opcode(opcode), Operand(operand) {}
143  virtual Value *Codegen();
144};
145
146/// BinaryExprAST - Expression class for a binary operator.
147class BinaryExprAST : public ExprAST {
148  char Op;
149  ExprAST *LHS, *RHS;
150public:
151  BinaryExprAST(char op, ExprAST *lhs, ExprAST *rhs)
152    : Op(op), LHS(lhs), RHS(rhs) {}
153  virtual Value *Codegen();
154};
155
156/// CallExprAST - Expression class for function calls.
157class CallExprAST : public ExprAST {
158  std::string Callee;
159  std::vector<ExprAST*> Args;
160public:
161  CallExprAST(const std::string &callee, std::vector<ExprAST*> &args)
162    : Callee(callee), Args(args) {}
163  virtual Value *Codegen();
164};
165
166/// IfExprAST - Expression class for if/then/else.
167class IfExprAST : public ExprAST {
168  ExprAST *Cond, *Then, *Else;
169public:
170  IfExprAST(ExprAST *cond, ExprAST *then, ExprAST *_else)
171  : Cond(cond), Then(then), Else(_else) {}
172  virtual Value *Codegen();
173};
174
175/// ForExprAST - Expression class for for/in.
176class ForExprAST : public ExprAST {
177  std::string VarName;
178  ExprAST *Start, *End, *Step, *Body;
179public:
180  ForExprAST(const std::string &varname, ExprAST *start, ExprAST *end,
181             ExprAST *step, ExprAST *body)
182    : VarName(varname), Start(start), End(end), Step(step), Body(body) {}
183  virtual Value *Codegen();
184};
185
186/// VarExprAST - Expression class for var/in
187class VarExprAST : public ExprAST {
188  std::vector<std::pair<std::string, ExprAST*> > VarNames;
189  ExprAST *Body;
190public:
191  VarExprAST(const std::vector<std::pair<std::string, ExprAST*> > &varnames,
192             ExprAST *body)
193  : VarNames(varnames), Body(body) {}
194
195  virtual Value *Codegen();
196};
197
198/// PrototypeAST - This class represents the "prototype" for a function,
199/// which captures its argument names as well as if it is an operator.
200class PrototypeAST {
201  std::string Name;
202  std::vector<std::string> Args;
203  bool isOperator;
204  unsigned Precedence;  // Precedence if a binary op.
205public:
206  PrototypeAST(const std::string &name, const std::vector<std::string> &args,
207               bool isoperator = false, unsigned prec = 0)
208  : Name(name), Args(args), isOperator(isoperator), Precedence(prec) {}
209
210  bool isUnaryOp() const { return isOperator && Args.size() == 1; }
211  bool isBinaryOp() const { return isOperator && Args.size() == 2; }
212
213  char getOperatorName() const {
214    assert(isUnaryOp() || isBinaryOp());
215    return Name[Name.size()-1];
216  }
217
218  unsigned getBinaryPrecedence() const { return Precedence; }
219
220  Function *Codegen();
221
222  void CreateArgumentAllocas(Function *F);
223};
224
225/// FunctionAST - This class represents a function definition itself.
226class FunctionAST {
227  PrototypeAST *Proto;
228  ExprAST *Body;
229public:
230  FunctionAST(PrototypeAST *proto, ExprAST *body)
231    : Proto(proto), Body(body) {}
232
233  Function *Codegen();
234};
235
236//===----------------------------------------------------------------------===//
237// Parser
238//===----------------------------------------------------------------------===//
239
240/// CurTok/getNextToken - Provide a simple token buffer.  CurTok is the current
241/// token the parser is looking at.  getNextToken reads another token from the
242/// lexer and updates CurTok with its results.
243static int CurTok;
244static int getNextToken() {
245  return CurTok = gettok();
246}
247
248/// BinopPrecedence - This holds the precedence for each binary operator that is
249/// defined.
250static std::map<char, int> BinopPrecedence;
251
252/// GetTokPrecedence - Get the precedence of the pending binary operator token.
253static int GetTokPrecedence() {
254  if (!isascii(CurTok))
255    return -1;
256
257  // Make sure it's a declared binop.
258  int TokPrec = BinopPrecedence[CurTok];
259  if (TokPrec <= 0) return -1;
260  return TokPrec;
261}
262
263/// Error* - These are little helper functions for error handling.
264ExprAST *Error(const char *Str) { fprintf(stderr, "Error: %s\n", Str);return 0;}
265PrototypeAST *ErrorP(const char *Str) { Error(Str); return 0; }
266FunctionAST *ErrorF(const char *Str) { Error(Str); return 0; }
267
268static ExprAST *ParseExpression();
269
270/// identifierexpr
271///   ::= identifier
272///   ::= identifier '(' expression* ')'
273static ExprAST *ParseIdentifierExpr() {
274  std::string IdName = IdentifierStr;
275
276  getNextToken();  // eat identifier.
277
278  if (CurTok != '(') // Simple variable ref.
279    return new VariableExprAST(IdName);
280
281  // Call.
282  getNextToken();  // eat (
283  std::vector<ExprAST*> Args;
284  if (CurTok != ')') {
285    while (1) {
286      ExprAST *Arg = ParseExpression();
287      if (!Arg) return 0;
288      Args.push_back(Arg);
289
290      if (CurTok == ')') break;
291
292      if (CurTok != ',')
293        return Error("Expected ')' or ',' in argument list");
294      getNextToken();
295    }
296  }
297
298  // Eat the ')'.
299  getNextToken();
300
301  return new CallExprAST(IdName, Args);
302}
303
304/// numberexpr ::= number
305static ExprAST *ParseNumberExpr() {
306  ExprAST *Result = new NumberExprAST(NumVal);
307  getNextToken(); // consume the number
308  return Result;
309}
310
311/// parenexpr ::= '(' expression ')'
312static ExprAST *ParseParenExpr() {
313  getNextToken();  // eat (.
314  ExprAST *V = ParseExpression();
315  if (!V) return 0;
316
317  if (CurTok != ')')
318    return Error("expected ')'");
319  getNextToken();  // eat ).
320  return V;
321}
322
323/// ifexpr ::= 'if' expression 'then' expression 'else' expression
324static ExprAST *ParseIfExpr() {
325  getNextToken();  // eat the if.
326
327  // condition.
328  ExprAST *Cond = ParseExpression();
329  if (!Cond) return 0;
330
331  if (CurTok != tok_then)
332    return Error("expected then");
333  getNextToken();  // eat the then
334
335  ExprAST *Then = ParseExpression();
336  if (Then == 0) return 0;
337
338  if (CurTok != tok_else)
339    return Error("expected else");
340
341  getNextToken();
342
343  ExprAST *Else = ParseExpression();
344  if (!Else) return 0;
345
346  return new IfExprAST(Cond, Then, Else);
347}
348
349/// forexpr ::= 'for' identifier '=' expr ',' expr (',' expr)? 'in' expression
350static ExprAST *ParseForExpr() {
351  getNextToken();  // eat the for.
352
353  if (CurTok != tok_identifier)
354    return Error("expected identifier after for");
355
356  std::string IdName = IdentifierStr;
357  getNextToken();  // eat identifier.
358
359  if (CurTok != '=')
360    return Error("expected '=' after for");
361  getNextToken();  // eat '='.
362
363
364  ExprAST *Start = ParseExpression();
365  if (Start == 0) return 0;
366  if (CurTok != ',')
367    return Error("expected ',' after for start value");
368  getNextToken();
369
370  ExprAST *End = ParseExpression();
371  if (End == 0) return 0;
372
373  // The step value is optional.
374  ExprAST *Step = 0;
375  if (CurTok == ',') {
376    getNextToken();
377    Step = ParseExpression();
378    if (Step == 0) return 0;
379  }
380
381  if (CurTok != tok_in)
382    return Error("expected 'in' after for");
383  getNextToken();  // eat 'in'.
384
385  ExprAST *Body = ParseExpression();
386  if (Body == 0) return 0;
387
388  return new ForExprAST(IdName, Start, End, Step, Body);
389}
390
391/// varexpr ::= 'var' identifier ('=' expression)?
392//                    (',' identifier ('=' expression)?)* 'in' expression
393static ExprAST *ParseVarExpr() {
394  getNextToken();  // eat the var.
395
396  std::vector<std::pair<std::string, ExprAST*> > VarNames;
397
398  // At least one variable name is required.
399  if (CurTok != tok_identifier)
400    return Error("expected identifier after var");
401
402  while (1) {
403    std::string Name = IdentifierStr;
404    getNextToken();  // eat identifier.
405
406    // Read the optional initializer.
407    ExprAST *Init = 0;
408    if (CurTok == '=') {
409      getNextToken(); // eat the '='.
410
411      Init = ParseExpression();
412      if (Init == 0) return 0;
413    }
414
415    VarNames.push_back(std::make_pair(Name, Init));
416
417    // End of var list, exit loop.
418    if (CurTok != ',') break;
419    getNextToken(); // eat the ','.
420
421    if (CurTok != tok_identifier)
422      return Error("expected identifier list after var");
423  }
424
425  // At this point, we have to have 'in'.
426  if (CurTok != tok_in)
427    return Error("expected 'in' keyword after 'var'");
428  getNextToken();  // eat 'in'.
429
430  ExprAST *Body = ParseExpression();
431  if (Body == 0) return 0;
432
433  return new VarExprAST(VarNames, Body);
434}
435
436/// primary
437///   ::= identifierexpr
438///   ::= numberexpr
439///   ::= parenexpr
440///   ::= ifexpr
441///   ::= forexpr
442///   ::= varexpr
443static ExprAST *ParsePrimary() {
444  switch (CurTok) {
445  default: return Error("unknown token when expecting an expression");
446  case tok_identifier: return ParseIdentifierExpr();
447  case tok_number:     return ParseNumberExpr();
448  case '(':            return ParseParenExpr();
449  case tok_if:         return ParseIfExpr();
450  case tok_for:        return ParseForExpr();
451  case tok_var:        return ParseVarExpr();
452  }
453}
454
455/// unary
456///   ::= primary
457///   ::= '!' unary
458static ExprAST *ParseUnary() {
459  // If the current token is not an operator, it must be a primary expr.
460  if (!isascii(CurTok) || CurTok == '(' || CurTok == ',')
461    return ParsePrimary();
462
463  // If this is a unary operator, read it.
464  int Opc = CurTok;
465  getNextToken();
466  if (ExprAST *Operand = ParseUnary())
467    return new UnaryExprAST(Opc, Operand);
468  return 0;
469}
470
471/// binoprhs
472///   ::= ('+' unary)*
473static ExprAST *ParseBinOpRHS(int ExprPrec, ExprAST *LHS) {
474  // If this is a binop, find its precedence.
475  while (1) {
476    int TokPrec = GetTokPrecedence();
477
478    // If this is a binop that binds at least as tightly as the current binop,
479    // consume it, otherwise we are done.
480    if (TokPrec < ExprPrec)
481      return LHS;
482
483    // Okay, we know this is a binop.
484    int BinOp = CurTok;
485    getNextToken();  // eat binop
486
487    // Parse the unary expression after the binary operator.
488    ExprAST *RHS = ParseUnary();
489    if (!RHS) return 0;
490
491    // If BinOp binds less tightly with RHS than the operator after RHS, let
492    // the pending operator take RHS as its LHS.
493    int NextPrec = GetTokPrecedence();
494    if (TokPrec < NextPrec) {
495      RHS = ParseBinOpRHS(TokPrec+1, RHS);
496      if (RHS == 0) return 0;
497    }
498
499    // Merge LHS/RHS.
500    LHS = new BinaryExprAST(BinOp, LHS, RHS);
501  }
502}
503
504/// expression
505///   ::= unary binoprhs
506///
507static ExprAST *ParseExpression() {
508  ExprAST *LHS = ParseUnary();
509  if (!LHS) return 0;
510
511  return ParseBinOpRHS(0, LHS);
512}
513
514/// prototype
515///   ::= id '(' id* ')'
516///   ::= binary LETTER number? (id, id)
517///   ::= unary LETTER (id)
518static PrototypeAST *ParsePrototype() {
519  std::string FnName;
520
521  unsigned Kind = 0; // 0 = identifier, 1 = unary, 2 = binary.
522  unsigned BinaryPrecedence = 30;
523
524  switch (CurTok) {
525  default:
526    return ErrorP("Expected function name in prototype");
527  case tok_identifier:
528    FnName = IdentifierStr;
529    Kind = 0;
530    getNextToken();
531    break;
532  case tok_unary:
533    getNextToken();
534    if (!isascii(CurTok))
535      return ErrorP("Expected unary operator");
536    FnName = "unary";
537    FnName += (char)CurTok;
538    Kind = 1;
539    getNextToken();
540    break;
541  case tok_binary:
542    getNextToken();
543    if (!isascii(CurTok))
544      return ErrorP("Expected binary operator");
545    FnName = "binary";
546    FnName += (char)CurTok;
547    Kind = 2;
548    getNextToken();
549
550    // Read the precedence if present.
551    if (CurTok == tok_number) {
552      if (NumVal < 1 || NumVal > 100)
553        return ErrorP("Invalid precedecnce: must be 1..100");
554      BinaryPrecedence = (unsigned)NumVal;
555      getNextToken();
556    }
557    break;
558  }
559
560  if (CurTok != '(')
561    return ErrorP("Expected '(' in prototype");
562
563  std::vector<std::string> ArgNames;
564  while (getNextToken() == tok_identifier)
565    ArgNames.push_back(IdentifierStr);
566  if (CurTok != ')')
567    return ErrorP("Expected ')' in prototype");
568
569  // success.
570  getNextToken();  // eat ')'.
571
572  // Verify right number of names for operator.
573  if (Kind && ArgNames.size() != Kind)
574    return ErrorP("Invalid number of operands for operator");
575
576  return new PrototypeAST(FnName, ArgNames, Kind != 0, BinaryPrecedence);
577}
578
579/// definition ::= 'def' prototype expression
580static FunctionAST *ParseDefinition() {
581  getNextToken();  // eat def.
582  PrototypeAST *Proto = ParsePrototype();
583  if (Proto == 0) return 0;
584
585  if (ExprAST *E = ParseExpression())
586    return new FunctionAST(Proto, E);
587  return 0;
588}
589
590/// toplevelexpr ::= expression
591static FunctionAST *ParseTopLevelExpr() {
592  if (ExprAST *E = ParseExpression()) {
593    // Make an anonymous proto.
594    PrototypeAST *Proto = new PrototypeAST("", std::vector<std::string>());
595    return new FunctionAST(Proto, E);
596  }
597  return 0;
598}
599
600/// external ::= 'extern' prototype
601static PrototypeAST *ParseExtern() {
602  getNextToken();  // eat extern.
603  return ParsePrototype();
604}
605
606//===----------------------------------------------------------------------===//
607// Quick and dirty hack
608//===----------------------------------------------------------------------===//
609
610// FIXME: Obviously we can do better than this
611std::string GenerateUniqueName(const char *root)
612{
613  static int i = 0;
614  char s[16];
615  sprintf(s, "%s%d", root, i++);
616  std::string S = s;
617  return S;
618}
619
620std::string MakeLegalFunctionName(std::string Name)
621{
622  std::string NewName;
623  if (!Name.length())
624      return GenerateUniqueName("anon_func_");
625
626  // Start with what we have
627  NewName = Name;
628
629  // Look for a numberic first character
630  if (NewName.find_first_of("0123456789") == 0) {
631    NewName.insert(0, 1, 'n');
632  }
633
634  // Replace illegal characters with their ASCII equivalent
635  std::string legal_elements = "_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
636  size_t pos;
637  while ((pos = NewName.find_first_not_of(legal_elements)) != std::string::npos) {
638    char old_c = NewName.at(pos);
639    char new_str[16];
640    sprintf(new_str, "%d", (int)old_c);
641    NewName = NewName.replace(pos, 1, new_str);
642  }
643
644  return NewName;
645}
646
647//===----------------------------------------------------------------------===//
648// MCJIT helper class
649//===----------------------------------------------------------------------===//
650
651class MCJITHelper
652{
653public:
654  MCJITHelper(LLVMContext& C) : Context(C), OpenModule(NULL) {}
655  ~MCJITHelper();
656
657  Function *getFunction(const std::string FnName);
658  Module *getModuleForNewFunction();
659  void *getPointerToFunction(Function* F);
660  void *getPointerToNamedFunction(const std::string &Name);
661  ExecutionEngine *compileModule(Module *M);
662  void closeCurrentModule();
663  void dump();
664
665private:
666  typedef std::vector<Module*> ModuleVector;
667
668  LLVMContext  &Context;
669  Module       *OpenModule;
670  ModuleVector  Modules;
671  std::map<Module *, ExecutionEngine *> EngineMap;
672};
673
674class HelpingMemoryManager : public SectionMemoryManager
675{
676  HelpingMemoryManager(const HelpingMemoryManager&) LLVM_DELETED_FUNCTION;
677  void operator=(const HelpingMemoryManager&) LLVM_DELETED_FUNCTION;
678
679public:
680  HelpingMemoryManager(MCJITHelper *Helper) : MasterHelper(Helper) {}
681  virtual ~HelpingMemoryManager() {}
682
683  /// This method returns the address of the specified function.
684  /// Our implementation will attempt to find functions in other
685  /// modules associated with the MCJITHelper to cross link functions
686  /// from one generated module to another.
687  ///
688  /// If \p AbortOnFailure is false and no function with the given name is
689  /// found, this function returns a null pointer. Otherwise, it prints a
690  /// message to stderr and aborts.
691  virtual void *getPointerToNamedFunction(const std::string &Name,
692                                          bool AbortOnFailure = true);
693private:
694  MCJITHelper *MasterHelper;
695};
696
697void *HelpingMemoryManager::getPointerToNamedFunction(const std::string &Name,
698                                        bool AbortOnFailure)
699{
700  // Try the standard symbol resolution first, but ask it not to abort.
701  void *pfn = SectionMemoryManager::getPointerToNamedFunction(Name, false);
702  if (pfn)
703    return pfn;
704
705  pfn = MasterHelper->getPointerToNamedFunction(Name);
706  if (!pfn && AbortOnFailure)
707    report_fatal_error("Program used external function '" + Name +
708                        "' which could not be resolved!");
709  return pfn;
710}
711
712MCJITHelper::~MCJITHelper()
713{
714  // Walk the vector of modules.
715  ModuleVector::iterator it, end;
716  for (it = Modules.begin(), end = Modules.end();
717       it != end; ++it) {
718    // See if we have an execution engine for this module.
719    std::map<Module*, ExecutionEngine*>::iterator mapIt = EngineMap.find(*it);
720    // If we have an EE, the EE owns the module so just delete the EE.
721    if (mapIt != EngineMap.end()) {
722      delete mapIt->second;
723    } else {
724      // Otherwise, we still own the module.  Delete it now.
725      delete *it;
726    }
727  }
728}
729
730Function *MCJITHelper::getFunction(const std::string FnName) {
731  ModuleVector::iterator begin = Modules.begin();
732  ModuleVector::iterator end = Modules.end();
733  ModuleVector::iterator it;
734  for (it = begin; it != end; ++it) {
735    Function *F = (*it)->getFunction(FnName);
736    if (F) {
737      if (*it == OpenModule)
738          return F;
739
740      assert(OpenModule != NULL);
741
742      // This function is in a module that has already been JITed.
743      // We need to generate a new prototype for external linkage.
744      Function *PF = OpenModule->getFunction(FnName);
745      if (PF && !PF->empty()) {
746        ErrorF("redefinition of function across modules");
747        return 0;
748      }
749
750      // If we don't have a prototype yet, create one.
751      if (!PF)
752        PF = Function::Create(F->getFunctionType(),
753                                      Function::ExternalLinkage,
754                                      FnName,
755                                      OpenModule);
756      return PF;
757    }
758  }
759  return NULL;
760}
761
762Module *MCJITHelper::getModuleForNewFunction() {
763  // If we have a Module that hasn't been JITed, use that.
764  if (OpenModule)
765    return OpenModule;
766
767  // Otherwise create a new Module.
768  std::string ModName = GenerateUniqueName("mcjit_module_");
769  Module *M = new Module(ModName, Context);
770  Modules.push_back(M);
771  OpenModule = M;
772  return M;
773}
774
775void *MCJITHelper::getPointerToFunction(Function* F) {
776  // Look for this function in an existing module
777  ModuleVector::iterator begin = Modules.begin();
778  ModuleVector::iterator end = Modules.end();
779  ModuleVector::iterator it;
780  std::string FnName = F->getName();
781  for (it = begin; it != end; ++it) {
782    Function *MF = (*it)->getFunction(FnName);
783    if (MF == F) {
784      std::map<Module*, ExecutionEngine*>::iterator eeIt = EngineMap.find(*it);
785      if (eeIt != EngineMap.end()) {
786        void *P = eeIt->second->getPointerToFunction(F);
787        if (P)
788          return P;
789      } else {
790        ExecutionEngine *EE = compileModule(*it);
791        void *P = EE->getPointerToFunction(F);
792        if (P)
793          return P;
794      }
795    }
796  }
797  return NULL;
798}
799
800void MCJITHelper::closeCurrentModule() {
801  OpenModule = NULL;
802}
803
804ExecutionEngine *MCJITHelper::compileModule(Module *M) {
805  if (M == OpenModule)
806    closeCurrentModule();
807
808  std::string ErrStr;
809  ExecutionEngine *NewEngine = EngineBuilder(M)
810                                            .setErrorStr(&ErrStr)
811                                            .setUseMCJIT(true)
812                                            .setMCJITMemoryManager(new HelpingMemoryManager(this))
813                                            .create();
814  if (!NewEngine) {
815    fprintf(stderr, "Could not create ExecutionEngine: %s\n", ErrStr.c_str());
816    exit(1);
817  }
818
819  // Create a function pass manager for this engine
820  FunctionPassManager *FPM = new FunctionPassManager(M);
821
822  // Set up the optimizer pipeline.  Start with registering info about how the
823  // target lays out data structures.
824  FPM->add(new DataLayout(*NewEngine->getDataLayout()));
825  // Provide basic AliasAnalysis support for GVN.
826  FPM->add(createBasicAliasAnalysisPass());
827  // Promote allocas to registers.
828  FPM->add(createPromoteMemoryToRegisterPass());
829  // Do simple "peephole" optimizations and bit-twiddling optzns.
830  FPM->add(createInstructionCombiningPass());
831  // Reassociate expressions.
832  FPM->add(createReassociatePass());
833  // Eliminate Common SubExpressions.
834  FPM->add(createGVNPass());
835  // Simplify the control flow graph (deleting unreachable blocks, etc).
836  FPM->add(createCFGSimplificationPass());
837  FPM->doInitialization();
838
839  // For each function in the module
840  Module::iterator it;
841  Module::iterator end = M->end();
842  for (it = M->begin(); it != end; ++it) {
843    // Run the FPM on this function
844    FPM->run(*it);
845  }
846
847  // We don't need this anymore
848  delete FPM;
849
850  // Store this engine
851  EngineMap[M] = NewEngine;
852  NewEngine->finalizeObject();
853
854  return NewEngine;
855}
856
857void *MCJITHelper::getPointerToNamedFunction(const std::string &Name)
858{
859  // Look for the functions in our modules, compiling only as necessary
860  ModuleVector::iterator begin = Modules.begin();
861  ModuleVector::iterator end = Modules.end();
862  ModuleVector::iterator it;
863  for (it = begin; it != end; ++it) {
864    Function *F = (*it)->getFunction(Name);
865    if (F && !F->empty()) {
866      std::map<Module*, ExecutionEngine*>::iterator eeIt = EngineMap.find(*it);
867      if (eeIt != EngineMap.end()) {
868        void *P = eeIt->second->getPointerToFunction(F);
869        if (P)
870          return P;
871      } else {
872        ExecutionEngine *EE = compileModule(*it);
873        void *P = EE->getPointerToFunction(F);
874        if (P)
875          return P;
876      }
877    }
878  }
879  return NULL;
880}
881
882void MCJITHelper::dump()
883{
884  ModuleVector::iterator begin = Modules.begin();
885  ModuleVector::iterator end = Modules.end();
886  ModuleVector::iterator it;
887  for (it = begin; it != end; ++it)
888    (*it)->dump();
889}
890
891//===----------------------------------------------------------------------===//
892// Code Generation
893//===----------------------------------------------------------------------===//
894
895static MCJITHelper *TheHelper;
896static IRBuilder<> Builder(getGlobalContext());
897static std::map<std::string, AllocaInst*> NamedValues;
898
899Value *ErrorV(const char *Str) { Error(Str); return 0; }
900
901/// CreateEntryBlockAlloca - Create an alloca instruction in the entry block of
902/// the function.  This is used for mutable variables etc.
903static AllocaInst *CreateEntryBlockAlloca(Function *TheFunction,
904                                          const std::string &VarName) {
905  IRBuilder<> TmpB(&TheFunction->getEntryBlock(),
906                 TheFunction->getEntryBlock().begin());
907  return TmpB.CreateAlloca(Type::getDoubleTy(getGlobalContext()), 0,
908                           VarName.c_str());
909}
910
911Value *NumberExprAST::Codegen() {
912  return ConstantFP::get(getGlobalContext(), APFloat(Val));
913}
914
915Value *VariableExprAST::Codegen() {
916  // Look this variable up in the function.
917  Value *V = NamedValues[Name];
918  char ErrStr[256];
919  sprintf(ErrStr, "Unknown variable name %s", Name.c_str());
920  if (V == 0) return ErrorV(ErrStr);
921
922  // Load the value.
923  return Builder.CreateLoad(V, Name.c_str());
924}
925
926Value *UnaryExprAST::Codegen() {
927  Value *OperandV = Operand->Codegen();
928  if (OperandV == 0) return 0;
929
930  Function *F = TheHelper->getFunction(MakeLegalFunctionName(std::string("unary")+Opcode));
931  if (F == 0)
932    return ErrorV("Unknown unary operator");
933
934  return Builder.CreateCall(F, OperandV, "unop");
935}
936
937Value *BinaryExprAST::Codegen() {
938  // Special case '=' because we don't want to emit the LHS as an expression.
939  if (Op == '=') {
940    // Assignment requires the LHS to be an identifier.
941    VariableExprAST *LHSE = reinterpret_cast<VariableExprAST*>(LHS);
942    if (!LHSE)
943      return ErrorV("destination of '=' must be a variable");
944    // Codegen the RHS.
945    Value *Val = RHS->Codegen();
946    if (Val == 0) return 0;
947
948    // Look up the name.
949    Value *Variable = NamedValues[LHSE->getName()];
950    if (Variable == 0) return ErrorV("Unknown variable name");
951
952    Builder.CreateStore(Val, Variable);
953    return Val;
954  }
955
956  Value *L = LHS->Codegen();
957  Value *R = RHS->Codegen();
958  if (L == 0 || R == 0) return 0;
959
960  switch (Op) {
961  case '+': return Builder.CreateFAdd(L, R, "addtmp");
962  case '-': return Builder.CreateFSub(L, R, "subtmp");
963  case '*': return Builder.CreateFMul(L, R, "multmp");
964  case '/': return Builder.CreateFDiv(L, R, "divtmp");
965  case '<':
966    L = Builder.CreateFCmpULT(L, R, "cmptmp");
967    // Convert bool 0/1 to double 0.0 or 1.0
968    return Builder.CreateUIToFP(L, Type::getDoubleTy(getGlobalContext()),
969                                "booltmp");
970  default: break;
971  }
972
973  // If it wasn't a builtin binary operator, it must be a user defined one. Emit
974  // a call to it.
975  Function *F = TheHelper->getFunction(MakeLegalFunctionName(std::string("binary")+Op));
976  assert(F && "binary operator not found!");
977
978  Value *Ops[] = { L, R };
979  return Builder.CreateCall(F, Ops, "binop");
980}
981
982Value *CallExprAST::Codegen() {
983  // Look up the name in the global module table.
984  Function *CalleeF = TheHelper->getFunction(Callee);
985  if (CalleeF == 0)
986    return ErrorV("Unknown function referenced");
987
988  // If argument mismatch error.
989  if (CalleeF->arg_size() != Args.size())
990    return ErrorV("Incorrect # arguments passed");
991
992  std::vector<Value*> ArgsV;
993  for (unsigned i = 0, e = Args.size(); i != e; ++i) {
994    ArgsV.push_back(Args[i]->Codegen());
995    if (ArgsV.back() == 0) return 0;
996  }
997
998  return Builder.CreateCall(CalleeF, ArgsV, "calltmp");
999}
1000
1001Value *IfExprAST::Codegen() {
1002  Value *CondV = Cond->Codegen();
1003  if (CondV == 0) return 0;
1004
1005  // Convert condition to a bool by comparing equal to 0.0.
1006  CondV = Builder.CreateFCmpONE(CondV,
1007                              ConstantFP::get(getGlobalContext(), APFloat(0.0)),
1008                                "ifcond");
1009
1010  Function *TheFunction = Builder.GetInsertBlock()->getParent();
1011
1012  // Create blocks for the then and else cases.  Insert the 'then' block at the
1013  // end of the function.
1014  BasicBlock *ThenBB = BasicBlock::Create(getGlobalContext(), "then", TheFunction);
1015  BasicBlock *ElseBB = BasicBlock::Create(getGlobalContext(), "else");
1016  BasicBlock *MergeBB = BasicBlock::Create(getGlobalContext(), "ifcont");
1017
1018  Builder.CreateCondBr(CondV, ThenBB, ElseBB);
1019
1020  // Emit then value.
1021  Builder.SetInsertPoint(ThenBB);
1022
1023  Value *ThenV = Then->Codegen();
1024  if (ThenV == 0) return 0;
1025
1026  Builder.CreateBr(MergeBB);
1027  // Codegen of 'Then' can change the current block, update ThenBB for the PHI.
1028  ThenBB = Builder.GetInsertBlock();
1029
1030  // Emit else block.
1031  TheFunction->getBasicBlockList().push_back(ElseBB);
1032  Builder.SetInsertPoint(ElseBB);
1033
1034  Value *ElseV = Else->Codegen();
1035  if (ElseV == 0) return 0;
1036
1037  Builder.CreateBr(MergeBB);
1038  // Codegen of 'Else' can change the current block, update ElseBB for the PHI.
1039  ElseBB = Builder.GetInsertBlock();
1040
1041  // Emit merge block.
1042  TheFunction->getBasicBlockList().push_back(MergeBB);
1043  Builder.SetInsertPoint(MergeBB);
1044  PHINode *PN = Builder.CreatePHI(Type::getDoubleTy(getGlobalContext()), 2,
1045                                  "iftmp");
1046
1047  PN->addIncoming(ThenV, ThenBB);
1048  PN->addIncoming(ElseV, ElseBB);
1049  return PN;
1050}
1051
1052Value *ForExprAST::Codegen() {
1053  // Output this as:
1054  //   var = alloca double
1055  //   ...
1056  //   start = startexpr
1057  //   store start -> var
1058  //   goto loop
1059  // loop:
1060  //   ...
1061  //   bodyexpr
1062  //   ...
1063  // loopend:
1064  //   step = stepexpr
1065  //   endcond = endexpr
1066  //
1067  //   curvar = load var
1068  //   nextvar = curvar + step
1069  //   store nextvar -> var
1070  //   br endcond, loop, endloop
1071  // outloop:
1072
1073  Function *TheFunction = Builder.GetInsertBlock()->getParent();
1074
1075  // Create an alloca for the variable in the entry block.
1076  AllocaInst *Alloca = CreateEntryBlockAlloca(TheFunction, VarName);
1077
1078  // Emit the start code first, without 'variable' in scope.
1079  Value *StartVal = Start->Codegen();
1080  if (StartVal == 0) return 0;
1081
1082  // Store the value into the alloca.
1083  Builder.CreateStore(StartVal, Alloca);
1084
1085  // Make the new basic block for the loop header, inserting after current
1086  // block.
1087  BasicBlock *LoopBB = BasicBlock::Create(getGlobalContext(), "loop", TheFunction);
1088
1089  // Insert an explicit fall through from the current block to the LoopBB.
1090  Builder.CreateBr(LoopBB);
1091
1092  // Start insertion in LoopBB.
1093  Builder.SetInsertPoint(LoopBB);
1094
1095  // Within the loop, the variable is defined equal to the PHI node.  If it
1096  // shadows an existing variable, we have to restore it, so save it now.
1097  AllocaInst *OldVal = NamedValues[VarName];
1098  NamedValues[VarName] = Alloca;
1099
1100  // Emit the body of the loop.  This, like any other expr, can change the
1101  // current BB.  Note that we ignore the value computed by the body, but don't
1102  // allow an error.
1103  if (Body->Codegen() == 0)
1104    return 0;
1105
1106  // Emit the step value.
1107  Value *StepVal;
1108  if (Step) {
1109    StepVal = Step->Codegen();
1110    if (StepVal == 0) return 0;
1111  } else {
1112    // If not specified, use 1.0.
1113    StepVal = ConstantFP::get(getGlobalContext(), APFloat(1.0));
1114  }
1115
1116  // Compute the end condition.
1117  Value *EndCond = End->Codegen();
1118  if (EndCond == 0) return EndCond;
1119
1120  // Reload, increment, and restore the alloca.  This handles the case where
1121  // the body of the loop mutates the variable.
1122  Value *CurVar = Builder.CreateLoad(Alloca, VarName.c_str());
1123  Value *NextVar = Builder.CreateFAdd(CurVar, StepVal, "nextvar");
1124  Builder.CreateStore(NextVar, Alloca);
1125
1126  // Convert condition to a bool by comparing equal to 0.0.
1127  EndCond = Builder.CreateFCmpONE(EndCond,
1128                              ConstantFP::get(getGlobalContext(), APFloat(0.0)),
1129                                  "loopcond");
1130
1131  // Create the "after loop" block and insert it.
1132  BasicBlock *AfterBB = BasicBlock::Create(getGlobalContext(), "afterloop", TheFunction);
1133
1134  // Insert the conditional branch into the end of LoopEndBB.
1135  Builder.CreateCondBr(EndCond, LoopBB, AfterBB);
1136
1137  // Any new code will be inserted in AfterBB.
1138  Builder.SetInsertPoint(AfterBB);
1139
1140  // Restore the unshadowed variable.
1141  if (OldVal)
1142    NamedValues[VarName] = OldVal;
1143  else
1144    NamedValues.erase(VarName);
1145
1146
1147  // for expr always returns 0.0.
1148  return Constant::getNullValue(Type::getDoubleTy(getGlobalContext()));
1149}
1150
1151Value *VarExprAST::Codegen() {
1152  std::vector<AllocaInst *> OldBindings;
1153
1154  Function *TheFunction = Builder.GetInsertBlock()->getParent();
1155
1156  // Register all variables and emit their initializer.
1157  for (unsigned i = 0, e = VarNames.size(); i != e; ++i) {
1158    const std::string &VarName = VarNames[i].first;
1159    ExprAST *Init = VarNames[i].second;
1160
1161    // Emit the initializer before adding the variable to scope, this prevents
1162    // the initializer from referencing the variable itself, and permits stuff
1163    // like this:
1164    //  var a = 1 in
1165    //    var a = a in ...   # refers to outer 'a'.
1166    Value *InitVal;
1167    if (Init) {
1168      InitVal = Init->Codegen();
1169      if (InitVal == 0) return 0;
1170    } else { // If not specified, use 0.0.
1171      InitVal = ConstantFP::get(getGlobalContext(), APFloat(0.0));
1172    }
1173
1174    AllocaInst *Alloca = CreateEntryBlockAlloca(TheFunction, VarName);
1175    Builder.CreateStore(InitVal, Alloca);
1176
1177    // Remember the old variable binding so that we can restore the binding when
1178    // we unrecurse.
1179    OldBindings.push_back(NamedValues[VarName]);
1180
1181    // Remember this binding.
1182    NamedValues[VarName] = Alloca;
1183  }
1184
1185  // Codegen the body, now that all vars are in scope.
1186  Value *BodyVal = Body->Codegen();
1187  if (BodyVal == 0) return 0;
1188
1189  // Pop all our variables from scope.
1190  for (unsigned i = 0, e = VarNames.size(); i != e; ++i)
1191    NamedValues[VarNames[i].first] = OldBindings[i];
1192
1193  // Return the body computation.
1194  return BodyVal;
1195}
1196
1197Function *PrototypeAST::Codegen() {
1198  // Make the function type:  double(double,double) etc.
1199  std::vector<Type*> Doubles(Args.size(),
1200                             Type::getDoubleTy(getGlobalContext()));
1201  FunctionType *FT = FunctionType::get(Type::getDoubleTy(getGlobalContext()),
1202                                       Doubles, false);
1203
1204  std::string FnName = MakeLegalFunctionName(Name);
1205
1206  Module* M = TheHelper->getModuleForNewFunction();
1207
1208  Function *F = Function::Create(FT, Function::ExternalLinkage, FnName, M);
1209
1210  // If F conflicted, there was already something named 'FnName'.  If it has a
1211  // body, don't allow redefinition or reextern.
1212  if (F->getName() != FnName) {
1213    // Delete the one we just made and get the existing one.
1214    F->eraseFromParent();
1215    F = M->getFunction(Name);
1216
1217    // If F already has a body, reject this.
1218    if (!F->empty()) {
1219      ErrorF("redefinition of function");
1220      return 0;
1221    }
1222
1223    // If F took a different number of args, reject.
1224    if (F->arg_size() != Args.size()) {
1225      ErrorF("redefinition of function with different # args");
1226      return 0;
1227    }
1228  }
1229
1230  // Set names for all arguments.
1231  unsigned Idx = 0;
1232  for (Function::arg_iterator AI = F->arg_begin(); Idx != Args.size();
1233       ++AI, ++Idx)
1234    AI->setName(Args[Idx]);
1235
1236  return F;
1237}
1238
1239/// CreateArgumentAllocas - Create an alloca for each argument and register the
1240/// argument in the symbol table so that references to it will succeed.
1241void PrototypeAST::CreateArgumentAllocas(Function *F) {
1242  Function::arg_iterator AI = F->arg_begin();
1243  for (unsigned Idx = 0, e = Args.size(); Idx != e; ++Idx, ++AI) {
1244    // Create an alloca for this variable.
1245    AllocaInst *Alloca = CreateEntryBlockAlloca(F, Args[Idx]);
1246
1247    // Store the initial value into the alloca.
1248    Builder.CreateStore(AI, Alloca);
1249
1250    // Add arguments to variable symbol table.
1251    NamedValues[Args[Idx]] = Alloca;
1252  }
1253}
1254
1255Function *FunctionAST::Codegen() {
1256  NamedValues.clear();
1257
1258  Function *TheFunction = Proto->Codegen();
1259  if (TheFunction == 0)
1260    return 0;
1261
1262  // If this is an operator, install it.
1263  if (Proto->isBinaryOp())
1264    BinopPrecedence[Proto->getOperatorName()] = Proto->getBinaryPrecedence();
1265
1266  // Create a new basic block to start insertion into.
1267  BasicBlock *BB = BasicBlock::Create(getGlobalContext(), "entry", TheFunction);
1268  Builder.SetInsertPoint(BB);
1269
1270  // Add all arguments to the symbol table and create their allocas.
1271  Proto->CreateArgumentAllocas(TheFunction);
1272
1273  if (Value *RetVal = Body->Codegen()) {
1274    // Finish off the function.
1275    Builder.CreateRet(RetVal);
1276
1277    // Validate the generated code, checking for consistency.
1278    verifyFunction(*TheFunction);
1279
1280    return TheFunction;
1281  }
1282
1283  // Error reading body, remove function.
1284  TheFunction->eraseFromParent();
1285
1286  if (Proto->isBinaryOp())
1287    BinopPrecedence.erase(Proto->getOperatorName());
1288  return 0;
1289}
1290
1291//===----------------------------------------------------------------------===//
1292// Top-Level parsing and JIT Driver
1293//===----------------------------------------------------------------------===//
1294
1295static void HandleDefinition() {
1296  if (FunctionAST *F = ParseDefinition()) {
1297    TheHelper->closeCurrentModule();
1298    if (Function *LF = F->Codegen()) {
1299#ifndef MINIMAL_STDERR_OUTPUT
1300      fprintf(stderr, "Read function definition:");
1301      LF->dump();
1302#endif
1303    }
1304  } else {
1305    // Skip token for error recovery.
1306    getNextToken();
1307  }
1308}
1309
1310static void HandleExtern() {
1311  if (PrototypeAST *P = ParseExtern()) {
1312    if (Function *F = P->Codegen()) {
1313#ifndef MINIMAL_STDERR_OUTPUT
1314      fprintf(stderr, "Read extern: ");
1315      F->dump();
1316#endif
1317    }
1318  } else {
1319    // Skip token for error recovery.
1320    getNextToken();
1321  }
1322}
1323
1324static void HandleTopLevelExpression() {
1325  // Evaluate a top-level expression into an anonymous function.
1326  if (FunctionAST *F = ParseTopLevelExpr()) {
1327    if (Function *LF = F->Codegen()) {
1328      // JIT the function, returning a function pointer.
1329      void *FPtr = TheHelper->getPointerToFunction(LF);
1330
1331      // Cast it to the right type (takes no arguments, returns a double) so we
1332      // can call it as a native function.
1333      double (*FP)() = (double (*)())(intptr_t)FPtr;
1334#ifdef MINIMAL_STDERR_OUTPUT
1335      FP();
1336#else
1337      fprintf(stderr, "Evaluated to %f\n", FP());
1338#endif
1339    }
1340  } else {
1341    // Skip token for error recovery.
1342    getNextToken();
1343  }
1344}
1345
1346/// top ::= definition | external | expression | ';'
1347static void MainLoop() {
1348  while (1) {
1349#ifndef MINIMAL_STDERR_OUTPUT
1350    fprintf(stderr, "ready> ");
1351#endif
1352    switch (CurTok) {
1353    case tok_eof:    return;
1354    case ';':        getNextToken(); break;  // ignore top-level semicolons.
1355    case tok_def:    HandleDefinition(); break;
1356    case tok_extern: HandleExtern(); break;
1357    default:         HandleTopLevelExpression(); break;
1358    }
1359  }
1360}
1361
1362//===----------------------------------------------------------------------===//
1363// "Library" functions that can be "extern'd" from user code.
1364//===----------------------------------------------------------------------===//
1365
1366/// putchard - putchar that takes a double and returns 0.
1367extern "C"
1368double putchard(double X) {
1369  putchar((char)X);
1370  return 0;
1371}
1372
1373/// printd - printf that takes a double prints it as "%f\n", returning 0.
1374extern "C"
1375double printd(double X) {
1376  printf("%f", X);
1377  return 0;
1378}
1379
1380extern "C"
1381double printlf() {
1382  printf("\n");
1383  return 0;
1384}
1385
1386//===----------------------------------------------------------------------===//
1387// Main driver code.
1388//===----------------------------------------------------------------------===//
1389
1390int main() {
1391  InitializeNativeTarget();
1392  InitializeNativeTargetAsmPrinter();
1393  InitializeNativeTargetAsmParser();
1394  LLVMContext &Context = getGlobalContext();
1395
1396  // Install standard binary operators.
1397  // 1 is lowest precedence.
1398  BinopPrecedence['='] = 2;
1399  BinopPrecedence['<'] = 10;
1400  BinopPrecedence['+'] = 20;
1401  BinopPrecedence['-'] = 20;
1402  BinopPrecedence['/'] = 40;
1403  BinopPrecedence['*'] = 40;  // highest.
1404
1405  // Prime the first token.
1406#ifndef MINIMAL_STDERR_OUTPUT
1407  fprintf(stderr, "ready> ");
1408#endif
1409  getNextToken();
1410
1411  // Make the helper, which holds all the code.
1412  TheHelper = new MCJITHelper(Context);
1413
1414  // Run the main "interpreter loop" now.
1415  MainLoop();
1416
1417#ifndef MINIMAL_STDERR_OUTPUT
1418  // Print out all of the generated code.
1419  TheHelper->dump();
1420#endif
1421
1422  return 0;
1423}
1424