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/Instructions.h"
17 #include "llvm/IR/LLVMContext.h"
18 #include "llvm/IR/Module.h"
19 #include "llvm/IR/PassManager.h"
20 #include "llvm/IR/Type.h"
21 #include "llvm/IR/Verifier.h"
22 #include "llvm/Passes/PassBuilder.h"
23 #include "llvm/Passes/StandardInstrumentations.h"
24 #include "llvm/Support/TargetSelect.h"
25 #include "llvm/Target/TargetMachine.h"
26 #include "llvm/Transforms/InstCombine/InstCombine.h"
27 #include "llvm/Transforms/Scalar.h"
28 #include "llvm/Transforms/Scalar/GVN.h"
29 #include "llvm/Transforms/Scalar/Reassociate.h"
30 #include "llvm/Transforms/Scalar/SimplifyCFG.h"
31 #include "llvm/Transforms/Utils.h"
45 using namespace llvm::orc
;
47 //===----------------------------------------------------------------------===//
49 //===----------------------------------------------------------------------===//
51 // The lexer returns tokens [0-255] if it is an unknown character, otherwise one
52 // of these for known things.
79 static std::string IdentifierStr
; // Filled in if tok_identifier
80 static double NumVal
; // Filled in if tok_number
82 /// gettok - Return the next token from standard input.
84 static int LastChar
= ' ';
86 // Skip any whitespace.
87 while (isspace(LastChar
))
90 if (isalpha(LastChar
)) { // identifier: [a-zA-Z][a-zA-Z0-9]*
91 IdentifierStr
= LastChar
;
92 while (isalnum((LastChar
= getchar())))
93 IdentifierStr
+= LastChar
;
95 if (IdentifierStr
== "def")
97 if (IdentifierStr
== "extern")
99 if (IdentifierStr
== "if")
101 if (IdentifierStr
== "then")
103 if (IdentifierStr
== "else")
105 if (IdentifierStr
== "for")
107 if (IdentifierStr
== "in")
109 if (IdentifierStr
== "binary")
111 if (IdentifierStr
== "unary")
113 if (IdentifierStr
== "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(), nullptr);
129 if (LastChar
== '#') {
130 // Comment until end of line.
132 LastChar
= getchar();
133 while (LastChar
!= EOF
&& LastChar
!= '\n' && LastChar
!= '\r');
139 // Check for end of file. Don't eat the EOF.
143 // Otherwise, just return the character as its ascii value.
144 int ThisChar
= LastChar
;
145 LastChar
= getchar();
149 //===----------------------------------------------------------------------===//
150 // Abstract Syntax Tree (aka Parse Tree)
151 //===----------------------------------------------------------------------===//
155 /// ExprAST - Base class for all expression nodes.
158 virtual ~ExprAST() = default;
160 virtual Value
*codegen() = 0;
163 /// NumberExprAST - Expression class for numeric literals like "1.0".
164 class NumberExprAST
: public ExprAST
{
168 NumberExprAST(double Val
) : Val(Val
) {}
170 Value
*codegen() override
;
173 /// VariableExprAST - Expression class for referencing a variable, like "a".
174 class VariableExprAST
: public ExprAST
{
178 VariableExprAST(const std::string
&Name
) : Name(Name
) {}
180 Value
*codegen() override
;
181 const std::string
&getName() const { return Name
; }
184 /// UnaryExprAST - Expression class for a unary operator.
185 class UnaryExprAST
: public ExprAST
{
187 std::unique_ptr
<ExprAST
> Operand
;
190 UnaryExprAST(char Opcode
, std::unique_ptr
<ExprAST
> Operand
)
191 : Opcode(Opcode
), Operand(std::move(Operand
)) {}
193 Value
*codegen() override
;
196 /// BinaryExprAST - Expression class for a binary operator.
197 class BinaryExprAST
: public ExprAST
{
199 std::unique_ptr
<ExprAST
> LHS
, RHS
;
202 BinaryExprAST(char Op
, std::unique_ptr
<ExprAST
> LHS
,
203 std::unique_ptr
<ExprAST
> RHS
)
204 : Op(Op
), LHS(std::move(LHS
)), RHS(std::move(RHS
)) {}
206 Value
*codegen() override
;
209 /// CallExprAST - Expression class for function calls.
210 class CallExprAST
: public ExprAST
{
212 std::vector
<std::unique_ptr
<ExprAST
>> Args
;
215 CallExprAST(const std::string
&Callee
,
216 std::vector
<std::unique_ptr
<ExprAST
>> Args
)
217 : Callee(Callee
), Args(std::move(Args
)) {}
219 Value
*codegen() override
;
222 /// IfExprAST - Expression class for if/then/else.
223 class IfExprAST
: public ExprAST
{
224 std::unique_ptr
<ExprAST
> Cond
, Then
, Else
;
227 IfExprAST(std::unique_ptr
<ExprAST
> Cond
, std::unique_ptr
<ExprAST
> Then
,
228 std::unique_ptr
<ExprAST
> Else
)
229 : Cond(std::move(Cond
)), Then(std::move(Then
)), Else(std::move(Else
)) {}
231 Value
*codegen() override
;
234 /// ForExprAST - Expression class for for/in.
235 class ForExprAST
: public ExprAST
{
237 std::unique_ptr
<ExprAST
> Start
, End
, Step
, Body
;
240 ForExprAST(const std::string
&VarName
, std::unique_ptr
<ExprAST
> Start
,
241 std::unique_ptr
<ExprAST
> End
, std::unique_ptr
<ExprAST
> Step
,
242 std::unique_ptr
<ExprAST
> Body
)
243 : VarName(VarName
), Start(std::move(Start
)), End(std::move(End
)),
244 Step(std::move(Step
)), Body(std::move(Body
)) {}
246 Value
*codegen() override
;
249 /// VarExprAST - Expression class for var/in
250 class VarExprAST
: public ExprAST
{
251 std::vector
<std::pair
<std::string
, std::unique_ptr
<ExprAST
>>> VarNames
;
252 std::unique_ptr
<ExprAST
> Body
;
256 std::vector
<std::pair
<std::string
, std::unique_ptr
<ExprAST
>>> VarNames
,
257 std::unique_ptr
<ExprAST
> Body
)
258 : VarNames(std::move(VarNames
)), Body(std::move(Body
)) {}
260 Value
*codegen() override
;
263 /// PrototypeAST - This class represents the "prototype" for a function,
264 /// which captures its name, and its argument names (thus implicitly the number
265 /// of arguments the function takes), as well as if it is an operator.
268 std::vector
<std::string
> Args
;
270 unsigned Precedence
; // Precedence if a binary op.
273 PrototypeAST(const std::string
&Name
, std::vector
<std::string
> Args
,
274 bool IsOperator
= false, unsigned Prec
= 0)
275 : Name(Name
), Args(std::move(Args
)), IsOperator(IsOperator
),
279 const std::string
&getName() const { return Name
; }
281 bool isUnaryOp() const { return IsOperator
&& Args
.size() == 1; }
282 bool isBinaryOp() const { return IsOperator
&& Args
.size() == 2; }
284 char getOperatorName() const {
285 assert(isUnaryOp() || isBinaryOp());
286 return Name
[Name
.size() - 1];
289 unsigned getBinaryPrecedence() const { return Precedence
; }
292 /// FunctionAST - This class represents a function definition itself.
294 std::unique_ptr
<PrototypeAST
> Proto
;
295 std::unique_ptr
<ExprAST
> Body
;
298 FunctionAST(std::unique_ptr
<PrototypeAST
> Proto
,
299 std::unique_ptr
<ExprAST
> Body
)
300 : Proto(std::move(Proto
)), Body(std::move(Body
)) {}
305 } // end anonymous namespace
307 //===----------------------------------------------------------------------===//
309 //===----------------------------------------------------------------------===//
311 /// CurTok/getNextToken - Provide a simple token buffer. CurTok is the current
312 /// token the parser is looking at. getNextToken reads another token from the
313 /// lexer and updates CurTok with its results.
315 static int getNextToken() { return CurTok
= gettok(); }
317 /// BinopPrecedence - This holds the precedence for each binary operator that is
319 static std::map
<char, int> BinopPrecedence
;
321 /// GetTokPrecedence - Get the precedence of the pending binary operator token.
322 static int GetTokPrecedence() {
323 if (!isascii(CurTok
))
326 // Make sure it's a declared binop.
327 int TokPrec
= BinopPrecedence
[CurTok
];
333 /// LogError* - These are little helper functions for error handling.
334 std::unique_ptr
<ExprAST
> LogError(const char *Str
) {
335 fprintf(stderr
, "Error: %s\n", Str
);
339 std::unique_ptr
<PrototypeAST
> LogErrorP(const char *Str
) {
344 static std::unique_ptr
<ExprAST
> ParseExpression();
346 /// numberexpr ::= number
347 static std::unique_ptr
<ExprAST
> ParseNumberExpr() {
348 auto Result
= std::make_unique
<NumberExprAST
>(NumVal
);
349 getNextToken(); // consume the number
350 return std::move(Result
);
353 /// parenexpr ::= '(' expression ')'
354 static std::unique_ptr
<ExprAST
> ParseParenExpr() {
355 getNextToken(); // eat (.
356 auto V
= ParseExpression();
361 return LogError("expected ')'");
362 getNextToken(); // eat ).
368 /// ::= identifier '(' expression* ')'
369 static std::unique_ptr
<ExprAST
> ParseIdentifierExpr() {
370 std::string IdName
= IdentifierStr
;
372 getNextToken(); // eat identifier.
374 if (CurTok
!= '(') // Simple variable ref.
375 return std::make_unique
<VariableExprAST
>(IdName
);
378 getNextToken(); // eat (
379 std::vector
<std::unique_ptr
<ExprAST
>> Args
;
382 if (auto Arg
= ParseExpression())
383 Args
.push_back(std::move(Arg
));
391 return LogError("Expected ')' or ',' in argument list");
399 return std::make_unique
<CallExprAST
>(IdName
, std::move(Args
));
402 /// ifexpr ::= 'if' expression 'then' expression 'else' expression
403 static std::unique_ptr
<ExprAST
> ParseIfExpr() {
404 getNextToken(); // eat the if.
407 auto Cond
= ParseExpression();
411 if (CurTok
!= tok_then
)
412 return LogError("expected then");
413 getNextToken(); // eat the then
415 auto Then
= ParseExpression();
419 if (CurTok
!= tok_else
)
420 return LogError("expected else");
424 auto Else
= ParseExpression();
428 return std::make_unique
<IfExprAST
>(std::move(Cond
), std::move(Then
),
432 /// forexpr ::= 'for' identifier '=' expr ',' expr (',' expr)? 'in' expression
433 static std::unique_ptr
<ExprAST
> ParseForExpr() {
434 getNextToken(); // eat the for.
436 if (CurTok
!= tok_identifier
)
437 return LogError("expected identifier after for");
439 std::string IdName
= IdentifierStr
;
440 getNextToken(); // eat identifier.
443 return LogError("expected '=' after for");
444 getNextToken(); // eat '='.
446 auto Start
= ParseExpression();
450 return LogError("expected ',' after for start value");
453 auto End
= ParseExpression();
457 // The step value is optional.
458 std::unique_ptr
<ExprAST
> Step
;
461 Step
= ParseExpression();
466 if (CurTok
!= tok_in
)
467 return LogError("expected 'in' after for");
468 getNextToken(); // eat 'in'.
470 auto Body
= ParseExpression();
474 return std::make_unique
<ForExprAST
>(IdName
, std::move(Start
), std::move(End
),
475 std::move(Step
), std::move(Body
));
478 /// varexpr ::= 'var' identifier ('=' expression)?
479 // (',' identifier ('=' expression)?)* 'in' expression
480 static std::unique_ptr
<ExprAST
> ParseVarExpr() {
481 getNextToken(); // eat the var.
483 std::vector
<std::pair
<std::string
, std::unique_ptr
<ExprAST
>>> VarNames
;
485 // At least one variable name is required.
486 if (CurTok
!= tok_identifier
)
487 return LogError("expected identifier after var");
490 std::string Name
= IdentifierStr
;
491 getNextToken(); // eat identifier.
493 // Read the optional initializer.
494 std::unique_ptr
<ExprAST
> Init
= nullptr;
496 getNextToken(); // eat the '='.
498 Init
= ParseExpression();
503 VarNames
.push_back(std::make_pair(Name
, std::move(Init
)));
505 // End of var list, exit loop.
508 getNextToken(); // eat the ','.
510 if (CurTok
!= tok_identifier
)
511 return LogError("expected identifier list after var");
514 // At this point, we have to have 'in'.
515 if (CurTok
!= tok_in
)
516 return LogError("expected 'in' keyword after 'var'");
517 getNextToken(); // eat 'in'.
519 auto Body
= ParseExpression();
523 return std::make_unique
<VarExprAST
>(std::move(VarNames
), std::move(Body
));
527 /// ::= identifierexpr
533 static std::unique_ptr
<ExprAST
> ParsePrimary() {
536 return LogError("unknown token when expecting an expression");
538 return ParseIdentifierExpr();
540 return ParseNumberExpr();
542 return ParseParenExpr();
544 return ParseIfExpr();
546 return ParseForExpr();
548 return ParseVarExpr();
555 static std::unique_ptr
<ExprAST
> ParseUnary() {
556 // If the current token is not an operator, it must be a primary expr.
557 if (!isascii(CurTok
) || CurTok
== '(' || CurTok
== ',')
558 return ParsePrimary();
560 // If this is a unary operator, read it.
563 if (auto Operand
= ParseUnary())
564 return std::make_unique
<UnaryExprAST
>(Opc
, std::move(Operand
));
570 static std::unique_ptr
<ExprAST
> ParseBinOpRHS(int ExprPrec
,
571 std::unique_ptr
<ExprAST
> LHS
) {
572 // If this is a binop, find its precedence.
574 int TokPrec
= GetTokPrecedence();
576 // If this is a binop that binds at least as tightly as the current binop,
577 // consume it, otherwise we are done.
578 if (TokPrec
< ExprPrec
)
581 // Okay, we know this is a binop.
583 getNextToken(); // eat binop
585 // Parse the unary expression after the binary operator.
586 auto RHS
= ParseUnary();
590 // If BinOp binds less tightly with RHS than the operator after RHS, let
591 // the pending operator take RHS as its LHS.
592 int NextPrec
= GetTokPrecedence();
593 if (TokPrec
< NextPrec
) {
594 RHS
= ParseBinOpRHS(TokPrec
+ 1, std::move(RHS
));
601 std::make_unique
<BinaryExprAST
>(BinOp
, std::move(LHS
), std::move(RHS
));
606 /// ::= unary binoprhs
608 static std::unique_ptr
<ExprAST
> ParseExpression() {
609 auto LHS
= ParseUnary();
613 return ParseBinOpRHS(0, std::move(LHS
));
617 /// ::= id '(' id* ')'
618 /// ::= binary LETTER number? (id, id)
619 /// ::= unary LETTER (id)
620 static std::unique_ptr
<PrototypeAST
> ParsePrototype() {
623 unsigned Kind
= 0; // 0 = identifier, 1 = unary, 2 = binary.
624 unsigned BinaryPrecedence
= 30;
628 return LogErrorP("Expected function name in prototype");
630 FnName
= IdentifierStr
;
636 if (!isascii(CurTok
))
637 return LogErrorP("Expected unary operator");
639 FnName
+= (char)CurTok
;
645 if (!isascii(CurTok
))
646 return LogErrorP("Expected binary operator");
648 FnName
+= (char)CurTok
;
652 // Read the precedence if present.
653 if (CurTok
== tok_number
) {
654 if (NumVal
< 1 || NumVal
> 100)
655 return LogErrorP("Invalid precedence: must be 1..100");
656 BinaryPrecedence
= (unsigned)NumVal
;
663 return LogErrorP("Expected '(' in prototype");
665 std::vector
<std::string
> ArgNames
;
666 while (getNextToken() == tok_identifier
)
667 ArgNames
.push_back(IdentifierStr
);
669 return LogErrorP("Expected ')' in prototype");
672 getNextToken(); // eat ')'.
674 // Verify right number of names for operator.
675 if (Kind
&& ArgNames
.size() != Kind
)
676 return LogErrorP("Invalid number of operands for operator");
678 return std::make_unique
<PrototypeAST
>(FnName
, ArgNames
, Kind
!= 0,
682 /// definition ::= 'def' prototype expression
683 static std::unique_ptr
<FunctionAST
> ParseDefinition() {
684 getNextToken(); // eat def.
685 auto Proto
= ParsePrototype();
689 if (auto E
= ParseExpression())
690 return std::make_unique
<FunctionAST
>(std::move(Proto
), std::move(E
));
694 /// toplevelexpr ::= expression
695 static std::unique_ptr
<FunctionAST
> ParseTopLevelExpr() {
696 if (auto E
= ParseExpression()) {
697 // Make an anonymous proto.
698 auto Proto
= std::make_unique
<PrototypeAST
>("__anon_expr",
699 std::vector
<std::string
>());
700 return std::make_unique
<FunctionAST
>(std::move(Proto
), std::move(E
));
705 /// external ::= 'extern' prototype
706 static std::unique_ptr
<PrototypeAST
> ParseExtern() {
707 getNextToken(); // eat extern.
708 return ParsePrototype();
711 //===----------------------------------------------------------------------===//
713 //===----------------------------------------------------------------------===//
715 static std::unique_ptr
<LLVMContext
> TheContext
;
716 static std::unique_ptr
<Module
> TheModule
;
717 static std::unique_ptr
<IRBuilder
<>> Builder
;
718 static std::map
<std::string
, AllocaInst
*> NamedValues
;
719 static std::unique_ptr
<KaleidoscopeJIT
> TheJIT
;
720 static std::unique_ptr
<FunctionPassManager
> TheFPM
;
721 static std::unique_ptr
<FunctionAnalysisManager
> TheFAM
;
722 static std::unique_ptr
<ModuleAnalysisManager
> TheMAM
;
723 static std::unique_ptr
<PassInstrumentationCallbacks
> ThePIC
;
724 static std::unique_ptr
<StandardInstrumentations
> TheSI
;
725 static std::map
<std::string
, std::unique_ptr
<PrototypeAST
>> FunctionProtos
;
726 static ExitOnError ExitOnErr
;
728 Value
*LogErrorV(const char *Str
) {
733 Function
*getFunction(std::string Name
) {
734 // First, see if the function has already been added to the current module.
735 if (auto *F
= TheModule
->getFunction(Name
))
738 // If not, check whether we can codegen the declaration from some existing
740 auto FI
= FunctionProtos
.find(Name
);
741 if (FI
!= FunctionProtos
.end())
742 return FI
->second
->codegen();
744 // If no existing prototype exists, return null.
748 /// CreateEntryBlockAlloca - Create an alloca instruction in the entry block of
749 /// the function. This is used for mutable variables etc.
750 static AllocaInst
*CreateEntryBlockAlloca(Function
*TheFunction
,
752 IRBuilder
<> TmpB(&TheFunction
->getEntryBlock(),
753 TheFunction
->getEntryBlock().begin());
754 return TmpB
.CreateAlloca(Type::getDoubleTy(*TheContext
), nullptr, VarName
);
757 Value
*NumberExprAST::codegen() {
758 return ConstantFP::get(*TheContext
, APFloat(Val
));
761 Value
*VariableExprAST::codegen() {
762 // Look this variable up in the function.
763 AllocaInst
*A
= NamedValues
[Name
];
765 return LogErrorV("Unknown variable name");
768 return Builder
->CreateLoad(A
->getAllocatedType(), A
, Name
.c_str());
771 Value
*UnaryExprAST::codegen() {
772 Value
*OperandV
= Operand
->codegen();
776 Function
*F
= getFunction(std::string("unary") + Opcode
);
778 return LogErrorV("Unknown unary operator");
780 return Builder
->CreateCall(F
, OperandV
, "unop");
783 Value
*BinaryExprAST::codegen() {
784 // Special case '=' because we don't want to emit the LHS as an expression.
786 // Assignment requires the LHS to be an identifier.
787 // This assume we're building without RTTI because LLVM builds that way by
788 // default. If you build LLVM with RTTI this can be changed to a
789 // dynamic_cast for automatic error checking.
790 VariableExprAST
*LHSE
= static_cast<VariableExprAST
*>(LHS
.get());
792 return LogErrorV("destination of '=' must be a variable");
794 Value
*Val
= RHS
->codegen();
799 Value
*Variable
= NamedValues
[LHSE
->getName()];
801 return LogErrorV("Unknown variable name");
803 Builder
->CreateStore(Val
, Variable
);
807 Value
*L
= LHS
->codegen();
808 Value
*R
= RHS
->codegen();
814 return Builder
->CreateFAdd(L
, R
, "addtmp");
816 return Builder
->CreateFSub(L
, R
, "subtmp");
818 return Builder
->CreateFMul(L
, R
, "multmp");
820 L
= Builder
->CreateFCmpULT(L
, R
, "cmptmp");
821 // Convert bool 0/1 to double 0.0 or 1.0
822 return Builder
->CreateUIToFP(L
, Type::getDoubleTy(*TheContext
), "booltmp");
827 // If it wasn't a builtin binary operator, it must be a user defined one. Emit
829 Function
*F
= getFunction(std::string("binary") + Op
);
830 assert(F
&& "binary operator not found!");
832 Value
*Ops
[] = {L
, R
};
833 return Builder
->CreateCall(F
, Ops
, "binop");
836 Value
*CallExprAST::codegen() {
837 // Look up the name in the global module table.
838 Function
*CalleeF
= getFunction(Callee
);
840 return LogErrorV("Unknown function referenced");
842 // If argument mismatch error.
843 if (CalleeF
->arg_size() != Args
.size())
844 return LogErrorV("Incorrect # arguments passed");
846 std::vector
<Value
*> ArgsV
;
847 for (unsigned i
= 0, e
= Args
.size(); i
!= e
; ++i
) {
848 ArgsV
.push_back(Args
[i
]->codegen());
853 return Builder
->CreateCall(CalleeF
, ArgsV
, "calltmp");
856 Value
*IfExprAST::codegen() {
857 Value
*CondV
= Cond
->codegen();
861 // Convert condition to a bool by comparing non-equal to 0.0.
862 CondV
= Builder
->CreateFCmpONE(
863 CondV
, ConstantFP::get(*TheContext
, APFloat(0.0)), "ifcond");
865 Function
*TheFunction
= Builder
->GetInsertBlock()->getParent();
867 // Create blocks for the then and else cases. Insert the 'then' block at the
868 // end of the function.
869 BasicBlock
*ThenBB
= BasicBlock::Create(*TheContext
, "then", TheFunction
);
870 BasicBlock
*ElseBB
= BasicBlock::Create(*TheContext
, "else");
871 BasicBlock
*MergeBB
= BasicBlock::Create(*TheContext
, "ifcont");
873 Builder
->CreateCondBr(CondV
, ThenBB
, ElseBB
);
876 Builder
->SetInsertPoint(ThenBB
);
878 Value
*ThenV
= Then
->codegen();
882 Builder
->CreateBr(MergeBB
);
883 // Codegen of 'Then' can change the current block, update ThenBB for the PHI.
884 ThenBB
= Builder
->GetInsertBlock();
887 TheFunction
->insert(TheFunction
->end(), ElseBB
);
888 Builder
->SetInsertPoint(ElseBB
);
890 Value
*ElseV
= Else
->codegen();
894 Builder
->CreateBr(MergeBB
);
895 // Codegen of 'Else' can change the current block, update ElseBB for the PHI.
896 ElseBB
= Builder
->GetInsertBlock();
899 TheFunction
->insert(TheFunction
->end(), MergeBB
);
900 Builder
->SetInsertPoint(MergeBB
);
901 PHINode
*PN
= Builder
->CreatePHI(Type::getDoubleTy(*TheContext
), 2, "iftmp");
903 PN
->addIncoming(ThenV
, ThenBB
);
904 PN
->addIncoming(ElseV
, ElseBB
);
908 // Output for-loop as:
909 // var = alloca double
912 // store start -> var
923 // nextvar = curvar + step
924 // store nextvar -> var
925 // br endcond, loop, endloop
927 Value
*ForExprAST::codegen() {
928 Function
*TheFunction
= Builder
->GetInsertBlock()->getParent();
930 // Create an alloca for the variable in the entry block.
931 AllocaInst
*Alloca
= CreateEntryBlockAlloca(TheFunction
, VarName
);
933 // Emit the start code first, without 'variable' in scope.
934 Value
*StartVal
= Start
->codegen();
938 // Store the value into the alloca.
939 Builder
->CreateStore(StartVal
, Alloca
);
941 // Make the new basic block for the loop header, inserting after current
943 BasicBlock
*LoopBB
= BasicBlock::Create(*TheContext
, "loop", TheFunction
);
945 // Insert an explicit fall through from the current block to the LoopBB.
946 Builder
->CreateBr(LoopBB
);
948 // Start insertion in LoopBB.
949 Builder
->SetInsertPoint(LoopBB
);
951 // Within the loop, the variable is defined equal to the PHI node. If it
952 // shadows an existing variable, we have to restore it, so save it now.
953 AllocaInst
*OldVal
= NamedValues
[VarName
];
954 NamedValues
[VarName
] = Alloca
;
956 // Emit the body of the loop. This, like any other expr, can change the
957 // current BB. Note that we ignore the value computed by the body, but don't
959 if (!Body
->codegen())
962 // Emit the step value.
963 Value
*StepVal
= nullptr;
965 StepVal
= Step
->codegen();
969 // If not specified, use 1.0.
970 StepVal
= ConstantFP::get(*TheContext
, APFloat(1.0));
973 // Compute the end condition.
974 Value
*EndCond
= End
->codegen();
978 // Reload, increment, and restore the alloca. This handles the case where
979 // the body of the loop mutates the variable.
981 Builder
->CreateLoad(Alloca
->getAllocatedType(), Alloca
, VarName
.c_str());
982 Value
*NextVar
= Builder
->CreateFAdd(CurVar
, StepVal
, "nextvar");
983 Builder
->CreateStore(NextVar
, Alloca
);
985 // Convert condition to a bool by comparing non-equal to 0.0.
986 EndCond
= Builder
->CreateFCmpONE(
987 EndCond
, ConstantFP::get(*TheContext
, APFloat(0.0)), "loopcond");
989 // Create the "after loop" block and insert it.
990 BasicBlock
*AfterBB
=
991 BasicBlock::Create(*TheContext
, "afterloop", TheFunction
);
993 // Insert the conditional branch into the end of LoopEndBB.
994 Builder
->CreateCondBr(EndCond
, LoopBB
, AfterBB
);
996 // Any new code will be inserted in AfterBB.
997 Builder
->SetInsertPoint(AfterBB
);
999 // Restore the unshadowed variable.
1001 NamedValues
[VarName
] = OldVal
;
1003 NamedValues
.erase(VarName
);
1005 // for expr always returns 0.0.
1006 return Constant::getNullValue(Type::getDoubleTy(*TheContext
));
1009 Value
*VarExprAST::codegen() {
1010 std::vector
<AllocaInst
*> OldBindings
;
1012 Function
*TheFunction
= Builder
->GetInsertBlock()->getParent();
1014 // Register all variables and emit their initializer.
1015 for (unsigned i
= 0, e
= VarNames
.size(); i
!= e
; ++i
) {
1016 const std::string
&VarName
= VarNames
[i
].first
;
1017 ExprAST
*Init
= VarNames
[i
].second
.get();
1019 // Emit the initializer before adding the variable to scope, this prevents
1020 // the initializer from referencing the variable itself, and permits stuff
1023 // var a = a in ... # refers to outer 'a'.
1026 InitVal
= Init
->codegen();
1029 } else { // If not specified, use 0.0.
1030 InitVal
= ConstantFP::get(*TheContext
, APFloat(0.0));
1033 AllocaInst
*Alloca
= CreateEntryBlockAlloca(TheFunction
, VarName
);
1034 Builder
->CreateStore(InitVal
, Alloca
);
1036 // Remember the old variable binding so that we can restore the binding when
1038 OldBindings
.push_back(NamedValues
[VarName
]);
1040 // Remember this binding.
1041 NamedValues
[VarName
] = Alloca
;
1044 // Codegen the body, now that all vars are in scope.
1045 Value
*BodyVal
= Body
->codegen();
1049 // Pop all our variables from scope.
1050 for (unsigned i
= 0, e
= VarNames
.size(); i
!= e
; ++i
)
1051 NamedValues
[VarNames
[i
].first
] = OldBindings
[i
];
1053 // Return the body computation.
1057 Function
*PrototypeAST::codegen() {
1058 // Make the function type: double(double,double) etc.
1059 std::vector
<Type
*> Doubles(Args
.size(), Type::getDoubleTy(*TheContext
));
1061 FunctionType::get(Type::getDoubleTy(*TheContext
), Doubles
, false);
1064 Function::Create(FT
, Function::ExternalLinkage
, Name
, TheModule
.get());
1066 // Set names for all arguments.
1068 for (auto &Arg
: F
->args())
1069 Arg
.setName(Args
[Idx
++]);
1074 Function
*FunctionAST::codegen() {
1075 // Transfer ownership of the prototype to the FunctionProtos map, but keep a
1076 // reference to it for use below.
1078 FunctionProtos
[Proto
->getName()] = std::move(Proto
);
1079 Function
*TheFunction
= getFunction(P
.getName());
1083 // If this is an operator, install it.
1085 BinopPrecedence
[P
.getOperatorName()] = P
.getBinaryPrecedence();
1087 // Create a new basic block to start insertion into.
1088 BasicBlock
*BB
= BasicBlock::Create(*TheContext
, "entry", TheFunction
);
1089 Builder
->SetInsertPoint(BB
);
1091 // Record the function arguments in the NamedValues map.
1092 NamedValues
.clear();
1093 for (auto &Arg
: TheFunction
->args()) {
1094 // Create an alloca for this variable.
1095 AllocaInst
*Alloca
= CreateEntryBlockAlloca(TheFunction
, Arg
.getName());
1097 // Store the initial value into the alloca.
1098 Builder
->CreateStore(&Arg
, Alloca
);
1100 // Add arguments to variable symbol table.
1101 NamedValues
[std::string(Arg
.getName())] = Alloca
;
1104 if (Value
*RetVal
= Body
->codegen()) {
1105 // Finish off the function.
1106 Builder
->CreateRet(RetVal
);
1108 // Validate the generated code, checking for consistency.
1109 verifyFunction(*TheFunction
);
1111 // Run the optimizer on the function.
1112 TheFPM
->run(*TheFunction
, *TheFAM
);
1117 // Error reading body, remove function.
1118 TheFunction
->eraseFromParent();
1121 BinopPrecedence
.erase(P
.getOperatorName());
1125 //===----------------------------------------------------------------------===//
1126 // Top-Level parsing and JIT Driver
1127 //===----------------------------------------------------------------------===//
1129 static void InitializeModuleAndManagers() {
1130 // Open a new context and module.
1131 TheContext
= std::make_unique
<LLVMContext
>();
1132 TheModule
= std::make_unique
<Module
>("KaleidoscopeJIT", *TheContext
);
1133 TheModule
->setDataLayout(TheJIT
->getDataLayout());
1135 // Create a new builder for the module.
1136 Builder
= std::make_unique
<IRBuilder
<>>(*TheContext
);
1138 // Create new pass and analysis managers.
1139 TheFPM
= std::make_unique
<FunctionPassManager
>();
1140 TheFAM
= std::make_unique
<FunctionAnalysisManager
>();
1141 TheMAM
= std::make_unique
<ModuleAnalysisManager
>();
1142 ThePIC
= std::make_unique
<PassInstrumentationCallbacks
>();
1143 TheSI
= std::make_unique
<StandardInstrumentations
>(*TheContext
,
1144 /*DebugLogging*/ true);
1145 TheSI
->registerCallbacks(*ThePIC
, TheMAM
.get());
1147 // Add transform passes.
1148 // Do simple "peephole" optimizations and bit-twiddling optzns.
1149 TheFPM
->addPass(InstCombinePass());
1150 // Reassociate expressions.
1151 TheFPM
->addPass(ReassociatePass());
1152 // Eliminate Common SubExpressions.
1153 TheFPM
->addPass(GVNPass());
1154 // Simplify the control flow graph (deleting unreachable blocks, etc).
1155 TheFPM
->addPass(SimplifyCFGPass());
1157 // Register analysis passes used in these transform passes.
1158 TheFAM
->registerPass([&] { return AAManager(); });
1159 TheFAM
->registerPass([&] { return AssumptionAnalysis(); });
1160 TheFAM
->registerPass([&] { return DominatorTreeAnalysis(); });
1161 TheFAM
->registerPass([&] { return LoopAnalysis(); });
1162 TheFAM
->registerPass([&] { return MemoryDependenceAnalysis(); });
1163 TheFAM
->registerPass([&] { return MemorySSAAnalysis(); });
1164 TheFAM
->registerPass([&] { return OptimizationRemarkEmitterAnalysis(); });
1165 TheFAM
->registerPass([&] {
1166 return OuterAnalysisManagerProxy
<ModuleAnalysisManager
, Function
>(*TheMAM
);
1168 TheFAM
->registerPass(
1169 [&] { return PassInstrumentationAnalysis(ThePIC
.get()); });
1170 TheFAM
->registerPass([&] { return TargetIRAnalysis(); });
1171 TheFAM
->registerPass([&] { return TargetLibraryAnalysis(); });
1173 TheMAM
->registerPass([&] { return ProfileSummaryAnalysis(); });
1176 static void HandleDefinition() {
1177 if (auto FnAST
= ParseDefinition()) {
1178 if (auto *FnIR
= FnAST
->codegen()) {
1179 fprintf(stderr
, "Read function definition:");
1180 FnIR
->print(errs());
1181 fprintf(stderr
, "\n");
1182 ExitOnErr(TheJIT
->addModule(
1183 ThreadSafeModule(std::move(TheModule
), std::move(TheContext
))));
1184 InitializeModuleAndManagers();
1187 // Skip token for error recovery.
1192 static void HandleExtern() {
1193 if (auto ProtoAST
= ParseExtern()) {
1194 if (auto *FnIR
= ProtoAST
->codegen()) {
1195 fprintf(stderr
, "Read extern: ");
1196 FnIR
->print(errs());
1197 fprintf(stderr
, "\n");
1198 FunctionProtos
[ProtoAST
->getName()] = std::move(ProtoAST
);
1201 // Skip token for error recovery.
1206 static void HandleTopLevelExpression() {
1207 // Evaluate a top-level expression into an anonymous function.
1208 if (auto FnAST
= ParseTopLevelExpr()) {
1209 if (FnAST
->codegen()) {
1210 // Create a ResourceTracker to track JIT'd memory allocated to our
1211 // anonymous expression -- that way we can free it after executing.
1212 auto RT
= TheJIT
->getMainJITDylib().createResourceTracker();
1214 auto TSM
= ThreadSafeModule(std::move(TheModule
), std::move(TheContext
));
1215 ExitOnErr(TheJIT
->addModule(std::move(TSM
), RT
));
1216 InitializeModuleAndManagers();
1218 // Search the JIT for the __anon_expr symbol.
1219 auto ExprSymbol
= ExitOnErr(TheJIT
->lookup("__anon_expr"));
1221 // Get the symbol's address and cast it to the right type (takes no
1222 // arguments, returns a double) so we can call it as a native function.
1223 double (*FP
)() = ExprSymbol
.getAddress().toPtr
<double (*)()>();
1224 fprintf(stderr
, "Evaluated to %f\n", FP());
1226 // Delete the anonymous expression module from the JIT.
1227 ExitOnErr(RT
->remove());
1230 // Skip token for error recovery.
1235 /// top ::= definition | external | expression | ';'
1236 static void MainLoop() {
1238 fprintf(stderr
, "ready> ");
1242 case ';': // ignore top-level semicolons.
1252 HandleTopLevelExpression();
1258 //===----------------------------------------------------------------------===//
1259 // "Library" functions that can be "extern'd" from user code.
1260 //===----------------------------------------------------------------------===//
1263 #define DLLEXPORT __declspec(dllexport)
1268 /// putchard - putchar that takes a double and returns 0.
1269 extern "C" DLLEXPORT
double putchard(double X
) {
1270 fputc((char)X
, stderr
);
1274 /// printd - printf that takes a double prints it as "%f\n", returning 0.
1275 extern "C" DLLEXPORT
double printd(double X
) {
1276 fprintf(stderr
, "%f\n", X
);
1280 //===----------------------------------------------------------------------===//
1281 // Main driver code.
1282 //===----------------------------------------------------------------------===//
1285 InitializeNativeTarget();
1286 InitializeNativeTargetAsmPrinter();
1287 InitializeNativeTargetAsmParser();
1289 // Install standard binary operators.
1290 // 1 is lowest precedence.
1291 BinopPrecedence
['='] = 2;
1292 BinopPrecedence
['<'] = 10;
1293 BinopPrecedence
['+'] = 20;
1294 BinopPrecedence
['-'] = 20;
1295 BinopPrecedence
['*'] = 40; // highest.
1297 // Prime the first token.
1298 fprintf(stderr
, "ready> ");
1301 TheJIT
= ExitOnErr(KaleidoscopeJIT::Create());
1303 InitializeModuleAndManagers();
1305 // Run the main "interpreter loop" now.