1 #include "../include/KaleidoscopeJIT.h"
2 #include "llvm/ADT/APFloat.h"
3 #include "llvm/ADT/STLExtras.h"
4 #include "llvm/IR/BasicBlock.h"
5 #include "llvm/IR/Constants.h"
6 #include "llvm/IR/DerivedTypes.h"
7 #include "llvm/IR/Function.h"
8 #include "llvm/IR/IRBuilder.h"
9 #include "llvm/IR/Instructions.h"
10 #include "llvm/IR/LLVMContext.h"
11 #include "llvm/IR/Module.h"
12 #include "llvm/IR/PassManager.h"
13 #include "llvm/IR/Type.h"
14 #include "llvm/IR/Verifier.h"
15 #include "llvm/Passes/PassBuilder.h"
16 #include "llvm/Passes/StandardInstrumentations.h"
17 #include "llvm/Support/TargetSelect.h"
18 #include "llvm/Target/TargetMachine.h"
19 #include "llvm/Transforms/InstCombine/InstCombine.h"
20 #include "llvm/Transforms/Scalar.h"
21 #include "llvm/Transforms/Scalar/GVN.h"
22 #include "llvm/Transforms/Scalar/Reassociate.h"
23 #include "llvm/Transforms/Scalar/SimplifyCFG.h"
24 #include "llvm/Transforms/Utils.h"
38 using namespace llvm::orc
;
40 //===----------------------------------------------------------------------===//
42 //===----------------------------------------------------------------------===//
44 // The lexer returns tokens [0-255] if it is an unknown character, otherwise one
45 // of these for known things.
72 static std::string IdentifierStr
; // Filled in if tok_identifier
73 static double NumVal
; // Filled in if tok_number
75 /// gettok - Return the next token from standard input.
77 static int LastChar
= ' ';
79 // Skip any whitespace.
80 while (isspace(LastChar
))
83 if (isalpha(LastChar
)) { // identifier: [a-zA-Z][a-zA-Z0-9]*
84 IdentifierStr
= LastChar
;
85 while (isalnum((LastChar
= getchar())))
86 IdentifierStr
+= LastChar
;
88 if (IdentifierStr
== "def")
90 if (IdentifierStr
== "extern")
92 if (IdentifierStr
== "if")
94 if (IdentifierStr
== "then")
96 if (IdentifierStr
== "else")
98 if (IdentifierStr
== "for")
100 if (IdentifierStr
== "in")
102 if (IdentifierStr
== "binary")
104 if (IdentifierStr
== "unary")
106 if (IdentifierStr
== "var")
108 return tok_identifier
;
111 if (isdigit(LastChar
) || LastChar
== '.') { // Number: [0-9.]+
115 LastChar
= getchar();
116 } while (isdigit(LastChar
) || LastChar
== '.');
118 NumVal
= strtod(NumStr
.c_str(), nullptr);
122 if (LastChar
== '#') {
123 // Comment until end of line.
125 LastChar
= getchar();
126 while (LastChar
!= EOF
&& LastChar
!= '\n' && LastChar
!= '\r');
132 // Check for end of file. Don't eat the EOF.
136 // Otherwise, just return the character as its ascii value.
137 int ThisChar
= LastChar
;
138 LastChar
= getchar();
142 //===----------------------------------------------------------------------===//
143 // Abstract Syntax Tree (aka Parse Tree)
144 //===----------------------------------------------------------------------===//
148 /// ExprAST - Base class for all expression nodes.
151 virtual ~ExprAST() = default;
153 virtual Value
*codegen() = 0;
156 /// NumberExprAST - Expression class for numeric literals like "1.0".
157 class NumberExprAST
: public ExprAST
{
161 NumberExprAST(double Val
) : Val(Val
) {}
163 Value
*codegen() override
;
166 /// VariableExprAST - Expression class for referencing a variable, like "a".
167 class VariableExprAST
: public ExprAST
{
171 VariableExprAST(const std::string
&Name
) : Name(Name
) {}
173 Value
*codegen() override
;
174 const std::string
&getName() const { return Name
; }
177 /// UnaryExprAST - Expression class for a unary operator.
178 class UnaryExprAST
: public ExprAST
{
180 std::unique_ptr
<ExprAST
> Operand
;
183 UnaryExprAST(char Opcode
, std::unique_ptr
<ExprAST
> Operand
)
184 : Opcode(Opcode
), Operand(std::move(Operand
)) {}
186 Value
*codegen() override
;
189 /// BinaryExprAST - Expression class for a binary operator.
190 class BinaryExprAST
: public ExprAST
{
192 std::unique_ptr
<ExprAST
> LHS
, RHS
;
195 BinaryExprAST(char Op
, std::unique_ptr
<ExprAST
> LHS
,
196 std::unique_ptr
<ExprAST
> RHS
)
197 : Op(Op
), LHS(std::move(LHS
)), RHS(std::move(RHS
)) {}
199 Value
*codegen() override
;
202 /// CallExprAST - Expression class for function calls.
203 class CallExprAST
: public ExprAST
{
205 std::vector
<std::unique_ptr
<ExprAST
>> Args
;
208 CallExprAST(const std::string
&Callee
,
209 std::vector
<std::unique_ptr
<ExprAST
>> Args
)
210 : Callee(Callee
), Args(std::move(Args
)) {}
212 Value
*codegen() override
;
215 /// IfExprAST - Expression class for if/then/else.
216 class IfExprAST
: public ExprAST
{
217 std::unique_ptr
<ExprAST
> Cond
, Then
, Else
;
220 IfExprAST(std::unique_ptr
<ExprAST
> Cond
, std::unique_ptr
<ExprAST
> Then
,
221 std::unique_ptr
<ExprAST
> Else
)
222 : Cond(std::move(Cond
)), Then(std::move(Then
)), Else(std::move(Else
)) {}
224 Value
*codegen() override
;
227 /// ForExprAST - Expression class for for/in.
228 class ForExprAST
: public ExprAST
{
230 std::unique_ptr
<ExprAST
> Start
, End
, Step
, Body
;
233 ForExprAST(const std::string
&VarName
, std::unique_ptr
<ExprAST
> Start
,
234 std::unique_ptr
<ExprAST
> End
, std::unique_ptr
<ExprAST
> Step
,
235 std::unique_ptr
<ExprAST
> Body
)
236 : VarName(VarName
), Start(std::move(Start
)), End(std::move(End
)),
237 Step(std::move(Step
)), Body(std::move(Body
)) {}
239 Value
*codegen() override
;
242 /// VarExprAST - Expression class for var/in
243 class VarExprAST
: public ExprAST
{
244 std::vector
<std::pair
<std::string
, std::unique_ptr
<ExprAST
>>> VarNames
;
245 std::unique_ptr
<ExprAST
> Body
;
249 std::vector
<std::pair
<std::string
, std::unique_ptr
<ExprAST
>>> VarNames
,
250 std::unique_ptr
<ExprAST
> Body
)
251 : VarNames(std::move(VarNames
)), Body(std::move(Body
)) {}
253 Value
*codegen() override
;
256 /// PrototypeAST - This class represents the "prototype" for a function,
257 /// which captures its name, and its argument names (thus implicitly the number
258 /// of arguments the function takes), as well as if it is an operator.
261 std::vector
<std::string
> Args
;
263 unsigned Precedence
; // Precedence if a binary op.
266 PrototypeAST(const std::string
&Name
, std::vector
<std::string
> Args
,
267 bool IsOperator
= false, unsigned Prec
= 0)
268 : Name(Name
), Args(std::move(Args
)), IsOperator(IsOperator
),
272 const std::string
&getName() const { return Name
; }
274 bool isUnaryOp() const { return IsOperator
&& Args
.size() == 1; }
275 bool isBinaryOp() const { return IsOperator
&& Args
.size() == 2; }
277 char getOperatorName() const {
278 assert(isUnaryOp() || isBinaryOp());
279 return Name
[Name
.size() - 1];
282 unsigned getBinaryPrecedence() const { return Precedence
; }
285 /// FunctionAST - This class represents a function definition itself.
287 std::unique_ptr
<PrototypeAST
> Proto
;
288 std::unique_ptr
<ExprAST
> Body
;
291 FunctionAST(std::unique_ptr
<PrototypeAST
> Proto
,
292 std::unique_ptr
<ExprAST
> Body
)
293 : Proto(std::move(Proto
)), Body(std::move(Body
)) {}
298 } // end anonymous namespace
300 //===----------------------------------------------------------------------===//
302 //===----------------------------------------------------------------------===//
304 /// CurTok/getNextToken - Provide a simple token buffer. CurTok is the current
305 /// token the parser is looking at. getNextToken reads another token from the
306 /// lexer and updates CurTok with its results.
308 static int getNextToken() { return CurTok
= gettok(); }
310 /// BinopPrecedence - This holds the precedence for each binary operator that is
312 static std::map
<char, int> BinopPrecedence
;
314 /// GetTokPrecedence - Get the precedence of the pending binary operator token.
315 static int GetTokPrecedence() {
316 if (!isascii(CurTok
))
319 // Make sure it's a declared binop.
320 int TokPrec
= BinopPrecedence
[CurTok
];
326 /// LogError* - These are little helper functions for error handling.
327 std::unique_ptr
<ExprAST
> LogError(const char *Str
) {
328 fprintf(stderr
, "Error: %s\n", Str
);
332 std::unique_ptr
<PrototypeAST
> LogErrorP(const char *Str
) {
337 static std::unique_ptr
<ExprAST
> ParseExpression();
339 /// numberexpr ::= number
340 static std::unique_ptr
<ExprAST
> ParseNumberExpr() {
341 auto Result
= std::make_unique
<NumberExprAST
>(NumVal
);
342 getNextToken(); // consume the number
343 return std::move(Result
);
346 /// parenexpr ::= '(' expression ')'
347 static std::unique_ptr
<ExprAST
> ParseParenExpr() {
348 getNextToken(); // eat (.
349 auto V
= ParseExpression();
354 return LogError("expected ')'");
355 getNextToken(); // eat ).
361 /// ::= identifier '(' expression* ')'
362 static std::unique_ptr
<ExprAST
> ParseIdentifierExpr() {
363 std::string IdName
= IdentifierStr
;
365 getNextToken(); // eat identifier.
367 if (CurTok
!= '(') // Simple variable ref.
368 return std::make_unique
<VariableExprAST
>(IdName
);
371 getNextToken(); // eat (
372 std::vector
<std::unique_ptr
<ExprAST
>> Args
;
375 if (auto Arg
= ParseExpression())
376 Args
.push_back(std::move(Arg
));
384 return LogError("Expected ')' or ',' in argument list");
392 return std::make_unique
<CallExprAST
>(IdName
, std::move(Args
));
395 /// ifexpr ::= 'if' expression 'then' expression 'else' expression
396 static std::unique_ptr
<ExprAST
> ParseIfExpr() {
397 getNextToken(); // eat the if.
400 auto Cond
= ParseExpression();
404 if (CurTok
!= tok_then
)
405 return LogError("expected then");
406 getNextToken(); // eat the then
408 auto Then
= ParseExpression();
412 if (CurTok
!= tok_else
)
413 return LogError("expected else");
417 auto Else
= ParseExpression();
421 return std::make_unique
<IfExprAST
>(std::move(Cond
), std::move(Then
),
425 /// forexpr ::= 'for' identifier '=' expr ',' expr (',' expr)? 'in' expression
426 static std::unique_ptr
<ExprAST
> ParseForExpr() {
427 getNextToken(); // eat the for.
429 if (CurTok
!= tok_identifier
)
430 return LogError("expected identifier after for");
432 std::string IdName
= IdentifierStr
;
433 getNextToken(); // eat identifier.
436 return LogError("expected '=' after for");
437 getNextToken(); // eat '='.
439 auto Start
= ParseExpression();
443 return LogError("expected ',' after for start value");
446 auto End
= ParseExpression();
450 // The step value is optional.
451 std::unique_ptr
<ExprAST
> Step
;
454 Step
= ParseExpression();
459 if (CurTok
!= tok_in
)
460 return LogError("expected 'in' after for");
461 getNextToken(); // eat 'in'.
463 auto Body
= ParseExpression();
467 return std::make_unique
<ForExprAST
>(IdName
, std::move(Start
), std::move(End
),
468 std::move(Step
), std::move(Body
));
471 /// varexpr ::= 'var' identifier ('=' expression)?
472 // (',' identifier ('=' expression)?)* 'in' expression
473 static std::unique_ptr
<ExprAST
> ParseVarExpr() {
474 getNextToken(); // eat the var.
476 std::vector
<std::pair
<std::string
, std::unique_ptr
<ExprAST
>>> VarNames
;
478 // At least one variable name is required.
479 if (CurTok
!= tok_identifier
)
480 return LogError("expected identifier after var");
483 std::string Name
= IdentifierStr
;
484 getNextToken(); // eat identifier.
486 // Read the optional initializer.
487 std::unique_ptr
<ExprAST
> Init
= nullptr;
489 getNextToken(); // eat the '='.
491 Init
= ParseExpression();
496 VarNames
.push_back(std::make_pair(Name
, std::move(Init
)));
498 // End of var list, exit loop.
501 getNextToken(); // eat the ','.
503 if (CurTok
!= tok_identifier
)
504 return LogError("expected identifier list after var");
507 // At this point, we have to have 'in'.
508 if (CurTok
!= tok_in
)
509 return LogError("expected 'in' keyword after 'var'");
510 getNextToken(); // eat 'in'.
512 auto Body
= ParseExpression();
516 return std::make_unique
<VarExprAST
>(std::move(VarNames
), std::move(Body
));
520 /// ::= identifierexpr
526 static std::unique_ptr
<ExprAST
> ParsePrimary() {
529 return LogError("unknown token when expecting an expression");
531 return ParseIdentifierExpr();
533 return ParseNumberExpr();
535 return ParseParenExpr();
537 return ParseIfExpr();
539 return ParseForExpr();
541 return ParseVarExpr();
548 static std::unique_ptr
<ExprAST
> ParseUnary() {
549 // If the current token is not an operator, it must be a primary expr.
550 if (!isascii(CurTok
) || CurTok
== '(' || CurTok
== ',')
551 return ParsePrimary();
553 // If this is a unary operator, read it.
556 if (auto Operand
= ParseUnary())
557 return std::make_unique
<UnaryExprAST
>(Opc
, std::move(Operand
));
563 static std::unique_ptr
<ExprAST
> ParseBinOpRHS(int ExprPrec
,
564 std::unique_ptr
<ExprAST
> LHS
) {
565 // If this is a binop, find its precedence.
567 int TokPrec
= GetTokPrecedence();
569 // If this is a binop that binds at least as tightly as the current binop,
570 // consume it, otherwise we are done.
571 if (TokPrec
< ExprPrec
)
574 // Okay, we know this is a binop.
576 getNextToken(); // eat binop
578 // Parse the unary expression after the binary operator.
579 auto RHS
= ParseUnary();
583 // If BinOp binds less tightly with RHS than the operator after RHS, let
584 // the pending operator take RHS as its LHS.
585 int NextPrec
= GetTokPrecedence();
586 if (TokPrec
< NextPrec
) {
587 RHS
= ParseBinOpRHS(TokPrec
+ 1, std::move(RHS
));
594 std::make_unique
<BinaryExprAST
>(BinOp
, std::move(LHS
), std::move(RHS
));
599 /// ::= unary binoprhs
601 static std::unique_ptr
<ExprAST
> ParseExpression() {
602 auto LHS
= ParseUnary();
606 return ParseBinOpRHS(0, std::move(LHS
));
610 /// ::= id '(' id* ')'
611 /// ::= binary LETTER number? (id, id)
612 /// ::= unary LETTER (id)
613 static std::unique_ptr
<PrototypeAST
> ParsePrototype() {
616 unsigned Kind
= 0; // 0 = identifier, 1 = unary, 2 = binary.
617 unsigned BinaryPrecedence
= 30;
621 return LogErrorP("Expected function name in prototype");
623 FnName
= IdentifierStr
;
629 if (!isascii(CurTok
))
630 return LogErrorP("Expected unary operator");
632 FnName
+= (char)CurTok
;
638 if (!isascii(CurTok
))
639 return LogErrorP("Expected binary operator");
641 FnName
+= (char)CurTok
;
645 // Read the precedence if present.
646 if (CurTok
== tok_number
) {
647 if (NumVal
< 1 || NumVal
> 100)
648 return LogErrorP("Invalid precedence: must be 1..100");
649 BinaryPrecedence
= (unsigned)NumVal
;
656 return LogErrorP("Expected '(' in prototype");
658 std::vector
<std::string
> ArgNames
;
659 while (getNextToken() == tok_identifier
)
660 ArgNames
.push_back(IdentifierStr
);
662 return LogErrorP("Expected ')' in prototype");
665 getNextToken(); // eat ')'.
667 // Verify right number of names for operator.
668 if (Kind
&& ArgNames
.size() != Kind
)
669 return LogErrorP("Invalid number of operands for operator");
671 return std::make_unique
<PrototypeAST
>(FnName
, ArgNames
, Kind
!= 0,
675 /// definition ::= 'def' prototype expression
676 static std::unique_ptr
<FunctionAST
> ParseDefinition() {
677 getNextToken(); // eat def.
678 auto Proto
= ParsePrototype();
682 if (auto E
= ParseExpression())
683 return std::make_unique
<FunctionAST
>(std::move(Proto
), std::move(E
));
687 /// toplevelexpr ::= expression
688 static std::unique_ptr
<FunctionAST
> ParseTopLevelExpr() {
689 if (auto E
= ParseExpression()) {
690 // Make an anonymous proto.
691 auto Proto
= std::make_unique
<PrototypeAST
>("__anon_expr",
692 std::vector
<std::string
>());
693 return std::make_unique
<FunctionAST
>(std::move(Proto
), std::move(E
));
698 /// external ::= 'extern' prototype
699 static std::unique_ptr
<PrototypeAST
> ParseExtern() {
700 getNextToken(); // eat extern.
701 return ParsePrototype();
704 //===----------------------------------------------------------------------===//
706 //===----------------------------------------------------------------------===//
708 static std::unique_ptr
<LLVMContext
> TheContext
;
709 static std::unique_ptr
<Module
> TheModule
;
710 static std::unique_ptr
<IRBuilder
<>> Builder
;
711 static std::map
<std::string
, AllocaInst
*> NamedValues
;
712 static std::unique_ptr
<KaleidoscopeJIT
> TheJIT
;
713 static std::unique_ptr
<FunctionPassManager
> TheFPM
;
714 static std::unique_ptr
<LoopAnalysisManager
> TheLAM
;
715 static std::unique_ptr
<FunctionAnalysisManager
> TheFAM
;
716 static std::unique_ptr
<CGSCCAnalysisManager
> TheCGAM
;
717 static std::unique_ptr
<ModuleAnalysisManager
> TheMAM
;
718 static std::unique_ptr
<PassInstrumentationCallbacks
> ThePIC
;
719 static std::unique_ptr
<StandardInstrumentations
> TheSI
;
720 static std::map
<std::string
, std::unique_ptr
<PrototypeAST
>> FunctionProtos
;
721 static ExitOnError ExitOnErr
;
723 Value
*LogErrorV(const char *Str
) {
728 Function
*getFunction(std::string Name
) {
729 // First, see if the function has already been added to the current module.
730 if (auto *F
= TheModule
->getFunction(Name
))
733 // If not, check whether we can codegen the declaration from some existing
735 auto FI
= FunctionProtos
.find(Name
);
736 if (FI
!= FunctionProtos
.end())
737 return FI
->second
->codegen();
739 // If no existing prototype exists, return null.
743 /// CreateEntryBlockAlloca - Create an alloca instruction in the entry block of
744 /// the function. This is used for mutable variables etc.
745 static AllocaInst
*CreateEntryBlockAlloca(Function
*TheFunction
,
747 IRBuilder
<> TmpB(&TheFunction
->getEntryBlock(),
748 TheFunction
->getEntryBlock().begin());
749 return TmpB
.CreateAlloca(Type::getDoubleTy(*TheContext
), nullptr, VarName
);
752 Value
*NumberExprAST::codegen() {
753 return ConstantFP::get(*TheContext
, APFloat(Val
));
756 Value
*VariableExprAST::codegen() {
757 // Look this variable up in the function.
758 AllocaInst
*A
= NamedValues
[Name
];
760 return LogErrorV("Unknown variable name");
763 return Builder
->CreateLoad(A
->getAllocatedType(), A
, Name
.c_str());
766 Value
*UnaryExprAST::codegen() {
767 Value
*OperandV
= Operand
->codegen();
771 Function
*F
= getFunction(std::string("unary") + Opcode
);
773 return LogErrorV("Unknown unary operator");
775 return Builder
->CreateCall(F
, OperandV
, "unop");
778 Value
*BinaryExprAST::codegen() {
779 // Special case '=' because we don't want to emit the LHS as an expression.
781 // Assignment requires the LHS to be an identifier.
782 // This assume we're building without RTTI because LLVM builds that way by
783 // default. If you build LLVM with RTTI this can be changed to a
784 // dynamic_cast for automatic error checking.
785 VariableExprAST
*LHSE
= static_cast<VariableExprAST
*>(LHS
.get());
787 return LogErrorV("destination of '=' must be a variable");
789 Value
*Val
= RHS
->codegen();
794 Value
*Variable
= NamedValues
[LHSE
->getName()];
796 return LogErrorV("Unknown variable name");
798 Builder
->CreateStore(Val
, Variable
);
802 Value
*L
= LHS
->codegen();
803 Value
*R
= RHS
->codegen();
809 return Builder
->CreateFAdd(L
, R
, "addtmp");
811 return Builder
->CreateFSub(L
, R
, "subtmp");
813 return Builder
->CreateFMul(L
, R
, "multmp");
815 L
= Builder
->CreateFCmpULT(L
, R
, "cmptmp");
816 // Convert bool 0/1 to double 0.0 or 1.0
817 return Builder
->CreateUIToFP(L
, Type::getDoubleTy(*TheContext
), "booltmp");
822 // If it wasn't a builtin binary operator, it must be a user defined one. Emit
824 Function
*F
= getFunction(std::string("binary") + Op
);
825 assert(F
&& "binary operator not found!");
827 Value
*Ops
[] = {L
, R
};
828 return Builder
->CreateCall(F
, Ops
, "binop");
831 Value
*CallExprAST::codegen() {
832 // Look up the name in the global module table.
833 Function
*CalleeF
= getFunction(Callee
);
835 return LogErrorV("Unknown function referenced");
837 // If argument mismatch error.
838 if (CalleeF
->arg_size() != Args
.size())
839 return LogErrorV("Incorrect # arguments passed");
841 std::vector
<Value
*> ArgsV
;
842 for (unsigned i
= 0, e
= Args
.size(); i
!= e
; ++i
) {
843 ArgsV
.push_back(Args
[i
]->codegen());
848 return Builder
->CreateCall(CalleeF
, ArgsV
, "calltmp");
851 Value
*IfExprAST::codegen() {
852 Value
*CondV
= Cond
->codegen();
856 // Convert condition to a bool by comparing non-equal to 0.0.
857 CondV
= Builder
->CreateFCmpONE(
858 CondV
, ConstantFP::get(*TheContext
, APFloat(0.0)), "ifcond");
860 Function
*TheFunction
= Builder
->GetInsertBlock()->getParent();
862 // Create blocks for the then and else cases. Insert the 'then' block at the
863 // end of the function.
864 BasicBlock
*ThenBB
= BasicBlock::Create(*TheContext
, "then", TheFunction
);
865 BasicBlock
*ElseBB
= BasicBlock::Create(*TheContext
, "else");
866 BasicBlock
*MergeBB
= BasicBlock::Create(*TheContext
, "ifcont");
868 Builder
->CreateCondBr(CondV
, ThenBB
, ElseBB
);
871 Builder
->SetInsertPoint(ThenBB
);
873 Value
*ThenV
= Then
->codegen();
877 Builder
->CreateBr(MergeBB
);
878 // Codegen of 'Then' can change the current block, update ThenBB for the PHI.
879 ThenBB
= Builder
->GetInsertBlock();
882 TheFunction
->insert(TheFunction
->end(), ElseBB
);
883 Builder
->SetInsertPoint(ElseBB
);
885 Value
*ElseV
= Else
->codegen();
889 Builder
->CreateBr(MergeBB
);
890 // Codegen of 'Else' can change the current block, update ElseBB for the PHI.
891 ElseBB
= Builder
->GetInsertBlock();
894 TheFunction
->insert(TheFunction
->end(), MergeBB
);
895 Builder
->SetInsertPoint(MergeBB
);
896 PHINode
*PN
= Builder
->CreatePHI(Type::getDoubleTy(*TheContext
), 2, "iftmp");
898 PN
->addIncoming(ThenV
, ThenBB
);
899 PN
->addIncoming(ElseV
, ElseBB
);
903 // Output for-loop as:
904 // var = alloca double
907 // store start -> var
918 // nextvar = curvar + step
919 // store nextvar -> var
920 // br endcond, loop, endloop
922 Value
*ForExprAST::codegen() {
923 Function
*TheFunction
= Builder
->GetInsertBlock()->getParent();
925 // Create an alloca for the variable in the entry block.
926 AllocaInst
*Alloca
= CreateEntryBlockAlloca(TheFunction
, VarName
);
928 // Emit the start code first, without 'variable' in scope.
929 Value
*StartVal
= Start
->codegen();
933 // Store the value into the alloca.
934 Builder
->CreateStore(StartVal
, Alloca
);
936 // Make the new basic block for the loop header, inserting after current
938 BasicBlock
*LoopBB
= BasicBlock::Create(*TheContext
, "loop", TheFunction
);
940 // Insert an explicit fall through from the current block to the LoopBB.
941 Builder
->CreateBr(LoopBB
);
943 // Start insertion in LoopBB.
944 Builder
->SetInsertPoint(LoopBB
);
946 // Within the loop, the variable is defined equal to the PHI node. If it
947 // shadows an existing variable, we have to restore it, so save it now.
948 AllocaInst
*OldVal
= NamedValues
[VarName
];
949 NamedValues
[VarName
] = Alloca
;
951 // Emit the body of the loop. This, like any other expr, can change the
952 // current BB. Note that we ignore the value computed by the body, but don't
954 if (!Body
->codegen())
957 // Emit the step value.
958 Value
*StepVal
= nullptr;
960 StepVal
= Step
->codegen();
964 // If not specified, use 1.0.
965 StepVal
= ConstantFP::get(*TheContext
, APFloat(1.0));
968 // Compute the end condition.
969 Value
*EndCond
= End
->codegen();
973 // Reload, increment, and restore the alloca. This handles the case where
974 // the body of the loop mutates the variable.
976 Builder
->CreateLoad(Alloca
->getAllocatedType(), Alloca
, VarName
.c_str());
977 Value
*NextVar
= Builder
->CreateFAdd(CurVar
, StepVal
, "nextvar");
978 Builder
->CreateStore(NextVar
, Alloca
);
980 // Convert condition to a bool by comparing non-equal to 0.0.
981 EndCond
= Builder
->CreateFCmpONE(
982 EndCond
, ConstantFP::get(*TheContext
, APFloat(0.0)), "loopcond");
984 // Create the "after loop" block and insert it.
985 BasicBlock
*AfterBB
=
986 BasicBlock::Create(*TheContext
, "afterloop", TheFunction
);
988 // Insert the conditional branch into the end of LoopEndBB.
989 Builder
->CreateCondBr(EndCond
, LoopBB
, AfterBB
);
991 // Any new code will be inserted in AfterBB.
992 Builder
->SetInsertPoint(AfterBB
);
994 // Restore the unshadowed variable.
996 NamedValues
[VarName
] = OldVal
;
998 NamedValues
.erase(VarName
);
1000 // for expr always returns 0.0.
1001 return Constant::getNullValue(Type::getDoubleTy(*TheContext
));
1004 Value
*VarExprAST::codegen() {
1005 std::vector
<AllocaInst
*> OldBindings
;
1007 Function
*TheFunction
= Builder
->GetInsertBlock()->getParent();
1009 // Register all variables and emit their initializer.
1010 for (unsigned i
= 0, e
= VarNames
.size(); i
!= e
; ++i
) {
1011 const std::string
&VarName
= VarNames
[i
].first
;
1012 ExprAST
*Init
= VarNames
[i
].second
.get();
1014 // Emit the initializer before adding the variable to scope, this prevents
1015 // the initializer from referencing the variable itself, and permits stuff
1018 // var a = a in ... # refers to outer 'a'.
1021 InitVal
= Init
->codegen();
1024 } else { // If not specified, use 0.0.
1025 InitVal
= ConstantFP::get(*TheContext
, APFloat(0.0));
1028 AllocaInst
*Alloca
= CreateEntryBlockAlloca(TheFunction
, VarName
);
1029 Builder
->CreateStore(InitVal
, Alloca
);
1031 // Remember the old variable binding so that we can restore the binding when
1033 OldBindings
.push_back(NamedValues
[VarName
]);
1035 // Remember this binding.
1036 NamedValues
[VarName
] = Alloca
;
1039 // Codegen the body, now that all vars are in scope.
1040 Value
*BodyVal
= Body
->codegen();
1044 // Pop all our variables from scope.
1045 for (unsigned i
= 0, e
= VarNames
.size(); i
!= e
; ++i
)
1046 NamedValues
[VarNames
[i
].first
] = OldBindings
[i
];
1048 // Return the body computation.
1052 Function
*PrototypeAST::codegen() {
1053 // Make the function type: double(double,double) etc.
1054 std::vector
<Type
*> Doubles(Args
.size(), Type::getDoubleTy(*TheContext
));
1056 FunctionType::get(Type::getDoubleTy(*TheContext
), Doubles
, false);
1059 Function::Create(FT
, Function::ExternalLinkage
, Name
, TheModule
.get());
1061 // Set names for all arguments.
1063 for (auto &Arg
: F
->args())
1064 Arg
.setName(Args
[Idx
++]);
1069 Function
*FunctionAST::codegen() {
1070 // Transfer ownership of the prototype to the FunctionProtos map, but keep a
1071 // reference to it for use below.
1073 FunctionProtos
[Proto
->getName()] = std::move(Proto
);
1074 Function
*TheFunction
= getFunction(P
.getName());
1078 // If this is an operator, install it.
1080 BinopPrecedence
[P
.getOperatorName()] = P
.getBinaryPrecedence();
1082 // Create a new basic block to start insertion into.
1083 BasicBlock
*BB
= BasicBlock::Create(*TheContext
, "entry", TheFunction
);
1084 Builder
->SetInsertPoint(BB
);
1086 // Record the function arguments in the NamedValues map.
1087 NamedValues
.clear();
1088 for (auto &Arg
: TheFunction
->args()) {
1089 // Create an alloca for this variable.
1090 AllocaInst
*Alloca
= CreateEntryBlockAlloca(TheFunction
, Arg
.getName());
1092 // Store the initial value into the alloca.
1093 Builder
->CreateStore(&Arg
, Alloca
);
1095 // Add arguments to variable symbol table.
1096 NamedValues
[std::string(Arg
.getName())] = Alloca
;
1099 if (Value
*RetVal
= Body
->codegen()) {
1100 // Finish off the function.
1101 Builder
->CreateRet(RetVal
);
1103 // Validate the generated code, checking for consistency.
1104 verifyFunction(*TheFunction
);
1106 // Run the optimizer on the function.
1107 TheFPM
->run(*TheFunction
, *TheFAM
);
1112 // Error reading body, remove function.
1113 TheFunction
->eraseFromParent();
1116 BinopPrecedence
.erase(P
.getOperatorName());
1120 //===----------------------------------------------------------------------===//
1121 // Top-Level parsing and JIT Driver
1122 //===----------------------------------------------------------------------===//
1124 static void InitializeModuleAndManagers() {
1125 // Open a new context and module.
1126 TheContext
= std::make_unique
<LLVMContext
>();
1127 TheModule
= std::make_unique
<Module
>("KaleidoscopeJIT", *TheContext
);
1128 TheModule
->setDataLayout(TheJIT
->getDataLayout());
1130 // Create a new builder for the module.
1131 Builder
= std::make_unique
<IRBuilder
<>>(*TheContext
);
1133 // Create new pass and analysis managers.
1134 TheFPM
= std::make_unique
<FunctionPassManager
>();
1135 TheLAM
= std::make_unique
<LoopAnalysisManager
>();
1136 TheFAM
= std::make_unique
<FunctionAnalysisManager
>();
1137 TheCGAM
= std::make_unique
<CGSCCAnalysisManager
>();
1138 TheMAM
= std::make_unique
<ModuleAnalysisManager
>();
1139 ThePIC
= std::make_unique
<PassInstrumentationCallbacks
>();
1140 TheSI
= std::make_unique
<StandardInstrumentations
>(*TheContext
,
1141 /*DebugLogging*/ true);
1142 TheSI
->registerCallbacks(*ThePIC
, TheMAM
.get());
1144 // Add transform passes.
1145 // Do simple "peephole" optimizations and bit-twiddling optzns.
1146 TheFPM
->addPass(InstCombinePass());
1147 // Reassociate expressions.
1148 TheFPM
->addPass(ReassociatePass());
1149 // Eliminate Common SubExpressions.
1150 TheFPM
->addPass(GVNPass());
1151 // Simplify the control flow graph (deleting unreachable blocks, etc).
1152 TheFPM
->addPass(SimplifyCFGPass());
1154 // Register analysis passes used in these transform passes.
1156 PB
.registerModuleAnalyses(*TheMAM
);
1157 PB
.registerFunctionAnalyses(*TheFAM
);
1158 PB
.crossRegisterProxies(*TheLAM
, *TheFAM
, *TheCGAM
, *TheMAM
);
1161 static void HandleDefinition() {
1162 if (auto FnAST
= ParseDefinition()) {
1163 if (auto *FnIR
= FnAST
->codegen()) {
1164 fprintf(stderr
, "Read function definition:");
1165 FnIR
->print(errs());
1166 fprintf(stderr
, "\n");
1167 ExitOnErr(TheJIT
->addModule(
1168 ThreadSafeModule(std::move(TheModule
), std::move(TheContext
))));
1169 InitializeModuleAndManagers();
1172 // Skip token for error recovery.
1177 static void HandleExtern() {
1178 if (auto ProtoAST
= ParseExtern()) {
1179 if (auto *FnIR
= ProtoAST
->codegen()) {
1180 fprintf(stderr
, "Read extern: ");
1181 FnIR
->print(errs());
1182 fprintf(stderr
, "\n");
1183 FunctionProtos
[ProtoAST
->getName()] = std::move(ProtoAST
);
1186 // Skip token for error recovery.
1191 static void HandleTopLevelExpression() {
1192 // Evaluate a top-level expression into an anonymous function.
1193 if (auto FnAST
= ParseTopLevelExpr()) {
1194 if (FnAST
->codegen()) {
1195 // Create a ResourceTracker to track JIT'd memory allocated to our
1196 // anonymous expression -- that way we can free it after executing.
1197 auto RT
= TheJIT
->getMainJITDylib().createResourceTracker();
1199 auto TSM
= ThreadSafeModule(std::move(TheModule
), std::move(TheContext
));
1200 ExitOnErr(TheJIT
->addModule(std::move(TSM
), RT
));
1201 InitializeModuleAndManagers();
1203 // Search the JIT for the __anon_expr symbol.
1204 auto ExprSymbol
= ExitOnErr(TheJIT
->lookup("__anon_expr"));
1206 // Get the symbol's address and cast it to the right type (takes no
1207 // arguments, returns a double) so we can call it as a native function.
1208 double (*FP
)() = ExprSymbol
.getAddress().toPtr
<double (*)()>();
1209 fprintf(stderr
, "Evaluated to %f\n", FP());
1211 // Delete the anonymous expression module from the JIT.
1212 ExitOnErr(RT
->remove());
1215 // Skip token for error recovery.
1220 /// top ::= definition | external | expression | ';'
1221 static void MainLoop() {
1223 fprintf(stderr
, "ready> ");
1227 case ';': // ignore top-level semicolons.
1237 HandleTopLevelExpression();
1243 //===----------------------------------------------------------------------===//
1244 // "Library" functions that can be "extern'd" from user code.
1245 //===----------------------------------------------------------------------===//
1248 #define DLLEXPORT __declspec(dllexport)
1253 /// putchard - putchar that takes a double and returns 0.
1254 extern "C" DLLEXPORT
double putchard(double X
) {
1255 fputc((char)X
, stderr
);
1259 /// printd - printf that takes a double prints it as "%f\n", returning 0.
1260 extern "C" DLLEXPORT
double printd(double X
) {
1261 fprintf(stderr
, "%f\n", X
);
1265 //===----------------------------------------------------------------------===//
1266 // Main driver code.
1267 //===----------------------------------------------------------------------===//
1270 InitializeNativeTarget();
1271 InitializeNativeTargetAsmPrinter();
1272 InitializeNativeTargetAsmParser();
1274 // Install standard binary operators.
1275 // 1 is lowest precedence.
1276 BinopPrecedence
['='] = 2;
1277 BinopPrecedence
['<'] = 10;
1278 BinopPrecedence
['+'] = 20;
1279 BinopPrecedence
['-'] = 20;
1280 BinopPrecedence
['*'] = 40; // highest.
1282 // Prime the first token.
1283 fprintf(stderr
, "ready> ");
1286 TheJIT
= ExitOnErr(KaleidoscopeJIT::Create());
1288 InitializeModuleAndManagers();
1290 // Run the main "interpreter loop" now.