1 #include "../include/KaleidoscopeJIT.h"
2 #include "llvm/ADT/APFloat.h"
3 #include "llvm/ADT/STLExtras.h"
4 #include "llvm/Analysis/AssumptionCache.h"
5 #include "llvm/Analysis/BasicAliasAnalysis.h"
6 #include "llvm/Analysis/MemoryDependenceAnalysis.h"
7 #include "llvm/Analysis/MemorySSA.h"
8 #include "llvm/Analysis/OptimizationRemarkEmitter.h"
9 #include "llvm/Analysis/ProfileSummaryInfo.h"
10 #include "llvm/Analysis/TargetTransformInfo.h"
11 #include "llvm/IR/BasicBlock.h"
12 #include "llvm/IR/Constants.h"
13 #include "llvm/IR/DerivedTypes.h"
14 #include "llvm/IR/Function.h"
15 #include "llvm/IR/IRBuilder.h"
16 #include "llvm/IR/LLVMContext.h"
17 #include "llvm/IR/Module.h"
18 #include "llvm/IR/PassManager.h"
19 #include "llvm/IR/Type.h"
20 #include "llvm/IR/Verifier.h"
21 #include "llvm/Passes/PassBuilder.h"
22 #include "llvm/Passes/StandardInstrumentations.h"
23 #include "llvm/Support/TargetSelect.h"
24 #include "llvm/Target/TargetMachine.h"
25 #include "llvm/Transforms/InstCombine/InstCombine.h"
26 #include "llvm/Transforms/Scalar.h"
27 #include "llvm/Transforms/Scalar/GVN.h"
28 #include "llvm/Transforms/Scalar/Reassociate.h"
29 #include "llvm/Transforms/Scalar/SimplifyCFG.h"
42 using namespace llvm::orc
;
44 //===----------------------------------------------------------------------===//
46 //===----------------------------------------------------------------------===//
48 // The lexer returns tokens [0-255] if it is an unknown character, otherwise one
49 // of these for known things.
62 static std::string IdentifierStr
; // Filled in if tok_identifier
63 static double NumVal
; // Filled in if tok_number
65 /// gettok - Return the next token from standard input.
67 static int LastChar
= ' ';
69 // Skip any whitespace.
70 while (isspace(LastChar
))
73 if (isalpha(LastChar
)) { // identifier: [a-zA-Z][a-zA-Z0-9]*
74 IdentifierStr
= LastChar
;
75 while (isalnum((LastChar
= getchar())))
76 IdentifierStr
+= LastChar
;
78 if (IdentifierStr
== "def")
80 if (IdentifierStr
== "extern")
82 return tok_identifier
;
85 if (isdigit(LastChar
) || LastChar
== '.') { // Number: [0-9.]+
90 } while (isdigit(LastChar
) || LastChar
== '.');
92 NumVal
= strtod(NumStr
.c_str(), nullptr);
96 if (LastChar
== '#') {
97 // Comment until end of line.
100 while (LastChar
!= EOF
&& LastChar
!= '\n' && LastChar
!= '\r');
106 // Check for end of file. Don't eat the EOF.
110 // Otherwise, just return the character as its ascii value.
111 int ThisChar
= LastChar
;
112 LastChar
= getchar();
116 //===----------------------------------------------------------------------===//
117 // Abstract Syntax Tree (aka Parse Tree)
118 //===----------------------------------------------------------------------===//
122 /// ExprAST - Base class for all expression nodes.
125 virtual ~ExprAST() = default;
127 virtual Value
*codegen() = 0;
130 /// NumberExprAST - Expression class for numeric literals like "1.0".
131 class NumberExprAST
: public ExprAST
{
135 NumberExprAST(double Val
) : Val(Val
) {}
137 Value
*codegen() override
;
140 /// VariableExprAST - Expression class for referencing a variable, like "a".
141 class VariableExprAST
: public ExprAST
{
145 VariableExprAST(const std::string
&Name
) : Name(Name
) {}
147 Value
*codegen() override
;
150 /// BinaryExprAST - Expression class for a binary operator.
151 class BinaryExprAST
: public ExprAST
{
153 std::unique_ptr
<ExprAST
> LHS
, RHS
;
156 BinaryExprAST(char Op
, std::unique_ptr
<ExprAST
> LHS
,
157 std::unique_ptr
<ExprAST
> RHS
)
158 : Op(Op
), LHS(std::move(LHS
)), RHS(std::move(RHS
)) {}
160 Value
*codegen() override
;
163 /// CallExprAST - Expression class for function calls.
164 class CallExprAST
: public ExprAST
{
166 std::vector
<std::unique_ptr
<ExprAST
>> Args
;
169 CallExprAST(const std::string
&Callee
,
170 std::vector
<std::unique_ptr
<ExprAST
>> Args
)
171 : Callee(Callee
), Args(std::move(Args
)) {}
173 Value
*codegen() override
;
176 /// PrototypeAST - This class represents the "prototype" for a function,
177 /// which captures its name, and its argument names (thus implicitly the number
178 /// of arguments the function takes).
181 std::vector
<std::string
> Args
;
184 PrototypeAST(const std::string
&Name
, std::vector
<std::string
> Args
)
185 : Name(Name
), Args(std::move(Args
)) {}
188 const std::string
&getName() const { return Name
; }
191 /// FunctionAST - This class represents a function definition itself.
193 std::unique_ptr
<PrototypeAST
> Proto
;
194 std::unique_ptr
<ExprAST
> Body
;
197 FunctionAST(std::unique_ptr
<PrototypeAST
> Proto
,
198 std::unique_ptr
<ExprAST
> Body
)
199 : Proto(std::move(Proto
)), Body(std::move(Body
)) {}
204 } // end anonymous namespace
206 //===----------------------------------------------------------------------===//
208 //===----------------------------------------------------------------------===//
210 /// CurTok/getNextToken - Provide a simple token buffer. CurTok is the current
211 /// token the parser is looking at. getNextToken reads another token from the
212 /// lexer and updates CurTok with its results.
214 static int getNextToken() { return CurTok
= gettok(); }
216 /// BinopPrecedence - This holds the precedence for each binary operator that is
218 static std::map
<char, int> BinopPrecedence
;
220 /// GetTokPrecedence - Get the precedence of the pending binary operator token.
221 static int GetTokPrecedence() {
222 if (!isascii(CurTok
))
225 // Make sure it's a declared binop.
226 int TokPrec
= BinopPrecedence
[CurTok
];
232 /// LogError* - These are little helper functions for error handling.
233 std::unique_ptr
<ExprAST
> LogError(const char *Str
) {
234 fprintf(stderr
, "Error: %s\n", Str
);
238 std::unique_ptr
<PrototypeAST
> LogErrorP(const char *Str
) {
243 static std::unique_ptr
<ExprAST
> ParseExpression();
245 /// numberexpr ::= number
246 static std::unique_ptr
<ExprAST
> ParseNumberExpr() {
247 auto Result
= std::make_unique
<NumberExprAST
>(NumVal
);
248 getNextToken(); // consume the number
249 return std::move(Result
);
252 /// parenexpr ::= '(' expression ')'
253 static std::unique_ptr
<ExprAST
> ParseParenExpr() {
254 getNextToken(); // eat (.
255 auto V
= ParseExpression();
260 return LogError("expected ')'");
261 getNextToken(); // eat ).
267 /// ::= identifier '(' expression* ')'
268 static std::unique_ptr
<ExprAST
> ParseIdentifierExpr() {
269 std::string IdName
= IdentifierStr
;
271 getNextToken(); // eat identifier.
273 if (CurTok
!= '(') // Simple variable ref.
274 return std::make_unique
<VariableExprAST
>(IdName
);
277 getNextToken(); // eat (
278 std::vector
<std::unique_ptr
<ExprAST
>> Args
;
281 if (auto Arg
= ParseExpression())
282 Args
.push_back(std::move(Arg
));
290 return LogError("Expected ')' or ',' in argument list");
298 return std::make_unique
<CallExprAST
>(IdName
, std::move(Args
));
302 /// ::= identifierexpr
305 static std::unique_ptr
<ExprAST
> ParsePrimary() {
308 return LogError("unknown token when expecting an expression");
310 return ParseIdentifierExpr();
312 return ParseNumberExpr();
314 return ParseParenExpr();
319 /// ::= ('+' primary)*
320 static std::unique_ptr
<ExprAST
> ParseBinOpRHS(int ExprPrec
,
321 std::unique_ptr
<ExprAST
> LHS
) {
322 // If this is a binop, find its precedence.
324 int TokPrec
= GetTokPrecedence();
326 // If this is a binop that binds at least as tightly as the current binop,
327 // consume it, otherwise we are done.
328 if (TokPrec
< ExprPrec
)
331 // Okay, we know this is a binop.
333 getNextToken(); // eat binop
335 // Parse the primary expression after the binary operator.
336 auto RHS
= ParsePrimary();
340 // If BinOp binds less tightly with RHS than the operator after RHS, let
341 // the pending operator take RHS as its LHS.
342 int NextPrec
= GetTokPrecedence();
343 if (TokPrec
< NextPrec
) {
344 RHS
= ParseBinOpRHS(TokPrec
+ 1, std::move(RHS
));
351 std::make_unique
<BinaryExprAST
>(BinOp
, std::move(LHS
), std::move(RHS
));
356 /// ::= primary binoprhs
358 static std::unique_ptr
<ExprAST
> ParseExpression() {
359 auto LHS
= ParsePrimary();
363 return ParseBinOpRHS(0, std::move(LHS
));
367 /// ::= id '(' id* ')'
368 static std::unique_ptr
<PrototypeAST
> ParsePrototype() {
369 if (CurTok
!= tok_identifier
)
370 return LogErrorP("Expected function name in prototype");
372 std::string FnName
= IdentifierStr
;
376 return LogErrorP("Expected '(' in prototype");
378 std::vector
<std::string
> ArgNames
;
379 while (getNextToken() == tok_identifier
)
380 ArgNames
.push_back(IdentifierStr
);
382 return LogErrorP("Expected ')' in prototype");
385 getNextToken(); // eat ')'.
387 return std::make_unique
<PrototypeAST
>(FnName
, std::move(ArgNames
));
390 /// definition ::= 'def' prototype expression
391 static std::unique_ptr
<FunctionAST
> ParseDefinition() {
392 getNextToken(); // eat def.
393 auto Proto
= ParsePrototype();
397 if (auto E
= ParseExpression())
398 return std::make_unique
<FunctionAST
>(std::move(Proto
), std::move(E
));
402 /// toplevelexpr ::= expression
403 static std::unique_ptr
<FunctionAST
> ParseTopLevelExpr() {
404 if (auto E
= ParseExpression()) {
405 // Make an anonymous proto.
406 auto Proto
= std::make_unique
<PrototypeAST
>("__anon_expr",
407 std::vector
<std::string
>());
408 return std::make_unique
<FunctionAST
>(std::move(Proto
), std::move(E
));
413 /// external ::= 'extern' prototype
414 static std::unique_ptr
<PrototypeAST
> ParseExtern() {
415 getNextToken(); // eat extern.
416 return ParsePrototype();
419 //===----------------------------------------------------------------------===//
421 //===----------------------------------------------------------------------===//
423 static std::unique_ptr
<LLVMContext
> TheContext
;
424 static std::unique_ptr
<Module
> TheModule
;
425 static std::unique_ptr
<IRBuilder
<>> Builder
;
426 static std::map
<std::string
, Value
*> NamedValues
;
427 static std::unique_ptr
<KaleidoscopeJIT
> TheJIT
;
428 static std::unique_ptr
<FunctionPassManager
> TheFPM
;
429 static std::unique_ptr
<FunctionAnalysisManager
> TheFAM
;
430 static std::unique_ptr
<ModuleAnalysisManager
> TheMAM
;
431 static std::unique_ptr
<PassInstrumentationCallbacks
> ThePIC
;
432 static std::unique_ptr
<StandardInstrumentations
> TheSI
;
433 static std::map
<std::string
, std::unique_ptr
<PrototypeAST
>> FunctionProtos
;
434 static ExitOnError ExitOnErr
;
436 Value
*LogErrorV(const char *Str
) {
441 Function
*getFunction(std::string Name
) {
442 // First, see if the function has already been added to the current module.
443 if (auto *F
= TheModule
->getFunction(Name
))
446 // If not, check whether we can codegen the declaration from some existing
448 auto FI
= FunctionProtos
.find(Name
);
449 if (FI
!= FunctionProtos
.end())
450 return FI
->second
->codegen();
452 // If no existing prototype exists, return null.
456 Value
*NumberExprAST::codegen() {
457 return ConstantFP::get(*TheContext
, APFloat(Val
));
460 Value
*VariableExprAST::codegen() {
461 // Look this variable up in the function.
462 Value
*V
= NamedValues
[Name
];
464 return LogErrorV("Unknown variable name");
468 Value
*BinaryExprAST::codegen() {
469 Value
*L
= LHS
->codegen();
470 Value
*R
= RHS
->codegen();
476 return Builder
->CreateFAdd(L
, R
, "addtmp");
478 return Builder
->CreateFSub(L
, R
, "subtmp");
480 return Builder
->CreateFMul(L
, R
, "multmp");
482 L
= Builder
->CreateFCmpULT(L
, R
, "cmptmp");
483 // Convert bool 0/1 to double 0.0 or 1.0
484 return Builder
->CreateUIToFP(L
, Type::getDoubleTy(*TheContext
), "booltmp");
486 return LogErrorV("invalid binary operator");
490 Value
*CallExprAST::codegen() {
491 // Look up the name in the global module table.
492 Function
*CalleeF
= getFunction(Callee
);
494 return LogErrorV("Unknown function referenced");
496 // If argument mismatch error.
497 if (CalleeF
->arg_size() != Args
.size())
498 return LogErrorV("Incorrect # arguments passed");
500 std::vector
<Value
*> ArgsV
;
501 for (unsigned i
= 0, e
= Args
.size(); i
!= e
; ++i
) {
502 ArgsV
.push_back(Args
[i
]->codegen());
507 return Builder
->CreateCall(CalleeF
, ArgsV
, "calltmp");
510 Function
*PrototypeAST::codegen() {
511 // Make the function type: double(double,double) etc.
512 std::vector
<Type
*> Doubles(Args
.size(), Type::getDoubleTy(*TheContext
));
514 FunctionType::get(Type::getDoubleTy(*TheContext
), Doubles
, false);
517 Function::Create(FT
, Function::ExternalLinkage
, Name
, TheModule
.get());
519 // Set names for all arguments.
521 for (auto &Arg
: F
->args())
522 Arg
.setName(Args
[Idx
++]);
527 Function
*FunctionAST::codegen() {
528 // Transfer ownership of the prototype to the FunctionProtos map, but keep a
529 // reference to it for use below.
531 FunctionProtos
[Proto
->getName()] = std::move(Proto
);
532 Function
*TheFunction
= getFunction(P
.getName());
536 // Create a new basic block to start insertion into.
537 BasicBlock
*BB
= BasicBlock::Create(*TheContext
, "entry", TheFunction
);
538 Builder
->SetInsertPoint(BB
);
540 // Record the function arguments in the NamedValues map.
542 for (auto &Arg
: TheFunction
->args())
543 NamedValues
[std::string(Arg
.getName())] = &Arg
;
545 if (Value
*RetVal
= Body
->codegen()) {
546 // Finish off the function.
547 Builder
->CreateRet(RetVal
);
549 // Validate the generated code, checking for consistency.
550 verifyFunction(*TheFunction
);
552 // Run the optimizer on the function.
553 TheFPM
->run(*TheFunction
, *TheFAM
);
558 // Error reading body, remove function.
559 TheFunction
->eraseFromParent();
563 //===----------------------------------------------------------------------===//
564 // Top-Level parsing and JIT Driver
565 //===----------------------------------------------------------------------===//
567 static void InitializeModuleAndManagers() {
568 // Open a new context and module.
569 TheContext
= std::make_unique
<LLVMContext
>();
570 TheModule
= std::make_unique
<Module
>("KaleidoscopeJIT", *TheContext
);
571 TheModule
->setDataLayout(TheJIT
->getDataLayout());
573 // Create a new builder for the module.
574 Builder
= std::make_unique
<IRBuilder
<>>(*TheContext
);
576 // Create new pass and analysis managers.
577 TheFPM
= std::make_unique
<FunctionPassManager
>();
578 TheFAM
= std::make_unique
<FunctionAnalysisManager
>();
579 TheMAM
= std::make_unique
<ModuleAnalysisManager
>();
580 ThePIC
= std::make_unique
<PassInstrumentationCallbacks
>();
581 TheSI
= std::make_unique
<StandardInstrumentations
>(*TheContext
,
582 /*DebugLogging*/ true);
583 TheSI
->registerCallbacks(*ThePIC
, TheMAM
.get());
585 // Add transform passes.
586 // Do simple "peephole" optimizations and bit-twiddling optzns.
587 TheFPM
->addPass(InstCombinePass());
588 // Reassociate expressions.
589 TheFPM
->addPass(ReassociatePass());
590 // Eliminate Common SubExpressions.
591 TheFPM
->addPass(GVNPass());
592 // Simplify the control flow graph (deleting unreachable blocks, etc).
593 TheFPM
->addPass(SimplifyCFGPass());
595 // Register analysis passes used in these transform passes.
596 TheFAM
->registerPass([&] { return AAManager(); });
597 TheFAM
->registerPass([&] { return AssumptionAnalysis(); });
598 TheFAM
->registerPass([&] { return DominatorTreeAnalysis(); });
599 TheFAM
->registerPass([&] { return LoopAnalysis(); });
600 TheFAM
->registerPass([&] { return MemoryDependenceAnalysis(); });
601 TheFAM
->registerPass([&] { return MemorySSAAnalysis(); });
602 TheFAM
->registerPass([&] { return OptimizationRemarkEmitterAnalysis(); });
603 TheFAM
->registerPass([&] {
604 return OuterAnalysisManagerProxy
<ModuleAnalysisManager
, Function
>(*TheMAM
);
606 TheFAM
->registerPass(
607 [&] { return PassInstrumentationAnalysis(ThePIC
.get()); });
608 TheFAM
->registerPass([&] { return TargetIRAnalysis(); });
609 TheFAM
->registerPass([&] { return TargetLibraryAnalysis(); });
611 TheMAM
->registerPass([&] { return ProfileSummaryAnalysis(); });
614 static void HandleDefinition() {
615 if (auto FnAST
= ParseDefinition()) {
616 if (auto *FnIR
= FnAST
->codegen()) {
617 fprintf(stderr
, "Read function definition:");
619 fprintf(stderr
, "\n");
620 ExitOnErr(TheJIT
->addModule(
621 ThreadSafeModule(std::move(TheModule
), std::move(TheContext
))));
622 InitializeModuleAndManagers();
625 // Skip token for error recovery.
630 static void HandleExtern() {
631 if (auto ProtoAST
= ParseExtern()) {
632 if (auto *FnIR
= ProtoAST
->codegen()) {
633 fprintf(stderr
, "Read extern: ");
635 fprintf(stderr
, "\n");
636 FunctionProtos
[ProtoAST
->getName()] = std::move(ProtoAST
);
639 // Skip token for error recovery.
644 static void HandleTopLevelExpression() {
645 // Evaluate a top-level expression into an anonymous function.
646 if (auto FnAST
= ParseTopLevelExpr()) {
647 if (FnAST
->codegen()) {
648 // Create a ResourceTracker to track JIT'd memory allocated to our
649 // anonymous expression -- that way we can free it after executing.
650 auto RT
= TheJIT
->getMainJITDylib().createResourceTracker();
652 auto TSM
= ThreadSafeModule(std::move(TheModule
), std::move(TheContext
));
653 ExitOnErr(TheJIT
->addModule(std::move(TSM
), RT
));
654 InitializeModuleAndManagers();
656 // Search the JIT for the __anon_expr symbol.
657 auto ExprSymbol
= ExitOnErr(TheJIT
->lookup("__anon_expr"));
659 // Get the symbol's address and cast it to the right type (takes no
660 // arguments, returns a double) so we can call it as a native function.
661 double (*FP
)() = ExprSymbol
.getAddress().toPtr
<double (*)()>();
662 fprintf(stderr
, "Evaluated to %f\n", FP());
664 // Delete the anonymous expression module from the JIT.
665 ExitOnErr(RT
->remove());
668 // Skip token for error recovery.
673 /// top ::= definition | external | expression | ';'
674 static void MainLoop() {
676 fprintf(stderr
, "ready> ");
680 case ';': // ignore top-level semicolons.
690 HandleTopLevelExpression();
696 //===----------------------------------------------------------------------===//
697 // "Library" functions that can be "extern'd" from user code.
698 //===----------------------------------------------------------------------===//
701 #define DLLEXPORT __declspec(dllexport)
706 /// putchard - putchar that takes a double and returns 0.
707 extern "C" DLLEXPORT
double putchard(double X
) {
708 fputc((char)X
, stderr
);
712 /// printd - printf that takes a double prints it as "%f\n", returning 0.
713 extern "C" DLLEXPORT
double printd(double X
) {
714 fprintf(stderr
, "%f\n", X
);
718 //===----------------------------------------------------------------------===//
720 //===----------------------------------------------------------------------===//
723 InitializeNativeTarget();
724 InitializeNativeTargetAsmPrinter();
725 InitializeNativeTargetAsmParser();
727 // Install standard binary operators.
728 // 1 is lowest precedence.
729 BinopPrecedence
['<'] = 10;
730 BinopPrecedence
['+'] = 20;
731 BinopPrecedence
['-'] = 20;
732 BinopPrecedence
['*'] = 40; // highest.
734 // Prime the first token.
735 fprintf(stderr
, "ready> ");
738 TheJIT
= ExitOnErr(KaleidoscopeJIT::Create());
740 InitializeModuleAndManagers();
742 // Run the main "interpreter loop" now.