1 #include "llvm/Analysis/Passes.h"
2 #include "llvm/ExecutionEngine/ExecutionEngine.h"
3 #include "llvm/ExecutionEngine/MCJIT.h"
4 #include "llvm/ExecutionEngine/ObjectCache.h"
5 #include "llvm/ExecutionEngine/SectionMemoryManager.h"
6 #include "llvm/IR/DataLayout.h"
7 #include "llvm/IR/DerivedTypes.h"
8 #include "llvm/IR/IRBuilder.h"
9 #include "llvm/IR/LLVMContext.h"
10 #include "llvm/IR/LegacyPassManager.h"
11 #include "llvm/IR/Module.h"
12 #include "llvm/IR/Verifier.h"
13 #include "llvm/IRReader/IRReader.h"
14 #include "llvm/Support/CommandLine.h"
15 #include "llvm/Support/FileSystem.h"
16 #include "llvm/Support/Path.h"
17 #include "llvm/Support/SourceMgr.h"
18 #include "llvm/Support/TargetSelect.h"
19 #include "llvm/Support/raw_ostream.h"
20 #include "llvm/Transforms/Scalar.h"
29 //===----------------------------------------------------------------------===//
30 // Command-line options
31 //===----------------------------------------------------------------------===//
36 cl::desc("Specify the name of an IR file to load for function definitions"),
37 cl::value_desc("input IR file name"));
40 VerboseOutput("verbose",
41 cl::desc("Enable verbose output (results, IR, etc.) to stderr"),
45 SuppressPrompts("suppress-prompts",
46 cl::desc("Disable printing the 'ready' prompt"),
50 DumpModulesOnExit("dump-modules",
51 cl::desc("Dump IR from modules to stderr on shutdown"),
54 cl::opt<bool> EnableLazyCompilation(
55 "enable-lazy-compilation", cl::desc("Enable lazy compilation when using the MCJIT engine"),
58 cl::opt<bool> UseObjectCache(
59 "use-object-cache", cl::desc("Enable use of the MCJIT object caching"),
63 //===----------------------------------------------------------------------===//
65 //===----------------------------------------------------------------------===//
67 // The lexer returns tokens [0-255] if it is an unknown character, otherwise one
68 // of these for known things.
73 tok_def = -2, tok_extern = -3,
76 tok_identifier = -4, tok_number = -5,
79 tok_if = -6, tok_then = -7, tok_else = -8,
80 tok_for = -9, tok_in = -10,
83 tok_binary = -11, tok_unary = -12,
89 static std::string IdentifierStr; // Filled in if tok_identifier
90 static double NumVal; // Filled in if tok_number
92 /// gettok - Return the next token from standard input.
94 static int LastChar = ' ';
96 // Skip any whitespace.
97 while (isspace(LastChar))
100 if (isalpha(LastChar)) { // identifier: [a-zA-Z][a-zA-Z0-9]*
101 IdentifierStr = LastChar;
102 while (isalnum((LastChar = getchar())))
103 IdentifierStr += LastChar;
105 if (IdentifierStr == "def") return tok_def;
106 if (IdentifierStr == "extern") return tok_extern;
107 if (IdentifierStr == "if") return tok_if;
108 if (IdentifierStr == "then") return tok_then;
109 if (IdentifierStr == "else") return tok_else;
110 if (IdentifierStr == "for") return tok_for;
111 if (IdentifierStr == "in") return tok_in;
112 if (IdentifierStr == "binary") return tok_binary;
113 if (IdentifierStr == "unary") return tok_unary;
114 if (IdentifierStr == "var") return tok_var;
115 return tok_identifier;
118 if (isdigit(LastChar) || LastChar == '.') { // Number: [0-9.]+
122 LastChar = getchar();
123 } while (isdigit(LastChar) || LastChar == '.');
125 NumVal = strtod(NumStr.c_str(), 0);
129 if (LastChar == '#') {
130 // Comment until end of line.
131 do LastChar = getchar();
132 while (LastChar != EOF && LastChar != '\n' && LastChar != '\r');
138 // Check for end of file. Don't eat the EOF.
142 // Otherwise, just return the character as its ascii value.
143 int ThisChar = LastChar;
144 LastChar = getchar();
148 //===----------------------------------------------------------------------===//
149 // Abstract Syntax Tree (aka Parse Tree)
150 //===----------------------------------------------------------------------===//
152 /// ExprAST - Base class for all expression nodes.
155 virtual ~ExprAST() {}
156 virtual Value *Codegen() = 0;
159 /// NumberExprAST - Expression class for numeric literals like "1.0".
160 class NumberExprAST : public ExprAST {
163 NumberExprAST(double val) : Val(val) {}
164 virtual Value *Codegen();
167 /// VariableExprAST - Expression class for referencing a variable, like "a".
168 class VariableExprAST : public ExprAST {
171 VariableExprAST(const std::string &name) : Name(name) {}
172 const std::string &getName() const { return Name; }
173 virtual Value *Codegen();
176 /// UnaryExprAST - Expression class for a unary operator.
177 class UnaryExprAST : public ExprAST {
181 UnaryExprAST(char opcode, ExprAST *operand)
182 : Opcode(opcode), Operand(operand) {}
183 virtual Value *Codegen();
186 /// BinaryExprAST - Expression class for a binary operator.
187 class BinaryExprAST : public ExprAST {
191 BinaryExprAST(char op, ExprAST *lhs, ExprAST *rhs)
192 : Op(op), LHS(lhs), RHS(rhs) {}
193 virtual Value *Codegen();
196 /// CallExprAST - Expression class for function calls.
197 class CallExprAST : public ExprAST {
199 std::vector<ExprAST*> Args;
201 CallExprAST(const std::string &callee, std::vector<ExprAST*> &args)
202 : Callee(callee), Args(args) {}
203 virtual Value *Codegen();
206 /// IfExprAST - Expression class for if/then/else.
207 class IfExprAST : public ExprAST {
208 ExprAST *Cond, *Then, *Else;
210 IfExprAST(ExprAST *cond, ExprAST *then, ExprAST *_else)
211 : Cond(cond), Then(then), Else(_else) {}
212 virtual Value *Codegen();
215 /// ForExprAST - Expression class for for/in.
216 class ForExprAST : public ExprAST {
218 ExprAST *Start, *End, *Step, *Body;
220 ForExprAST(const std::string &varname, ExprAST *start, ExprAST *end,
221 ExprAST *step, ExprAST *body)
222 : VarName(varname), Start(start), End(end), Step(step), Body(body) {}
223 virtual Value *Codegen();
226 /// VarExprAST - Expression class for var/in
227 class VarExprAST : public ExprAST {
228 std::vector<std::pair<std::string, ExprAST*> > VarNames;
231 VarExprAST(const std::vector<std::pair<std::string, ExprAST*> > &varnames,
233 : VarNames(varnames), Body(body) {}
235 virtual Value *Codegen();
238 /// PrototypeAST - This class represents the "prototype" for a function,
239 /// which captures its argument names as well as if it is an operator.
242 std::vector<std::string> Args;
244 unsigned Precedence; // Precedence if a binary op.
246 PrototypeAST(const std::string &name, const std::vector<std::string> &args,
247 bool isoperator = false, unsigned prec = 0)
248 : Name(name), Args(args), isOperator(isoperator), Precedence(prec) {}
250 bool isUnaryOp() const { return isOperator && Args.size() == 1; }
251 bool isBinaryOp() const { return isOperator && Args.size() == 2; }
253 char getOperatorName() const {
254 assert(isUnaryOp() || isBinaryOp());
255 return Name[Name.size()-1];
258 unsigned getBinaryPrecedence() const { return Precedence; }
262 void CreateArgumentAllocas(Function *F);
265 /// FunctionAST - This class represents a function definition itself.
270 FunctionAST(PrototypeAST *proto, ExprAST *body)
271 : Proto(proto), Body(body) {}
276 //===----------------------------------------------------------------------===//
278 //===----------------------------------------------------------------------===//
280 /// CurTok/getNextToken - Provide a simple token buffer. CurTok is the current
281 /// token the parser is looking at. getNextToken reads another token from the
282 /// lexer and updates CurTok with its results.
284 static int getNextToken() {
285 return CurTok = gettok();
288 /// BinopPrecedence - This holds the precedence for each binary operator that is
290 static std::map<char, int> BinopPrecedence;
292 /// GetTokPrecedence - Get the precedence of the pending binary operator token.
293 static int GetTokPrecedence() {
294 if (!isascii(CurTok))
297 // Make sure it's a declared binop.
298 int TokPrec = BinopPrecedence[CurTok];
299 if (TokPrec <= 0) return -1;
303 /// Error* - These are little helper functions for error handling.
304 ExprAST *Error(const char *Str) { fprintf(stderr, "Error: %s\n", Str);return 0;}
305 PrototypeAST *ErrorP(const char *Str) { Error(Str); return 0; }
306 FunctionAST *ErrorF(const char *Str) { Error(Str); return 0; }
308 static ExprAST *ParseExpression();
312 /// ::= identifier '(' expression* ')'
313 static ExprAST *ParseIdentifierExpr() {
314 std::string IdName = IdentifierStr;
316 getNextToken(); // eat identifier.
318 if (CurTok != '(') // Simple variable ref.
319 return new VariableExprAST(IdName);
322 getNextToken(); // eat (
323 std::vector<ExprAST*> Args;
326 ExprAST *Arg = ParseExpression();
330 if (CurTok == ')') break;
333 return Error("Expected ')' or ',' in argument list");
341 return new CallExprAST(IdName, Args);
344 /// numberexpr ::= number
345 static ExprAST *ParseNumberExpr() {
346 ExprAST *Result = new NumberExprAST(NumVal);
347 getNextToken(); // consume the number
351 /// parenexpr ::= '(' expression ')'
352 static ExprAST *ParseParenExpr() {
353 getNextToken(); // eat (.
354 ExprAST *V = ParseExpression();
358 return Error("expected ')'");
359 getNextToken(); // eat ).
363 /// ifexpr ::= 'if' expression 'then' expression 'else' expression
364 static ExprAST *ParseIfExpr() {
365 getNextToken(); // eat the if.
368 ExprAST *Cond = ParseExpression();
371 if (CurTok != tok_then)
372 return Error("expected then");
373 getNextToken(); // eat the then
375 ExprAST *Then = ParseExpression();
376 if (Then == 0) return 0;
378 if (CurTok != tok_else)
379 return Error("expected else");
383 ExprAST *Else = ParseExpression();
386 return new IfExprAST(Cond, Then, Else);
389 /// forexpr ::= 'for' identifier '=' expr ',' expr (',' expr)? 'in' expression
390 static ExprAST *ParseForExpr() {
391 getNextToken(); // eat the for.
393 if (CurTok != tok_identifier)
394 return Error("expected identifier after for");
396 std::string IdName = IdentifierStr;
397 getNextToken(); // eat identifier.
400 return Error("expected '=' after for");
401 getNextToken(); // eat '='.
404 ExprAST *Start = ParseExpression();
405 if (Start == 0) return 0;
407 return Error("expected ',' after for start value");
410 ExprAST *End = ParseExpression();
411 if (End == 0) return 0;
413 // The step value is optional.
417 Step = ParseExpression();
418 if (Step == 0) return 0;
421 if (CurTok != tok_in)
422 return Error("expected 'in' after for");
423 getNextToken(); // eat 'in'.
425 ExprAST *Body = ParseExpression();
426 if (Body == 0) return 0;
428 return new ForExprAST(IdName, Start, End, Step, Body);
431 /// varexpr ::= 'var' identifier ('=' expression)?
432 // (',' identifier ('=' expression)?)* 'in' expression
433 static ExprAST *ParseVarExpr() {
434 getNextToken(); // eat the var.
436 std::vector<std::pair<std::string, ExprAST*> > VarNames;
438 // At least one variable name is required.
439 if (CurTok != tok_identifier)
440 return Error("expected identifier after var");
443 std::string Name = IdentifierStr;
444 getNextToken(); // eat identifier.
446 // Read the optional initializer.
449 getNextToken(); // eat the '='.
451 Init = ParseExpression();
452 if (Init == 0) return 0;
455 VarNames.push_back(std::make_pair(Name, Init));
457 // End of var list, exit loop.
458 if (CurTok != ',') break;
459 getNextToken(); // eat the ','.
461 if (CurTok != tok_identifier)
462 return Error("expected identifier list after var");
465 // At this point, we have to have 'in'.
466 if (CurTok != tok_in)
467 return Error("expected 'in' keyword after 'var'");
468 getNextToken(); // eat 'in'.
470 ExprAST *Body = ParseExpression();
471 if (Body == 0) return 0;
473 return new VarExprAST(VarNames, Body);
477 /// ::= identifierexpr
483 static ExprAST *ParsePrimary() {
485 default: return Error("unknown token when expecting an expression");
486 case tok_identifier: return ParseIdentifierExpr();
487 case tok_number: return ParseNumberExpr();
488 case '(': return ParseParenExpr();
489 case tok_if: return ParseIfExpr();
490 case tok_for: return ParseForExpr();
491 case tok_var: return ParseVarExpr();
498 static ExprAST *ParseUnary() {
499 // If the current token is not an operator, it must be a primary expr.
500 if (!isascii(CurTok) || CurTok == '(' || CurTok == ',')
501 return ParsePrimary();
503 // If this is a unary operator, read it.
506 if (ExprAST *Operand = ParseUnary())
507 return new UnaryExprAST(Opc, Operand);
513 static ExprAST *ParseBinOpRHS(int ExprPrec, ExprAST *LHS) {
514 // If this is a binop, find its precedence.
516 int TokPrec = GetTokPrecedence();
518 // If this is a binop that binds at least as tightly as the current binop,
519 // consume it, otherwise we are done.
520 if (TokPrec < ExprPrec)
523 // Okay, we know this is a binop.
525 getNextToken(); // eat binop
527 // Parse the unary expression after the binary operator.
528 ExprAST *RHS = ParseUnary();
531 // If BinOp binds less tightly with RHS than the operator after RHS, let
532 // the pending operator take RHS as its LHS.
533 int NextPrec = GetTokPrecedence();
534 if (TokPrec < NextPrec) {
535 RHS = ParseBinOpRHS(TokPrec+1, RHS);
536 if (RHS == 0) return 0;
540 LHS = new BinaryExprAST(BinOp, LHS, RHS);
545 /// ::= unary binoprhs
547 static ExprAST *ParseExpression() {
548 ExprAST *LHS = ParseUnary();
551 return ParseBinOpRHS(0, LHS);
555 /// ::= id '(' id* ')'
556 /// ::= binary LETTER number? (id, id)
557 /// ::= unary LETTER (id)
558 static PrototypeAST *ParsePrototype() {
561 unsigned Kind = 0; // 0 = identifier, 1 = unary, 2 = binary.
562 unsigned BinaryPrecedence = 30;
566 return ErrorP("Expected function name in prototype");
568 FnName = IdentifierStr;
574 if (!isascii(CurTok))
575 return ErrorP("Expected unary operator");
577 FnName += (char)CurTok;
583 if (!isascii(CurTok))
584 return ErrorP("Expected binary operator");
586 FnName += (char)CurTok;
590 // Read the precedence if present.
591 if (CurTok == tok_number) {
592 if (NumVal < 1 || NumVal > 100)
593 return ErrorP("Invalid precedecnce: must be 1..100");
594 BinaryPrecedence = (unsigned)NumVal;
601 return ErrorP("Expected '(' in prototype");
603 std::vector<std::string> ArgNames;
604 while (getNextToken() == tok_identifier)
605 ArgNames.push_back(IdentifierStr);
607 return ErrorP("Expected ')' in prototype");
610 getNextToken(); // eat ')'.
612 // Verify right number of names for operator.
613 if (Kind && ArgNames.size() != Kind)
614 return ErrorP("Invalid number of operands for operator");
616 return new PrototypeAST(FnName, ArgNames, Kind != 0, BinaryPrecedence);
619 /// definition ::= 'def' prototype expression
620 static FunctionAST *ParseDefinition() {
621 getNextToken(); // eat def.
622 PrototypeAST *Proto = ParsePrototype();
623 if (Proto == 0) return 0;
625 if (ExprAST *E = ParseExpression())
626 return new FunctionAST(Proto, E);
630 /// toplevelexpr ::= expression
631 static FunctionAST *ParseTopLevelExpr() {
632 if (ExprAST *E = ParseExpression()) {
633 // Make an anonymous proto.
634 PrototypeAST *Proto = new PrototypeAST("", std::vector<std::string>());
635 return new FunctionAST(Proto, E);
640 /// external ::= 'extern' prototype
641 static PrototypeAST *ParseExtern() {
642 getNextToken(); // eat extern.
643 return ParsePrototype();
646 //===----------------------------------------------------------------------===//
647 // Quick and dirty hack
648 //===----------------------------------------------------------------------===//
650 // FIXME: Obviously we can do better than this
651 std::string GenerateUniqueName(const char *root)
655 sprintf(s, "%s%d", root, i++);
660 std::string MakeLegalFunctionName(std::string Name)
664 return GenerateUniqueName("anon_func_");
666 // Start with what we have
669 // Look for a numberic first character
670 if (NewName.find_first_of("0123456789") == 0) {
671 NewName.insert(0, 1, 'n');
674 // Replace illegal characters with their ASCII equivalent
675 std::string legal_elements = "_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
677 while ((pos = NewName.find_first_not_of(legal_elements)) != std::string::npos) {
678 char old_c = NewName.at(pos);
680 sprintf(new_str, "%d", (int)old_c);
681 NewName = NewName.replace(pos, 1, new_str);
687 //===----------------------------------------------------------------------===//
688 // MCJIT object cache class
689 //===----------------------------------------------------------------------===//
691 class MCJITObjectCache : public ObjectCache {
694 // Set IR cache directory
695 sys::fs::current_path(CacheDir);
696 sys::path::append(CacheDir, "toy_object_cache");
699 virtual ~MCJITObjectCache() {
702 virtual void notifyObjectCompiled(const Module *M, const MemoryBuffer *Obj) {
704 const std::string ModuleID = M->getModuleIdentifier();
706 // If we've flagged this as an IR file, cache it
707 if (0 == ModuleID.compare(0, 3, "IR:")) {
708 std::string IRFileName = ModuleID.substr(3);
709 SmallString<128>IRCacheFile = CacheDir;
710 sys::path::append(IRCacheFile, IRFileName);
711 if (!sys::fs::exists(CacheDir.str()) && sys::fs::create_directory(CacheDir.str())) {
712 fprintf(stderr, "Unable to create cache directory\n");
716 raw_fd_ostream IRObjectFile(IRCacheFile.c_str(), ErrStr, raw_fd_ostream::F_Binary);
717 IRObjectFile << Obj->getBuffer();
721 // MCJIT will call this function before compiling any module
722 // MCJIT takes ownership of both the MemoryBuffer object and the memory
723 // to which it refers.
724 virtual MemoryBuffer* getObject(const Module* M) {
726 const std::string ModuleID = M->getModuleIdentifier();
728 // If we've flagged this as an IR file, cache it
729 if (0 == ModuleID.compare(0, 3, "IR:")) {
730 std::string IRFileName = ModuleID.substr(3);
731 SmallString<128> IRCacheFile = CacheDir;
732 sys::path::append(IRCacheFile, IRFileName);
733 if (!sys::fs::exists(IRCacheFile.str())) {
734 // This file isn't in our cache
737 std::unique_ptr<MemoryBuffer> IRObjectBuffer;
738 MemoryBuffer::getFile(IRCacheFile.c_str(), IRObjectBuffer, -1, false);
739 // MCJIT will want to write into this buffer, and we don't want that
740 // because the file has probably just been mmapped. Instead we make
741 // a copy. The filed-based buffer will be released when it goes
743 return MemoryBuffer::getMemBufferCopy(IRObjectBuffer->getBuffer());
750 SmallString<128> CacheDir;
753 //===----------------------------------------------------------------------===//
754 // IR input file handler
755 //===----------------------------------------------------------------------===//
757 Module* parseInputIR(std::string InputFile, LLVMContext &Context) {
759 Module *M = ParseIRFile(InputFile, Err, Context);
761 Err.print("IR parsing failed: ", errs());
766 sprintf(ModID, "IR:%s", InputFile.c_str());
767 M->setModuleIdentifier(ModID);
771 //===----------------------------------------------------------------------===//
772 // Helper class for execution engine abstraction
773 //===----------------------------------------------------------------------===//
779 virtual ~BaseHelper() {}
781 virtual Function *getFunction(const std::string FnName) = 0;
782 virtual Module *getModuleForNewFunction() = 0;
783 virtual void *getPointerToFunction(Function* F) = 0;
784 virtual void *getPointerToNamedFunction(const std::string &Name) = 0;
785 virtual void closeCurrentModule() = 0;
786 virtual void runFPM(Function &F) = 0;
790 //===----------------------------------------------------------------------===//
791 // MCJIT helper class
792 //===----------------------------------------------------------------------===//
794 class MCJITHelper : public BaseHelper
797 MCJITHelper(LLVMContext& C) : Context(C), CurrentModule(NULL) {
798 if (!InputIR.empty()) {
799 Module *M = parseInputIR(InputIR, Context);
800 Modules.push_back(M);
801 if (!EnableLazyCompilation)
807 Function *getFunction(const std::string FnName);
808 Module *getModuleForNewFunction();
809 void *getPointerToFunction(Function* F);
810 void *getPointerToNamedFunction(const std::string &Name);
811 void closeCurrentModule();
812 virtual void runFPM(Function &F) {} // Not needed, see compileModule
816 ExecutionEngine *compileModule(Module *M);
819 typedef std::vector<Module*> ModuleVector;
821 MCJITObjectCache OurObjectCache;
823 LLVMContext &Context;
824 ModuleVector Modules;
826 std::map<Module *, ExecutionEngine *> EngineMap;
828 Module *CurrentModule;
831 class HelpingMemoryManager : public SectionMemoryManager
833 HelpingMemoryManager(const HelpingMemoryManager&) LLVM_DELETED_FUNCTION;
834 void operator=(const HelpingMemoryManager&) LLVM_DELETED_FUNCTION;
837 HelpingMemoryManager(MCJITHelper *Helper) : MasterHelper(Helper) {}
838 virtual ~HelpingMemoryManager() {}
840 /// This method returns the address of the specified function.
841 /// Our implementation will attempt to find functions in other
842 /// modules associated with the MCJITHelper to cross link functions
843 /// from one generated module to another.
845 /// If \p AbortOnFailure is false and no function with the given name is
846 /// found, this function returns a null pointer. Otherwise, it prints a
847 /// message to stderr and aborts.
848 virtual void *getPointerToNamedFunction(const std::string &Name,
849 bool AbortOnFailure = true);
851 MCJITHelper *MasterHelper;
854 void *HelpingMemoryManager::getPointerToNamedFunction(const std::string &Name,
857 // Try the standard symbol resolution first, but ask it not to abort.
858 void *pfn = RTDyldMemoryManager::getPointerToNamedFunction(Name, false);
862 pfn = MasterHelper->getPointerToNamedFunction(Name);
863 if (!pfn && AbortOnFailure)
864 report_fatal_error("Program used external function '" + Name +
865 "' which could not be resolved!");
869 MCJITHelper::~MCJITHelper()
871 // Walk the vector of modules.
872 ModuleVector::iterator it, end;
873 for (it = Modules.begin(), end = Modules.end();
875 // See if we have an execution engine for this module.
876 std::map<Module*, ExecutionEngine*>::iterator mapIt = EngineMap.find(*it);
877 // If we have an EE, the EE owns the module so just delete the EE.
878 if (mapIt != EngineMap.end()) {
879 delete mapIt->second;
881 // Otherwise, we still own the module. Delete it now.
887 Function *MCJITHelper::getFunction(const std::string FnName) {
888 ModuleVector::iterator begin = Modules.begin();
889 ModuleVector::iterator end = Modules.end();
890 ModuleVector::iterator it;
891 for (it = begin; it != end; ++it) {
892 Function *F = (*it)->getFunction(FnName);
894 if (*it == CurrentModule)
897 assert(CurrentModule != NULL);
899 // This function is in a module that has already been JITed.
900 // We just need a prototype for external linkage.
901 Function *PF = CurrentModule->getFunction(FnName);
902 if (PF && !PF->empty()) {
903 ErrorF("redefinition of function across modules");
907 // If we don't have a prototype yet, create one.
909 PF = Function::Create(F->getFunctionType(),
910 Function::ExternalLinkage,
919 Module *MCJITHelper::getModuleForNewFunction() {
920 // If we have a Module that hasn't been JITed, use that.
922 return CurrentModule;
924 // Otherwise create a new Module.
925 std::string ModName = GenerateUniqueName("mcjit_module_");
926 Module *M = new Module(ModName, Context);
927 Modules.push_back(M);
933 ExecutionEngine *MCJITHelper::compileModule(Module *M) {
934 assert(EngineMap.find(M) == EngineMap.end());
936 if (M == CurrentModule)
937 closeCurrentModule();
940 ExecutionEngine *EE = EngineBuilder(M)
941 .setErrorStr(&ErrStr)
942 .setMCJITMemoryManager(new HelpingMemoryManager(this))
945 fprintf(stderr, "Could not create ExecutionEngine: %s\n", ErrStr.c_str());
950 EE->setObjectCache(&OurObjectCache);
951 // Get the ModuleID so we can identify IR input files
952 const std::string ModuleID = M->getModuleIdentifier();
954 // If we've flagged this as an IR file, it doesn't need function passes run.
955 if (0 != ModuleID.compare(0, 3, "IR:")) {
956 FunctionPassManager *FPM = 0;
958 // Create a FPM for this module
959 FPM = new FunctionPassManager(M);
961 // Set up the optimizer pipeline. Start with registering info about how the
962 // target lays out data structures.
963 FPM->add(new DataLayout(*EE->getDataLayout()));
964 // Provide basic AliasAnalysis support for GVN.
965 FPM->add(createBasicAliasAnalysisPass());
966 // Promote allocas to registers.
967 FPM->add(createPromoteMemoryToRegisterPass());
968 // Do simple "peephole" optimizations and bit-twiddling optzns.
969 FPM->add(createInstructionCombiningPass());
970 // Reassociate expressions.
971 FPM->add(createReassociatePass());
972 // Eliminate Common SubExpressions.
973 FPM->add(createGVNPass());
974 // Simplify the control flow graph (deleting unreachable blocks, etc).
975 FPM->add(createCFGSimplificationPass());
977 FPM->doInitialization();
979 // For each function in the module
981 Module::iterator end = M->end();
982 for (it = M->begin(); it != end; ++it) {
983 // Run the FPM on this function
990 EE->finalizeObject();
998 void *MCJITHelper::getPointerToFunction(Function* F) {
999 // Look for this function in an existing module
1000 ModuleVector::iterator begin = Modules.begin();
1001 ModuleVector::iterator end = Modules.end();
1002 ModuleVector::iterator it;
1003 std::string FnName = F->getName();
1004 for (it = begin; it != end; ++it) {
1005 Function *MF = (*it)->getFunction(FnName);
1007 std::map<Module*, ExecutionEngine*>::iterator eeIt = EngineMap.find(*it);
1008 if (eeIt != EngineMap.end()) {
1009 void *P = eeIt->second->getPointerToFunction(F);
1013 ExecutionEngine *EE = compileModule(*it);
1014 void *P = EE->getPointerToFunction(F);
1023 void MCJITHelper::closeCurrentModule() {
1024 // If we have an open module (and we should), pack it up
1025 if (CurrentModule) {
1026 CurrentModule = NULL;
1030 void *MCJITHelper::getPointerToNamedFunction(const std::string &Name)
1032 // Look for the functions in our modules, compiling only as necessary
1033 ModuleVector::iterator begin = Modules.begin();
1034 ModuleVector::iterator end = Modules.end();
1035 ModuleVector::iterator it;
1036 for (it = begin; it != end; ++it) {
1037 Function *F = (*it)->getFunction(Name);
1038 if (F && !F->empty()) {
1039 std::map<Module*, ExecutionEngine*>::iterator eeIt = EngineMap.find(*it);
1040 if (eeIt != EngineMap.end()) {
1041 void *P = eeIt->second->getPointerToFunction(F);
1045 ExecutionEngine *EE = compileModule(*it);
1046 void *P = EE->getPointerToFunction(F);
1055 void MCJITHelper::dump()
1057 ModuleVector::iterator begin = Modules.begin();
1058 ModuleVector::iterator end = Modules.end();
1059 ModuleVector::iterator it;
1060 for (it = begin; it != end; ++it)
1064 //===----------------------------------------------------------------------===//
1066 //===----------------------------------------------------------------------===//
1068 static BaseHelper *TheHelper;
1069 static IRBuilder<> Builder(getGlobalContext());
1070 static std::map<std::string, AllocaInst*> NamedValues;
1072 Value *ErrorV(const char *Str) { Error(Str); return 0; }
1074 /// CreateEntryBlockAlloca - Create an alloca instruction in the entry block of
1075 /// the function. This is used for mutable variables etc.
1076 static AllocaInst *CreateEntryBlockAlloca(Function *TheFunction,
1077 const std::string &VarName) {
1078 IRBuilder<> TmpB(&TheFunction->getEntryBlock(),
1079 TheFunction->getEntryBlock().begin());
1080 return TmpB.CreateAlloca(Type::getDoubleTy(getGlobalContext()), 0,
1084 Value *NumberExprAST::Codegen() {
1085 return ConstantFP::get(getGlobalContext(), APFloat(Val));
1088 Value *VariableExprAST::Codegen() {
1089 // Look this variable up in the function.
1090 Value *V = NamedValues[Name];
1091 if (V == 0) return ErrorV("Unknown variable name");
1094 return Builder.CreateLoad(V, Name.c_str());
1097 Value *UnaryExprAST::Codegen() {
1098 Value *OperandV = Operand->Codegen();
1099 if (OperandV == 0) return 0;
1101 F = TheHelper->getFunction(
1102 MakeLegalFunctionName(std::string("unary") + Opcode));
1104 return ErrorV("Unknown unary operator");
1106 return Builder.CreateCall(F, OperandV, "unop");
1109 Value *BinaryExprAST::Codegen() {
1110 // Special case '=' because we don't want to emit the LHS as an expression.
1112 // Assignment requires the LHS to be an identifier.
1113 // This assume we're building without RTTI because LLVM builds that way by
1114 // default. If you build LLVM with RTTI this can be changed to a
1115 // dynamic_cast for automatic error checking.
1116 VariableExprAST *LHSE = reinterpret_cast<VariableExprAST*>(LHS);
1118 return ErrorV("destination of '=' must be a variable");
1120 Value *Val = RHS->Codegen();
1121 if (Val == 0) return 0;
1123 // Look up the name.
1124 Value *Variable = NamedValues[LHSE->getName()];
1125 if (Variable == 0) return ErrorV("Unknown variable name");
1127 Builder.CreateStore(Val, Variable);
1131 Value *L = LHS->Codegen();
1132 Value *R = RHS->Codegen();
1133 if (L == 0 || R == 0) return 0;
1136 case '+': return Builder.CreateFAdd(L, R, "addtmp");
1137 case '-': return Builder.CreateFSub(L, R, "subtmp");
1138 case '*': return Builder.CreateFMul(L, R, "multmp");
1139 case '/': return Builder.CreateFDiv(L, R, "divtmp");
1141 L = Builder.CreateFCmpULT(L, R, "cmptmp");
1142 // Convert bool 0/1 to double 0.0 or 1.0
1143 return Builder.CreateUIToFP(L, Type::getDoubleTy(getGlobalContext()),
1148 // If it wasn't a builtin binary operator, it must be a user defined one. Emit
1151 F = TheHelper->getFunction(MakeLegalFunctionName(std::string("binary")+Op));
1152 assert(F && "binary operator not found!");
1154 Value *Ops[] = { L, R };
1155 return Builder.CreateCall(F, Ops, "binop");
1158 Value *CallExprAST::Codegen() {
1159 // Look up the name in the global module table.
1160 Function *CalleeF = TheHelper->getFunction(Callee);
1163 sprintf(error_str, "Unknown function referenced %s", Callee.c_str());
1164 return ErrorV(error_str);
1167 // If argument mismatch error.
1168 if (CalleeF->arg_size() != Args.size())
1169 return ErrorV("Incorrect # arguments passed");
1171 std::vector<Value*> ArgsV;
1172 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
1173 ArgsV.push_back(Args[i]->Codegen());
1174 if (ArgsV.back() == 0) return 0;
1177 return Builder.CreateCall(CalleeF, ArgsV, "calltmp");
1180 Value *IfExprAST::Codegen() {
1181 Value *CondV = Cond->Codegen();
1182 if (CondV == 0) return 0;
1184 // Convert condition to a bool by comparing equal to 0.0.
1185 CondV = Builder.CreateFCmpONE(CondV,
1186 ConstantFP::get(getGlobalContext(), APFloat(0.0)),
1189 Function *TheFunction = Builder.GetInsertBlock()->getParent();
1191 // Create blocks for the then and else cases. Insert the 'then' block at the
1192 // end of the function.
1193 BasicBlock *ThenBB = BasicBlock::Create(getGlobalContext(), "then", TheFunction);
1194 BasicBlock *ElseBB = BasicBlock::Create(getGlobalContext(), "else");
1195 BasicBlock *MergeBB = BasicBlock::Create(getGlobalContext(), "ifcont");
1197 Builder.CreateCondBr(CondV, ThenBB, ElseBB);
1200 Builder.SetInsertPoint(ThenBB);
1202 Value *ThenV = Then->Codegen();
1203 if (ThenV == 0) return 0;
1205 Builder.CreateBr(MergeBB);
1206 // Codegen of 'Then' can change the current block, update ThenBB for the PHI.
1207 ThenBB = Builder.GetInsertBlock();
1210 TheFunction->getBasicBlockList().push_back(ElseBB);
1211 Builder.SetInsertPoint(ElseBB);
1213 Value *ElseV = Else->Codegen();
1214 if (ElseV == 0) return 0;
1216 Builder.CreateBr(MergeBB);
1217 // Codegen of 'Else' can change the current block, update ElseBB for the PHI.
1218 ElseBB = Builder.GetInsertBlock();
1220 // Emit merge block.
1221 TheFunction->getBasicBlockList().push_back(MergeBB);
1222 Builder.SetInsertPoint(MergeBB);
1223 PHINode *PN = Builder.CreatePHI(Type::getDoubleTy(getGlobalContext()), 2,
1226 PN->addIncoming(ThenV, ThenBB);
1227 PN->addIncoming(ElseV, ElseBB);
1231 Value *ForExprAST::Codegen() {
1233 // var = alloca double
1235 // start = startexpr
1236 // store start -> var
1244 // endcond = endexpr
1246 // curvar = load var
1247 // nextvar = curvar + step
1248 // store nextvar -> var
1249 // br endcond, loop, endloop
1252 Function *TheFunction = Builder.GetInsertBlock()->getParent();
1254 // Create an alloca for the variable in the entry block.
1255 AllocaInst *Alloca = CreateEntryBlockAlloca(TheFunction, VarName);
1257 // Emit the start code first, without 'variable' in scope.
1258 Value *StartVal = Start->Codegen();
1259 if (StartVal == 0) return 0;
1261 // Store the value into the alloca.
1262 Builder.CreateStore(StartVal, Alloca);
1264 // Make the new basic block for the loop header, inserting after current
1266 BasicBlock *LoopBB = BasicBlock::Create(getGlobalContext(), "loop", TheFunction);
1268 // Insert an explicit fall through from the current block to the LoopBB.
1269 Builder.CreateBr(LoopBB);
1271 // Start insertion in LoopBB.
1272 Builder.SetInsertPoint(LoopBB);
1274 // Within the loop, the variable is defined equal to the PHI node. If it
1275 // shadows an existing variable, we have to restore it, so save it now.
1276 AllocaInst *OldVal = NamedValues[VarName];
1277 NamedValues[VarName] = Alloca;
1279 // Emit the body of the loop. This, like any other expr, can change the
1280 // current BB. Note that we ignore the value computed by the body, but don't
1282 if (Body->Codegen() == 0)
1285 // Emit the step value.
1288 StepVal = Step->Codegen();
1289 if (StepVal == 0) return 0;
1291 // If not specified, use 1.0.
1292 StepVal = ConstantFP::get(getGlobalContext(), APFloat(1.0));
1295 // Compute the end condition.
1296 Value *EndCond = End->Codegen();
1297 if (EndCond == 0) return EndCond;
1299 // Reload, increment, and restore the alloca. This handles the case where
1300 // the body of the loop mutates the variable.
1301 Value *CurVar = Builder.CreateLoad(Alloca, VarName.c_str());
1302 Value *NextVar = Builder.CreateFAdd(CurVar, StepVal, "nextvar");
1303 Builder.CreateStore(NextVar, Alloca);
1305 // Convert condition to a bool by comparing equal to 0.0.
1306 EndCond = Builder.CreateFCmpONE(EndCond,
1307 ConstantFP::get(getGlobalContext(), APFloat(0.0)),
1310 // Create the "after loop" block and insert it.
1311 BasicBlock *AfterBB = BasicBlock::Create(getGlobalContext(), "afterloop", TheFunction);
1313 // Insert the conditional branch into the end of LoopEndBB.
1314 Builder.CreateCondBr(EndCond, LoopBB, AfterBB);
1316 // Any new code will be inserted in AfterBB.
1317 Builder.SetInsertPoint(AfterBB);
1319 // Restore the unshadowed variable.
1321 NamedValues[VarName] = OldVal;
1323 NamedValues.erase(VarName);
1326 // for expr always returns 0.0.
1327 return Constant::getNullValue(Type::getDoubleTy(getGlobalContext()));
1330 Value *VarExprAST::Codegen() {
1331 std::vector<AllocaInst *> OldBindings;
1333 Function *TheFunction = Builder.GetInsertBlock()->getParent();
1335 // Register all variables and emit their initializer.
1336 for (unsigned i = 0, e = VarNames.size(); i != e; ++i) {
1337 const std::string &VarName = VarNames[i].first;
1338 ExprAST *Init = VarNames[i].second;
1340 // Emit the initializer before adding the variable to scope, this prevents
1341 // the initializer from referencing the variable itself, and permits stuff
1344 // var a = a in ... # refers to outer 'a'.
1347 InitVal = Init->Codegen();
1348 if (InitVal == 0) return 0;
1349 } else { // If not specified, use 0.0.
1350 InitVal = ConstantFP::get(getGlobalContext(), APFloat(0.0));
1353 AllocaInst *Alloca = CreateEntryBlockAlloca(TheFunction, VarName);
1354 Builder.CreateStore(InitVal, Alloca);
1356 // Remember the old variable binding so that we can restore the binding when
1358 OldBindings.push_back(NamedValues[VarName]);
1360 // Remember this binding.
1361 NamedValues[VarName] = Alloca;
1364 // Codegen the body, now that all vars are in scope.
1365 Value *BodyVal = Body->Codegen();
1366 if (BodyVal == 0) return 0;
1368 // Pop all our variables from scope.
1369 for (unsigned i = 0, e = VarNames.size(); i != e; ++i)
1370 NamedValues[VarNames[i].first] = OldBindings[i];
1372 // Return the body computation.
1376 Function *PrototypeAST::Codegen() {
1377 // Make the function type: double(double,double) etc.
1378 std::vector<Type*> Doubles(Args.size(),
1379 Type::getDoubleTy(getGlobalContext()));
1380 FunctionType *FT = FunctionType::get(Type::getDoubleTy(getGlobalContext()),
1384 FnName = MakeLegalFunctionName(Name);
1386 Module* M = TheHelper->getModuleForNewFunction();
1387 Function *F = Function::Create(FT, Function::ExternalLinkage, FnName, M);
1389 // FIXME: Implement duplicate function detection.
1390 // The check below will only work if the duplicate is in the open module.
1391 // If F conflicted, there was already something named 'Name'. If it has a
1392 // body, don't allow redefinition or reextern.
1393 if (F->getName() != FnName) {
1394 // Delete the one we just made and get the existing one.
1395 F->eraseFromParent();
1396 F = M->getFunction(FnName);
1397 // If F already has a body, reject this.
1399 ErrorF("redefinition of function");
1402 // If F took a different number of args, reject.
1403 if (F->arg_size() != Args.size()) {
1404 ErrorF("redefinition of function with different # args");
1409 // Set names for all arguments.
1411 for (Function::arg_iterator AI = F->arg_begin(); Idx != Args.size();
1413 AI->setName(Args[Idx]);
1418 /// CreateArgumentAllocas - Create an alloca for each argument and register the
1419 /// argument in the symbol table so that references to it will succeed.
1420 void PrototypeAST::CreateArgumentAllocas(Function *F) {
1421 Function::arg_iterator AI = F->arg_begin();
1422 for (unsigned Idx = 0, e = Args.size(); Idx != e; ++Idx, ++AI) {
1423 // Create an alloca for this variable.
1424 AllocaInst *Alloca = CreateEntryBlockAlloca(F, Args[Idx]);
1426 // Store the initial value into the alloca.
1427 Builder.CreateStore(AI, Alloca);
1429 // Add arguments to variable symbol table.
1430 NamedValues[Args[Idx]] = Alloca;
1434 Function *FunctionAST::Codegen() {
1435 NamedValues.clear();
1437 Function *TheFunction = Proto->Codegen();
1438 if (TheFunction == 0)
1441 // If this is an operator, install it.
1442 if (Proto->isBinaryOp())
1443 BinopPrecedence[Proto->getOperatorName()] = Proto->getBinaryPrecedence();
1445 // Create a new basic block to start insertion into.
1446 BasicBlock *BB = BasicBlock::Create(getGlobalContext(), "entry", TheFunction);
1447 Builder.SetInsertPoint(BB);
1449 // Add all arguments to the symbol table and create their allocas.
1450 Proto->CreateArgumentAllocas(TheFunction);
1452 if (Value *RetVal = Body->Codegen()) {
1453 // Finish off the function.
1454 Builder.CreateRet(RetVal);
1456 // Validate the generated code, checking for consistency.
1457 verifyFunction(*TheFunction);
1462 // Error reading body, remove function.
1463 TheFunction->eraseFromParent();
1465 if (Proto->isBinaryOp())
1466 BinopPrecedence.erase(Proto->getOperatorName());
1470 //===----------------------------------------------------------------------===//
1471 // Top-Level parsing and JIT Driver
1472 //===----------------------------------------------------------------------===//
1474 static void HandleDefinition() {
1475 if (FunctionAST *F = ParseDefinition()) {
1476 if (EnableLazyCompilation)
1477 TheHelper->closeCurrentModule();
1478 Function *LF = F->Codegen();
1479 if (LF && VerboseOutput) {
1480 fprintf(stderr, "Read function definition:");
1484 // Skip token for error recovery.
1489 static void HandleExtern() {
1490 if (PrototypeAST *P = ParseExtern()) {
1491 Function *F = P->Codegen();
1492 if (F && VerboseOutput) {
1493 fprintf(stderr, "Read extern: ");
1497 // Skip token for error recovery.
1502 static void HandleTopLevelExpression() {
1503 // Evaluate a top-level expression into an anonymous function.
1504 if (FunctionAST *F = ParseTopLevelExpr()) {
1505 if (Function *LF = F->Codegen()) {
1506 // JIT the function, returning a function pointer.
1507 void *FPtr = TheHelper->getPointerToFunction(LF);
1508 // Cast it to the right type (takes no arguments, returns a double) so we
1509 // can call it as a native function.
1510 double (*FP)() = (double (*)())(intptr_t)FPtr;
1511 double Result = FP();
1513 fprintf(stderr, "Evaluated to %f\n", Result);
1516 // Skip token for error recovery.
1521 /// top ::= definition | external | expression | ';'
1522 static void MainLoop() {
1524 if (!SuppressPrompts)
1525 fprintf(stderr, "ready> ");
1527 case tok_eof: return;
1528 case ';': getNextToken(); break; // ignore top-level semicolons.
1529 case tok_def: HandleDefinition(); break;
1530 case tok_extern: HandleExtern(); break;
1531 default: HandleTopLevelExpression(); break;
1536 //===----------------------------------------------------------------------===//
1537 // "Library" functions that can be "extern'd" from user code.
1538 //===----------------------------------------------------------------------===//
1540 /// putchard - putchar that takes a double and returns 0.
1542 double putchard(double X) {
1547 /// printd - printf that takes a double prints it as "%f\n", returning 0.
1549 double printd(double X) {
1560 //===----------------------------------------------------------------------===//
1561 // Main driver code.
1562 //===----------------------------------------------------------------------===//
1564 int main(int argc, char **argv) {
1565 InitializeNativeTarget();
1566 InitializeNativeTargetAsmPrinter();
1567 InitializeNativeTargetAsmParser();
1568 LLVMContext &Context = getGlobalContext();
1570 cl::ParseCommandLineOptions(argc, argv,
1571 "Kaleidoscope example program\n");
1573 // Install standard binary operators.
1574 // 1 is lowest precedence.
1575 BinopPrecedence['='] = 2;
1576 BinopPrecedence['<'] = 10;
1577 BinopPrecedence['+'] = 20;
1578 BinopPrecedence['-'] = 20;
1579 BinopPrecedence['/'] = 40;
1580 BinopPrecedence['*'] = 40; // highest.
1582 // Make the Helper, which holds all the code.
1583 TheHelper = new MCJITHelper(Context);
1585 // Prime the first token.
1586 if (!SuppressPrompts)
1587 fprintf(stderr, "ready> ");
1590 // Run the main "interpreter loop" now.
1593 // Print out all of the generated code.
1594 if (DumpModulesOnExit)