1 #include "llvm/ADT/STLExtras.h"
2 #include "llvm/Analysis/Passes.h"
3 #include "llvm/ExecutionEngine/ExecutionEngine.h"
4 #include "llvm/ExecutionEngine/MCJIT.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/Support/TargetSelect.h"
14 #include "llvm/Transforms/Scalar.h"
22 //===----------------------------------------------------------------------===//
24 //===----------------------------------------------------------------------===//
26 // The lexer returns tokens [0-255] if it is an unknown character, otherwise one
27 // of these for known things.
47 static std::string IdentifierStr; // Filled in if tok_identifier
48 static double NumVal; // Filled in if tok_number
50 /// gettok - Return the next token from standard input.
52 static int LastChar = ' ';
54 // Skip any whitespace.
55 while (isspace(LastChar))
58 if (isalpha(LastChar)) { // identifier: [a-zA-Z][a-zA-Z0-9]*
59 IdentifierStr = LastChar;
60 while (isalnum((LastChar = getchar())))
61 IdentifierStr += LastChar;
63 if (IdentifierStr == "def")
65 if (IdentifierStr == "extern")
67 if (IdentifierStr == "if")
69 if (IdentifierStr == "then")
71 if (IdentifierStr == "else")
73 if (IdentifierStr == "for")
75 if (IdentifierStr == "in")
77 return tok_identifier;
80 if (isdigit(LastChar) || LastChar == '.') { // Number: [0-9.]+
85 } while (isdigit(LastChar) || LastChar == '.');
87 NumVal = strtod(NumStr.c_str(), 0);
91 if (LastChar == '#') {
92 // Comment until end of line.
95 while (LastChar != EOF && LastChar != '\n' && LastChar != '\r');
101 // Check for end of file. Don't eat the EOF.
105 // Otherwise, just return the character as its ascii value.
106 int ThisChar = LastChar;
107 LastChar = getchar();
111 //===----------------------------------------------------------------------===//
112 // Abstract Syntax Tree (aka Parse Tree)
113 //===----------------------------------------------------------------------===//
115 /// ExprAST - Base class for all expression nodes.
118 virtual ~ExprAST() {}
119 virtual Value *Codegen() = 0;
122 /// NumberExprAST - Expression class for numeric literals like "1.0".
123 class NumberExprAST : public ExprAST {
127 NumberExprAST(double val) : Val(val) {}
128 virtual Value *Codegen();
131 /// VariableExprAST - Expression class for referencing a variable, like "a".
132 class VariableExprAST : public ExprAST {
136 VariableExprAST(const std::string &name) : Name(name) {}
137 virtual Value *Codegen();
140 /// BinaryExprAST - Expression class for a binary operator.
141 class BinaryExprAST : public ExprAST {
146 BinaryExprAST(char op, ExprAST *lhs, ExprAST *rhs)
147 : Op(op), LHS(lhs), RHS(rhs) {}
148 virtual Value *Codegen();
151 /// CallExprAST - Expression class for function calls.
152 class CallExprAST : public ExprAST {
154 std::vector<ExprAST *> Args;
157 CallExprAST(const std::string &callee, std::vector<ExprAST *> &args)
158 : Callee(callee), Args(args) {}
159 virtual Value *Codegen();
162 /// IfExprAST - Expression class for if/then/else.
163 class IfExprAST : public ExprAST {
164 ExprAST *Cond, *Then, *Else;
167 IfExprAST(ExprAST *cond, ExprAST *then, ExprAST *_else)
168 : Cond(cond), Then(then), Else(_else) {}
169 virtual Value *Codegen();
172 /// ForExprAST - Expression class for for/in.
173 class ForExprAST : public ExprAST {
175 ExprAST *Start, *End, *Step, *Body;
178 ForExprAST(const std::string &varname, ExprAST *start, ExprAST *end,
179 ExprAST *step, ExprAST *body)
180 : VarName(varname), Start(start), End(end), Step(step), Body(body) {}
181 virtual Value *Codegen();
184 /// PrototypeAST - This class represents the "prototype" for a function,
185 /// which captures its name, and its argument names (thus implicitly the number
186 /// of arguments the function takes).
189 std::vector<std::string> Args;
192 PrototypeAST(const std::string &name, const std::vector<std::string> &args)
193 : Name(name), Args(args) {}
198 /// FunctionAST - This class represents a function definition itself.
204 FunctionAST(PrototypeAST *proto, ExprAST *body) : Proto(proto), Body(body) {}
208 } // end anonymous namespace
210 //===----------------------------------------------------------------------===//
212 //===----------------------------------------------------------------------===//
214 /// CurTok/getNextToken - Provide a simple token buffer. CurTok is the current
215 /// token the parser is looking at. getNextToken reads another token from the
216 /// lexer and updates CurTok with its results.
218 static int getNextToken() { return CurTok = gettok(); }
220 /// BinopPrecedence - This holds the precedence for each binary operator that is
222 static std::map<char, int> BinopPrecedence;
224 /// GetTokPrecedence - Get the precedence of the pending binary operator token.
225 static int GetTokPrecedence() {
226 if (!isascii(CurTok))
229 // Make sure it's a declared binop.
230 int TokPrec = BinopPrecedence[CurTok];
236 /// Error* - These are little helper functions for error handling.
237 ExprAST *Error(const char *Str) {
238 fprintf(stderr, "Error: %s\n", Str);
241 PrototypeAST *ErrorP(const char *Str) {
245 FunctionAST *ErrorF(const char *Str) {
250 static ExprAST *ParseExpression();
254 /// ::= identifier '(' expression* ')'
255 static ExprAST *ParseIdentifierExpr() {
256 std::string IdName = IdentifierStr;
258 getNextToken(); // eat identifier.
260 if (CurTok != '(') // Simple variable ref.
261 return new VariableExprAST(IdName);
264 getNextToken(); // eat (
265 std::vector<ExprAST *> Args;
268 ExprAST *Arg = ParseExpression();
277 return Error("Expected ')' or ',' in argument list");
285 return new CallExprAST(IdName, Args);
288 /// numberexpr ::= number
289 static ExprAST *ParseNumberExpr() {
290 ExprAST *Result = new NumberExprAST(NumVal);
291 getNextToken(); // consume the number
295 /// parenexpr ::= '(' expression ')'
296 static ExprAST *ParseParenExpr() {
297 getNextToken(); // eat (.
298 ExprAST *V = ParseExpression();
303 return Error("expected ')'");
304 getNextToken(); // eat ).
308 /// ifexpr ::= 'if' expression 'then' expression 'else' expression
309 static ExprAST *ParseIfExpr() {
310 getNextToken(); // eat the if.
313 ExprAST *Cond = ParseExpression();
317 if (CurTok != tok_then)
318 return Error("expected then");
319 getNextToken(); // eat the then
321 ExprAST *Then = ParseExpression();
325 if (CurTok != tok_else)
326 return Error("expected else");
330 ExprAST *Else = ParseExpression();
334 return new IfExprAST(Cond, Then, Else);
337 /// forexpr ::= 'for' identifier '=' expr ',' expr (',' expr)? 'in' expression
338 static ExprAST *ParseForExpr() {
339 getNextToken(); // eat the for.
341 if (CurTok != tok_identifier)
342 return Error("expected identifier after for");
344 std::string IdName = IdentifierStr;
345 getNextToken(); // eat identifier.
348 return Error("expected '=' after for");
349 getNextToken(); // eat '='.
351 ExprAST *Start = ParseExpression();
355 return Error("expected ',' after for start value");
358 ExprAST *End = ParseExpression();
362 // The step value is optional.
366 Step = ParseExpression();
371 if (CurTok != tok_in)
372 return Error("expected 'in' after for");
373 getNextToken(); // eat 'in'.
375 ExprAST *Body = ParseExpression();
379 return new ForExprAST(IdName, Start, End, Step, Body);
383 /// ::= identifierexpr
388 static ExprAST *ParsePrimary() {
391 return Error("unknown token when expecting an expression");
393 return ParseIdentifierExpr();
395 return ParseNumberExpr();
397 return ParseParenExpr();
399 return ParseIfExpr();
401 return ParseForExpr();
406 /// ::= ('+' primary)*
407 static ExprAST *ParseBinOpRHS(int ExprPrec, ExprAST *LHS) {
408 // If this is a binop, find its precedence.
410 int TokPrec = GetTokPrecedence();
412 // If this is a binop that binds at least as tightly as the current binop,
413 // consume it, otherwise we are done.
414 if (TokPrec < ExprPrec)
417 // Okay, we know this is a binop.
419 getNextToken(); // eat binop
421 // Parse the primary expression after the binary operator.
422 ExprAST *RHS = ParsePrimary();
426 // If BinOp binds less tightly with RHS than the operator after RHS, let
427 // the pending operator take RHS as its LHS.
428 int NextPrec = GetTokPrecedence();
429 if (TokPrec < NextPrec) {
430 RHS = ParseBinOpRHS(TokPrec + 1, RHS);
436 LHS = new BinaryExprAST(BinOp, LHS, RHS);
441 /// ::= primary binoprhs
443 static ExprAST *ParseExpression() {
444 ExprAST *LHS = ParsePrimary();
448 return ParseBinOpRHS(0, LHS);
452 /// ::= id '(' id* ')'
453 static PrototypeAST *ParsePrototype() {
454 if (CurTok != tok_identifier)
455 return ErrorP("Expected function name in prototype");
457 std::string FnName = IdentifierStr;
461 return ErrorP("Expected '(' in prototype");
463 std::vector<std::string> ArgNames;
464 while (getNextToken() == tok_identifier)
465 ArgNames.push_back(IdentifierStr);
467 return ErrorP("Expected ')' in prototype");
470 getNextToken(); // eat ')'.
472 return new PrototypeAST(FnName, ArgNames);
475 /// definition ::= 'def' prototype expression
476 static FunctionAST *ParseDefinition() {
477 getNextToken(); // eat def.
478 PrototypeAST *Proto = ParsePrototype();
482 if (ExprAST *E = ParseExpression())
483 return new FunctionAST(Proto, E);
487 /// toplevelexpr ::= expression
488 static FunctionAST *ParseTopLevelExpr() {
489 if (ExprAST *E = ParseExpression()) {
490 // Make an anonymous proto.
491 PrototypeAST *Proto = new PrototypeAST("", std::vector<std::string>());
492 return new FunctionAST(Proto, E);
497 /// external ::= 'extern' prototype
498 static PrototypeAST *ParseExtern() {
499 getNextToken(); // eat extern.
500 return ParsePrototype();
503 //===----------------------------------------------------------------------===//
505 //===----------------------------------------------------------------------===//
507 static Module *TheModule;
508 static IRBuilder<> Builder(getGlobalContext());
509 static std::map<std::string, Value *> NamedValues;
510 static legacy::FunctionPassManager *TheFPM;
512 Value *ErrorV(const char *Str) {
517 Value *NumberExprAST::Codegen() {
518 return ConstantFP::get(getGlobalContext(), APFloat(Val));
521 Value *VariableExprAST::Codegen() {
522 // Look this variable up in the function.
523 Value *V = NamedValues[Name];
524 return V ? V : ErrorV("Unknown variable name");
527 Value *BinaryExprAST::Codegen() {
528 Value *L = LHS->Codegen();
529 Value *R = RHS->Codegen();
530 if (L == 0 || R == 0)
535 return Builder.CreateFAdd(L, R, "addtmp");
537 return Builder.CreateFSub(L, R, "subtmp");
539 return Builder.CreateFMul(L, R, "multmp");
541 L = Builder.CreateFCmpULT(L, R, "cmptmp");
542 // Convert bool 0/1 to double 0.0 or 1.0
543 return Builder.CreateUIToFP(L, Type::getDoubleTy(getGlobalContext()),
546 return ErrorV("invalid binary operator");
550 Value *CallExprAST::Codegen() {
551 // Look up the name in the global module table.
552 Function *CalleeF = TheModule->getFunction(Callee);
554 return ErrorV("Unknown function referenced");
556 // If argument mismatch error.
557 if (CalleeF->arg_size() != Args.size())
558 return ErrorV("Incorrect # arguments passed");
560 std::vector<Value *> ArgsV;
561 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
562 ArgsV.push_back(Args[i]->Codegen());
563 if (ArgsV.back() == 0)
567 return Builder.CreateCall(CalleeF, ArgsV, "calltmp");
570 Value *IfExprAST::Codegen() {
571 Value *CondV = Cond->Codegen();
575 // Convert condition to a bool by comparing equal to 0.0.
576 CondV = Builder.CreateFCmpONE(
577 CondV, ConstantFP::get(getGlobalContext(), APFloat(0.0)), "ifcond");
579 Function *TheFunction = Builder.GetInsertBlock()->getParent();
581 // Create blocks for the then and else cases. Insert the 'then' block at the
582 // end of the function.
584 BasicBlock::Create(getGlobalContext(), "then", TheFunction);
585 BasicBlock *ElseBB = BasicBlock::Create(getGlobalContext(), "else");
586 BasicBlock *MergeBB = BasicBlock::Create(getGlobalContext(), "ifcont");
588 Builder.CreateCondBr(CondV, ThenBB, ElseBB);
591 Builder.SetInsertPoint(ThenBB);
593 Value *ThenV = Then->Codegen();
597 Builder.CreateBr(MergeBB);
598 // Codegen of 'Then' can change the current block, update ThenBB for the PHI.
599 ThenBB = Builder.GetInsertBlock();
602 TheFunction->getBasicBlockList().push_back(ElseBB);
603 Builder.SetInsertPoint(ElseBB);
605 Value *ElseV = Else->Codegen();
609 Builder.CreateBr(MergeBB);
610 // Codegen of 'Else' can change the current block, update ElseBB for the PHI.
611 ElseBB = Builder.GetInsertBlock();
614 TheFunction->getBasicBlockList().push_back(MergeBB);
615 Builder.SetInsertPoint(MergeBB);
617 Builder.CreatePHI(Type::getDoubleTy(getGlobalContext()), 2, "iftmp");
619 PN->addIncoming(ThenV, ThenBB);
620 PN->addIncoming(ElseV, ElseBB);
624 Value *ForExprAST::Codegen() {
630 // variable = phi [start, loopheader], [nextvariable, loopend]
636 // nextvariable = variable + step
638 // br endcond, loop, endloop
641 // Emit the start code first, without 'variable' in scope.
642 Value *StartVal = Start->Codegen();
646 // Make the new basic block for the loop header, inserting after current
648 Function *TheFunction = Builder.GetInsertBlock()->getParent();
649 BasicBlock *PreheaderBB = Builder.GetInsertBlock();
651 BasicBlock::Create(getGlobalContext(), "loop", TheFunction);
653 // Insert an explicit fall through from the current block to the LoopBB.
654 Builder.CreateBr(LoopBB);
656 // Start insertion in LoopBB.
657 Builder.SetInsertPoint(LoopBB);
659 // Start the PHI node with an entry for Start.
660 PHINode *Variable = Builder.CreatePHI(Type::getDoubleTy(getGlobalContext()),
662 Variable->addIncoming(StartVal, PreheaderBB);
664 // Within the loop, the variable is defined equal to the PHI node. If it
665 // shadows an existing variable, we have to restore it, so save it now.
666 Value *OldVal = NamedValues[VarName];
667 NamedValues[VarName] = Variable;
669 // Emit the body of the loop. This, like any other expr, can change the
670 // current BB. Note that we ignore the value computed by the body, but don't
672 if (Body->Codegen() == 0)
675 // Emit the step value.
678 StepVal = Step->Codegen();
682 // If not specified, use 1.0.
683 StepVal = ConstantFP::get(getGlobalContext(), APFloat(1.0));
686 Value *NextVar = Builder.CreateFAdd(Variable, StepVal, "nextvar");
688 // Compute the end condition.
689 Value *EndCond = End->Codegen();
693 // Convert condition to a bool by comparing equal to 0.0.
694 EndCond = Builder.CreateFCmpONE(
695 EndCond, ConstantFP::get(getGlobalContext(), APFloat(0.0)), "loopcond");
697 // Create the "after loop" block and insert it.
698 BasicBlock *LoopEndBB = Builder.GetInsertBlock();
699 BasicBlock *AfterBB =
700 BasicBlock::Create(getGlobalContext(), "afterloop", TheFunction);
702 // Insert the conditional branch into the end of LoopEndBB.
703 Builder.CreateCondBr(EndCond, LoopBB, AfterBB);
705 // Any new code will be inserted in AfterBB.
706 Builder.SetInsertPoint(AfterBB);
708 // Add a new entry to the PHI node for the backedge.
709 Variable->addIncoming(NextVar, LoopEndBB);
711 // Restore the unshadowed variable.
713 NamedValues[VarName] = OldVal;
715 NamedValues.erase(VarName);
717 // for expr always returns 0.0.
718 return Constant::getNullValue(Type::getDoubleTy(getGlobalContext()));
721 Function *PrototypeAST::Codegen() {
722 // Make the function type: double(double,double) etc.
723 std::vector<Type *> Doubles(Args.size(),
724 Type::getDoubleTy(getGlobalContext()));
726 FunctionType::get(Type::getDoubleTy(getGlobalContext()), Doubles, false);
729 Function::Create(FT, Function::ExternalLinkage, Name, TheModule);
731 // If F conflicted, there was already something named 'Name'. If it has a
732 // body, don't allow redefinition or reextern.
733 if (F->getName() != Name) {
734 // Delete the one we just made and get the existing one.
735 F->eraseFromParent();
736 F = TheModule->getFunction(Name);
738 // If F already has a body, reject this.
740 ErrorF("redefinition of function");
744 // If F took a different number of args, reject.
745 if (F->arg_size() != Args.size()) {
746 ErrorF("redefinition of function with different # args");
751 // Set names for all arguments.
753 for (Function::arg_iterator AI = F->arg_begin(); Idx != Args.size();
755 AI->setName(Args[Idx]);
757 // Add arguments to variable symbol table.
758 NamedValues[Args[Idx]] = AI;
764 Function *FunctionAST::Codegen() {
767 Function *TheFunction = Proto->Codegen();
768 if (TheFunction == 0)
771 // Create a new basic block to start insertion into.
772 BasicBlock *BB = BasicBlock::Create(getGlobalContext(), "entry", TheFunction);
773 Builder.SetInsertPoint(BB);
775 if (Value *RetVal = Body->Codegen()) {
776 // Finish off the function.
777 Builder.CreateRet(RetVal);
779 // Validate the generated code, checking for consistency.
780 verifyFunction(*TheFunction);
782 // Optimize the function.
783 TheFPM->run(*TheFunction);
788 // Error reading body, remove function.
789 TheFunction->eraseFromParent();
793 //===----------------------------------------------------------------------===//
794 // Top-Level parsing and JIT Driver
795 //===----------------------------------------------------------------------===//
797 static ExecutionEngine *TheExecutionEngine;
799 static void HandleDefinition() {
800 if (FunctionAST *F = ParseDefinition()) {
801 if (Function *LF = F->Codegen()) {
802 fprintf(stderr, "Read function definition:");
806 // Skip token for error recovery.
811 static void HandleExtern() {
812 if (PrototypeAST *P = ParseExtern()) {
813 if (Function *F = P->Codegen()) {
814 fprintf(stderr, "Read extern: ");
818 // Skip token for error recovery.
823 static void HandleTopLevelExpression() {
824 // Evaluate a top-level expression into an anonymous function.
825 if (FunctionAST *F = ParseTopLevelExpr()) {
826 if (Function *LF = F->Codegen()) {
827 TheExecutionEngine->finalizeObject();
828 // JIT the function, returning a function pointer.
829 void *FPtr = TheExecutionEngine->getPointerToFunction(LF);
831 // Cast it to the right type (takes no arguments, returns a double) so we
832 // can call it as a native function.
833 double (*FP)() = (double (*)())(intptr_t)FPtr;
834 fprintf(stderr, "Evaluated to %f\n", FP());
837 // Skip token for error recovery.
842 /// top ::= definition | external | expression | ';'
843 static void MainLoop() {
845 fprintf(stderr, "ready> ");
851 break; // ignore top-level semicolons.
859 HandleTopLevelExpression();
865 //===----------------------------------------------------------------------===//
866 // "Library" functions that can be "extern'd" from user code.
867 //===----------------------------------------------------------------------===//
869 /// putchard - putchar that takes a double and returns 0.
870 extern "C" double putchard(double X) {
875 //===----------------------------------------------------------------------===//
877 //===----------------------------------------------------------------------===//
880 InitializeNativeTarget();
881 InitializeNativeTargetAsmPrinter();
882 InitializeNativeTargetAsmParser();
883 LLVMContext &Context = getGlobalContext();
885 // Install standard binary operators.
886 // 1 is lowest precedence.
887 BinopPrecedence['<'] = 10;
888 BinopPrecedence['+'] = 20;
889 BinopPrecedence['-'] = 20;
890 BinopPrecedence['*'] = 40; // highest.
892 // Prime the first token.
893 fprintf(stderr, "ready> ");
896 // Make the module, which holds all the code.
897 std::unique_ptr<Module> Owner = make_unique<Module>("my cool jit", Context);
898 TheModule = Owner.get();
900 // Create the JIT. This takes ownership of the module.
903 EngineBuilder(std::move(Owner))
904 .setErrorStr(&ErrStr)
905 .setMCJITMemoryManager(llvm::make_unique<SectionMemoryManager>())
907 if (!TheExecutionEngine) {
908 fprintf(stderr, "Could not create ExecutionEngine: %s\n", ErrStr.c_str());
912 legacy::FunctionPassManager OurFPM(TheModule);
914 // Set up the optimizer pipeline. Start with registering info about how the
915 // target lays out data structures.
916 TheModule->setDataLayout(*TheExecutionEngine->getDataLayout());
917 // Provide basic AliasAnalysis support for GVN.
918 OurFPM.add(createBasicAliasAnalysisPass());
919 // Do simple "peephole" optimizations and bit-twiddling optzns.
920 OurFPM.add(createInstructionCombiningPass());
921 // Reassociate expressions.
922 OurFPM.add(createReassociatePass());
923 // Eliminate Common SubExpressions.
924 OurFPM.add(createGVNPass());
925 // Simplify the control flow graph (deleting unreachable blocks, etc).
926 OurFPM.add(createCFGSimplificationPass());
928 OurFPM.doInitialization();
930 // Set the global so the code gen can use this.
933 // Run the main "interpreter loop" now.
938 // Print out all of the generated code.