1 #include "llvm/Analysis/Passes.h"
2 #include "llvm/ExecutionEngine/ExecutionEngine.h"
3 #include "llvm/ExecutionEngine/JIT.h"
4 #include "llvm/ExecutionEngine/MCJIT.h"
5 #include "llvm/ExecutionEngine/ObjectCache.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/IRReader/IRReader.h"
14 #include "llvm/PassManager.h"
15 #include "llvm/Support/CommandLine.h"
16 #include "llvm/Support/FileSystem.h"
17 #include "llvm/Support/Path.h"
18 #include "llvm/Support/SourceMgr.h"
19 #include "llvm/Support/TargetSelect.h"
20 #include "llvm/Support/raw_ostream.h"
21 #include "llvm/Transforms/Scalar.h"
30 //===----------------------------------------------------------------------===//
31 // Command-line options
32 //===----------------------------------------------------------------------===//
37 cl::desc("Specify the name of an IR file to load for function definitions"),
38 cl::value_desc("input IR file name"));
41 VerboseOutput("verbose",
42 cl::desc("Enable verbose output (results, IR, etc.) to stderr"),
46 SuppressPrompts("suppress-prompts",
47 cl::desc("Disable printing the 'ready' prompt"),
51 DumpModulesOnExit("dump-modules",
52 cl::desc("Dump IR from modules to stderr on shutdown"),
55 cl::opt<bool> UseMCJIT(
56 "use-mcjit", cl::desc("Use the MCJIT execution engine"),
59 cl::opt<bool> EnableLazyCompilation(
60 "enable-lazy-compilation", cl::desc("Enable lazy compilation when using the MCJIT engine"),
63 cl::opt<bool> UseObjectCache(
64 "use-object-cache", cl::desc("Enable use of the MCJIT object caching"),
68 //===----------------------------------------------------------------------===//
70 //===----------------------------------------------------------------------===//
72 // The lexer returns tokens [0-255] if it is an unknown character, otherwise one
73 // of these for known things.
78 tok_def = -2, tok_extern = -3,
81 tok_identifier = -4, tok_number = -5,
84 tok_if = -6, tok_then = -7, tok_else = -8,
85 tok_for = -9, tok_in = -10,
88 tok_binary = -11, tok_unary = -12,
94 static std::string IdentifierStr; // Filled in if tok_identifier
95 static double NumVal; // Filled in if tok_number
97 /// gettok - Return the next token from standard input.
99 static int LastChar = ' ';
101 // Skip any whitespace.
102 while (isspace(LastChar))
103 LastChar = getchar();
105 if (isalpha(LastChar)) { // identifier: [a-zA-Z][a-zA-Z0-9]*
106 IdentifierStr = LastChar;
107 while (isalnum((LastChar = getchar())))
108 IdentifierStr += LastChar;
110 if (IdentifierStr == "def") return tok_def;
111 if (IdentifierStr == "extern") return tok_extern;
112 if (IdentifierStr == "if") return tok_if;
113 if (IdentifierStr == "then") return tok_then;
114 if (IdentifierStr == "else") return tok_else;
115 if (IdentifierStr == "for") return tok_for;
116 if (IdentifierStr == "in") return tok_in;
117 if (IdentifierStr == "binary") return tok_binary;
118 if (IdentifierStr == "unary") return tok_unary;
119 if (IdentifierStr == "var") return tok_var;
120 return tok_identifier;
123 if (isdigit(LastChar) || LastChar == '.') { // Number: [0-9.]+
127 LastChar = getchar();
128 } while (isdigit(LastChar) || LastChar == '.');
130 NumVal = strtod(NumStr.c_str(), 0);
134 if (LastChar == '#') {
135 // Comment until end of line.
136 do LastChar = getchar();
137 while (LastChar != EOF && LastChar != '\n' && LastChar != '\r');
143 // Check for end of file. Don't eat the EOF.
147 // Otherwise, just return the character as its ascii value.
148 int ThisChar = LastChar;
149 LastChar = getchar();
153 //===----------------------------------------------------------------------===//
154 // Abstract Syntax Tree (aka Parse Tree)
155 //===----------------------------------------------------------------------===//
157 /// ExprAST - Base class for all expression nodes.
160 virtual ~ExprAST() {}
161 virtual Value *Codegen() = 0;
164 /// NumberExprAST - Expression class for numeric literals like "1.0".
165 class NumberExprAST : public ExprAST {
168 NumberExprAST(double val) : Val(val) {}
169 virtual Value *Codegen();
172 /// VariableExprAST - Expression class for referencing a variable, like "a".
173 class VariableExprAST : public ExprAST {
176 VariableExprAST(const std::string &name) : Name(name) {}
177 const std::string &getName() const { return Name; }
178 virtual Value *Codegen();
181 /// UnaryExprAST - Expression class for a unary operator.
182 class UnaryExprAST : public ExprAST {
186 UnaryExprAST(char opcode, ExprAST *operand)
187 : Opcode(opcode), Operand(operand) {}
188 virtual Value *Codegen();
191 /// BinaryExprAST - Expression class for a binary operator.
192 class BinaryExprAST : public ExprAST {
196 BinaryExprAST(char op, ExprAST *lhs, ExprAST *rhs)
197 : Op(op), LHS(lhs), RHS(rhs) {}
198 virtual Value *Codegen();
201 /// CallExprAST - Expression class for function calls.
202 class CallExprAST : public ExprAST {
204 std::vector<ExprAST*> Args;
206 CallExprAST(const std::string &callee, std::vector<ExprAST*> &args)
207 : Callee(callee), Args(args) {}
208 virtual Value *Codegen();
211 /// IfExprAST - Expression class for if/then/else.
212 class IfExprAST : public ExprAST {
213 ExprAST *Cond, *Then, *Else;
215 IfExprAST(ExprAST *cond, ExprAST *then, ExprAST *_else)
216 : Cond(cond), Then(then), Else(_else) {}
217 virtual Value *Codegen();
220 /// ForExprAST - Expression class for for/in.
221 class ForExprAST : public ExprAST {
223 ExprAST *Start, *End, *Step, *Body;
225 ForExprAST(const std::string &varname, ExprAST *start, ExprAST *end,
226 ExprAST *step, ExprAST *body)
227 : VarName(varname), Start(start), End(end), Step(step), Body(body) {}
228 virtual Value *Codegen();
231 /// VarExprAST - Expression class for var/in
232 class VarExprAST : public ExprAST {
233 std::vector<std::pair<std::string, ExprAST*> > VarNames;
236 VarExprAST(const std::vector<std::pair<std::string, ExprAST*> > &varnames,
238 : VarNames(varnames), Body(body) {}
240 virtual Value *Codegen();
243 /// PrototypeAST - This class represents the "prototype" for a function,
244 /// which captures its argument names as well as if it is an operator.
247 std::vector<std::string> Args;
249 unsigned Precedence; // Precedence if a binary op.
251 PrototypeAST(const std::string &name, const std::vector<std::string> &args,
252 bool isoperator = false, unsigned prec = 0)
253 : Name(name), Args(args), isOperator(isoperator), Precedence(prec) {}
255 bool isUnaryOp() const { return isOperator && Args.size() == 1; }
256 bool isBinaryOp() const { return isOperator && Args.size() == 2; }
258 char getOperatorName() const {
259 assert(isUnaryOp() || isBinaryOp());
260 return Name[Name.size()-1];
263 unsigned getBinaryPrecedence() const { return Precedence; }
267 void CreateArgumentAllocas(Function *F);
270 /// FunctionAST - This class represents a function definition itself.
275 FunctionAST(PrototypeAST *proto, ExprAST *body)
276 : Proto(proto), Body(body) {}
281 //===----------------------------------------------------------------------===//
283 //===----------------------------------------------------------------------===//
285 /// CurTok/getNextToken - Provide a simple token buffer. CurTok is the current
286 /// token the parser is looking at. getNextToken reads another token from the
287 /// lexer and updates CurTok with its results.
289 static int getNextToken() {
290 return CurTok = gettok();
293 /// BinopPrecedence - This holds the precedence for each binary operator that is
295 static std::map<char, int> BinopPrecedence;
297 /// GetTokPrecedence - Get the precedence of the pending binary operator token.
298 static int GetTokPrecedence() {
299 if (!isascii(CurTok))
302 // Make sure it's a declared binop.
303 int TokPrec = BinopPrecedence[CurTok];
304 if (TokPrec <= 0) return -1;
308 /// Error* - These are little helper functions for error handling.
309 ExprAST *Error(const char *Str) { fprintf(stderr, "Error: %s\n", Str);return 0;}
310 PrototypeAST *ErrorP(const char *Str) { Error(Str); return 0; }
311 FunctionAST *ErrorF(const char *Str) { Error(Str); return 0; }
313 static ExprAST *ParseExpression();
317 /// ::= identifier '(' expression* ')'
318 static ExprAST *ParseIdentifierExpr() {
319 std::string IdName = IdentifierStr;
321 getNextToken(); // eat identifier.
323 if (CurTok != '(') // Simple variable ref.
324 return new VariableExprAST(IdName);
327 getNextToken(); // eat (
328 std::vector<ExprAST*> Args;
331 ExprAST *Arg = ParseExpression();
335 if (CurTok == ')') break;
338 return Error("Expected ')' or ',' in argument list");
346 return new CallExprAST(IdName, Args);
349 /// numberexpr ::= number
350 static ExprAST *ParseNumberExpr() {
351 ExprAST *Result = new NumberExprAST(NumVal);
352 getNextToken(); // consume the number
356 /// parenexpr ::= '(' expression ')'
357 static ExprAST *ParseParenExpr() {
358 getNextToken(); // eat (.
359 ExprAST *V = ParseExpression();
363 return Error("expected ')'");
364 getNextToken(); // eat ).
368 /// ifexpr ::= 'if' expression 'then' expression 'else' expression
369 static ExprAST *ParseIfExpr() {
370 getNextToken(); // eat the if.
373 ExprAST *Cond = ParseExpression();
376 if (CurTok != tok_then)
377 return Error("expected then");
378 getNextToken(); // eat the then
380 ExprAST *Then = ParseExpression();
381 if (Then == 0) return 0;
383 if (CurTok != tok_else)
384 return Error("expected else");
388 ExprAST *Else = ParseExpression();
391 return new IfExprAST(Cond, Then, Else);
394 /// forexpr ::= 'for' identifier '=' expr ',' expr (',' expr)? 'in' expression
395 static ExprAST *ParseForExpr() {
396 getNextToken(); // eat the for.
398 if (CurTok != tok_identifier)
399 return Error("expected identifier after for");
401 std::string IdName = IdentifierStr;
402 getNextToken(); // eat identifier.
405 return Error("expected '=' after for");
406 getNextToken(); // eat '='.
409 ExprAST *Start = ParseExpression();
410 if (Start == 0) return 0;
412 return Error("expected ',' after for start value");
415 ExprAST *End = ParseExpression();
416 if (End == 0) return 0;
418 // The step value is optional.
422 Step = ParseExpression();
423 if (Step == 0) return 0;
426 if (CurTok != tok_in)
427 return Error("expected 'in' after for");
428 getNextToken(); // eat 'in'.
430 ExprAST *Body = ParseExpression();
431 if (Body == 0) return 0;
433 return new ForExprAST(IdName, Start, End, Step, Body);
436 /// varexpr ::= 'var' identifier ('=' expression)?
437 // (',' identifier ('=' expression)?)* 'in' expression
438 static ExprAST *ParseVarExpr() {
439 getNextToken(); // eat the var.
441 std::vector<std::pair<std::string, ExprAST*> > VarNames;
443 // At least one variable name is required.
444 if (CurTok != tok_identifier)
445 return Error("expected identifier after var");
448 std::string Name = IdentifierStr;
449 getNextToken(); // eat identifier.
451 // Read the optional initializer.
454 getNextToken(); // eat the '='.
456 Init = ParseExpression();
457 if (Init == 0) return 0;
460 VarNames.push_back(std::make_pair(Name, Init));
462 // End of var list, exit loop.
463 if (CurTok != ',') break;
464 getNextToken(); // eat the ','.
466 if (CurTok != tok_identifier)
467 return Error("expected identifier list after var");
470 // At this point, we have to have 'in'.
471 if (CurTok != tok_in)
472 return Error("expected 'in' keyword after 'var'");
473 getNextToken(); // eat 'in'.
475 ExprAST *Body = ParseExpression();
476 if (Body == 0) return 0;
478 return new VarExprAST(VarNames, Body);
482 /// ::= identifierexpr
488 static ExprAST *ParsePrimary() {
490 default: return Error("unknown token when expecting an expression");
491 case tok_identifier: return ParseIdentifierExpr();
492 case tok_number: return ParseNumberExpr();
493 case '(': return ParseParenExpr();
494 case tok_if: return ParseIfExpr();
495 case tok_for: return ParseForExpr();
496 case tok_var: return ParseVarExpr();
503 static ExprAST *ParseUnary() {
504 // If the current token is not an operator, it must be a primary expr.
505 if (!isascii(CurTok) || CurTok == '(' || CurTok == ',')
506 return ParsePrimary();
508 // If this is a unary operator, read it.
511 if (ExprAST *Operand = ParseUnary())
512 return new UnaryExprAST(Opc, Operand);
518 static ExprAST *ParseBinOpRHS(int ExprPrec, ExprAST *LHS) {
519 // If this is a binop, find its precedence.
521 int TokPrec = GetTokPrecedence();
523 // If this is a binop that binds at least as tightly as the current binop,
524 // consume it, otherwise we are done.
525 if (TokPrec < ExprPrec)
528 // Okay, we know this is a binop.
530 getNextToken(); // eat binop
532 // Parse the unary expression after the binary operator.
533 ExprAST *RHS = ParseUnary();
536 // If BinOp binds less tightly with RHS than the operator after RHS, let
537 // the pending operator take RHS as its LHS.
538 int NextPrec = GetTokPrecedence();
539 if (TokPrec < NextPrec) {
540 RHS = ParseBinOpRHS(TokPrec+1, RHS);
541 if (RHS == 0) return 0;
545 LHS = new BinaryExprAST(BinOp, LHS, RHS);
550 /// ::= unary binoprhs
552 static ExprAST *ParseExpression() {
553 ExprAST *LHS = ParseUnary();
556 return ParseBinOpRHS(0, LHS);
560 /// ::= id '(' id* ')'
561 /// ::= binary LETTER number? (id, id)
562 /// ::= unary LETTER (id)
563 static PrototypeAST *ParsePrototype() {
566 unsigned Kind = 0; // 0 = identifier, 1 = unary, 2 = binary.
567 unsigned BinaryPrecedence = 30;
571 return ErrorP("Expected function name in prototype");
573 FnName = IdentifierStr;
579 if (!isascii(CurTok))
580 return ErrorP("Expected unary operator");
582 FnName += (char)CurTok;
588 if (!isascii(CurTok))
589 return ErrorP("Expected binary operator");
591 FnName += (char)CurTok;
595 // Read the precedence if present.
596 if (CurTok == tok_number) {
597 if (NumVal < 1 || NumVal > 100)
598 return ErrorP("Invalid precedecnce: must be 1..100");
599 BinaryPrecedence = (unsigned)NumVal;
606 return ErrorP("Expected '(' in prototype");
608 std::vector<std::string> ArgNames;
609 while (getNextToken() == tok_identifier)
610 ArgNames.push_back(IdentifierStr);
612 return ErrorP("Expected ')' in prototype");
615 getNextToken(); // eat ')'.
617 // Verify right number of names for operator.
618 if (Kind && ArgNames.size() != Kind)
619 return ErrorP("Invalid number of operands for operator");
621 return new PrototypeAST(FnName, ArgNames, Kind != 0, BinaryPrecedence);
624 /// definition ::= 'def' prototype expression
625 static FunctionAST *ParseDefinition() {
626 getNextToken(); // eat def.
627 PrototypeAST *Proto = ParsePrototype();
628 if (Proto == 0) return 0;
630 if (ExprAST *E = ParseExpression())
631 return new FunctionAST(Proto, E);
635 /// toplevelexpr ::= expression
636 static FunctionAST *ParseTopLevelExpr() {
637 if (ExprAST *E = ParseExpression()) {
638 // Make an anonymous proto.
639 PrototypeAST *Proto = new PrototypeAST("", std::vector<std::string>());
640 return new FunctionAST(Proto, E);
645 /// external ::= 'extern' prototype
646 static PrototypeAST *ParseExtern() {
647 getNextToken(); // eat extern.
648 return ParsePrototype();
651 //===----------------------------------------------------------------------===//
652 // Quick and dirty hack
653 //===----------------------------------------------------------------------===//
655 // FIXME: Obviously we can do better than this
656 std::string GenerateUniqueName(const char *root)
660 sprintf(s, "%s%d", root, i++);
665 std::string MakeLegalFunctionName(std::string Name)
669 return GenerateUniqueName("anon_func_");
671 // Start with what we have
674 // Look for a numberic first character
675 if (NewName.find_first_of("0123456789") == 0) {
676 NewName.insert(0, 1, 'n');
679 // Replace illegal characters with their ASCII equivalent
680 std::string legal_elements = "_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
682 while ((pos = NewName.find_first_not_of(legal_elements)) != std::string::npos) {
683 char old_c = NewName.at(pos);
685 sprintf(new_str, "%d", (int)old_c);
686 NewName = NewName.replace(pos, 1, new_str);
692 //===----------------------------------------------------------------------===//
693 // MCJIT object cache class
694 //===----------------------------------------------------------------------===//
696 class MCJITObjectCache : public ObjectCache {
699 // Set IR cache directory
700 sys::fs::current_path(CacheDir);
701 sys::path::append(CacheDir, "toy_object_cache");
704 virtual ~MCJITObjectCache() {
707 virtual void notifyObjectCompiled(const Module *M, const MemoryBuffer *Obj) {
709 const std::string ModuleID = M->getModuleIdentifier();
711 // If we've flagged this as an IR file, cache it
712 if (0 == ModuleID.compare(0, 3, "IR:")) {
713 std::string IRFileName = ModuleID.substr(3);
714 SmallString<128>IRCacheFile = CacheDir;
715 sys::path::append(IRCacheFile, IRFileName);
716 if (!sys::fs::exists(CacheDir.str()) && sys::fs::create_directory(CacheDir.str())) {
717 fprintf(stderr, "Unable to create cache directory\n");
721 raw_fd_ostream IRObjectFile(IRCacheFile.c_str(), ErrStr, raw_fd_ostream::F_Binary);
722 IRObjectFile << Obj->getBuffer();
726 // MCJIT will call this function before compiling any module
727 // MCJIT takes ownership of both the MemoryBuffer object and the memory
728 // to which it refers.
729 virtual MemoryBuffer* getObject(const Module* M) {
731 const std::string ModuleID = M->getModuleIdentifier();
733 // If we've flagged this as an IR file, cache it
734 if (0 == ModuleID.compare(0, 3, "IR:")) {
735 std::string IRFileName = ModuleID.substr(3);
736 SmallString<128> IRCacheFile = CacheDir;
737 sys::path::append(IRCacheFile, IRFileName);
738 if (!sys::fs::exists(IRCacheFile.str())) {
739 // This file isn't in our cache
742 std::unique_ptr<MemoryBuffer> IRObjectBuffer;
743 MemoryBuffer::getFile(IRCacheFile.c_str(), IRObjectBuffer, -1, false);
744 // MCJIT will want to write into this buffer, and we don't want that
745 // because the file has probably just been mmapped. Instead we make
746 // a copy. The filed-based buffer will be released when it goes
748 return MemoryBuffer::getMemBufferCopy(IRObjectBuffer->getBuffer());
755 SmallString<128> CacheDir;
758 //===----------------------------------------------------------------------===//
759 // IR input file handler
760 //===----------------------------------------------------------------------===//
762 Module* parseInputIR(std::string InputFile, LLVMContext &Context) {
764 Module *M = ParseIRFile(InputFile, Err, Context);
766 Err.print("IR parsing failed: ", errs());
771 sprintf(ModID, "IR:%s", InputFile.c_str());
772 M->setModuleIdentifier(ModID);
776 //===----------------------------------------------------------------------===//
777 // Helper class for execution engine abstraction
778 //===----------------------------------------------------------------------===//
784 virtual ~BaseHelper() {}
786 virtual Function *getFunction(const std::string FnName) = 0;
787 virtual Module *getModuleForNewFunction() = 0;
788 virtual void *getPointerToFunction(Function* F) = 0;
789 virtual void *getPointerToNamedFunction(const std::string &Name) = 0;
790 virtual void closeCurrentModule() = 0;
791 virtual void runFPM(Function &F) = 0;
795 //===----------------------------------------------------------------------===//
796 // Helper class for JIT execution engine
797 //===----------------------------------------------------------------------===//
799 class JITHelper : public BaseHelper {
801 JITHelper(LLVMContext &Context) {
802 // Make the module, which holds all the code.
803 if (!InputIR.empty()) {
804 TheModule = parseInputIR(InputIR, Context);
806 TheModule = new Module("my cool jit", Context);
809 // Create the JIT. This takes ownership of the module.
811 TheExecutionEngine = EngineBuilder(TheModule).setErrorStr(&ErrStr).create();
812 if (!TheExecutionEngine) {
813 fprintf(stderr, "Could not create ExecutionEngine: %s\n", ErrStr.c_str());
817 TheFPM = new FunctionPassManager(TheModule);
819 // Set up the optimizer pipeline. Start with registering info about how the
820 // target lays out data structures.
821 TheFPM->add(new DataLayout(*TheExecutionEngine->getDataLayout()));
822 // Provide basic AliasAnalysis support for GVN.
823 TheFPM->add(createBasicAliasAnalysisPass());
824 // Promote allocas to registers.
825 TheFPM->add(createPromoteMemoryToRegisterPass());
826 // Do simple "peephole" optimizations and bit-twiddling optzns.
827 TheFPM->add(createInstructionCombiningPass());
828 // Reassociate expressions.
829 TheFPM->add(createReassociatePass());
830 // Eliminate Common SubExpressions.
831 TheFPM->add(createGVNPass());
832 // Simplify the control flow graph (deleting unreachable blocks, etc).
833 TheFPM->add(createCFGSimplificationPass());
835 TheFPM->doInitialization();
838 virtual ~JITHelper() {
841 if (TheExecutionEngine)
842 delete TheExecutionEngine;
845 virtual Function *getFunction(const std::string FnName) {
847 return TheModule->getFunction(FnName);
850 virtual Module *getModuleForNewFunction() {
855 virtual void *getPointerToFunction(Function* F) {
856 assert(TheExecutionEngine);
857 return TheExecutionEngine->getPointerToFunction(F);
860 virtual void *getPointerToNamedFunction(const std::string &Name) {
861 return TheExecutionEngine->getPointerToNamedFunction(Name);
864 virtual void runFPM(Function &F) {
869 virtual void closeCurrentModule() {
870 // This should never be called for JIT
874 virtual void dump() {
881 ExecutionEngine *TheExecutionEngine;
882 FunctionPassManager *TheFPM;
885 //===----------------------------------------------------------------------===//
886 // MCJIT helper class
887 //===----------------------------------------------------------------------===//
889 class MCJITHelper : public BaseHelper
892 MCJITHelper(LLVMContext& C) : Context(C), CurrentModule(NULL) {
893 if (!InputIR.empty()) {
894 Module *M = parseInputIR(InputIR, Context);
895 Modules.push_back(M);
896 if (!EnableLazyCompilation)
902 Function *getFunction(const std::string FnName);
903 Module *getModuleForNewFunction();
904 void *getPointerToFunction(Function* F);
905 void *getPointerToNamedFunction(const std::string &Name);
906 void closeCurrentModule();
907 virtual void runFPM(Function &F) {} // Not needed, see compileModule
911 ExecutionEngine *compileModule(Module *M);
914 typedef std::vector<Module*> ModuleVector;
916 MCJITObjectCache OurObjectCache;
918 LLVMContext &Context;
919 ModuleVector Modules;
921 std::map<Module *, ExecutionEngine *> EngineMap;
923 Module *CurrentModule;
926 class HelpingMemoryManager : public SectionMemoryManager
928 HelpingMemoryManager(const HelpingMemoryManager&) LLVM_DELETED_FUNCTION;
929 void operator=(const HelpingMemoryManager&) LLVM_DELETED_FUNCTION;
932 HelpingMemoryManager(MCJITHelper *Helper) : MasterHelper(Helper) {}
933 virtual ~HelpingMemoryManager() {}
935 /// This method returns the address of the specified function.
936 /// Our implementation will attempt to find functions in other
937 /// modules associated with the MCJITHelper to cross link functions
938 /// from one generated module to another.
940 /// If \p AbortOnFailure is false and no function with the given name is
941 /// found, this function returns a null pointer. Otherwise, it prints a
942 /// message to stderr and aborts.
943 virtual void *getPointerToNamedFunction(const std::string &Name,
944 bool AbortOnFailure = true);
946 MCJITHelper *MasterHelper;
949 void *HelpingMemoryManager::getPointerToNamedFunction(const std::string &Name,
952 // Try the standard symbol resolution first, but ask it not to abort.
953 void *pfn = RTDyldMemoryManager::getPointerToNamedFunction(Name, false);
957 pfn = MasterHelper->getPointerToNamedFunction(Name);
958 if (!pfn && AbortOnFailure)
959 report_fatal_error("Program used external function '" + Name +
960 "' which could not be resolved!");
964 MCJITHelper::~MCJITHelper()
966 // Walk the vector of modules.
967 ModuleVector::iterator it, end;
968 for (it = Modules.begin(), end = Modules.end();
970 // See if we have an execution engine for this module.
971 std::map<Module*, ExecutionEngine*>::iterator mapIt = EngineMap.find(*it);
972 // If we have an EE, the EE owns the module so just delete the EE.
973 if (mapIt != EngineMap.end()) {
974 delete mapIt->second;
976 // Otherwise, we still own the module. Delete it now.
982 Function *MCJITHelper::getFunction(const std::string FnName) {
983 ModuleVector::iterator begin = Modules.begin();
984 ModuleVector::iterator end = Modules.end();
985 ModuleVector::iterator it;
986 for (it = begin; it != end; ++it) {
987 Function *F = (*it)->getFunction(FnName);
989 if (*it == CurrentModule)
992 assert(CurrentModule != NULL);
994 // This function is in a module that has already been JITed.
995 // We just need a prototype for external linkage.
996 Function *PF = CurrentModule->getFunction(FnName);
997 if (PF && !PF->empty()) {
998 ErrorF("redefinition of function across modules");
1002 // If we don't have a prototype yet, create one.
1004 PF = Function::Create(F->getFunctionType(),
1005 Function::ExternalLinkage,
1014 Module *MCJITHelper::getModuleForNewFunction() {
1015 // If we have a Module that hasn't been JITed, use that.
1017 return CurrentModule;
1019 // Otherwise create a new Module.
1020 std::string ModName = GenerateUniqueName("mcjit_module_");
1021 Module *M = new Module(ModName, Context);
1022 Modules.push_back(M);
1028 ExecutionEngine *MCJITHelper::compileModule(Module *M) {
1029 assert(EngineMap.find(M) == EngineMap.end());
1031 if (M == CurrentModule)
1032 closeCurrentModule();
1035 ExecutionEngine *EE = EngineBuilder(M)
1036 .setErrorStr(&ErrStr)
1038 .setMCJITMemoryManager(new HelpingMemoryManager(this))
1041 fprintf(stderr, "Could not create ExecutionEngine: %s\n", ErrStr.c_str());
1046 EE->setObjectCache(&OurObjectCache);
1047 // Get the ModuleID so we can identify IR input files
1048 const std::string ModuleID = M->getModuleIdentifier();
1050 // If we've flagged this as an IR file, it doesn't need function passes run.
1051 if (0 != ModuleID.compare(0, 3, "IR:")) {
1052 FunctionPassManager *FPM = 0;
1054 // Create a FPM for this module
1055 FPM = new FunctionPassManager(M);
1057 // Set up the optimizer pipeline. Start with registering info about how the
1058 // target lays out data structures.
1059 FPM->add(new DataLayout(*EE->getDataLayout()));
1060 // Provide basic AliasAnalysis support for GVN.
1061 FPM->add(createBasicAliasAnalysisPass());
1062 // Promote allocas to registers.
1063 FPM->add(createPromoteMemoryToRegisterPass());
1064 // Do simple "peephole" optimizations and bit-twiddling optzns.
1065 FPM->add(createInstructionCombiningPass());
1066 // Reassociate expressions.
1067 FPM->add(createReassociatePass());
1068 // Eliminate Common SubExpressions.
1069 FPM->add(createGVNPass());
1070 // Simplify the control flow graph (deleting unreachable blocks, etc).
1071 FPM->add(createCFGSimplificationPass());
1073 FPM->doInitialization();
1075 // For each function in the module
1076 Module::iterator it;
1077 Module::iterator end = M->end();
1078 for (it = M->begin(); it != end; ++it) {
1079 // Run the FPM on this function
1086 EE->finalizeObject();
1088 // Store this engine
1094 void *MCJITHelper::getPointerToFunction(Function* F) {
1095 // Look for this function in an existing module
1096 ModuleVector::iterator begin = Modules.begin();
1097 ModuleVector::iterator end = Modules.end();
1098 ModuleVector::iterator it;
1099 std::string FnName = F->getName();
1100 for (it = begin; it != end; ++it) {
1101 Function *MF = (*it)->getFunction(FnName);
1103 std::map<Module*, ExecutionEngine*>::iterator eeIt = EngineMap.find(*it);
1104 if (eeIt != EngineMap.end()) {
1105 void *P = eeIt->second->getPointerToFunction(F);
1109 ExecutionEngine *EE = compileModule(*it);
1110 void *P = EE->getPointerToFunction(F);
1119 void MCJITHelper::closeCurrentModule() {
1120 // If we have an open module (and we should), pack it up
1121 if (CurrentModule) {
1122 CurrentModule = NULL;
1126 void *MCJITHelper::getPointerToNamedFunction(const std::string &Name)
1128 // Look for the functions in our modules, compiling only as necessary
1129 ModuleVector::iterator begin = Modules.begin();
1130 ModuleVector::iterator end = Modules.end();
1131 ModuleVector::iterator it;
1132 for (it = begin; it != end; ++it) {
1133 Function *F = (*it)->getFunction(Name);
1134 if (F && !F->empty()) {
1135 std::map<Module*, ExecutionEngine*>::iterator eeIt = EngineMap.find(*it);
1136 if (eeIt != EngineMap.end()) {
1137 void *P = eeIt->second->getPointerToFunction(F);
1141 ExecutionEngine *EE = compileModule(*it);
1142 void *P = EE->getPointerToFunction(F);
1151 void MCJITHelper::dump()
1153 ModuleVector::iterator begin = Modules.begin();
1154 ModuleVector::iterator end = Modules.end();
1155 ModuleVector::iterator it;
1156 for (it = begin; it != end; ++it)
1160 //===----------------------------------------------------------------------===//
1162 //===----------------------------------------------------------------------===//
1164 static BaseHelper *TheHelper;
1165 static IRBuilder<> Builder(getGlobalContext());
1166 static std::map<std::string, AllocaInst*> NamedValues;
1168 Value *ErrorV(const char *Str) { Error(Str); return 0; }
1170 /// CreateEntryBlockAlloca - Create an alloca instruction in the entry block of
1171 /// the function. This is used for mutable variables etc.
1172 static AllocaInst *CreateEntryBlockAlloca(Function *TheFunction,
1173 const std::string &VarName) {
1174 IRBuilder<> TmpB(&TheFunction->getEntryBlock(),
1175 TheFunction->getEntryBlock().begin());
1176 return TmpB.CreateAlloca(Type::getDoubleTy(getGlobalContext()), 0,
1180 Value *NumberExprAST::Codegen() {
1181 return ConstantFP::get(getGlobalContext(), APFloat(Val));
1184 Value *VariableExprAST::Codegen() {
1185 // Look this variable up in the function.
1186 Value *V = NamedValues[Name];
1187 if (V == 0) return ErrorV("Unknown variable name");
1190 return Builder.CreateLoad(V, Name.c_str());
1193 Value *UnaryExprAST::Codegen() {
1194 Value *OperandV = Operand->Codegen();
1195 if (OperandV == 0) return 0;
1198 F = TheHelper->getFunction(MakeLegalFunctionName(std::string("unary")+Opcode));
1200 F = TheHelper->getFunction(std::string("unary")+Opcode);
1202 return ErrorV("Unknown unary operator");
1204 return Builder.CreateCall(F, OperandV, "unop");
1207 Value *BinaryExprAST::Codegen() {
1208 // Special case '=' because we don't want to emit the LHS as an expression.
1210 // Assignment requires the LHS to be an identifier.
1211 // This assume we're building without RTTI because LLVM builds that way by
1212 // default. If you build LLVM with RTTI this can be changed to a
1213 // dynamic_cast for automatic error checking.
1214 VariableExprAST *LHSE = reinterpret_cast<VariableExprAST*>(LHS);
1216 return ErrorV("destination of '=' must be a variable");
1218 Value *Val = RHS->Codegen();
1219 if (Val == 0) return 0;
1221 // Look up the name.
1222 Value *Variable = NamedValues[LHSE->getName()];
1223 if (Variable == 0) return ErrorV("Unknown variable name");
1225 Builder.CreateStore(Val, Variable);
1229 Value *L = LHS->Codegen();
1230 Value *R = RHS->Codegen();
1231 if (L == 0 || R == 0) return 0;
1234 case '+': return Builder.CreateFAdd(L, R, "addtmp");
1235 case '-': return Builder.CreateFSub(L, R, "subtmp");
1236 case '*': return Builder.CreateFMul(L, R, "multmp");
1237 case '/': return Builder.CreateFDiv(L, R, "divtmp");
1239 L = Builder.CreateFCmpULT(L, R, "cmptmp");
1240 // Convert bool 0/1 to double 0.0 or 1.0
1241 return Builder.CreateUIToFP(L, Type::getDoubleTy(getGlobalContext()),
1246 // If it wasn't a builtin binary operator, it must be a user defined one. Emit
1250 F = TheHelper->getFunction(MakeLegalFunctionName(std::string("binary")+Op));
1252 F = TheHelper->getFunction(std::string("binary")+Op);
1253 assert(F && "binary operator not found!");
1255 Value *Ops[] = { L, R };
1256 return Builder.CreateCall(F, Ops, "binop");
1259 Value *CallExprAST::Codegen() {
1260 // Look up the name in the global module table.
1261 Function *CalleeF = TheHelper->getFunction(Callee);
1264 sprintf(error_str, "Unknown function referenced %s", Callee.c_str());
1265 return ErrorV(error_str);
1268 // If argument mismatch error.
1269 if (CalleeF->arg_size() != Args.size())
1270 return ErrorV("Incorrect # arguments passed");
1272 std::vector<Value*> ArgsV;
1273 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
1274 ArgsV.push_back(Args[i]->Codegen());
1275 if (ArgsV.back() == 0) return 0;
1278 return Builder.CreateCall(CalleeF, ArgsV, "calltmp");
1281 Value *IfExprAST::Codegen() {
1282 Value *CondV = Cond->Codegen();
1283 if (CondV == 0) return 0;
1285 // Convert condition to a bool by comparing equal to 0.0.
1286 CondV = Builder.CreateFCmpONE(CondV,
1287 ConstantFP::get(getGlobalContext(), APFloat(0.0)),
1290 Function *TheFunction = Builder.GetInsertBlock()->getParent();
1292 // Create blocks for the then and else cases. Insert the 'then' block at the
1293 // end of the function.
1294 BasicBlock *ThenBB = BasicBlock::Create(getGlobalContext(), "then", TheFunction);
1295 BasicBlock *ElseBB = BasicBlock::Create(getGlobalContext(), "else");
1296 BasicBlock *MergeBB = BasicBlock::Create(getGlobalContext(), "ifcont");
1298 Builder.CreateCondBr(CondV, ThenBB, ElseBB);
1301 Builder.SetInsertPoint(ThenBB);
1303 Value *ThenV = Then->Codegen();
1304 if (ThenV == 0) return 0;
1306 Builder.CreateBr(MergeBB);
1307 // Codegen of 'Then' can change the current block, update ThenBB for the PHI.
1308 ThenBB = Builder.GetInsertBlock();
1311 TheFunction->getBasicBlockList().push_back(ElseBB);
1312 Builder.SetInsertPoint(ElseBB);
1314 Value *ElseV = Else->Codegen();
1315 if (ElseV == 0) return 0;
1317 Builder.CreateBr(MergeBB);
1318 // Codegen of 'Else' can change the current block, update ElseBB for the PHI.
1319 ElseBB = Builder.GetInsertBlock();
1321 // Emit merge block.
1322 TheFunction->getBasicBlockList().push_back(MergeBB);
1323 Builder.SetInsertPoint(MergeBB);
1324 PHINode *PN = Builder.CreatePHI(Type::getDoubleTy(getGlobalContext()), 2,
1327 PN->addIncoming(ThenV, ThenBB);
1328 PN->addIncoming(ElseV, ElseBB);
1332 Value *ForExprAST::Codegen() {
1334 // var = alloca double
1336 // start = startexpr
1337 // store start -> var
1345 // endcond = endexpr
1347 // curvar = load var
1348 // nextvar = curvar + step
1349 // store nextvar -> var
1350 // br endcond, loop, endloop
1353 Function *TheFunction = Builder.GetInsertBlock()->getParent();
1355 // Create an alloca for the variable in the entry block.
1356 AllocaInst *Alloca = CreateEntryBlockAlloca(TheFunction, VarName);
1358 // Emit the start code first, without 'variable' in scope.
1359 Value *StartVal = Start->Codegen();
1360 if (StartVal == 0) return 0;
1362 // Store the value into the alloca.
1363 Builder.CreateStore(StartVal, Alloca);
1365 // Make the new basic block for the loop header, inserting after current
1367 BasicBlock *LoopBB = BasicBlock::Create(getGlobalContext(), "loop", TheFunction);
1369 // Insert an explicit fall through from the current block to the LoopBB.
1370 Builder.CreateBr(LoopBB);
1372 // Start insertion in LoopBB.
1373 Builder.SetInsertPoint(LoopBB);
1375 // Within the loop, the variable is defined equal to the PHI node. If it
1376 // shadows an existing variable, we have to restore it, so save it now.
1377 AllocaInst *OldVal = NamedValues[VarName];
1378 NamedValues[VarName] = Alloca;
1380 // Emit the body of the loop. This, like any other expr, can change the
1381 // current BB. Note that we ignore the value computed by the body, but don't
1383 if (Body->Codegen() == 0)
1386 // Emit the step value.
1389 StepVal = Step->Codegen();
1390 if (StepVal == 0) return 0;
1392 // If not specified, use 1.0.
1393 StepVal = ConstantFP::get(getGlobalContext(), APFloat(1.0));
1396 // Compute the end condition.
1397 Value *EndCond = End->Codegen();
1398 if (EndCond == 0) return EndCond;
1400 // Reload, increment, and restore the alloca. This handles the case where
1401 // the body of the loop mutates the variable.
1402 Value *CurVar = Builder.CreateLoad(Alloca, VarName.c_str());
1403 Value *NextVar = Builder.CreateFAdd(CurVar, StepVal, "nextvar");
1404 Builder.CreateStore(NextVar, Alloca);
1406 // Convert condition to a bool by comparing equal to 0.0.
1407 EndCond = Builder.CreateFCmpONE(EndCond,
1408 ConstantFP::get(getGlobalContext(), APFloat(0.0)),
1411 // Create the "after loop" block and insert it.
1412 BasicBlock *AfterBB = BasicBlock::Create(getGlobalContext(), "afterloop", TheFunction);
1414 // Insert the conditional branch into the end of LoopEndBB.
1415 Builder.CreateCondBr(EndCond, LoopBB, AfterBB);
1417 // Any new code will be inserted in AfterBB.
1418 Builder.SetInsertPoint(AfterBB);
1420 // Restore the unshadowed variable.
1422 NamedValues[VarName] = OldVal;
1424 NamedValues.erase(VarName);
1427 // for expr always returns 0.0.
1428 return Constant::getNullValue(Type::getDoubleTy(getGlobalContext()));
1431 Value *VarExprAST::Codegen() {
1432 std::vector<AllocaInst *> OldBindings;
1434 Function *TheFunction = Builder.GetInsertBlock()->getParent();
1436 // Register all variables and emit their initializer.
1437 for (unsigned i = 0, e = VarNames.size(); i != e; ++i) {
1438 const std::string &VarName = VarNames[i].first;
1439 ExprAST *Init = VarNames[i].second;
1441 // Emit the initializer before adding the variable to scope, this prevents
1442 // the initializer from referencing the variable itself, and permits stuff
1445 // var a = a in ... # refers to outer 'a'.
1448 InitVal = Init->Codegen();
1449 if (InitVal == 0) return 0;
1450 } else { // If not specified, use 0.0.
1451 InitVal = ConstantFP::get(getGlobalContext(), APFloat(0.0));
1454 AllocaInst *Alloca = CreateEntryBlockAlloca(TheFunction, VarName);
1455 Builder.CreateStore(InitVal, Alloca);
1457 // Remember the old variable binding so that we can restore the binding when
1459 OldBindings.push_back(NamedValues[VarName]);
1461 // Remember this binding.
1462 NamedValues[VarName] = Alloca;
1465 // Codegen the body, now that all vars are in scope.
1466 Value *BodyVal = Body->Codegen();
1467 if (BodyVal == 0) return 0;
1469 // Pop all our variables from scope.
1470 for (unsigned i = 0, e = VarNames.size(); i != e; ++i)
1471 NamedValues[VarNames[i].first] = OldBindings[i];
1473 // Return the body computation.
1477 Function *PrototypeAST::Codegen() {
1478 // Make the function type: double(double,double) etc.
1479 std::vector<Type*> Doubles(Args.size(),
1480 Type::getDoubleTy(getGlobalContext()));
1481 FunctionType *FT = FunctionType::get(Type::getDoubleTy(getGlobalContext()),
1486 FnName = MakeLegalFunctionName(Name);
1490 Module* M = TheHelper->getModuleForNewFunction();
1491 Function *F = Function::Create(FT, Function::ExternalLinkage, FnName, M);
1493 // FIXME: Implement duplicate function detection.
1494 // The check below will only work if the duplicate is in the open module.
1495 // If F conflicted, there was already something named 'Name'. If it has a
1496 // body, don't allow redefinition or reextern.
1497 if (F->getName() != FnName) {
1498 // Delete the one we just made and get the existing one.
1499 F->eraseFromParent();
1500 F = M->getFunction(FnName);
1501 // If F already has a body, reject this.
1503 ErrorF("redefinition of function");
1506 // If F took a different number of args, reject.
1507 if (F->arg_size() != Args.size()) {
1508 ErrorF("redefinition of function with different # args");
1513 // Set names for all arguments.
1515 for (Function::arg_iterator AI = F->arg_begin(); Idx != Args.size();
1517 AI->setName(Args[Idx]);
1522 /// CreateArgumentAllocas - Create an alloca for each argument and register the
1523 /// argument in the symbol table so that references to it will succeed.
1524 void PrototypeAST::CreateArgumentAllocas(Function *F) {
1525 Function::arg_iterator AI = F->arg_begin();
1526 for (unsigned Idx = 0, e = Args.size(); Idx != e; ++Idx, ++AI) {
1527 // Create an alloca for this variable.
1528 AllocaInst *Alloca = CreateEntryBlockAlloca(F, Args[Idx]);
1530 // Store the initial value into the alloca.
1531 Builder.CreateStore(AI, Alloca);
1533 // Add arguments to variable symbol table.
1534 NamedValues[Args[Idx]] = Alloca;
1538 Function *FunctionAST::Codegen() {
1539 NamedValues.clear();
1541 Function *TheFunction = Proto->Codegen();
1542 if (TheFunction == 0)
1545 // If this is an operator, install it.
1546 if (Proto->isBinaryOp())
1547 BinopPrecedence[Proto->getOperatorName()] = Proto->getBinaryPrecedence();
1549 // Create a new basic block to start insertion into.
1550 BasicBlock *BB = BasicBlock::Create(getGlobalContext(), "entry", TheFunction);
1551 Builder.SetInsertPoint(BB);
1553 // Add all arguments to the symbol table and create their allocas.
1554 Proto->CreateArgumentAllocas(TheFunction);
1556 if (Value *RetVal = Body->Codegen()) {
1557 // Finish off the function.
1558 Builder.CreateRet(RetVal);
1560 // Validate the generated code, checking for consistency.
1561 verifyFunction(*TheFunction);
1563 // Optimize the function.
1565 TheHelper->runFPM(*TheFunction);
1570 // Error reading body, remove function.
1571 TheFunction->eraseFromParent();
1573 if (Proto->isBinaryOp())
1574 BinopPrecedence.erase(Proto->getOperatorName());
1578 //===----------------------------------------------------------------------===//
1579 // Top-Level parsing and JIT Driver
1580 //===----------------------------------------------------------------------===//
1582 static void HandleDefinition() {
1583 if (FunctionAST *F = ParseDefinition()) {
1584 if (UseMCJIT && EnableLazyCompilation)
1585 TheHelper->closeCurrentModule();
1586 Function *LF = F->Codegen();
1587 if (LF && VerboseOutput) {
1588 fprintf(stderr, "Read function definition:");
1592 // Skip token for error recovery.
1597 static void HandleExtern() {
1598 if (PrototypeAST *P = ParseExtern()) {
1599 Function *F = P->Codegen();
1600 if (F && VerboseOutput) {
1601 fprintf(stderr, "Read extern: ");
1605 // Skip token for error recovery.
1610 static void HandleTopLevelExpression() {
1611 // Evaluate a top-level expression into an anonymous function.
1612 if (FunctionAST *F = ParseTopLevelExpr()) {
1613 if (Function *LF = F->Codegen()) {
1614 // JIT the function, returning a function pointer.
1615 void *FPtr = TheHelper->getPointerToFunction(LF);
1616 // Cast it to the right type (takes no arguments, returns a double) so we
1617 // can call it as a native function.
1618 double (*FP)() = (double (*)())(intptr_t)FPtr;
1619 double Result = FP();
1621 fprintf(stderr, "Evaluated to %f\n", Result);
1624 // Skip token for error recovery.
1629 /// top ::= definition | external | expression | ';'
1630 static void MainLoop() {
1632 if (!SuppressPrompts)
1633 fprintf(stderr, "ready> ");
1635 case tok_eof: return;
1636 case ';': getNextToken(); break; // ignore top-level semicolons.
1637 case tok_def: HandleDefinition(); break;
1638 case tok_extern: HandleExtern(); break;
1639 default: HandleTopLevelExpression(); break;
1644 //===----------------------------------------------------------------------===//
1645 // "Library" functions that can be "extern'd" from user code.
1646 //===----------------------------------------------------------------------===//
1648 /// putchard - putchar that takes a double and returns 0.
1650 double putchard(double X) {
1655 /// printd - printf that takes a double prints it as "%f\n", returning 0.
1657 double printd(double X) {
1668 //===----------------------------------------------------------------------===//
1669 // Main driver code.
1670 //===----------------------------------------------------------------------===//
1672 int main(int argc, char **argv) {
1673 InitializeNativeTarget();
1675 InitializeNativeTargetAsmPrinter();
1676 InitializeNativeTargetAsmParser();
1678 LLVMContext &Context = getGlobalContext();
1680 cl::ParseCommandLineOptions(argc, argv,
1681 "Kaleidoscope example program\n");
1683 // Install standard binary operators.
1684 // 1 is lowest precedence.
1685 BinopPrecedence['='] = 2;
1686 BinopPrecedence['<'] = 10;
1687 BinopPrecedence['+'] = 20;
1688 BinopPrecedence['-'] = 20;
1689 BinopPrecedence['/'] = 40;
1690 BinopPrecedence['*'] = 40; // highest.
1692 // Make the Helper, which holds all the code.
1694 TheHelper = new MCJITHelper(Context);
1696 TheHelper = new JITHelper(Context);
1698 // Prime the first token.
1699 if (!SuppressPrompts)
1700 fprintf(stderr, "ready> ");
1703 // Run the main "interpreter loop" now.
1706 // Print out all of the generated code.
1707 if (DumpModulesOnExit)