1 #include "llvm/ADT/STLExtras.h"
2 #include "llvm/Analysis/BasicAliasAnalysis.h"
3 #include "llvm/Analysis/Passes.h"
4 #include "llvm/IR/DIBuilder.h"
5 #include "llvm/IR/IRBuilder.h"
6 #include "llvm/IR/LLVMContext.h"
7 #include "llvm/IR/LegacyPassManager.h"
8 #include "llvm/IR/Module.h"
9 #include "llvm/IR/Verifier.h"
10 #include "llvm/Support/TargetSelect.h"
11 #include "llvm/Transforms/Scalar.h"
17 #include "../include/KaleidoscopeJIT.h"
20 using namespace llvm::orc
;
22 //===----------------------------------------------------------------------===//
24 //===----------------------------------------------------------------------===//
26 // The lexer returns tokens [0-255] if it is an unknown character, otherwise one
27 // of these for known things.
54 std::string
getTokName(int Tok
) {
83 return std::string(1, (char)Tok
);
90 static LLVMContext TheContext
;
91 static IRBuilder
<> Builder(TheContext
);
95 std::vector
<DIScope
*> LexicalBlocks
;
97 void emitLocation(ExprAST
*AST
);
98 DIType
*getDoubleTy();
101 struct SourceLocation
{
105 static SourceLocation CurLoc
;
106 static SourceLocation LexLoc
= {1, 0};
108 static int advance() {
109 int LastChar
= getchar();
111 if (LastChar
== '\n' || LastChar
== '\r') {
119 static std::string IdentifierStr
; // Filled in if tok_identifier
120 static double NumVal
; // Filled in if tok_number
122 /// gettok - Return the next token from standard input.
123 static int gettok() {
124 static int LastChar
= ' ';
126 // Skip any whitespace.
127 while (isspace(LastChar
))
128 LastChar
= advance();
132 if (isalpha(LastChar
)) { // identifier: [a-zA-Z][a-zA-Z0-9]*
133 IdentifierStr
= LastChar
;
134 while (isalnum((LastChar
= advance())))
135 IdentifierStr
+= LastChar
;
137 if (IdentifierStr
== "def")
139 if (IdentifierStr
== "extern")
141 if (IdentifierStr
== "if")
143 if (IdentifierStr
== "then")
145 if (IdentifierStr
== "else")
147 if (IdentifierStr
== "for")
149 if (IdentifierStr
== "in")
151 if (IdentifierStr
== "binary")
153 if (IdentifierStr
== "unary")
155 if (IdentifierStr
== "var")
157 return tok_identifier
;
160 if (isdigit(LastChar
) || LastChar
== '.') { // Number: [0-9.]+
164 LastChar
= advance();
165 } while (isdigit(LastChar
) || LastChar
== '.');
167 NumVal
= strtod(NumStr
.c_str(), nullptr);
171 if (LastChar
== '#') {
172 // Comment until end of line.
174 LastChar
= advance();
175 while (LastChar
!= EOF
&& LastChar
!= '\n' && LastChar
!= '\r');
181 // Check for end of file. Don't eat the EOF.
185 // Otherwise, just return the character as its ascii value.
186 int ThisChar
= LastChar
;
187 LastChar
= advance();
191 //===----------------------------------------------------------------------===//
192 // Abstract Syntax Tree (aka Parse Tree)
193 //===----------------------------------------------------------------------===//
196 raw_ostream
&indent(raw_ostream
&O
, int size
) {
197 return O
<< std::string(size
, ' ');
200 /// ExprAST - Base class for all expression nodes.
205 ExprAST(SourceLocation Loc
= CurLoc
) : Loc(Loc
) {}
206 virtual ~ExprAST() {}
207 virtual Value
*codegen() = 0;
208 int getLine() const { return Loc
.Line
; }
209 int getCol() const { return Loc
.Col
; }
210 virtual raw_ostream
&dump(raw_ostream
&out
, int ind
) {
211 return out
<< ':' << getLine() << ':' << getCol() << '\n';
215 /// NumberExprAST - Expression class for numeric literals like "1.0".
216 class NumberExprAST
: public ExprAST
{
220 NumberExprAST(double Val
) : Val(Val
) {}
221 raw_ostream
&dump(raw_ostream
&out
, int ind
) override
{
222 return ExprAST::dump(out
<< Val
, ind
);
224 Value
*codegen() override
;
227 /// VariableExprAST - Expression class for referencing a variable, like "a".
228 class VariableExprAST
: public ExprAST
{
232 VariableExprAST(SourceLocation Loc
, const std::string
&Name
)
233 : ExprAST(Loc
), Name(Name
) {}
234 const std::string
&getName() const { return Name
; }
235 Value
*codegen() override
;
236 raw_ostream
&dump(raw_ostream
&out
, int ind
) override
{
237 return ExprAST::dump(out
<< Name
, ind
);
241 /// UnaryExprAST - Expression class for a unary operator.
242 class UnaryExprAST
: public ExprAST
{
244 std::unique_ptr
<ExprAST
> Operand
;
247 UnaryExprAST(char Opcode
, std::unique_ptr
<ExprAST
> Operand
)
248 : Opcode(Opcode
), Operand(std::move(Operand
)) {}
249 Value
*codegen() override
;
250 raw_ostream
&dump(raw_ostream
&out
, int ind
) override
{
251 ExprAST::dump(out
<< "unary" << Opcode
, ind
);
252 Operand
->dump(out
, ind
+ 1);
257 /// BinaryExprAST - Expression class for a binary operator.
258 class BinaryExprAST
: public ExprAST
{
260 std::unique_ptr
<ExprAST
> LHS
, RHS
;
263 BinaryExprAST(SourceLocation Loc
, char Op
, std::unique_ptr
<ExprAST
> LHS
,
264 std::unique_ptr
<ExprAST
> RHS
)
265 : ExprAST(Loc
), Op(Op
), LHS(std::move(LHS
)), RHS(std::move(RHS
)) {}
266 Value
*codegen() override
;
267 raw_ostream
&dump(raw_ostream
&out
, int ind
) override
{
268 ExprAST::dump(out
<< "binary" << Op
, ind
);
269 LHS
->dump(indent(out
, ind
) << "LHS:", ind
+ 1);
270 RHS
->dump(indent(out
, ind
) << "RHS:", ind
+ 1);
275 /// CallExprAST - Expression class for function calls.
276 class CallExprAST
: public ExprAST
{
278 std::vector
<std::unique_ptr
<ExprAST
>> Args
;
281 CallExprAST(SourceLocation Loc
, const std::string
&Callee
,
282 std::vector
<std::unique_ptr
<ExprAST
>> Args
)
283 : ExprAST(Loc
), Callee(Callee
), Args(std::move(Args
)) {}
284 Value
*codegen() override
;
285 raw_ostream
&dump(raw_ostream
&out
, int ind
) override
{
286 ExprAST::dump(out
<< "call " << Callee
, ind
);
287 for (const auto &Arg
: Args
)
288 Arg
->dump(indent(out
, ind
+ 1), ind
+ 1);
293 /// IfExprAST - Expression class for if/then/else.
294 class IfExprAST
: public ExprAST
{
295 std::unique_ptr
<ExprAST
> Cond
, Then
, Else
;
298 IfExprAST(SourceLocation Loc
, std::unique_ptr
<ExprAST
> Cond
,
299 std::unique_ptr
<ExprAST
> Then
, std::unique_ptr
<ExprAST
> Else
)
300 : ExprAST(Loc
), Cond(std::move(Cond
)), Then(std::move(Then
)),
301 Else(std::move(Else
)) {}
302 Value
*codegen() override
;
303 raw_ostream
&dump(raw_ostream
&out
, int ind
) override
{
304 ExprAST::dump(out
<< "if", ind
);
305 Cond
->dump(indent(out
, ind
) << "Cond:", ind
+ 1);
306 Then
->dump(indent(out
, ind
) << "Then:", ind
+ 1);
307 Else
->dump(indent(out
, ind
) << "Else:", ind
+ 1);
312 /// ForExprAST - Expression class for for/in.
313 class ForExprAST
: public ExprAST
{
315 std::unique_ptr
<ExprAST
> Start
, End
, Step
, Body
;
318 ForExprAST(const std::string
&VarName
, std::unique_ptr
<ExprAST
> Start
,
319 std::unique_ptr
<ExprAST
> End
, std::unique_ptr
<ExprAST
> Step
,
320 std::unique_ptr
<ExprAST
> Body
)
321 : VarName(VarName
), Start(std::move(Start
)), End(std::move(End
)),
322 Step(std::move(Step
)), Body(std::move(Body
)) {}
323 Value
*codegen() override
;
324 raw_ostream
&dump(raw_ostream
&out
, int ind
) override
{
325 ExprAST::dump(out
<< "for", ind
);
326 Start
->dump(indent(out
, ind
) << "Cond:", ind
+ 1);
327 End
->dump(indent(out
, ind
) << "End:", ind
+ 1);
328 Step
->dump(indent(out
, ind
) << "Step:", ind
+ 1);
329 Body
->dump(indent(out
, ind
) << "Body:", ind
+ 1);
334 /// VarExprAST - Expression class for var/in
335 class VarExprAST
: public ExprAST
{
336 std::vector
<std::pair
<std::string
, std::unique_ptr
<ExprAST
>>> VarNames
;
337 std::unique_ptr
<ExprAST
> Body
;
341 std::vector
<std::pair
<std::string
, std::unique_ptr
<ExprAST
>>> VarNames
,
342 std::unique_ptr
<ExprAST
> Body
)
343 : VarNames(std::move(VarNames
)), Body(std::move(Body
)) {}
344 Value
*codegen() override
;
345 raw_ostream
&dump(raw_ostream
&out
, int ind
) override
{
346 ExprAST::dump(out
<< "var", ind
);
347 for (const auto &NamedVar
: VarNames
)
348 NamedVar
.second
->dump(indent(out
, ind
) << NamedVar
.first
<< ':', ind
+ 1);
349 Body
->dump(indent(out
, ind
) << "Body:", ind
+ 1);
354 /// PrototypeAST - This class represents the "prototype" for a function,
355 /// which captures its name, and its argument names (thus implicitly the number
356 /// of arguments the function takes), as well as if it is an operator.
359 std::vector
<std::string
> Args
;
361 unsigned Precedence
; // Precedence if a binary op.
365 PrototypeAST(SourceLocation Loc
, const std::string
&Name
,
366 std::vector
<std::string
> Args
, bool IsOperator
= false,
368 : Name(Name
), Args(std::move(Args
)), IsOperator(IsOperator
),
369 Precedence(Prec
), Line(Loc
.Line
) {}
371 const std::string
&getName() const { return Name
; }
373 bool isUnaryOp() const { return IsOperator
&& Args
.size() == 1; }
374 bool isBinaryOp() const { return IsOperator
&& Args
.size() == 2; }
376 char getOperatorName() const {
377 assert(isUnaryOp() || isBinaryOp());
378 return Name
[Name
.size() - 1];
381 unsigned getBinaryPrecedence() const { return Precedence
; }
382 int getLine() const { return Line
; }
385 /// FunctionAST - This class represents a function definition itself.
387 std::unique_ptr
<PrototypeAST
> Proto
;
388 std::unique_ptr
<ExprAST
> Body
;
391 FunctionAST(std::unique_ptr
<PrototypeAST
> Proto
,
392 std::unique_ptr
<ExprAST
> Body
)
393 : Proto(std::move(Proto
)), Body(std::move(Body
)) {}
395 raw_ostream
&dump(raw_ostream
&out
, int ind
) {
396 indent(out
, ind
) << "FunctionAST\n";
398 indent(out
, ind
) << "Body:";
399 return Body
? Body
->dump(out
, ind
) : out
<< "null\n";
402 } // end anonymous namespace
404 //===----------------------------------------------------------------------===//
406 //===----------------------------------------------------------------------===//
408 /// CurTok/getNextToken - Provide a simple token buffer. CurTok is the current
409 /// token the parser is looking at. getNextToken reads another token from the
410 /// lexer and updates CurTok with its results.
412 static int getNextToken() { return CurTok
= gettok(); }
414 /// BinopPrecedence - This holds the precedence for each binary operator that is
416 static std::map
<char, int> BinopPrecedence
;
418 /// GetTokPrecedence - Get the precedence of the pending binary operator token.
419 static int GetTokPrecedence() {
420 if (!isascii(CurTok
))
423 // Make sure it's a declared binop.
424 int TokPrec
= BinopPrecedence
[CurTok
];
430 /// LogError* - These are little helper functions for error handling.
431 std::unique_ptr
<ExprAST
> LogError(const char *Str
) {
432 fprintf(stderr
, "Error: %s\n", Str
);
436 std::unique_ptr
<PrototypeAST
> LogErrorP(const char *Str
) {
441 static std::unique_ptr
<ExprAST
> ParseExpression();
443 /// numberexpr ::= number
444 static std::unique_ptr
<ExprAST
> ParseNumberExpr() {
445 auto Result
= llvm::make_unique
<NumberExprAST
>(NumVal
);
446 getNextToken(); // consume the number
447 return std::move(Result
);
450 /// parenexpr ::= '(' expression ')'
451 static std::unique_ptr
<ExprAST
> ParseParenExpr() {
452 getNextToken(); // eat (.
453 auto V
= ParseExpression();
458 return LogError("expected ')'");
459 getNextToken(); // eat ).
465 /// ::= identifier '(' expression* ')'
466 static std::unique_ptr
<ExprAST
> ParseIdentifierExpr() {
467 std::string IdName
= IdentifierStr
;
469 SourceLocation LitLoc
= CurLoc
;
471 getNextToken(); // eat identifier.
473 if (CurTok
!= '(') // Simple variable ref.
474 return llvm::make_unique
<VariableExprAST
>(LitLoc
, IdName
);
477 getNextToken(); // eat (
478 std::vector
<std::unique_ptr
<ExprAST
>> Args
;
481 if (auto Arg
= ParseExpression())
482 Args
.push_back(std::move(Arg
));
490 return LogError("Expected ')' or ',' in argument list");
498 return llvm::make_unique
<CallExprAST
>(LitLoc
, IdName
, std::move(Args
));
501 /// ifexpr ::= 'if' expression 'then' expression 'else' expression
502 static std::unique_ptr
<ExprAST
> ParseIfExpr() {
503 SourceLocation IfLoc
= CurLoc
;
505 getNextToken(); // eat the if.
508 auto Cond
= ParseExpression();
512 if (CurTok
!= tok_then
)
513 return LogError("expected then");
514 getNextToken(); // eat the then
516 auto Then
= ParseExpression();
520 if (CurTok
!= tok_else
)
521 return LogError("expected else");
525 auto Else
= ParseExpression();
529 return llvm::make_unique
<IfExprAST
>(IfLoc
, std::move(Cond
), std::move(Then
),
533 /// forexpr ::= 'for' identifier '=' expr ',' expr (',' expr)? 'in' expression
534 static std::unique_ptr
<ExprAST
> ParseForExpr() {
535 getNextToken(); // eat the for.
537 if (CurTok
!= tok_identifier
)
538 return LogError("expected identifier after for");
540 std::string IdName
= IdentifierStr
;
541 getNextToken(); // eat identifier.
544 return LogError("expected '=' after for");
545 getNextToken(); // eat '='.
547 auto Start
= ParseExpression();
551 return LogError("expected ',' after for start value");
554 auto End
= ParseExpression();
558 // The step value is optional.
559 std::unique_ptr
<ExprAST
> Step
;
562 Step
= ParseExpression();
567 if (CurTok
!= tok_in
)
568 return LogError("expected 'in' after for");
569 getNextToken(); // eat 'in'.
571 auto Body
= ParseExpression();
575 return llvm::make_unique
<ForExprAST
>(IdName
, std::move(Start
), std::move(End
),
576 std::move(Step
), std::move(Body
));
579 /// varexpr ::= 'var' identifier ('=' expression)?
580 // (',' identifier ('=' expression)?)* 'in' expression
581 static std::unique_ptr
<ExprAST
> ParseVarExpr() {
582 getNextToken(); // eat the var.
584 std::vector
<std::pair
<std::string
, std::unique_ptr
<ExprAST
>>> VarNames
;
586 // At least one variable name is required.
587 if (CurTok
!= tok_identifier
)
588 return LogError("expected identifier after var");
591 std::string Name
= IdentifierStr
;
592 getNextToken(); // eat identifier.
594 // Read the optional initializer.
595 std::unique_ptr
<ExprAST
> Init
= nullptr;
597 getNextToken(); // eat the '='.
599 Init
= ParseExpression();
604 VarNames
.push_back(std::make_pair(Name
, std::move(Init
)));
606 // End of var list, exit loop.
609 getNextToken(); // eat the ','.
611 if (CurTok
!= tok_identifier
)
612 return LogError("expected identifier list after var");
615 // At this point, we have to have 'in'.
616 if (CurTok
!= tok_in
)
617 return LogError("expected 'in' keyword after 'var'");
618 getNextToken(); // eat 'in'.
620 auto Body
= ParseExpression();
624 return llvm::make_unique
<VarExprAST
>(std::move(VarNames
), std::move(Body
));
628 /// ::= identifierexpr
634 static std::unique_ptr
<ExprAST
> ParsePrimary() {
637 return LogError("unknown token when expecting an expression");
639 return ParseIdentifierExpr();
641 return ParseNumberExpr();
643 return ParseParenExpr();
645 return ParseIfExpr();
647 return ParseForExpr();
649 return ParseVarExpr();
656 static std::unique_ptr
<ExprAST
> ParseUnary() {
657 // If the current token is not an operator, it must be a primary expr.
658 if (!isascii(CurTok
) || CurTok
== '(' || CurTok
== ',')
659 return ParsePrimary();
661 // If this is a unary operator, read it.
664 if (auto Operand
= ParseUnary())
665 return llvm::make_unique
<UnaryExprAST
>(Opc
, std::move(Operand
));
671 static std::unique_ptr
<ExprAST
> ParseBinOpRHS(int ExprPrec
,
672 std::unique_ptr
<ExprAST
> LHS
) {
673 // If this is a binop, find its precedence.
675 int TokPrec
= GetTokPrecedence();
677 // If this is a binop that binds at least as tightly as the current binop,
678 // consume it, otherwise we are done.
679 if (TokPrec
< ExprPrec
)
682 // Okay, we know this is a binop.
684 SourceLocation BinLoc
= CurLoc
;
685 getNextToken(); // eat binop
687 // Parse the unary expression after the binary operator.
688 auto RHS
= ParseUnary();
692 // If BinOp binds less tightly with RHS than the operator after RHS, let
693 // the pending operator take RHS as its LHS.
694 int NextPrec
= GetTokPrecedence();
695 if (TokPrec
< NextPrec
) {
696 RHS
= ParseBinOpRHS(TokPrec
+ 1, std::move(RHS
));
702 LHS
= llvm::make_unique
<BinaryExprAST
>(BinLoc
, BinOp
, std::move(LHS
),
708 /// ::= unary binoprhs
710 static std::unique_ptr
<ExprAST
> ParseExpression() {
711 auto LHS
= ParseUnary();
715 return ParseBinOpRHS(0, std::move(LHS
));
719 /// ::= id '(' id* ')'
720 /// ::= binary LETTER number? (id, id)
721 /// ::= unary LETTER (id)
722 static std::unique_ptr
<PrototypeAST
> ParsePrototype() {
725 SourceLocation FnLoc
= CurLoc
;
727 unsigned Kind
= 0; // 0 = identifier, 1 = unary, 2 = binary.
728 unsigned BinaryPrecedence
= 30;
732 return LogErrorP("Expected function name in prototype");
734 FnName
= IdentifierStr
;
740 if (!isascii(CurTok
))
741 return LogErrorP("Expected unary operator");
743 FnName
+= (char)CurTok
;
749 if (!isascii(CurTok
))
750 return LogErrorP("Expected binary operator");
752 FnName
+= (char)CurTok
;
756 // Read the precedence if present.
757 if (CurTok
== tok_number
) {
758 if (NumVal
< 1 || NumVal
> 100)
759 return LogErrorP("Invalid precedence: must be 1..100");
760 BinaryPrecedence
= (unsigned)NumVal
;
767 return LogErrorP("Expected '(' in prototype");
769 std::vector
<std::string
> ArgNames
;
770 while (getNextToken() == tok_identifier
)
771 ArgNames
.push_back(IdentifierStr
);
773 return LogErrorP("Expected ')' in prototype");
776 getNextToken(); // eat ')'.
778 // Verify right number of names for operator.
779 if (Kind
&& ArgNames
.size() != Kind
)
780 return LogErrorP("Invalid number of operands for operator");
782 return llvm::make_unique
<PrototypeAST
>(FnLoc
, FnName
, ArgNames
, Kind
!= 0,
786 /// definition ::= 'def' prototype expression
787 static std::unique_ptr
<FunctionAST
> ParseDefinition() {
788 getNextToken(); // eat def.
789 auto Proto
= ParsePrototype();
793 if (auto E
= ParseExpression())
794 return llvm::make_unique
<FunctionAST
>(std::move(Proto
), std::move(E
));
798 /// toplevelexpr ::= expression
799 static std::unique_ptr
<FunctionAST
> ParseTopLevelExpr() {
800 SourceLocation FnLoc
= CurLoc
;
801 if (auto E
= ParseExpression()) {
802 // Make an anonymous proto.
803 auto Proto
= llvm::make_unique
<PrototypeAST
>(FnLoc
, "__anon_expr",
804 std::vector
<std::string
>());
805 return llvm::make_unique
<FunctionAST
>(std::move(Proto
), std::move(E
));
810 /// external ::= 'extern' prototype
811 static std::unique_ptr
<PrototypeAST
> ParseExtern() {
812 getNextToken(); // eat extern.
813 return ParsePrototype();
816 //===----------------------------------------------------------------------===//
817 // Debug Info Support
818 //===----------------------------------------------------------------------===//
820 static std::unique_ptr
<DIBuilder
> DBuilder
;
822 DIType
*DebugInfo::getDoubleTy() {
826 DblTy
= DBuilder
->createBasicType("double", 64, dwarf::DW_ATE_float
);
830 void DebugInfo::emitLocation(ExprAST
*AST
) {
832 return Builder
.SetCurrentDebugLocation(DebugLoc());
834 if (LexicalBlocks
.empty())
837 Scope
= LexicalBlocks
.back();
838 Builder
.SetCurrentDebugLocation(
839 DebugLoc::get(AST
->getLine(), AST
->getCol(), Scope
));
842 static DISubroutineType
*CreateFunctionType(unsigned NumArgs
, DIFile
*Unit
) {
843 SmallVector
<Metadata
*, 8> EltTys
;
844 DIType
*DblTy
= KSDbgInfo
.getDoubleTy();
846 // Add the result type.
847 EltTys
.push_back(DblTy
);
849 for (unsigned i
= 0, e
= NumArgs
; i
!= e
; ++i
)
850 EltTys
.push_back(DblTy
);
852 return DBuilder
->createSubroutineType(DBuilder
->getOrCreateTypeArray(EltTys
));
855 //===----------------------------------------------------------------------===//
857 //===----------------------------------------------------------------------===//
859 static std::unique_ptr
<Module
> TheModule
;
860 static std::map
<std::string
, AllocaInst
*> NamedValues
;
861 static std::unique_ptr
<KaleidoscopeJIT
> TheJIT
;
862 static std::map
<std::string
, std::unique_ptr
<PrototypeAST
>> FunctionProtos
;
864 Value
*LogErrorV(const char *Str
) {
869 Function
*getFunction(std::string Name
) {
870 // First, see if the function has already been added to the current module.
871 if (auto *F
= TheModule
->getFunction(Name
))
874 // If not, check whether we can codegen the declaration from some existing
876 auto FI
= FunctionProtos
.find(Name
);
877 if (FI
!= FunctionProtos
.end())
878 return FI
->second
->codegen();
880 // If no existing prototype exists, return null.
884 /// CreateEntryBlockAlloca - Create an alloca instruction in the entry block of
885 /// the function. This is used for mutable variables etc.
886 static AllocaInst
*CreateEntryBlockAlloca(Function
*TheFunction
,
887 const std::string
&VarName
) {
888 IRBuilder
<> TmpB(&TheFunction
->getEntryBlock(),
889 TheFunction
->getEntryBlock().begin());
890 return TmpB
.CreateAlloca(Type::getDoubleTy(TheContext
), nullptr,
894 Value
*NumberExprAST::codegen() {
895 KSDbgInfo
.emitLocation(this);
896 return ConstantFP::get(TheContext
, APFloat(Val
));
899 Value
*VariableExprAST::codegen() {
900 // Look this variable up in the function.
901 Value
*V
= NamedValues
[Name
];
903 return LogErrorV("Unknown variable name");
905 KSDbgInfo
.emitLocation(this);
907 return Builder
.CreateLoad(V
, Name
.c_str());
910 Value
*UnaryExprAST::codegen() {
911 Value
*OperandV
= Operand
->codegen();
915 Function
*F
= getFunction(std::string("unary") + Opcode
);
917 return LogErrorV("Unknown unary operator");
919 KSDbgInfo
.emitLocation(this);
920 return Builder
.CreateCall(F
, OperandV
, "unop");
923 Value
*BinaryExprAST::codegen() {
924 KSDbgInfo
.emitLocation(this);
926 // Special case '=' because we don't want to emit the LHS as an expression.
928 // Assignment requires the LHS to be an identifier.
929 // This assume we're building without RTTI because LLVM builds that way by
930 // default. If you build LLVM with RTTI this can be changed to a
931 // dynamic_cast for automatic error checking.
932 VariableExprAST
*LHSE
= static_cast<VariableExprAST
*>(LHS
.get());
934 return LogErrorV("destination of '=' must be a variable");
936 Value
*Val
= RHS
->codegen();
941 Value
*Variable
= NamedValues
[LHSE
->getName()];
943 return LogErrorV("Unknown variable name");
945 Builder
.CreateStore(Val
, Variable
);
949 Value
*L
= LHS
->codegen();
950 Value
*R
= RHS
->codegen();
956 return Builder
.CreateFAdd(L
, R
, "addtmp");
958 return Builder
.CreateFSub(L
, R
, "subtmp");
960 return Builder
.CreateFMul(L
, R
, "multmp");
962 L
= Builder
.CreateFCmpULT(L
, R
, "cmptmp");
963 // Convert bool 0/1 to double 0.0 or 1.0
964 return Builder
.CreateUIToFP(L
, Type::getDoubleTy(TheContext
), "booltmp");
969 // If it wasn't a builtin binary operator, it must be a user defined one. Emit
971 Function
*F
= getFunction(std::string("binary") + Op
);
972 assert(F
&& "binary operator not found!");
974 Value
*Ops
[] = {L
, R
};
975 return Builder
.CreateCall(F
, Ops
, "binop");
978 Value
*CallExprAST::codegen() {
979 KSDbgInfo
.emitLocation(this);
981 // Look up the name in the global module table.
982 Function
*CalleeF
= getFunction(Callee
);
984 return LogErrorV("Unknown function referenced");
986 // If argument mismatch error.
987 if (CalleeF
->arg_size() != Args
.size())
988 return LogErrorV("Incorrect # arguments passed");
990 std::vector
<Value
*> ArgsV
;
991 for (unsigned i
= 0, e
= Args
.size(); i
!= e
; ++i
) {
992 ArgsV
.push_back(Args
[i
]->codegen());
997 return Builder
.CreateCall(CalleeF
, ArgsV
, "calltmp");
1000 Value
*IfExprAST::codegen() {
1001 KSDbgInfo
.emitLocation(this);
1003 Value
*CondV
= Cond
->codegen();
1007 // Convert condition to a bool by comparing non-equal to 0.0.
1008 CondV
= Builder
.CreateFCmpONE(
1009 CondV
, ConstantFP::get(TheContext
, APFloat(0.0)), "ifcond");
1011 Function
*TheFunction
= Builder
.GetInsertBlock()->getParent();
1013 // Create blocks for the then and else cases. Insert the 'then' block at the
1014 // end of the function.
1015 BasicBlock
*ThenBB
= BasicBlock::Create(TheContext
, "then", TheFunction
);
1016 BasicBlock
*ElseBB
= BasicBlock::Create(TheContext
, "else");
1017 BasicBlock
*MergeBB
= BasicBlock::Create(TheContext
, "ifcont");
1019 Builder
.CreateCondBr(CondV
, ThenBB
, ElseBB
);
1022 Builder
.SetInsertPoint(ThenBB
);
1024 Value
*ThenV
= Then
->codegen();
1028 Builder
.CreateBr(MergeBB
);
1029 // Codegen of 'Then' can change the current block, update ThenBB for the PHI.
1030 ThenBB
= Builder
.GetInsertBlock();
1033 TheFunction
->getBasicBlockList().push_back(ElseBB
);
1034 Builder
.SetInsertPoint(ElseBB
);
1036 Value
*ElseV
= Else
->codegen();
1040 Builder
.CreateBr(MergeBB
);
1041 // Codegen of 'Else' can change the current block, update ElseBB for the PHI.
1042 ElseBB
= Builder
.GetInsertBlock();
1044 // Emit merge block.
1045 TheFunction
->getBasicBlockList().push_back(MergeBB
);
1046 Builder
.SetInsertPoint(MergeBB
);
1047 PHINode
*PN
= Builder
.CreatePHI(Type::getDoubleTy(TheContext
), 2, "iftmp");
1049 PN
->addIncoming(ThenV
, ThenBB
);
1050 PN
->addIncoming(ElseV
, ElseBB
);
1054 // Output for-loop as:
1055 // var = alloca double
1057 // start = startexpr
1058 // store start -> var
1066 // endcond = endexpr
1068 // curvar = load var
1069 // nextvar = curvar + step
1070 // store nextvar -> var
1071 // br endcond, loop, endloop
1073 Value
*ForExprAST::codegen() {
1074 Function
*TheFunction
= Builder
.GetInsertBlock()->getParent();
1076 // Create an alloca for the variable in the entry block.
1077 AllocaInst
*Alloca
= CreateEntryBlockAlloca(TheFunction
, VarName
);
1079 KSDbgInfo
.emitLocation(this);
1081 // Emit the start code first, without 'variable' in scope.
1082 Value
*StartVal
= Start
->codegen();
1086 // Store the value into the alloca.
1087 Builder
.CreateStore(StartVal
, Alloca
);
1089 // Make the new basic block for the loop header, inserting after current
1091 BasicBlock
*LoopBB
= BasicBlock::Create(TheContext
, "loop", TheFunction
);
1093 // Insert an explicit fall through from the current block to the LoopBB.
1094 Builder
.CreateBr(LoopBB
);
1096 // Start insertion in LoopBB.
1097 Builder
.SetInsertPoint(LoopBB
);
1099 // Within the loop, the variable is defined equal to the PHI node. If it
1100 // shadows an existing variable, we have to restore it, so save it now.
1101 AllocaInst
*OldVal
= NamedValues
[VarName
];
1102 NamedValues
[VarName
] = Alloca
;
1104 // Emit the body of the loop. This, like any other expr, can change the
1105 // current BB. Note that we ignore the value computed by the body, but don't
1107 if (!Body
->codegen())
1110 // Emit the step value.
1111 Value
*StepVal
= nullptr;
1113 StepVal
= Step
->codegen();
1117 // If not specified, use 1.0.
1118 StepVal
= ConstantFP::get(TheContext
, APFloat(1.0));
1121 // Compute the end condition.
1122 Value
*EndCond
= End
->codegen();
1126 // Reload, increment, and restore the alloca. This handles the case where
1127 // the body of the loop mutates the variable.
1128 Value
*CurVar
= Builder
.CreateLoad(Alloca
, VarName
.c_str());
1129 Value
*NextVar
= Builder
.CreateFAdd(CurVar
, StepVal
, "nextvar");
1130 Builder
.CreateStore(NextVar
, Alloca
);
1132 // Convert condition to a bool by comparing non-equal to 0.0.
1133 EndCond
= Builder
.CreateFCmpONE(
1134 EndCond
, ConstantFP::get(TheContext
, APFloat(0.0)), "loopcond");
1136 // Create the "after loop" block and insert it.
1137 BasicBlock
*AfterBB
=
1138 BasicBlock::Create(TheContext
, "afterloop", TheFunction
);
1140 // Insert the conditional branch into the end of LoopEndBB.
1141 Builder
.CreateCondBr(EndCond
, LoopBB
, AfterBB
);
1143 // Any new code will be inserted in AfterBB.
1144 Builder
.SetInsertPoint(AfterBB
);
1146 // Restore the unshadowed variable.
1148 NamedValues
[VarName
] = OldVal
;
1150 NamedValues
.erase(VarName
);
1152 // for expr always returns 0.0.
1153 return Constant::getNullValue(Type::getDoubleTy(TheContext
));
1156 Value
*VarExprAST::codegen() {
1157 std::vector
<AllocaInst
*> OldBindings
;
1159 Function
*TheFunction
= Builder
.GetInsertBlock()->getParent();
1161 // Register all variables and emit their initializer.
1162 for (unsigned i
= 0, e
= VarNames
.size(); i
!= e
; ++i
) {
1163 const std::string
&VarName
= VarNames
[i
].first
;
1164 ExprAST
*Init
= VarNames
[i
].second
.get();
1166 // Emit the initializer before adding the variable to scope, this prevents
1167 // the initializer from referencing the variable itself, and permits stuff
1170 // var a = a in ... # refers to outer 'a'.
1173 InitVal
= Init
->codegen();
1176 } else { // If not specified, use 0.0.
1177 InitVal
= ConstantFP::get(TheContext
, APFloat(0.0));
1180 AllocaInst
*Alloca
= CreateEntryBlockAlloca(TheFunction
, VarName
);
1181 Builder
.CreateStore(InitVal
, Alloca
);
1183 // Remember the old variable binding so that we can restore the binding when
1185 OldBindings
.push_back(NamedValues
[VarName
]);
1187 // Remember this binding.
1188 NamedValues
[VarName
] = Alloca
;
1191 KSDbgInfo
.emitLocation(this);
1193 // Codegen the body, now that all vars are in scope.
1194 Value
*BodyVal
= Body
->codegen();
1198 // Pop all our variables from scope.
1199 for (unsigned i
= 0, e
= VarNames
.size(); i
!= e
; ++i
)
1200 NamedValues
[VarNames
[i
].first
] = OldBindings
[i
];
1202 // Return the body computation.
1206 Function
*PrototypeAST::codegen() {
1207 // Make the function type: double(double,double) etc.
1208 std::vector
<Type
*> Doubles(Args
.size(), Type::getDoubleTy(TheContext
));
1210 FunctionType::get(Type::getDoubleTy(TheContext
), Doubles
, false);
1213 Function::Create(FT
, Function::ExternalLinkage
, Name
, TheModule
.get());
1215 // Set names for all arguments.
1217 for (auto &Arg
: F
->args())
1218 Arg
.setName(Args
[Idx
++]);
1223 Function
*FunctionAST::codegen() {
1224 // Transfer ownership of the prototype to the FunctionProtos map, but keep a
1225 // reference to it for use below.
1227 FunctionProtos
[Proto
->getName()] = std::move(Proto
);
1228 Function
*TheFunction
= getFunction(P
.getName());
1232 // If this is an operator, install it.
1234 BinopPrecedence
[P
.getOperatorName()] = P
.getBinaryPrecedence();
1236 // Create a new basic block to start insertion into.
1237 BasicBlock
*BB
= BasicBlock::Create(TheContext
, "entry", TheFunction
);
1238 Builder
.SetInsertPoint(BB
);
1240 // Create a subprogram DIE for this function.
1241 DIFile
*Unit
= DBuilder
->createFile(KSDbgInfo
.TheCU
->getFilename(),
1242 KSDbgInfo
.TheCU
->getDirectory());
1243 DIScope
*FContext
= Unit
;
1244 unsigned LineNo
= P
.getLine();
1245 unsigned ScopeLine
= LineNo
;
1246 DISubprogram
*SP
= DBuilder
->createFunction(
1247 FContext
, P
.getName(), StringRef(), Unit
, LineNo
,
1248 CreateFunctionType(TheFunction
->arg_size(), Unit
),
1249 false /* internal linkage */, true /* definition */, ScopeLine
,
1250 DINode::FlagPrototyped
, false);
1251 TheFunction
->setSubprogram(SP
);
1253 // Push the current scope.
1254 KSDbgInfo
.LexicalBlocks
.push_back(SP
);
1256 // Unset the location for the prologue emission (leading instructions with no
1257 // location in a function are considered part of the prologue and the debugger
1258 // will run past them when breaking on a function)
1259 KSDbgInfo
.emitLocation(nullptr);
1261 // Record the function arguments in the NamedValues map.
1262 NamedValues
.clear();
1263 unsigned ArgIdx
= 0;
1264 for (auto &Arg
: TheFunction
->args()) {
1265 // Create an alloca for this variable.
1266 AllocaInst
*Alloca
= CreateEntryBlockAlloca(TheFunction
, Arg
.getName());
1268 // Create a debug descriptor for the variable.
1269 DILocalVariable
*D
= DBuilder
->createParameterVariable(
1270 SP
, Arg
.getName(), ++ArgIdx
, Unit
, LineNo
, KSDbgInfo
.getDoubleTy(),
1273 DBuilder
->insertDeclare(Alloca
, D
, DBuilder
->createExpression(),
1274 DebugLoc::get(LineNo
, 0, SP
),
1275 Builder
.GetInsertBlock());
1277 // Store the initial value into the alloca.
1278 Builder
.CreateStore(&Arg
, Alloca
);
1280 // Add arguments to variable symbol table.
1281 NamedValues
[Arg
.getName()] = Alloca
;
1284 KSDbgInfo
.emitLocation(Body
.get());
1286 if (Value
*RetVal
= Body
->codegen()) {
1287 // Finish off the function.
1288 Builder
.CreateRet(RetVal
);
1290 // Pop off the lexical block for the function.
1291 KSDbgInfo
.LexicalBlocks
.pop_back();
1293 // Validate the generated code, checking for consistency.
1294 verifyFunction(*TheFunction
);
1299 // Error reading body, remove function.
1300 TheFunction
->eraseFromParent();
1303 BinopPrecedence
.erase(Proto
->getOperatorName());
1305 // Pop off the lexical block for the function since we added it
1307 KSDbgInfo
.LexicalBlocks
.pop_back();
1312 //===----------------------------------------------------------------------===//
1313 // Top-Level parsing and JIT Driver
1314 //===----------------------------------------------------------------------===//
1316 static void InitializeModule() {
1317 // Open a new module.
1318 TheModule
= llvm::make_unique
<Module
>("my cool jit", TheContext
);
1319 TheModule
->setDataLayout(TheJIT
->getTargetMachine().createDataLayout());
1322 static void HandleDefinition() {
1323 if (auto FnAST
= ParseDefinition()) {
1324 if (!FnAST
->codegen())
1325 fprintf(stderr
, "Error reading function definition:");
1327 // Skip token for error recovery.
1332 static void HandleExtern() {
1333 if (auto ProtoAST
= ParseExtern()) {
1334 if (!ProtoAST
->codegen())
1335 fprintf(stderr
, "Error reading extern");
1337 FunctionProtos
[ProtoAST
->getName()] = std::move(ProtoAST
);
1339 // Skip token for error recovery.
1344 static void HandleTopLevelExpression() {
1345 // Evaluate a top-level expression into an anonymous function.
1346 if (auto FnAST
= ParseTopLevelExpr()) {
1347 if (!FnAST
->codegen()) {
1348 fprintf(stderr
, "Error generating code for top level expr");
1351 // Skip token for error recovery.
1356 /// top ::= definition | external | expression | ';'
1357 static void MainLoop() {
1362 case ';': // ignore top-level semicolons.
1372 HandleTopLevelExpression();
1378 //===----------------------------------------------------------------------===//
1379 // "Library" functions that can be "extern'd" from user code.
1380 //===----------------------------------------------------------------------===//
1383 #define DLLEXPORT __declspec(dllexport)
1388 /// putchard - putchar that takes a double and returns 0.
1389 extern "C" DLLEXPORT
double putchard(double X
) {
1390 fputc((char)X
, stderr
);
1394 /// printd - printf that takes a double prints it as "%f\n", returning 0.
1395 extern "C" DLLEXPORT
double printd(double X
) {
1396 fprintf(stderr
, "%f\n", X
);
1400 //===----------------------------------------------------------------------===//
1401 // Main driver code.
1402 //===----------------------------------------------------------------------===//
1405 InitializeNativeTarget();
1406 InitializeNativeTargetAsmPrinter();
1407 InitializeNativeTargetAsmParser();
1409 // Install standard binary operators.
1410 // 1 is lowest precedence.
1411 BinopPrecedence
['='] = 2;
1412 BinopPrecedence
['<'] = 10;
1413 BinopPrecedence
['+'] = 20;
1414 BinopPrecedence
['-'] = 20;
1415 BinopPrecedence
['*'] = 40; // highest.
1417 // Prime the first token.
1420 TheJIT
= llvm::make_unique
<KaleidoscopeJIT
>();
1424 // Add the current debug info version into the module.
1425 TheModule
->addModuleFlag(Module::Warning
, "Debug Info Version",
1426 DEBUG_METADATA_VERSION
);
1428 // Darwin only supports dwarf2.
1429 if (Triple(sys::getProcessTriple()).isOSDarwin())
1430 TheModule
->addModuleFlag(llvm::Module::Warning
, "Dwarf Version", 2);
1432 // Construct the DIBuilder, we do this here because we need the module.
1433 DBuilder
= llvm::make_unique
<DIBuilder
>(*TheModule
);
1435 // Create the compile unit for the module.
1436 // Currently down as "fib.ks" as a filename since we're redirecting stdin
1437 // but we'd like actual source locations.
1438 KSDbgInfo
.TheCU
= DBuilder
->createCompileUnit(
1439 dwarf::DW_LANG_C
, DBuilder
->createFile("fib.ks", "."),
1440 "Kaleidoscope Compiler", 0, "", 0);
1442 // Run the main "interpreter loop" now.
1445 // Finalize the debug info.
1446 DBuilder
->finalize();
1448 // Print out all of the generated code.
1449 TheModule
->print(errs(), nullptr);