1 #include "llvm/ADT/APFloat.h"
2 #include "llvm/ADT/STLExtras.h"
3 #include "llvm/IR/BasicBlock.h"
4 #include "llvm/IR/Constants.h"
5 #include "llvm/IR/DerivedTypes.h"
6 #include "llvm/IR/Function.h"
7 #include "llvm/IR/Instructions.h"
8 #include "llvm/IR/IRBuilder.h"
9 #include "llvm/IR/LLVMContext.h"
10 #include "llvm/IR/Module.h"
11 #include "llvm/IR/Type.h"
12 #include "llvm/IR/Verifier.h"
13 #include "llvm/Support/TargetSelect.h"
14 #include "llvm/Target/TargetMachine.h"
15 #include "KaleidoscopeJIT.h"
29 using namespace llvm::orc
;
31 //===----------------------------------------------------------------------===//
33 //===----------------------------------------------------------------------===//
35 // The lexer returns tokens [0-255] if it is an unknown character, otherwise one
36 // of these for known things.
63 static std::string IdentifierStr
; // Filled in if tok_identifier
64 static double NumVal
; // Filled in if tok_number
66 /// gettok - Return the next token from standard input.
68 static int LastChar
= ' ';
70 // Skip any whitespace.
71 while (isspace(LastChar
))
74 if (isalpha(LastChar
)) { // identifier: [a-zA-Z][a-zA-Z0-9]*
75 IdentifierStr
= LastChar
;
76 while (isalnum((LastChar
= getchar())))
77 IdentifierStr
+= LastChar
;
79 if (IdentifierStr
== "def")
81 if (IdentifierStr
== "extern")
83 if (IdentifierStr
== "if")
85 if (IdentifierStr
== "then")
87 if (IdentifierStr
== "else")
89 if (IdentifierStr
== "for")
91 if (IdentifierStr
== "in")
93 if (IdentifierStr
== "binary")
95 if (IdentifierStr
== "unary")
97 if (IdentifierStr
== "var")
99 return tok_identifier
;
102 if (isdigit(LastChar
) || LastChar
== '.') { // Number: [0-9.]+
106 LastChar
= getchar();
107 } while (isdigit(LastChar
) || LastChar
== '.');
109 NumVal
= strtod(NumStr
.c_str(), nullptr);
113 if (LastChar
== '#') {
114 // Comment until end of line.
116 LastChar
= getchar();
117 while (LastChar
!= EOF
&& LastChar
!= '\n' && LastChar
!= '\r');
123 // Check for end of file. Don't eat the EOF.
127 // Otherwise, just return the character as its ascii value.
128 int ThisChar
= LastChar
;
129 LastChar
= getchar();
133 //===----------------------------------------------------------------------===//
134 // Abstract Syntax Tree (aka Parse Tree)
135 //===----------------------------------------------------------------------===//
139 /// ExprAST - Base class for all expression nodes.
142 virtual ~ExprAST() = default;
144 virtual Value
*codegen() = 0;
147 /// NumberExprAST - Expression class for numeric literals like "1.0".
148 class NumberExprAST
: public ExprAST
{
152 NumberExprAST(double Val
) : Val(Val
) {}
154 Value
*codegen() override
;
157 /// VariableExprAST - Expression class for referencing a variable, like "a".
158 class VariableExprAST
: public ExprAST
{
162 VariableExprAST(const std::string
&Name
) : Name(Name
) {}
164 Value
*codegen() override
;
165 const std::string
&getName() const { return Name
; }
168 /// UnaryExprAST - Expression class for a unary operator.
169 class UnaryExprAST
: public ExprAST
{
171 std::unique_ptr
<ExprAST
> Operand
;
174 UnaryExprAST(char Opcode
, std::unique_ptr
<ExprAST
> Operand
)
175 : Opcode(Opcode
), Operand(std::move(Operand
)) {}
177 Value
*codegen() override
;
180 /// BinaryExprAST - Expression class for a binary operator.
181 class BinaryExprAST
: public ExprAST
{
183 std::unique_ptr
<ExprAST
> LHS
, RHS
;
186 BinaryExprAST(char Op
, std::unique_ptr
<ExprAST
> LHS
,
187 std::unique_ptr
<ExprAST
> RHS
)
188 : Op(Op
), LHS(std::move(LHS
)), RHS(std::move(RHS
)) {}
190 Value
*codegen() override
;
193 /// CallExprAST - Expression class for function calls.
194 class CallExprAST
: public ExprAST
{
196 std::vector
<std::unique_ptr
<ExprAST
>> Args
;
199 CallExprAST(const std::string
&Callee
,
200 std::vector
<std::unique_ptr
<ExprAST
>> Args
)
201 : Callee(Callee
), Args(std::move(Args
)) {}
203 Value
*codegen() override
;
206 /// IfExprAST - Expression class for if/then/else.
207 class IfExprAST
: public ExprAST
{
208 std::unique_ptr
<ExprAST
> Cond
, Then
, Else
;
211 IfExprAST(std::unique_ptr
<ExprAST
> Cond
, std::unique_ptr
<ExprAST
> Then
,
212 std::unique_ptr
<ExprAST
> Else
)
213 : Cond(std::move(Cond
)), Then(std::move(Then
)), Else(std::move(Else
)) {}
215 Value
*codegen() override
;
218 /// ForExprAST - Expression class for for/in.
219 class ForExprAST
: public ExprAST
{
221 std::unique_ptr
<ExprAST
> Start
, End
, Step
, Body
;
224 ForExprAST(const std::string
&VarName
, std::unique_ptr
<ExprAST
> Start
,
225 std::unique_ptr
<ExprAST
> End
, std::unique_ptr
<ExprAST
> Step
,
226 std::unique_ptr
<ExprAST
> Body
)
227 : VarName(VarName
), Start(std::move(Start
)), End(std::move(End
)),
228 Step(std::move(Step
)), Body(std::move(Body
)) {}
230 Value
*codegen() override
;
233 /// VarExprAST - Expression class for var/in
234 class VarExprAST
: public ExprAST
{
235 std::vector
<std::pair
<std::string
, std::unique_ptr
<ExprAST
>>> VarNames
;
236 std::unique_ptr
<ExprAST
> Body
;
240 std::vector
<std::pair
<std::string
, std::unique_ptr
<ExprAST
>>> VarNames
,
241 std::unique_ptr
<ExprAST
> Body
)
242 : VarNames(std::move(VarNames
)), Body(std::move(Body
)) {}
244 Value
*codegen() override
;
247 /// PrototypeAST - This class represents the "prototype" for a function,
248 /// which captures its name, and its argument names (thus implicitly the number
249 /// of arguments the function takes), as well as if it is an operator.
252 std::vector
<std::string
> Args
;
254 unsigned Precedence
; // Precedence if a binary op.
257 PrototypeAST(const std::string
&Name
, std::vector
<std::string
> Args
,
258 bool IsOperator
= false, unsigned Prec
= 0)
259 : Name(Name
), Args(std::move(Args
)), IsOperator(IsOperator
),
263 const std::string
&getName() const { return Name
; }
265 bool isUnaryOp() const { return IsOperator
&& Args
.size() == 1; }
266 bool isBinaryOp() const { return IsOperator
&& Args
.size() == 2; }
268 char getOperatorName() const {
269 assert(isUnaryOp() || isBinaryOp());
270 return Name
[Name
.size() - 1];
273 unsigned getBinaryPrecedence() const { return Precedence
; }
276 /// FunctionAST - This class represents a function definition itself.
278 std::unique_ptr
<PrototypeAST
> Proto
;
279 std::unique_ptr
<ExprAST
> Body
;
282 FunctionAST(std::unique_ptr
<PrototypeAST
> Proto
,
283 std::unique_ptr
<ExprAST
> Body
)
284 : Proto(std::move(Proto
)), Body(std::move(Body
)) {}
289 } // end anonymous namespace
291 //===----------------------------------------------------------------------===//
293 //===----------------------------------------------------------------------===//
295 /// CurTok/getNextToken - Provide a simple token buffer. CurTok is the current
296 /// token the parser is looking at. getNextToken reads another token from the
297 /// lexer and updates CurTok with its results.
299 static int getNextToken() { return CurTok
= gettok(); }
301 /// BinopPrecedence - This holds the precedence for each binary operator that is
303 static std::map
<char, int> BinopPrecedence
;
305 /// GetTokPrecedence - Get the precedence of the pending binary operator token.
306 static int GetTokPrecedence() {
307 if (!isascii(CurTok
))
310 // Make sure it's a declared binop.
311 int TokPrec
= BinopPrecedence
[CurTok
];
317 /// LogError* - These are little helper functions for error handling.
318 std::unique_ptr
<ExprAST
> LogError(const char *Str
) {
319 fprintf(stderr
, "Error: %s\n", Str
);
323 std::unique_ptr
<PrototypeAST
> LogErrorP(const char *Str
) {
328 static std::unique_ptr
<ExprAST
> ParseExpression();
330 /// numberexpr ::= number
331 static std::unique_ptr
<ExprAST
> ParseNumberExpr() {
332 auto Result
= std::make_unique
<NumberExprAST
>(NumVal
);
333 getNextToken(); // consume the number
334 return std::move(Result
);
337 /// parenexpr ::= '(' expression ')'
338 static std::unique_ptr
<ExprAST
> ParseParenExpr() {
339 getNextToken(); // eat (.
340 auto V
= ParseExpression();
345 return LogError("expected ')'");
346 getNextToken(); // eat ).
352 /// ::= identifier '(' expression* ')'
353 static std::unique_ptr
<ExprAST
> ParseIdentifierExpr() {
354 std::string IdName
= IdentifierStr
;
356 getNextToken(); // eat identifier.
358 if (CurTok
!= '(') // Simple variable ref.
359 return std::make_unique
<VariableExprAST
>(IdName
);
362 getNextToken(); // eat (
363 std::vector
<std::unique_ptr
<ExprAST
>> Args
;
366 if (auto Arg
= ParseExpression())
367 Args
.push_back(std::move(Arg
));
375 return LogError("Expected ')' or ',' in argument list");
383 return std::make_unique
<CallExprAST
>(IdName
, std::move(Args
));
386 /// ifexpr ::= 'if' expression 'then' expression 'else' expression
387 static std::unique_ptr
<ExprAST
> ParseIfExpr() {
388 getNextToken(); // eat the if.
391 auto Cond
= ParseExpression();
395 if (CurTok
!= tok_then
)
396 return LogError("expected then");
397 getNextToken(); // eat the then
399 auto Then
= ParseExpression();
403 if (CurTok
!= tok_else
)
404 return LogError("expected else");
408 auto Else
= ParseExpression();
412 return std::make_unique
<IfExprAST
>(std::move(Cond
), std::move(Then
),
416 /// forexpr ::= 'for' identifier '=' expr ',' expr (',' expr)? 'in' expression
417 static std::unique_ptr
<ExprAST
> ParseForExpr() {
418 getNextToken(); // eat the for.
420 if (CurTok
!= tok_identifier
)
421 return LogError("expected identifier after for");
423 std::string IdName
= IdentifierStr
;
424 getNextToken(); // eat identifier.
427 return LogError("expected '=' after for");
428 getNextToken(); // eat '='.
430 auto Start
= ParseExpression();
434 return LogError("expected ',' after for start value");
437 auto End
= ParseExpression();
441 // The step value is optional.
442 std::unique_ptr
<ExprAST
> Step
;
445 Step
= ParseExpression();
450 if (CurTok
!= tok_in
)
451 return LogError("expected 'in' after for");
452 getNextToken(); // eat 'in'.
454 auto Body
= ParseExpression();
458 return std::make_unique
<ForExprAST
>(IdName
, std::move(Start
), std::move(End
),
459 std::move(Step
), std::move(Body
));
462 /// varexpr ::= 'var' identifier ('=' expression)?
463 // (',' identifier ('=' expression)?)* 'in' expression
464 static std::unique_ptr
<ExprAST
> ParseVarExpr() {
465 getNextToken(); // eat the var.
467 std::vector
<std::pair
<std::string
, std::unique_ptr
<ExprAST
>>> VarNames
;
469 // At least one variable name is required.
470 if (CurTok
!= tok_identifier
)
471 return LogError("expected identifier after var");
474 std::string Name
= IdentifierStr
;
475 getNextToken(); // eat identifier.
477 // Read the optional initializer.
478 std::unique_ptr
<ExprAST
> Init
= nullptr;
480 getNextToken(); // eat the '='.
482 Init
= ParseExpression();
487 VarNames
.push_back(std::make_pair(Name
, std::move(Init
)));
489 // End of var list, exit loop.
492 getNextToken(); // eat the ','.
494 if (CurTok
!= tok_identifier
)
495 return LogError("expected identifier list after var");
498 // At this point, we have to have 'in'.
499 if (CurTok
!= tok_in
)
500 return LogError("expected 'in' keyword after 'var'");
501 getNextToken(); // eat 'in'.
503 auto Body
= ParseExpression();
507 return std::make_unique
<VarExprAST
>(std::move(VarNames
), std::move(Body
));
511 /// ::= identifierexpr
517 static std::unique_ptr
<ExprAST
> ParsePrimary() {
520 return LogError("unknown token when expecting an expression");
522 return ParseIdentifierExpr();
524 return ParseNumberExpr();
526 return ParseParenExpr();
528 return ParseIfExpr();
530 return ParseForExpr();
532 return ParseVarExpr();
539 static std::unique_ptr
<ExprAST
> ParseUnary() {
540 // If the current token is not an operator, it must be a primary expr.
541 if (!isascii(CurTok
) || CurTok
== '(' || CurTok
== ',')
542 return ParsePrimary();
544 // If this is a unary operator, read it.
547 if (auto Operand
= ParseUnary())
548 return std::make_unique
<UnaryExprAST
>(Opc
, std::move(Operand
));
554 static std::unique_ptr
<ExprAST
> ParseBinOpRHS(int ExprPrec
,
555 std::unique_ptr
<ExprAST
> LHS
) {
556 // If this is a binop, find its precedence.
558 int TokPrec
= GetTokPrecedence();
560 // If this is a binop that binds at least as tightly as the current binop,
561 // consume it, otherwise we are done.
562 if (TokPrec
< ExprPrec
)
565 // Okay, we know this is a binop.
567 getNextToken(); // eat binop
569 // Parse the unary expression after the binary operator.
570 auto RHS
= ParseUnary();
574 // If BinOp binds less tightly with RHS than the operator after RHS, let
575 // the pending operator take RHS as its LHS.
576 int NextPrec
= GetTokPrecedence();
577 if (TokPrec
< NextPrec
) {
578 RHS
= ParseBinOpRHS(TokPrec
+ 1, std::move(RHS
));
585 std::make_unique
<BinaryExprAST
>(BinOp
, std::move(LHS
), std::move(RHS
));
590 /// ::= unary binoprhs
592 static std::unique_ptr
<ExprAST
> ParseExpression() {
593 auto LHS
= ParseUnary();
597 return ParseBinOpRHS(0, std::move(LHS
));
601 /// ::= id '(' id* ')'
602 /// ::= binary LETTER number? (id, id)
603 /// ::= unary LETTER (id)
604 static std::unique_ptr
<PrototypeAST
> ParsePrototype() {
607 unsigned Kind
= 0; // 0 = identifier, 1 = unary, 2 = binary.
608 unsigned BinaryPrecedence
= 30;
612 return LogErrorP("Expected function name in prototype");
614 FnName
= IdentifierStr
;
620 if (!isascii(CurTok
))
621 return LogErrorP("Expected unary operator");
623 FnName
+= (char)CurTok
;
629 if (!isascii(CurTok
))
630 return LogErrorP("Expected binary operator");
632 FnName
+= (char)CurTok
;
636 // Read the precedence if present.
637 if (CurTok
== tok_number
) {
638 if (NumVal
< 1 || NumVal
> 100)
639 return LogErrorP("Invalid precedecnce: must be 1..100");
640 BinaryPrecedence
= (unsigned)NumVal
;
647 return LogErrorP("Expected '(' in prototype");
649 std::vector
<std::string
> ArgNames
;
650 while (getNextToken() == tok_identifier
)
651 ArgNames
.push_back(IdentifierStr
);
653 return LogErrorP("Expected ')' in prototype");
656 getNextToken(); // eat ')'.
658 // Verify right number of names for operator.
659 if (Kind
&& ArgNames
.size() != Kind
)
660 return LogErrorP("Invalid number of operands for operator");
662 return std::make_unique
<PrototypeAST
>(FnName
, ArgNames
, Kind
!= 0,
666 /// definition ::= 'def' prototype expression
667 static std::unique_ptr
<FunctionAST
> ParseDefinition() {
668 getNextToken(); // eat def.
669 auto Proto
= ParsePrototype();
673 if (auto E
= ParseExpression())
674 return std::make_unique
<FunctionAST
>(std::move(Proto
), std::move(E
));
678 /// toplevelexpr ::= expression
679 static std::unique_ptr
<FunctionAST
> ParseTopLevelExpr(unsigned ExprCount
) {
680 if (auto E
= ParseExpression()) {
681 // Make an anonymous proto.
682 auto Proto
= std::make_unique
<PrototypeAST
>(
683 ("__anon_expr" + Twine(ExprCount
)).str(), std::vector
<std::string
>());
684 return std::make_unique
<FunctionAST
>(std::move(Proto
), std::move(E
));
689 /// external ::= 'extern' prototype
690 static std::unique_ptr
<PrototypeAST
> ParseExtern() {
691 getNextToken(); // eat extern.
692 return ParsePrototype();
695 //===----------------------------------------------------------------------===//
697 //===----------------------------------------------------------------------===//
699 static std::unique_ptr
<KaleidoscopeJIT
> TheJIT
;
700 static LLVMContext
*TheContext
;
701 static std::unique_ptr
<IRBuilder
<>> Builder
;
702 static std::unique_ptr
<Module
> TheModule
;
703 static std::map
<std::string
, AllocaInst
*> NamedValues
;
704 static std::map
<std::string
, std::unique_ptr
<PrototypeAST
>> FunctionProtos
;
705 static ExitOnError ExitOnErr
;
707 Value
*LogErrorV(const char *Str
) {
712 Function
*getFunction(std::string Name
) {
713 // First, see if the function has already been added to the current module.
714 if (auto *F
= TheModule
->getFunction(Name
))
717 // If not, check whether we can codegen the declaration from some existing
719 auto FI
= FunctionProtos
.find(Name
);
720 if (FI
!= FunctionProtos
.end())
721 return FI
->second
->codegen();
723 // If no existing prototype exists, return null.
727 /// CreateEntryBlockAlloca - Create an alloca instruction in the entry block of
728 /// the function. This is used for mutable variables etc.
729 static AllocaInst
*CreateEntryBlockAlloca(Function
*TheFunction
,
730 const std::string
&VarName
) {
731 IRBuilder
<> TmpB(&TheFunction
->getEntryBlock(),
732 TheFunction
->getEntryBlock().begin());
733 return TmpB
.CreateAlloca(Type::getDoubleTy(*TheContext
), nullptr, VarName
);
736 Value
*NumberExprAST::codegen() {
737 return ConstantFP::get(*TheContext
, APFloat(Val
));
740 Value
*VariableExprAST::codegen() {
741 // Look this variable up in the function.
742 Value
*V
= NamedValues
[Name
];
744 return LogErrorV("Unknown variable name");
747 return Builder
->CreateLoad(V
, Name
.c_str());
750 Value
*UnaryExprAST::codegen() {
751 Value
*OperandV
= Operand
->codegen();
755 Function
*F
= getFunction(std::string("unary") + Opcode
);
757 return LogErrorV("Unknown unary operator");
759 return Builder
->CreateCall(F
, OperandV
, "unop");
762 Value
*BinaryExprAST::codegen() {
763 // Special case '=' because we don't want to emit the LHS as an expression.
765 // Assignment requires the LHS to be an identifier.
766 // This assume we're building without RTTI because LLVM builds that way by
767 // default. If you build LLVM with RTTI this can be changed to a
768 // dynamic_cast for automatic error checking.
769 VariableExprAST
*LHSE
= static_cast<VariableExprAST
*>(LHS
.get());
771 return LogErrorV("destination of '=' must be a variable");
773 Value
*Val
= RHS
->codegen();
778 Value
*Variable
= NamedValues
[LHSE
->getName()];
780 return LogErrorV("Unknown variable name");
782 Builder
->CreateStore(Val
, Variable
);
786 Value
*L
= LHS
->codegen();
787 Value
*R
= RHS
->codegen();
793 return Builder
->CreateFAdd(L
, R
, "addtmp");
795 return Builder
->CreateFSub(L
, R
, "subtmp");
797 return Builder
->CreateFMul(L
, R
, "multmp");
799 L
= Builder
->CreateFCmpULT(L
, R
, "cmptmp");
800 // Convert bool 0/1 to double 0.0 or 1.0
801 return Builder
->CreateUIToFP(L
, Type::getDoubleTy(*TheContext
), "booltmp");
806 // If it wasn't a builtin binary operator, it must be a user defined one. Emit
808 Function
*F
= getFunction(std::string("binary") + Op
);
809 assert(F
&& "binary operator not found!");
811 Value
*Ops
[] = {L
, R
};
812 return Builder
->CreateCall(F
, Ops
, "binop");
815 Value
*CallExprAST::codegen() {
816 // Look up the name in the global module table.
817 Function
*CalleeF
= getFunction(Callee
);
819 return LogErrorV("Unknown function referenced");
821 // If argument mismatch error.
822 if (CalleeF
->arg_size() != Args
.size())
823 return LogErrorV("Incorrect # arguments passed");
825 std::vector
<Value
*> ArgsV
;
826 for (unsigned i
= 0, e
= Args
.size(); i
!= e
; ++i
) {
827 ArgsV
.push_back(Args
[i
]->codegen());
832 return Builder
->CreateCall(CalleeF
, ArgsV
, "calltmp");
835 Value
*IfExprAST::codegen() {
836 Value
*CondV
= Cond
->codegen();
840 // Convert condition to a bool by comparing equal to 0.0.
841 CondV
= Builder
->CreateFCmpONE(
842 CondV
, ConstantFP::get(*TheContext
, APFloat(0.0)), "ifcond");
844 Function
*TheFunction
= Builder
->GetInsertBlock()->getParent();
846 // Create blocks for the then and else cases. Insert the 'then' block at the
847 // end of the function.
848 BasicBlock
*ThenBB
= BasicBlock::Create(*TheContext
, "then", TheFunction
);
849 BasicBlock
*ElseBB
= BasicBlock::Create(*TheContext
, "else");
850 BasicBlock
*MergeBB
= BasicBlock::Create(*TheContext
, "ifcont");
852 Builder
->CreateCondBr(CondV
, ThenBB
, ElseBB
);
855 Builder
->SetInsertPoint(ThenBB
);
857 Value
*ThenV
= Then
->codegen();
861 Builder
->CreateBr(MergeBB
);
862 // Codegen of 'Then' can change the current block, update ThenBB for the PHI.
863 ThenBB
= Builder
->GetInsertBlock();
866 TheFunction
->getBasicBlockList().push_back(ElseBB
);
867 Builder
->SetInsertPoint(ElseBB
);
869 Value
*ElseV
= Else
->codegen();
873 Builder
->CreateBr(MergeBB
);
874 // Codegen of 'Else' can change the current block, update ElseBB for the PHI.
875 ElseBB
= Builder
->GetInsertBlock();
878 TheFunction
->getBasicBlockList().push_back(MergeBB
);
879 Builder
->SetInsertPoint(MergeBB
);
880 PHINode
*PN
= Builder
->CreatePHI(Type::getDoubleTy(*TheContext
), 2, "iftmp");
882 PN
->addIncoming(ThenV
, ThenBB
);
883 PN
->addIncoming(ElseV
, ElseBB
);
887 // Output for-loop as:
888 // var = alloca double
891 // store start -> var
902 // nextvar = curvar + step
903 // store nextvar -> var
904 // br endcond, loop, endloop
906 Value
*ForExprAST::codegen() {
907 Function
*TheFunction
= Builder
->GetInsertBlock()->getParent();
909 // Create an alloca for the variable in the entry block.
910 AllocaInst
*Alloca
= CreateEntryBlockAlloca(TheFunction
, VarName
);
912 // Emit the start code first, without 'variable' in scope.
913 Value
*StartVal
= Start
->codegen();
917 // Store the value into the alloca.
918 Builder
->CreateStore(StartVal
, Alloca
);
920 // Make the new basic block for the loop header, inserting after current
922 BasicBlock
*LoopBB
= BasicBlock::Create(*TheContext
, "loop", TheFunction
);
924 // Insert an explicit fall through from the current block to the LoopBB.
925 Builder
->CreateBr(LoopBB
);
927 // Start insertion in LoopBB.
928 Builder
->SetInsertPoint(LoopBB
);
930 // Within the loop, the variable is defined equal to the PHI node. If it
931 // shadows an existing variable, we have to restore it, so save it now.
932 AllocaInst
*OldVal
= NamedValues
[VarName
];
933 NamedValues
[VarName
] = Alloca
;
935 // Emit the body of the loop. This, like any other expr, can change the
936 // current BB. Note that we ignore the value computed by the body, but don't
938 if (!Body
->codegen())
941 // Emit the step value.
942 Value
*StepVal
= nullptr;
944 StepVal
= Step
->codegen();
948 // If not specified, use 1.0.
949 StepVal
= ConstantFP::get(*TheContext
, APFloat(1.0));
952 // Compute the end condition.
953 Value
*EndCond
= End
->codegen();
957 // Reload, increment, and restore the alloca. This handles the case where
958 // the body of the loop mutates the variable.
959 Value
*CurVar
= Builder
->CreateLoad(Alloca
, VarName
.c_str());
960 Value
*NextVar
= Builder
->CreateFAdd(CurVar
, StepVal
, "nextvar");
961 Builder
->CreateStore(NextVar
, Alloca
);
963 // Convert condition to a bool by comparing equal to 0.0.
964 EndCond
= Builder
->CreateFCmpONE(
965 EndCond
, ConstantFP::get(*TheContext
, APFloat(0.0)), "loopcond");
967 // Create the "after loop" block and insert it.
968 BasicBlock
*AfterBB
=
969 BasicBlock::Create(*TheContext
, "afterloop", TheFunction
);
971 // Insert the conditional branch into the end of LoopEndBB.
972 Builder
->CreateCondBr(EndCond
, LoopBB
, AfterBB
);
974 // Any new code will be inserted in AfterBB.
975 Builder
->SetInsertPoint(AfterBB
);
977 // Restore the unshadowed variable.
979 NamedValues
[VarName
] = OldVal
;
981 NamedValues
.erase(VarName
);
983 // for expr always returns 0.0.
984 return Constant::getNullValue(Type::getDoubleTy(*TheContext
));
987 Value
*VarExprAST::codegen() {
988 std::vector
<AllocaInst
*> OldBindings
;
990 Function
*TheFunction
= Builder
->GetInsertBlock()->getParent();
992 // Register all variables and emit their initializer.
993 for (unsigned i
= 0, e
= VarNames
.size(); i
!= e
; ++i
) {
994 const std::string
&VarName
= VarNames
[i
].first
;
995 ExprAST
*Init
= VarNames
[i
].second
.get();
997 // Emit the initializer before adding the variable to scope, this prevents
998 // the initializer from referencing the variable itself, and permits stuff
1001 // var a = a in ... # refers to outer 'a'.
1004 InitVal
= Init
->codegen();
1007 } else { // If not specified, use 0.0.
1008 InitVal
= ConstantFP::get(*TheContext
, APFloat(0.0));
1011 AllocaInst
*Alloca
= CreateEntryBlockAlloca(TheFunction
, VarName
);
1012 Builder
->CreateStore(InitVal
, Alloca
);
1014 // Remember the old variable binding so that we can restore the binding when
1016 OldBindings
.push_back(NamedValues
[VarName
]);
1018 // Remember this binding.
1019 NamedValues
[VarName
] = Alloca
;
1022 // Codegen the body, now that all vars are in scope.
1023 Value
*BodyVal
= Body
->codegen();
1027 // Pop all our variables from scope.
1028 for (unsigned i
= 0, e
= VarNames
.size(); i
!= e
; ++i
)
1029 NamedValues
[VarNames
[i
].first
] = OldBindings
[i
];
1031 // Return the body computation.
1035 Function
*PrototypeAST::codegen() {
1036 // Make the function type: double(double,double) etc.
1037 std::vector
<Type
*> Doubles(Args
.size(), Type::getDoubleTy(*TheContext
));
1039 FunctionType::get(Type::getDoubleTy(*TheContext
), Doubles
, false);
1042 Function::Create(FT
, Function::ExternalLinkage
, Name
, TheModule
.get());
1044 // Set names for all arguments.
1046 for (auto &Arg
: F
->args())
1047 Arg
.setName(Args
[Idx
++]);
1052 Function
*FunctionAST::codegen() {
1053 // Transfer ownership of the prototype to the FunctionProtos map, but keep a
1054 // reference to it for use below.
1056 FunctionProtos
[Proto
->getName()] = std::move(Proto
);
1057 Function
*TheFunction
= getFunction(P
.getName());
1061 // If this is an operator, install it.
1063 BinopPrecedence
[P
.getOperatorName()] = P
.getBinaryPrecedence();
1065 // Create a new basic block to start insertion into.
1066 BasicBlock
*BB
= BasicBlock::Create(*TheContext
, "entry", TheFunction
);
1067 Builder
->SetInsertPoint(BB
);
1069 // Record the function arguments in the NamedValues map.
1070 NamedValues
.clear();
1071 for (auto &Arg
: TheFunction
->args()) {
1072 // Create an alloca for this variable.
1073 AllocaInst
*Alloca
= CreateEntryBlockAlloca(TheFunction
, Arg
.getName());
1075 // Store the initial value into the alloca.
1076 Builder
->CreateStore(&Arg
, Alloca
);
1078 // Add arguments to variable symbol table.
1079 NamedValues
[Arg
.getName()] = Alloca
;
1082 if (Value
*RetVal
= Body
->codegen()) {
1083 // Finish off the function.
1084 Builder
->CreateRet(RetVal
);
1086 // Validate the generated code, checking for consistency.
1087 verifyFunction(*TheFunction
);
1092 // Error reading body, remove function.
1093 TheFunction
->eraseFromParent();
1096 BinopPrecedence
.erase(P
.getOperatorName());
1100 //===----------------------------------------------------------------------===//
1101 // Top-Level parsing and JIT Driver
1102 //===----------------------------------------------------------------------===//
1104 static void InitializeModule() {
1105 // Open a new module.
1106 TheModule
= std::make_unique
<Module
>("my cool jit", *TheContext
);
1107 TheModule
->setDataLayout(TheJIT
->getDataLayout());
1109 // Create a new builder for the module.
1110 Builder
= std::make_unique
<IRBuilder
<>>(*TheContext
);
1113 static void HandleDefinition() {
1114 if (auto FnAST
= ParseDefinition()) {
1115 if (auto *FnIR
= FnAST
->codegen()) {
1116 fprintf(stderr
, "Read function definition:");
1117 FnIR
->print(errs());
1118 fprintf(stderr
, "\n");
1119 ExitOnErr(TheJIT
->addModule(std::move(TheModule
)));
1123 // Skip token for error recovery.
1128 static void HandleExtern() {
1129 if (auto ProtoAST
= ParseExtern()) {
1130 if (auto *FnIR
= ProtoAST
->codegen()) {
1131 fprintf(stderr
, "Read extern: ");
1132 FnIR
->print(errs());
1133 fprintf(stderr
, "\n");
1134 FunctionProtos
[ProtoAST
->getName()] = std::move(ProtoAST
);
1137 // Skip token for error recovery.
1142 static void HandleTopLevelExpression() {
1143 static unsigned ExprCount
= 0;
1145 // Update ExprCount. This number will be added to anonymous expressions to
1146 // prevent them from clashing.
1149 // Evaluate a top-level expression into an anonymous function.
1150 if (auto FnAST
= ParseTopLevelExpr(ExprCount
)) {
1151 if (FnAST
->codegen()) {
1152 // JIT the module containing the anonymous expression, keeping a handle so
1153 // we can free it later.
1154 ExitOnErr(TheJIT
->addModule(std::move(TheModule
)));
1157 // Get the anonymous expression's JITSymbol.
1159 ExitOnErr(TheJIT
->lookup(("__anon_expr" + Twine(ExprCount
)).str()));
1161 auto *FP
= (double (*)())(intptr_t)Sym
.getAddress();
1162 assert(FP
&& "Failed to codegen function");
1163 fprintf(stderr
, "Evaluated to %f\n", FP());
1166 // Skip token for error recovery.
1171 /// top ::= definition | external | expression | ';'
1172 static void MainLoop() {
1174 fprintf(stderr
, "ready> ");
1178 case ';': // ignore top-level semicolons.
1188 HandleTopLevelExpression();
1194 //===----------------------------------------------------------------------===//
1195 // "Library" functions that can be "extern'd" from user code.
1196 //===----------------------------------------------------------------------===//
1198 /// putchard - putchar that takes a double and returns 0.
1199 extern "C" double putchard(double X
) {
1200 fputc((char)X
, stderr
);
1204 /// printd - printf that takes a double prints it as "%f\n", returning 0.
1205 extern "C" double printd(double X
) {
1206 fprintf(stderr
, "%f\n", X
);
1210 //===----------------------------------------------------------------------===//
1211 // Main driver code.
1212 //===----------------------------------------------------------------------===//
1215 InitializeNativeTarget();
1216 InitializeNativeTargetAsmPrinter();
1217 InitializeNativeTargetAsmParser();
1219 // Install standard binary operators.
1220 // 1 is lowest precedence.
1221 BinopPrecedence
['='] = 2;
1222 BinopPrecedence
['<'] = 10;
1223 BinopPrecedence
['+'] = 20;
1224 BinopPrecedence
['-'] = 20;
1225 BinopPrecedence
['*'] = 40; // highest.
1227 // Prime the first token.
1228 fprintf(stderr
, "ready> ");
1231 TheJIT
= ExitOnErr(KaleidoscopeJIT::Create());
1232 TheContext
= &TheJIT
->getContext();
1236 // Run the main "interpreter loop" now.