xref: /openbsd-src/gnu/llvm/llvm/examples/Kaleidoscope/Chapter7/toy.cpp (revision d415bd752c734aee168c4ee86ff32e8cc249eb16)
109467b48Spatrick #include "../include/KaleidoscopeJIT.h"
209467b48Spatrick #include "llvm/ADT/APFloat.h"
309467b48Spatrick #include "llvm/ADT/STLExtras.h"
409467b48Spatrick #include "llvm/IR/BasicBlock.h"
509467b48Spatrick #include "llvm/IR/Constants.h"
609467b48Spatrick #include "llvm/IR/DerivedTypes.h"
709467b48Spatrick #include "llvm/IR/Function.h"
809467b48Spatrick #include "llvm/IR/IRBuilder.h"
909467b48Spatrick #include "llvm/IR/Instructions.h"
1009467b48Spatrick #include "llvm/IR/LLVMContext.h"
1109467b48Spatrick #include "llvm/IR/LegacyPassManager.h"
1209467b48Spatrick #include "llvm/IR/Module.h"
1309467b48Spatrick #include "llvm/IR/Type.h"
1409467b48Spatrick #include "llvm/IR/Verifier.h"
1509467b48Spatrick #include "llvm/Support/TargetSelect.h"
1609467b48Spatrick #include "llvm/Target/TargetMachine.h"
1709467b48Spatrick #include "llvm/Transforms/InstCombine/InstCombine.h"
1809467b48Spatrick #include "llvm/Transforms/Scalar.h"
1909467b48Spatrick #include "llvm/Transforms/Scalar/GVN.h"
2009467b48Spatrick #include "llvm/Transforms/Utils.h"
2109467b48Spatrick #include <algorithm>
2209467b48Spatrick #include <cassert>
2309467b48Spatrick #include <cctype>
2409467b48Spatrick #include <cstdint>
2509467b48Spatrick #include <cstdio>
2609467b48Spatrick #include <cstdlib>
2709467b48Spatrick #include <map>
2809467b48Spatrick #include <memory>
2909467b48Spatrick #include <string>
3009467b48Spatrick #include <utility>
3109467b48Spatrick #include <vector>
3209467b48Spatrick 
3309467b48Spatrick using namespace llvm;
3409467b48Spatrick using namespace llvm::orc;
3509467b48Spatrick 
3609467b48Spatrick //===----------------------------------------------------------------------===//
3709467b48Spatrick // Lexer
3809467b48Spatrick //===----------------------------------------------------------------------===//
3909467b48Spatrick 
4009467b48Spatrick // The lexer returns tokens [0-255] if it is an unknown character, otherwise one
4109467b48Spatrick // of these for known things.
4209467b48Spatrick enum Token {
4309467b48Spatrick   tok_eof = -1,
4409467b48Spatrick 
4509467b48Spatrick   // commands
4609467b48Spatrick   tok_def = -2,
4709467b48Spatrick   tok_extern = -3,
4809467b48Spatrick 
4909467b48Spatrick   // primary
5009467b48Spatrick   tok_identifier = -4,
5109467b48Spatrick   tok_number = -5,
5209467b48Spatrick 
5309467b48Spatrick   // control
5409467b48Spatrick   tok_if = -6,
5509467b48Spatrick   tok_then = -7,
5609467b48Spatrick   tok_else = -8,
5709467b48Spatrick   tok_for = -9,
5809467b48Spatrick   tok_in = -10,
5909467b48Spatrick 
6009467b48Spatrick   // operators
6109467b48Spatrick   tok_binary = -11,
6209467b48Spatrick   tok_unary = -12,
6309467b48Spatrick 
6409467b48Spatrick   // var definition
6509467b48Spatrick   tok_var = -13
6609467b48Spatrick };
6709467b48Spatrick 
6809467b48Spatrick static std::string IdentifierStr; // Filled in if tok_identifier
6909467b48Spatrick static double NumVal;             // Filled in if tok_number
7009467b48Spatrick 
7109467b48Spatrick /// gettok - Return the next token from standard input.
gettok()7209467b48Spatrick static int gettok() {
7309467b48Spatrick   static int LastChar = ' ';
7409467b48Spatrick 
7509467b48Spatrick   // Skip any whitespace.
7609467b48Spatrick   while (isspace(LastChar))
7709467b48Spatrick     LastChar = getchar();
7809467b48Spatrick 
7909467b48Spatrick   if (isalpha(LastChar)) { // identifier: [a-zA-Z][a-zA-Z0-9]*
8009467b48Spatrick     IdentifierStr = LastChar;
8109467b48Spatrick     while (isalnum((LastChar = getchar())))
8209467b48Spatrick       IdentifierStr += LastChar;
8309467b48Spatrick 
8409467b48Spatrick     if (IdentifierStr == "def")
8509467b48Spatrick       return tok_def;
8609467b48Spatrick     if (IdentifierStr == "extern")
8709467b48Spatrick       return tok_extern;
8809467b48Spatrick     if (IdentifierStr == "if")
8909467b48Spatrick       return tok_if;
9009467b48Spatrick     if (IdentifierStr == "then")
9109467b48Spatrick       return tok_then;
9209467b48Spatrick     if (IdentifierStr == "else")
9309467b48Spatrick       return tok_else;
9409467b48Spatrick     if (IdentifierStr == "for")
9509467b48Spatrick       return tok_for;
9609467b48Spatrick     if (IdentifierStr == "in")
9709467b48Spatrick       return tok_in;
9809467b48Spatrick     if (IdentifierStr == "binary")
9909467b48Spatrick       return tok_binary;
10009467b48Spatrick     if (IdentifierStr == "unary")
10109467b48Spatrick       return tok_unary;
10209467b48Spatrick     if (IdentifierStr == "var")
10309467b48Spatrick       return tok_var;
10409467b48Spatrick     return tok_identifier;
10509467b48Spatrick   }
10609467b48Spatrick 
10709467b48Spatrick   if (isdigit(LastChar) || LastChar == '.') { // Number: [0-9.]+
10809467b48Spatrick     std::string NumStr;
10909467b48Spatrick     do {
11009467b48Spatrick       NumStr += LastChar;
11109467b48Spatrick       LastChar = getchar();
11209467b48Spatrick     } while (isdigit(LastChar) || LastChar == '.');
11309467b48Spatrick 
11409467b48Spatrick     NumVal = strtod(NumStr.c_str(), nullptr);
11509467b48Spatrick     return tok_number;
11609467b48Spatrick   }
11709467b48Spatrick 
11809467b48Spatrick   if (LastChar == '#') {
11909467b48Spatrick     // Comment until end of line.
12009467b48Spatrick     do
12109467b48Spatrick       LastChar = getchar();
12209467b48Spatrick     while (LastChar != EOF && LastChar != '\n' && LastChar != '\r');
12309467b48Spatrick 
12409467b48Spatrick     if (LastChar != EOF)
12509467b48Spatrick       return gettok();
12609467b48Spatrick   }
12709467b48Spatrick 
12809467b48Spatrick   // Check for end of file.  Don't eat the EOF.
12909467b48Spatrick   if (LastChar == EOF)
13009467b48Spatrick     return tok_eof;
13109467b48Spatrick 
13209467b48Spatrick   // Otherwise, just return the character as its ascii value.
13309467b48Spatrick   int ThisChar = LastChar;
13409467b48Spatrick   LastChar = getchar();
13509467b48Spatrick   return ThisChar;
13609467b48Spatrick }
13709467b48Spatrick 
13809467b48Spatrick //===----------------------------------------------------------------------===//
13909467b48Spatrick // Abstract Syntax Tree (aka Parse Tree)
14009467b48Spatrick //===----------------------------------------------------------------------===//
14109467b48Spatrick 
14209467b48Spatrick namespace {
14309467b48Spatrick 
14409467b48Spatrick /// ExprAST - Base class for all expression nodes.
14509467b48Spatrick class ExprAST {
14609467b48Spatrick public:
14709467b48Spatrick   virtual ~ExprAST() = default;
14809467b48Spatrick 
14909467b48Spatrick   virtual Value *codegen() = 0;
15009467b48Spatrick };
15109467b48Spatrick 
15209467b48Spatrick /// NumberExprAST - Expression class for numeric literals like "1.0".
15309467b48Spatrick class NumberExprAST : public ExprAST {
15409467b48Spatrick   double Val;
15509467b48Spatrick 
15609467b48Spatrick public:
NumberExprAST(double Val)15709467b48Spatrick   NumberExprAST(double Val) : Val(Val) {}
15809467b48Spatrick 
15909467b48Spatrick   Value *codegen() override;
16009467b48Spatrick };
16109467b48Spatrick 
16209467b48Spatrick /// VariableExprAST - Expression class for referencing a variable, like "a".
16309467b48Spatrick class VariableExprAST : public ExprAST {
16409467b48Spatrick   std::string Name;
16509467b48Spatrick 
16609467b48Spatrick public:
VariableExprAST(const std::string & Name)16709467b48Spatrick   VariableExprAST(const std::string &Name) : Name(Name) {}
16809467b48Spatrick 
16909467b48Spatrick   Value *codegen() override;
getName() const17009467b48Spatrick   const std::string &getName() const { return Name; }
17109467b48Spatrick };
17209467b48Spatrick 
17309467b48Spatrick /// UnaryExprAST - Expression class for a unary operator.
17409467b48Spatrick class UnaryExprAST : public ExprAST {
17509467b48Spatrick   char Opcode;
17609467b48Spatrick   std::unique_ptr<ExprAST> Operand;
17709467b48Spatrick 
17809467b48Spatrick public:
UnaryExprAST(char Opcode,std::unique_ptr<ExprAST> Operand)17909467b48Spatrick   UnaryExprAST(char Opcode, std::unique_ptr<ExprAST> Operand)
18009467b48Spatrick       : Opcode(Opcode), Operand(std::move(Operand)) {}
18109467b48Spatrick 
18209467b48Spatrick   Value *codegen() override;
18309467b48Spatrick };
18409467b48Spatrick 
18509467b48Spatrick /// BinaryExprAST - Expression class for a binary operator.
18609467b48Spatrick class BinaryExprAST : public ExprAST {
18709467b48Spatrick   char Op;
18809467b48Spatrick   std::unique_ptr<ExprAST> LHS, RHS;
18909467b48Spatrick 
19009467b48Spatrick public:
BinaryExprAST(char Op,std::unique_ptr<ExprAST> LHS,std::unique_ptr<ExprAST> RHS)19109467b48Spatrick   BinaryExprAST(char Op, std::unique_ptr<ExprAST> LHS,
19209467b48Spatrick                 std::unique_ptr<ExprAST> RHS)
19309467b48Spatrick       : Op(Op), LHS(std::move(LHS)), RHS(std::move(RHS)) {}
19409467b48Spatrick 
19509467b48Spatrick   Value *codegen() override;
19609467b48Spatrick };
19709467b48Spatrick 
19809467b48Spatrick /// CallExprAST - Expression class for function calls.
19909467b48Spatrick class CallExprAST : public ExprAST {
20009467b48Spatrick   std::string Callee;
20109467b48Spatrick   std::vector<std::unique_ptr<ExprAST>> Args;
20209467b48Spatrick 
20309467b48Spatrick public:
CallExprAST(const std::string & Callee,std::vector<std::unique_ptr<ExprAST>> Args)20409467b48Spatrick   CallExprAST(const std::string &Callee,
20509467b48Spatrick               std::vector<std::unique_ptr<ExprAST>> Args)
20609467b48Spatrick       : Callee(Callee), Args(std::move(Args)) {}
20709467b48Spatrick 
20809467b48Spatrick   Value *codegen() override;
20909467b48Spatrick };
21009467b48Spatrick 
21109467b48Spatrick /// IfExprAST - Expression class for if/then/else.
21209467b48Spatrick class IfExprAST : public ExprAST {
21309467b48Spatrick   std::unique_ptr<ExprAST> Cond, Then, Else;
21409467b48Spatrick 
21509467b48Spatrick public:
IfExprAST(std::unique_ptr<ExprAST> Cond,std::unique_ptr<ExprAST> Then,std::unique_ptr<ExprAST> Else)21609467b48Spatrick   IfExprAST(std::unique_ptr<ExprAST> Cond, std::unique_ptr<ExprAST> Then,
21709467b48Spatrick             std::unique_ptr<ExprAST> Else)
21809467b48Spatrick       : Cond(std::move(Cond)), Then(std::move(Then)), Else(std::move(Else)) {}
21909467b48Spatrick 
22009467b48Spatrick   Value *codegen() override;
22109467b48Spatrick };
22209467b48Spatrick 
22309467b48Spatrick /// ForExprAST - Expression class for for/in.
22409467b48Spatrick class ForExprAST : public ExprAST {
22509467b48Spatrick   std::string VarName;
22609467b48Spatrick   std::unique_ptr<ExprAST> Start, End, Step, Body;
22709467b48Spatrick 
22809467b48Spatrick public:
ForExprAST(const std::string & VarName,std::unique_ptr<ExprAST> Start,std::unique_ptr<ExprAST> End,std::unique_ptr<ExprAST> Step,std::unique_ptr<ExprAST> Body)22909467b48Spatrick   ForExprAST(const std::string &VarName, std::unique_ptr<ExprAST> Start,
23009467b48Spatrick              std::unique_ptr<ExprAST> End, std::unique_ptr<ExprAST> Step,
23109467b48Spatrick              std::unique_ptr<ExprAST> Body)
23209467b48Spatrick       : VarName(VarName), Start(std::move(Start)), End(std::move(End)),
23309467b48Spatrick         Step(std::move(Step)), Body(std::move(Body)) {}
23409467b48Spatrick 
23509467b48Spatrick   Value *codegen() override;
23609467b48Spatrick };
23709467b48Spatrick 
23809467b48Spatrick /// VarExprAST - Expression class for var/in
23909467b48Spatrick class VarExprAST : public ExprAST {
24009467b48Spatrick   std::vector<std::pair<std::string, std::unique_ptr<ExprAST>>> VarNames;
24109467b48Spatrick   std::unique_ptr<ExprAST> Body;
24209467b48Spatrick 
24309467b48Spatrick public:
VarExprAST(std::vector<std::pair<std::string,std::unique_ptr<ExprAST>>> VarNames,std::unique_ptr<ExprAST> Body)24409467b48Spatrick   VarExprAST(
24509467b48Spatrick       std::vector<std::pair<std::string, std::unique_ptr<ExprAST>>> VarNames,
24609467b48Spatrick       std::unique_ptr<ExprAST> Body)
24709467b48Spatrick       : VarNames(std::move(VarNames)), Body(std::move(Body)) {}
24809467b48Spatrick 
24909467b48Spatrick   Value *codegen() override;
25009467b48Spatrick };
25109467b48Spatrick 
25209467b48Spatrick /// PrototypeAST - This class represents the "prototype" for a function,
25309467b48Spatrick /// which captures its name, and its argument names (thus implicitly the number
25409467b48Spatrick /// of arguments the function takes), as well as if it is an operator.
25509467b48Spatrick class PrototypeAST {
25609467b48Spatrick   std::string Name;
25709467b48Spatrick   std::vector<std::string> Args;
25809467b48Spatrick   bool IsOperator;
25909467b48Spatrick   unsigned Precedence; // Precedence if a binary op.
26009467b48Spatrick 
26109467b48Spatrick public:
PrototypeAST(const std::string & Name,std::vector<std::string> Args,bool IsOperator=false,unsigned Prec=0)26209467b48Spatrick   PrototypeAST(const std::string &Name, std::vector<std::string> Args,
26309467b48Spatrick                bool IsOperator = false, unsigned Prec = 0)
26409467b48Spatrick       : Name(Name), Args(std::move(Args)), IsOperator(IsOperator),
26509467b48Spatrick         Precedence(Prec) {}
26609467b48Spatrick 
26709467b48Spatrick   Function *codegen();
getName() const26809467b48Spatrick   const std::string &getName() const { return Name; }
26909467b48Spatrick 
isUnaryOp() const27009467b48Spatrick   bool isUnaryOp() const { return IsOperator && Args.size() == 1; }
isBinaryOp() const27109467b48Spatrick   bool isBinaryOp() const { return IsOperator && Args.size() == 2; }
27209467b48Spatrick 
getOperatorName() const27309467b48Spatrick   char getOperatorName() const {
27409467b48Spatrick     assert(isUnaryOp() || isBinaryOp());
27509467b48Spatrick     return Name[Name.size() - 1];
27609467b48Spatrick   }
27709467b48Spatrick 
getBinaryPrecedence() const27809467b48Spatrick   unsigned getBinaryPrecedence() const { return Precedence; }
27909467b48Spatrick };
28009467b48Spatrick 
28109467b48Spatrick /// FunctionAST - This class represents a function definition itself.
28209467b48Spatrick class FunctionAST {
28309467b48Spatrick   std::unique_ptr<PrototypeAST> Proto;
28409467b48Spatrick   std::unique_ptr<ExprAST> Body;
28509467b48Spatrick 
28609467b48Spatrick public:
FunctionAST(std::unique_ptr<PrototypeAST> Proto,std::unique_ptr<ExprAST> Body)28709467b48Spatrick   FunctionAST(std::unique_ptr<PrototypeAST> Proto,
28809467b48Spatrick               std::unique_ptr<ExprAST> Body)
28909467b48Spatrick       : Proto(std::move(Proto)), Body(std::move(Body)) {}
29009467b48Spatrick 
29109467b48Spatrick   Function *codegen();
29209467b48Spatrick };
29309467b48Spatrick 
29409467b48Spatrick } // end anonymous namespace
29509467b48Spatrick 
29609467b48Spatrick //===----------------------------------------------------------------------===//
29709467b48Spatrick // Parser
29809467b48Spatrick //===----------------------------------------------------------------------===//
29909467b48Spatrick 
30009467b48Spatrick /// CurTok/getNextToken - Provide a simple token buffer.  CurTok is the current
30109467b48Spatrick /// token the parser is looking at.  getNextToken reads another token from the
30209467b48Spatrick /// lexer and updates CurTok with its results.
30309467b48Spatrick static int CurTok;
getNextToken()30409467b48Spatrick static int getNextToken() { return CurTok = gettok(); }
30509467b48Spatrick 
30609467b48Spatrick /// BinopPrecedence - This holds the precedence for each binary operator that is
30709467b48Spatrick /// defined.
30809467b48Spatrick static std::map<char, int> BinopPrecedence;
30909467b48Spatrick 
31009467b48Spatrick /// GetTokPrecedence - Get the precedence of the pending binary operator token.
GetTokPrecedence()31109467b48Spatrick static int GetTokPrecedence() {
31209467b48Spatrick   if (!isascii(CurTok))
31309467b48Spatrick     return -1;
31409467b48Spatrick 
31509467b48Spatrick   // Make sure it's a declared binop.
31609467b48Spatrick   int TokPrec = BinopPrecedence[CurTok];
31709467b48Spatrick   if (TokPrec <= 0)
31809467b48Spatrick     return -1;
31909467b48Spatrick   return TokPrec;
32009467b48Spatrick }
32109467b48Spatrick 
32209467b48Spatrick /// LogError* - These are little helper functions for error handling.
LogError(const char * Str)32309467b48Spatrick std::unique_ptr<ExprAST> LogError(const char *Str) {
32409467b48Spatrick   fprintf(stderr, "Error: %s\n", Str);
32509467b48Spatrick   return nullptr;
32609467b48Spatrick }
32709467b48Spatrick 
LogErrorP(const char * Str)32809467b48Spatrick std::unique_ptr<PrototypeAST> LogErrorP(const char *Str) {
32909467b48Spatrick   LogError(Str);
33009467b48Spatrick   return nullptr;
33109467b48Spatrick }
33209467b48Spatrick 
33309467b48Spatrick static std::unique_ptr<ExprAST> ParseExpression();
33409467b48Spatrick 
33509467b48Spatrick /// numberexpr ::= number
ParseNumberExpr()33609467b48Spatrick static std::unique_ptr<ExprAST> ParseNumberExpr() {
33709467b48Spatrick   auto Result = std::make_unique<NumberExprAST>(NumVal);
33809467b48Spatrick   getNextToken(); // consume the number
33909467b48Spatrick   return std::move(Result);
34009467b48Spatrick }
34109467b48Spatrick 
34209467b48Spatrick /// parenexpr ::= '(' expression ')'
ParseParenExpr()34309467b48Spatrick static std::unique_ptr<ExprAST> ParseParenExpr() {
34409467b48Spatrick   getNextToken(); // eat (.
34509467b48Spatrick   auto V = ParseExpression();
34609467b48Spatrick   if (!V)
34709467b48Spatrick     return nullptr;
34809467b48Spatrick 
34909467b48Spatrick   if (CurTok != ')')
35009467b48Spatrick     return LogError("expected ')'");
35109467b48Spatrick   getNextToken(); // eat ).
35209467b48Spatrick   return V;
35309467b48Spatrick }
35409467b48Spatrick 
35509467b48Spatrick /// identifierexpr
35609467b48Spatrick ///   ::= identifier
35709467b48Spatrick ///   ::= identifier '(' expression* ')'
ParseIdentifierExpr()35809467b48Spatrick static std::unique_ptr<ExprAST> ParseIdentifierExpr() {
35909467b48Spatrick   std::string IdName = IdentifierStr;
36009467b48Spatrick 
36109467b48Spatrick   getNextToken(); // eat identifier.
36209467b48Spatrick 
36309467b48Spatrick   if (CurTok != '(') // Simple variable ref.
36409467b48Spatrick     return std::make_unique<VariableExprAST>(IdName);
36509467b48Spatrick 
36609467b48Spatrick   // Call.
36709467b48Spatrick   getNextToken(); // eat (
36809467b48Spatrick   std::vector<std::unique_ptr<ExprAST>> Args;
36909467b48Spatrick   if (CurTok != ')') {
37009467b48Spatrick     while (true) {
37109467b48Spatrick       if (auto Arg = ParseExpression())
37209467b48Spatrick         Args.push_back(std::move(Arg));
37309467b48Spatrick       else
37409467b48Spatrick         return nullptr;
37509467b48Spatrick 
37609467b48Spatrick       if (CurTok == ')')
37709467b48Spatrick         break;
37809467b48Spatrick 
37909467b48Spatrick       if (CurTok != ',')
38009467b48Spatrick         return LogError("Expected ')' or ',' in argument list");
38109467b48Spatrick       getNextToken();
38209467b48Spatrick     }
38309467b48Spatrick   }
38409467b48Spatrick 
38509467b48Spatrick   // Eat the ')'.
38609467b48Spatrick   getNextToken();
38709467b48Spatrick 
38809467b48Spatrick   return std::make_unique<CallExprAST>(IdName, std::move(Args));
38909467b48Spatrick }
39009467b48Spatrick 
39109467b48Spatrick /// ifexpr ::= 'if' expression 'then' expression 'else' expression
ParseIfExpr()39209467b48Spatrick static std::unique_ptr<ExprAST> ParseIfExpr() {
39309467b48Spatrick   getNextToken(); // eat the if.
39409467b48Spatrick 
39509467b48Spatrick   // condition.
39609467b48Spatrick   auto Cond = ParseExpression();
39709467b48Spatrick   if (!Cond)
39809467b48Spatrick     return nullptr;
39909467b48Spatrick 
40009467b48Spatrick   if (CurTok != tok_then)
40109467b48Spatrick     return LogError("expected then");
40209467b48Spatrick   getNextToken(); // eat the then
40309467b48Spatrick 
40409467b48Spatrick   auto Then = ParseExpression();
40509467b48Spatrick   if (!Then)
40609467b48Spatrick     return nullptr;
40709467b48Spatrick 
40809467b48Spatrick   if (CurTok != tok_else)
40909467b48Spatrick     return LogError("expected else");
41009467b48Spatrick 
41109467b48Spatrick   getNextToken();
41209467b48Spatrick 
41309467b48Spatrick   auto Else = ParseExpression();
41409467b48Spatrick   if (!Else)
41509467b48Spatrick     return nullptr;
41609467b48Spatrick 
41709467b48Spatrick   return std::make_unique<IfExprAST>(std::move(Cond), std::move(Then),
41809467b48Spatrick                                       std::move(Else));
41909467b48Spatrick }
42009467b48Spatrick 
42109467b48Spatrick /// forexpr ::= 'for' identifier '=' expr ',' expr (',' expr)? 'in' expression
ParseForExpr()42209467b48Spatrick static std::unique_ptr<ExprAST> ParseForExpr() {
42309467b48Spatrick   getNextToken(); // eat the for.
42409467b48Spatrick 
42509467b48Spatrick   if (CurTok != tok_identifier)
42609467b48Spatrick     return LogError("expected identifier after for");
42709467b48Spatrick 
42809467b48Spatrick   std::string IdName = IdentifierStr;
42909467b48Spatrick   getNextToken(); // eat identifier.
43009467b48Spatrick 
43109467b48Spatrick   if (CurTok != '=')
43209467b48Spatrick     return LogError("expected '=' after for");
43309467b48Spatrick   getNextToken(); // eat '='.
43409467b48Spatrick 
43509467b48Spatrick   auto Start = ParseExpression();
43609467b48Spatrick   if (!Start)
43709467b48Spatrick     return nullptr;
43809467b48Spatrick   if (CurTok != ',')
43909467b48Spatrick     return LogError("expected ',' after for start value");
44009467b48Spatrick   getNextToken();
44109467b48Spatrick 
44209467b48Spatrick   auto End = ParseExpression();
44309467b48Spatrick   if (!End)
44409467b48Spatrick     return nullptr;
44509467b48Spatrick 
44609467b48Spatrick   // The step value is optional.
44709467b48Spatrick   std::unique_ptr<ExprAST> Step;
44809467b48Spatrick   if (CurTok == ',') {
44909467b48Spatrick     getNextToken();
45009467b48Spatrick     Step = ParseExpression();
45109467b48Spatrick     if (!Step)
45209467b48Spatrick       return nullptr;
45309467b48Spatrick   }
45409467b48Spatrick 
45509467b48Spatrick   if (CurTok != tok_in)
45609467b48Spatrick     return LogError("expected 'in' after for");
45709467b48Spatrick   getNextToken(); // eat 'in'.
45809467b48Spatrick 
45909467b48Spatrick   auto Body = ParseExpression();
46009467b48Spatrick   if (!Body)
46109467b48Spatrick     return nullptr;
46209467b48Spatrick 
46309467b48Spatrick   return std::make_unique<ForExprAST>(IdName, std::move(Start), std::move(End),
46409467b48Spatrick                                        std::move(Step), std::move(Body));
46509467b48Spatrick }
46609467b48Spatrick 
46709467b48Spatrick /// varexpr ::= 'var' identifier ('=' expression)?
46809467b48Spatrick //                    (',' identifier ('=' expression)?)* 'in' expression
ParseVarExpr()46909467b48Spatrick static std::unique_ptr<ExprAST> ParseVarExpr() {
47009467b48Spatrick   getNextToken(); // eat the var.
47109467b48Spatrick 
47209467b48Spatrick   std::vector<std::pair<std::string, std::unique_ptr<ExprAST>>> VarNames;
47309467b48Spatrick 
47409467b48Spatrick   // At least one variable name is required.
47509467b48Spatrick   if (CurTok != tok_identifier)
47609467b48Spatrick     return LogError("expected identifier after var");
47709467b48Spatrick 
47809467b48Spatrick   while (true) {
47909467b48Spatrick     std::string Name = IdentifierStr;
48009467b48Spatrick     getNextToken(); // eat identifier.
48109467b48Spatrick 
48209467b48Spatrick     // Read the optional initializer.
48309467b48Spatrick     std::unique_ptr<ExprAST> Init = nullptr;
48409467b48Spatrick     if (CurTok == '=') {
48509467b48Spatrick       getNextToken(); // eat the '='.
48609467b48Spatrick 
48709467b48Spatrick       Init = ParseExpression();
48809467b48Spatrick       if (!Init)
48909467b48Spatrick         return nullptr;
49009467b48Spatrick     }
49109467b48Spatrick 
49209467b48Spatrick     VarNames.push_back(std::make_pair(Name, std::move(Init)));
49309467b48Spatrick 
49409467b48Spatrick     // End of var list, exit loop.
49509467b48Spatrick     if (CurTok != ',')
49609467b48Spatrick       break;
49709467b48Spatrick     getNextToken(); // eat the ','.
49809467b48Spatrick 
49909467b48Spatrick     if (CurTok != tok_identifier)
50009467b48Spatrick       return LogError("expected identifier list after var");
50109467b48Spatrick   }
50209467b48Spatrick 
50309467b48Spatrick   // At this point, we have to have 'in'.
50409467b48Spatrick   if (CurTok != tok_in)
50509467b48Spatrick     return LogError("expected 'in' keyword after 'var'");
50609467b48Spatrick   getNextToken(); // eat 'in'.
50709467b48Spatrick 
50809467b48Spatrick   auto Body = ParseExpression();
50909467b48Spatrick   if (!Body)
51009467b48Spatrick     return nullptr;
51109467b48Spatrick 
51209467b48Spatrick   return std::make_unique<VarExprAST>(std::move(VarNames), std::move(Body));
51309467b48Spatrick }
51409467b48Spatrick 
51509467b48Spatrick /// primary
51609467b48Spatrick ///   ::= identifierexpr
51709467b48Spatrick ///   ::= numberexpr
51809467b48Spatrick ///   ::= parenexpr
51909467b48Spatrick ///   ::= ifexpr
52009467b48Spatrick ///   ::= forexpr
52109467b48Spatrick ///   ::= varexpr
ParsePrimary()52209467b48Spatrick static std::unique_ptr<ExprAST> ParsePrimary() {
52309467b48Spatrick   switch (CurTok) {
52409467b48Spatrick   default:
52509467b48Spatrick     return LogError("unknown token when expecting an expression");
52609467b48Spatrick   case tok_identifier:
52709467b48Spatrick     return ParseIdentifierExpr();
52809467b48Spatrick   case tok_number:
52909467b48Spatrick     return ParseNumberExpr();
53009467b48Spatrick   case '(':
53109467b48Spatrick     return ParseParenExpr();
53209467b48Spatrick   case tok_if:
53309467b48Spatrick     return ParseIfExpr();
53409467b48Spatrick   case tok_for:
53509467b48Spatrick     return ParseForExpr();
53609467b48Spatrick   case tok_var:
53709467b48Spatrick     return ParseVarExpr();
53809467b48Spatrick   }
53909467b48Spatrick }
54009467b48Spatrick 
54109467b48Spatrick /// unary
54209467b48Spatrick ///   ::= primary
54309467b48Spatrick ///   ::= '!' unary
ParseUnary()54409467b48Spatrick static std::unique_ptr<ExprAST> ParseUnary() {
54509467b48Spatrick   // If the current token is not an operator, it must be a primary expr.
54609467b48Spatrick   if (!isascii(CurTok) || CurTok == '(' || CurTok == ',')
54709467b48Spatrick     return ParsePrimary();
54809467b48Spatrick 
54909467b48Spatrick   // If this is a unary operator, read it.
55009467b48Spatrick   int Opc = CurTok;
55109467b48Spatrick   getNextToken();
55209467b48Spatrick   if (auto Operand = ParseUnary())
55309467b48Spatrick     return std::make_unique<UnaryExprAST>(Opc, std::move(Operand));
55409467b48Spatrick   return nullptr;
55509467b48Spatrick }
55609467b48Spatrick 
55709467b48Spatrick /// binoprhs
55809467b48Spatrick ///   ::= ('+' unary)*
ParseBinOpRHS(int ExprPrec,std::unique_ptr<ExprAST> LHS)55909467b48Spatrick static std::unique_ptr<ExprAST> ParseBinOpRHS(int ExprPrec,
56009467b48Spatrick                                               std::unique_ptr<ExprAST> LHS) {
56109467b48Spatrick   // If this is a binop, find its precedence.
56209467b48Spatrick   while (true) {
56309467b48Spatrick     int TokPrec = GetTokPrecedence();
56409467b48Spatrick 
56509467b48Spatrick     // If this is a binop that binds at least as tightly as the current binop,
56609467b48Spatrick     // consume it, otherwise we are done.
56709467b48Spatrick     if (TokPrec < ExprPrec)
56809467b48Spatrick       return LHS;
56909467b48Spatrick 
57009467b48Spatrick     // Okay, we know this is a binop.
57109467b48Spatrick     int BinOp = CurTok;
57209467b48Spatrick     getNextToken(); // eat binop
57309467b48Spatrick 
57409467b48Spatrick     // Parse the unary expression after the binary operator.
57509467b48Spatrick     auto RHS = ParseUnary();
57609467b48Spatrick     if (!RHS)
57709467b48Spatrick       return nullptr;
57809467b48Spatrick 
57909467b48Spatrick     // If BinOp binds less tightly with RHS than the operator after RHS, let
58009467b48Spatrick     // the pending operator take RHS as its LHS.
58109467b48Spatrick     int NextPrec = GetTokPrecedence();
58209467b48Spatrick     if (TokPrec < NextPrec) {
58309467b48Spatrick       RHS = ParseBinOpRHS(TokPrec + 1, std::move(RHS));
58409467b48Spatrick       if (!RHS)
58509467b48Spatrick         return nullptr;
58609467b48Spatrick     }
58709467b48Spatrick 
58809467b48Spatrick     // Merge LHS/RHS.
58909467b48Spatrick     LHS =
59009467b48Spatrick         std::make_unique<BinaryExprAST>(BinOp, std::move(LHS), std::move(RHS));
59109467b48Spatrick   }
59209467b48Spatrick }
59309467b48Spatrick 
59409467b48Spatrick /// expression
59509467b48Spatrick ///   ::= unary binoprhs
59609467b48Spatrick ///
ParseExpression()59709467b48Spatrick static std::unique_ptr<ExprAST> ParseExpression() {
59809467b48Spatrick   auto LHS = ParseUnary();
59909467b48Spatrick   if (!LHS)
60009467b48Spatrick     return nullptr;
60109467b48Spatrick 
60209467b48Spatrick   return ParseBinOpRHS(0, std::move(LHS));
60309467b48Spatrick }
60409467b48Spatrick 
60509467b48Spatrick /// prototype
60609467b48Spatrick ///   ::= id '(' id* ')'
60709467b48Spatrick ///   ::= binary LETTER number? (id, id)
60809467b48Spatrick ///   ::= unary LETTER (id)
ParsePrototype()60909467b48Spatrick static std::unique_ptr<PrototypeAST> ParsePrototype() {
61009467b48Spatrick   std::string FnName;
61109467b48Spatrick 
61209467b48Spatrick   unsigned Kind = 0; // 0 = identifier, 1 = unary, 2 = binary.
61309467b48Spatrick   unsigned BinaryPrecedence = 30;
61409467b48Spatrick 
61509467b48Spatrick   switch (CurTok) {
61609467b48Spatrick   default:
61709467b48Spatrick     return LogErrorP("Expected function name in prototype");
61809467b48Spatrick   case tok_identifier:
61909467b48Spatrick     FnName = IdentifierStr;
62009467b48Spatrick     Kind = 0;
62109467b48Spatrick     getNextToken();
62209467b48Spatrick     break;
62309467b48Spatrick   case tok_unary:
62409467b48Spatrick     getNextToken();
62509467b48Spatrick     if (!isascii(CurTok))
62609467b48Spatrick       return LogErrorP("Expected unary operator");
62709467b48Spatrick     FnName = "unary";
62809467b48Spatrick     FnName += (char)CurTok;
62909467b48Spatrick     Kind = 1;
63009467b48Spatrick     getNextToken();
63109467b48Spatrick     break;
63209467b48Spatrick   case tok_binary:
63309467b48Spatrick     getNextToken();
63409467b48Spatrick     if (!isascii(CurTok))
63509467b48Spatrick       return LogErrorP("Expected binary operator");
63609467b48Spatrick     FnName = "binary";
63709467b48Spatrick     FnName += (char)CurTok;
63809467b48Spatrick     Kind = 2;
63909467b48Spatrick     getNextToken();
64009467b48Spatrick 
64109467b48Spatrick     // Read the precedence if present.
64209467b48Spatrick     if (CurTok == tok_number) {
64309467b48Spatrick       if (NumVal < 1 || NumVal > 100)
64409467b48Spatrick         return LogErrorP("Invalid precedence: must be 1..100");
64509467b48Spatrick       BinaryPrecedence = (unsigned)NumVal;
64609467b48Spatrick       getNextToken();
64709467b48Spatrick     }
64809467b48Spatrick     break;
64909467b48Spatrick   }
65009467b48Spatrick 
65109467b48Spatrick   if (CurTok != '(')
65209467b48Spatrick     return LogErrorP("Expected '(' in prototype");
65309467b48Spatrick 
65409467b48Spatrick   std::vector<std::string> ArgNames;
65509467b48Spatrick   while (getNextToken() == tok_identifier)
65609467b48Spatrick     ArgNames.push_back(IdentifierStr);
65709467b48Spatrick   if (CurTok != ')')
65809467b48Spatrick     return LogErrorP("Expected ')' in prototype");
65909467b48Spatrick 
66009467b48Spatrick   // success.
66109467b48Spatrick   getNextToken(); // eat ')'.
66209467b48Spatrick 
66309467b48Spatrick   // Verify right number of names for operator.
66409467b48Spatrick   if (Kind && ArgNames.size() != Kind)
66509467b48Spatrick     return LogErrorP("Invalid number of operands for operator");
66609467b48Spatrick 
66709467b48Spatrick   return std::make_unique<PrototypeAST>(FnName, ArgNames, Kind != 0,
66809467b48Spatrick                                          BinaryPrecedence);
66909467b48Spatrick }
67009467b48Spatrick 
67109467b48Spatrick /// definition ::= 'def' prototype expression
ParseDefinition()67209467b48Spatrick static std::unique_ptr<FunctionAST> ParseDefinition() {
67309467b48Spatrick   getNextToken(); // eat def.
67409467b48Spatrick   auto Proto = ParsePrototype();
67509467b48Spatrick   if (!Proto)
67609467b48Spatrick     return nullptr;
67709467b48Spatrick 
67809467b48Spatrick   if (auto E = ParseExpression())
67909467b48Spatrick     return std::make_unique<FunctionAST>(std::move(Proto), std::move(E));
68009467b48Spatrick   return nullptr;
68109467b48Spatrick }
68209467b48Spatrick 
68309467b48Spatrick /// toplevelexpr ::= expression
ParseTopLevelExpr()68409467b48Spatrick static std::unique_ptr<FunctionAST> ParseTopLevelExpr() {
68509467b48Spatrick   if (auto E = ParseExpression()) {
68609467b48Spatrick     // Make an anonymous proto.
68709467b48Spatrick     auto Proto = std::make_unique<PrototypeAST>("__anon_expr",
68809467b48Spatrick                                                  std::vector<std::string>());
68909467b48Spatrick     return std::make_unique<FunctionAST>(std::move(Proto), std::move(E));
69009467b48Spatrick   }
69109467b48Spatrick   return nullptr;
69209467b48Spatrick }
69309467b48Spatrick 
69409467b48Spatrick /// external ::= 'extern' prototype
ParseExtern()69509467b48Spatrick static std::unique_ptr<PrototypeAST> ParseExtern() {
69609467b48Spatrick   getNextToken(); // eat extern.
69709467b48Spatrick   return ParsePrototype();
69809467b48Spatrick }
69909467b48Spatrick 
70009467b48Spatrick //===----------------------------------------------------------------------===//
70109467b48Spatrick // Code Generation
70209467b48Spatrick //===----------------------------------------------------------------------===//
70309467b48Spatrick 
70473471bf0Spatrick static std::unique_ptr<LLVMContext> TheContext;
70509467b48Spatrick static std::unique_ptr<Module> TheModule;
70673471bf0Spatrick static std::unique_ptr<IRBuilder<>> Builder;
70709467b48Spatrick static std::map<std::string, AllocaInst *> NamedValues;
70809467b48Spatrick static std::unique_ptr<legacy::FunctionPassManager> TheFPM;
70909467b48Spatrick static std::unique_ptr<KaleidoscopeJIT> TheJIT;
71009467b48Spatrick static std::map<std::string, std::unique_ptr<PrototypeAST>> FunctionProtos;
71173471bf0Spatrick static ExitOnError ExitOnErr;
71209467b48Spatrick 
LogErrorV(const char * Str)71309467b48Spatrick Value *LogErrorV(const char *Str) {
71409467b48Spatrick   LogError(Str);
71509467b48Spatrick   return nullptr;
71609467b48Spatrick }
71709467b48Spatrick 
getFunction(std::string Name)71809467b48Spatrick Function *getFunction(std::string Name) {
71909467b48Spatrick   // First, see if the function has already been added to the current module.
72009467b48Spatrick   if (auto *F = TheModule->getFunction(Name))
72109467b48Spatrick     return F;
72209467b48Spatrick 
72309467b48Spatrick   // If not, check whether we can codegen the declaration from some existing
72409467b48Spatrick   // prototype.
72509467b48Spatrick   auto FI = FunctionProtos.find(Name);
72609467b48Spatrick   if (FI != FunctionProtos.end())
72709467b48Spatrick     return FI->second->codegen();
72809467b48Spatrick 
72909467b48Spatrick   // If no existing prototype exists, return null.
73009467b48Spatrick   return nullptr;
73109467b48Spatrick }
73209467b48Spatrick 
73309467b48Spatrick /// CreateEntryBlockAlloca - Create an alloca instruction in the entry block of
73409467b48Spatrick /// the function.  This is used for mutable variables etc.
CreateEntryBlockAlloca(Function * TheFunction,StringRef VarName)73509467b48Spatrick static AllocaInst *CreateEntryBlockAlloca(Function *TheFunction,
736097a140dSpatrick                                           StringRef VarName) {
73709467b48Spatrick   IRBuilder<> TmpB(&TheFunction->getEntryBlock(),
73809467b48Spatrick                    TheFunction->getEntryBlock().begin());
73973471bf0Spatrick   return TmpB.CreateAlloca(Type::getDoubleTy(*TheContext), nullptr, VarName);
74009467b48Spatrick }
74109467b48Spatrick 
codegen()74209467b48Spatrick Value *NumberExprAST::codegen() {
74373471bf0Spatrick   return ConstantFP::get(*TheContext, APFloat(Val));
74409467b48Spatrick }
74509467b48Spatrick 
codegen()74609467b48Spatrick Value *VariableExprAST::codegen() {
74709467b48Spatrick   // Look this variable up in the function.
74873471bf0Spatrick   AllocaInst *A = NamedValues[Name];
74973471bf0Spatrick   if (!A)
75009467b48Spatrick     return LogErrorV("Unknown variable name");
75109467b48Spatrick 
75209467b48Spatrick   // Load the value.
75373471bf0Spatrick   return Builder->CreateLoad(A->getAllocatedType(), A, Name.c_str());
75409467b48Spatrick }
75509467b48Spatrick 
codegen()75609467b48Spatrick Value *UnaryExprAST::codegen() {
75709467b48Spatrick   Value *OperandV = Operand->codegen();
75809467b48Spatrick   if (!OperandV)
75909467b48Spatrick     return nullptr;
76009467b48Spatrick 
76109467b48Spatrick   Function *F = getFunction(std::string("unary") + Opcode);
76209467b48Spatrick   if (!F)
76309467b48Spatrick     return LogErrorV("Unknown unary operator");
76409467b48Spatrick 
76573471bf0Spatrick   return Builder->CreateCall(F, OperandV, "unop");
76609467b48Spatrick }
76709467b48Spatrick 
codegen()76809467b48Spatrick Value *BinaryExprAST::codegen() {
76909467b48Spatrick   // Special case '=' because we don't want to emit the LHS as an expression.
77009467b48Spatrick   if (Op == '=') {
77109467b48Spatrick     // Assignment requires the LHS to be an identifier.
77209467b48Spatrick     // This assume we're building without RTTI because LLVM builds that way by
77309467b48Spatrick     // default.  If you build LLVM with RTTI this can be changed to a
77409467b48Spatrick     // dynamic_cast for automatic error checking.
77509467b48Spatrick     VariableExprAST *LHSE = static_cast<VariableExprAST *>(LHS.get());
77609467b48Spatrick     if (!LHSE)
77709467b48Spatrick       return LogErrorV("destination of '=' must be a variable");
77809467b48Spatrick     // Codegen the RHS.
77909467b48Spatrick     Value *Val = RHS->codegen();
78009467b48Spatrick     if (!Val)
78109467b48Spatrick       return nullptr;
78209467b48Spatrick 
78309467b48Spatrick     // Look up the name.
78409467b48Spatrick     Value *Variable = NamedValues[LHSE->getName()];
78509467b48Spatrick     if (!Variable)
78609467b48Spatrick       return LogErrorV("Unknown variable name");
78709467b48Spatrick 
78873471bf0Spatrick     Builder->CreateStore(Val, Variable);
78909467b48Spatrick     return Val;
79009467b48Spatrick   }
79109467b48Spatrick 
79209467b48Spatrick   Value *L = LHS->codegen();
79309467b48Spatrick   Value *R = RHS->codegen();
79409467b48Spatrick   if (!L || !R)
79509467b48Spatrick     return nullptr;
79609467b48Spatrick 
79709467b48Spatrick   switch (Op) {
79809467b48Spatrick   case '+':
79973471bf0Spatrick     return Builder->CreateFAdd(L, R, "addtmp");
80009467b48Spatrick   case '-':
80173471bf0Spatrick     return Builder->CreateFSub(L, R, "subtmp");
80209467b48Spatrick   case '*':
80373471bf0Spatrick     return Builder->CreateFMul(L, R, "multmp");
80409467b48Spatrick   case '<':
80573471bf0Spatrick     L = Builder->CreateFCmpULT(L, R, "cmptmp");
80609467b48Spatrick     // Convert bool 0/1 to double 0.0 or 1.0
80773471bf0Spatrick     return Builder->CreateUIToFP(L, Type::getDoubleTy(*TheContext), "booltmp");
80809467b48Spatrick   default:
80909467b48Spatrick     break;
81009467b48Spatrick   }
81109467b48Spatrick 
81209467b48Spatrick   // If it wasn't a builtin binary operator, it must be a user defined one. Emit
81309467b48Spatrick   // a call to it.
81409467b48Spatrick   Function *F = getFunction(std::string("binary") + Op);
81509467b48Spatrick   assert(F && "binary operator not found!");
81609467b48Spatrick 
81709467b48Spatrick   Value *Ops[] = {L, R};
81873471bf0Spatrick   return Builder->CreateCall(F, Ops, "binop");
81909467b48Spatrick }
82009467b48Spatrick 
codegen()82109467b48Spatrick Value *CallExprAST::codegen() {
82209467b48Spatrick   // Look up the name in the global module table.
82309467b48Spatrick   Function *CalleeF = getFunction(Callee);
82409467b48Spatrick   if (!CalleeF)
82509467b48Spatrick     return LogErrorV("Unknown function referenced");
82609467b48Spatrick 
82709467b48Spatrick   // If argument mismatch error.
82809467b48Spatrick   if (CalleeF->arg_size() != Args.size())
82909467b48Spatrick     return LogErrorV("Incorrect # arguments passed");
83009467b48Spatrick 
83109467b48Spatrick   std::vector<Value *> ArgsV;
83209467b48Spatrick   for (unsigned i = 0, e = Args.size(); i != e; ++i) {
83309467b48Spatrick     ArgsV.push_back(Args[i]->codegen());
83409467b48Spatrick     if (!ArgsV.back())
83509467b48Spatrick       return nullptr;
83609467b48Spatrick   }
83709467b48Spatrick 
83873471bf0Spatrick   return Builder->CreateCall(CalleeF, ArgsV, "calltmp");
83909467b48Spatrick }
84009467b48Spatrick 
codegen()84109467b48Spatrick Value *IfExprAST::codegen() {
84209467b48Spatrick   Value *CondV = Cond->codegen();
84309467b48Spatrick   if (!CondV)
84409467b48Spatrick     return nullptr;
84509467b48Spatrick 
84609467b48Spatrick   // Convert condition to a bool by comparing non-equal to 0.0.
84773471bf0Spatrick   CondV = Builder->CreateFCmpONE(
84873471bf0Spatrick       CondV, ConstantFP::get(*TheContext, APFloat(0.0)), "ifcond");
84909467b48Spatrick 
85073471bf0Spatrick   Function *TheFunction = Builder->GetInsertBlock()->getParent();
85109467b48Spatrick 
85209467b48Spatrick   // Create blocks for the then and else cases.  Insert the 'then' block at the
85309467b48Spatrick   // end of the function.
85473471bf0Spatrick   BasicBlock *ThenBB = BasicBlock::Create(*TheContext, "then", TheFunction);
85573471bf0Spatrick   BasicBlock *ElseBB = BasicBlock::Create(*TheContext, "else");
85673471bf0Spatrick   BasicBlock *MergeBB = BasicBlock::Create(*TheContext, "ifcont");
85709467b48Spatrick 
85873471bf0Spatrick   Builder->CreateCondBr(CondV, ThenBB, ElseBB);
85909467b48Spatrick 
86009467b48Spatrick   // Emit then value.
86173471bf0Spatrick   Builder->SetInsertPoint(ThenBB);
86209467b48Spatrick 
86309467b48Spatrick   Value *ThenV = Then->codegen();
86409467b48Spatrick   if (!ThenV)
86509467b48Spatrick     return nullptr;
86609467b48Spatrick 
86773471bf0Spatrick   Builder->CreateBr(MergeBB);
86809467b48Spatrick   // Codegen of 'Then' can change the current block, update ThenBB for the PHI.
86973471bf0Spatrick   ThenBB = Builder->GetInsertBlock();
87009467b48Spatrick 
87109467b48Spatrick   // Emit else block.
872*d415bd75Srobert   TheFunction->insert(TheFunction->end(), ElseBB);
87373471bf0Spatrick   Builder->SetInsertPoint(ElseBB);
87409467b48Spatrick 
87509467b48Spatrick   Value *ElseV = Else->codegen();
87609467b48Spatrick   if (!ElseV)
87709467b48Spatrick     return nullptr;
87809467b48Spatrick 
87973471bf0Spatrick   Builder->CreateBr(MergeBB);
88009467b48Spatrick   // Codegen of 'Else' can change the current block, update ElseBB for the PHI.
88173471bf0Spatrick   ElseBB = Builder->GetInsertBlock();
88209467b48Spatrick 
88309467b48Spatrick   // Emit merge block.
884*d415bd75Srobert   TheFunction->insert(TheFunction->end(), MergeBB);
88573471bf0Spatrick   Builder->SetInsertPoint(MergeBB);
88673471bf0Spatrick   PHINode *PN = Builder->CreatePHI(Type::getDoubleTy(*TheContext), 2, "iftmp");
88709467b48Spatrick 
88809467b48Spatrick   PN->addIncoming(ThenV, ThenBB);
88909467b48Spatrick   PN->addIncoming(ElseV, ElseBB);
89009467b48Spatrick   return PN;
89109467b48Spatrick }
89209467b48Spatrick 
89309467b48Spatrick // Output for-loop as:
89409467b48Spatrick //   var = alloca double
89509467b48Spatrick //   ...
89609467b48Spatrick //   start = startexpr
89709467b48Spatrick //   store start -> var
89809467b48Spatrick //   goto loop
89909467b48Spatrick // loop:
90009467b48Spatrick //   ...
90109467b48Spatrick //   bodyexpr
90209467b48Spatrick //   ...
90309467b48Spatrick // loopend:
90409467b48Spatrick //   step = stepexpr
90509467b48Spatrick //   endcond = endexpr
90609467b48Spatrick //
90709467b48Spatrick //   curvar = load var
90809467b48Spatrick //   nextvar = curvar + step
90909467b48Spatrick //   store nextvar -> var
91009467b48Spatrick //   br endcond, loop, endloop
91109467b48Spatrick // outloop:
codegen()91209467b48Spatrick Value *ForExprAST::codegen() {
91373471bf0Spatrick   Function *TheFunction = Builder->GetInsertBlock()->getParent();
91409467b48Spatrick 
91509467b48Spatrick   // Create an alloca for the variable in the entry block.
91609467b48Spatrick   AllocaInst *Alloca = CreateEntryBlockAlloca(TheFunction, VarName);
91709467b48Spatrick 
91809467b48Spatrick   // Emit the start code first, without 'variable' in scope.
91909467b48Spatrick   Value *StartVal = Start->codegen();
92009467b48Spatrick   if (!StartVal)
92109467b48Spatrick     return nullptr;
92209467b48Spatrick 
92309467b48Spatrick   // Store the value into the alloca.
92473471bf0Spatrick   Builder->CreateStore(StartVal, Alloca);
92509467b48Spatrick 
92609467b48Spatrick   // Make the new basic block for the loop header, inserting after current
92709467b48Spatrick   // block.
92873471bf0Spatrick   BasicBlock *LoopBB = BasicBlock::Create(*TheContext, "loop", TheFunction);
92909467b48Spatrick 
93009467b48Spatrick   // Insert an explicit fall through from the current block to the LoopBB.
93173471bf0Spatrick   Builder->CreateBr(LoopBB);
93209467b48Spatrick 
93309467b48Spatrick   // Start insertion in LoopBB.
93473471bf0Spatrick   Builder->SetInsertPoint(LoopBB);
93509467b48Spatrick 
93609467b48Spatrick   // Within the loop, the variable is defined equal to the PHI node.  If it
93709467b48Spatrick   // shadows an existing variable, we have to restore it, so save it now.
93809467b48Spatrick   AllocaInst *OldVal = NamedValues[VarName];
93909467b48Spatrick   NamedValues[VarName] = Alloca;
94009467b48Spatrick 
94109467b48Spatrick   // Emit the body of the loop.  This, like any other expr, can change the
94209467b48Spatrick   // current BB.  Note that we ignore the value computed by the body, but don't
94309467b48Spatrick   // allow an error.
94409467b48Spatrick   if (!Body->codegen())
94509467b48Spatrick     return nullptr;
94609467b48Spatrick 
94709467b48Spatrick   // Emit the step value.
94809467b48Spatrick   Value *StepVal = nullptr;
94909467b48Spatrick   if (Step) {
95009467b48Spatrick     StepVal = Step->codegen();
95109467b48Spatrick     if (!StepVal)
95209467b48Spatrick       return nullptr;
95309467b48Spatrick   } else {
95409467b48Spatrick     // If not specified, use 1.0.
95573471bf0Spatrick     StepVal = ConstantFP::get(*TheContext, APFloat(1.0));
95609467b48Spatrick   }
95709467b48Spatrick 
95809467b48Spatrick   // Compute the end condition.
95909467b48Spatrick   Value *EndCond = End->codegen();
96009467b48Spatrick   if (!EndCond)
96109467b48Spatrick     return nullptr;
96209467b48Spatrick 
96309467b48Spatrick   // Reload, increment, and restore the alloca.  This handles the case where
96409467b48Spatrick   // the body of the loop mutates the variable.
96573471bf0Spatrick   Value *CurVar =
96673471bf0Spatrick       Builder->CreateLoad(Alloca->getAllocatedType(), Alloca, VarName.c_str());
96773471bf0Spatrick   Value *NextVar = Builder->CreateFAdd(CurVar, StepVal, "nextvar");
96873471bf0Spatrick   Builder->CreateStore(NextVar, Alloca);
96909467b48Spatrick 
97009467b48Spatrick   // Convert condition to a bool by comparing non-equal to 0.0.
97173471bf0Spatrick   EndCond = Builder->CreateFCmpONE(
97273471bf0Spatrick       EndCond, ConstantFP::get(*TheContext, APFloat(0.0)), "loopcond");
97309467b48Spatrick 
97409467b48Spatrick   // Create the "after loop" block and insert it.
97509467b48Spatrick   BasicBlock *AfterBB =
97673471bf0Spatrick       BasicBlock::Create(*TheContext, "afterloop", TheFunction);
97709467b48Spatrick 
97809467b48Spatrick   // Insert the conditional branch into the end of LoopEndBB.
97973471bf0Spatrick   Builder->CreateCondBr(EndCond, LoopBB, AfterBB);
98009467b48Spatrick 
98109467b48Spatrick   // Any new code will be inserted in AfterBB.
98273471bf0Spatrick   Builder->SetInsertPoint(AfterBB);
98309467b48Spatrick 
98409467b48Spatrick   // Restore the unshadowed variable.
98509467b48Spatrick   if (OldVal)
98609467b48Spatrick     NamedValues[VarName] = OldVal;
98709467b48Spatrick   else
98809467b48Spatrick     NamedValues.erase(VarName);
98909467b48Spatrick 
99009467b48Spatrick   // for expr always returns 0.0.
99173471bf0Spatrick   return Constant::getNullValue(Type::getDoubleTy(*TheContext));
99209467b48Spatrick }
99309467b48Spatrick 
codegen()99409467b48Spatrick Value *VarExprAST::codegen() {
99509467b48Spatrick   std::vector<AllocaInst *> OldBindings;
99609467b48Spatrick 
99773471bf0Spatrick   Function *TheFunction = Builder->GetInsertBlock()->getParent();
99809467b48Spatrick 
99909467b48Spatrick   // Register all variables and emit their initializer.
100009467b48Spatrick   for (unsigned i = 0, e = VarNames.size(); i != e; ++i) {
100109467b48Spatrick     const std::string &VarName = VarNames[i].first;
100209467b48Spatrick     ExprAST *Init = VarNames[i].second.get();
100309467b48Spatrick 
100409467b48Spatrick     // Emit the initializer before adding the variable to scope, this prevents
100509467b48Spatrick     // the initializer from referencing the variable itself, and permits stuff
100609467b48Spatrick     // like this:
100709467b48Spatrick     //  var a = 1 in
100809467b48Spatrick     //    var a = a in ...   # refers to outer 'a'.
100909467b48Spatrick     Value *InitVal;
101009467b48Spatrick     if (Init) {
101109467b48Spatrick       InitVal = Init->codegen();
101209467b48Spatrick       if (!InitVal)
101309467b48Spatrick         return nullptr;
101409467b48Spatrick     } else { // If not specified, use 0.0.
101573471bf0Spatrick       InitVal = ConstantFP::get(*TheContext, APFloat(0.0));
101609467b48Spatrick     }
101709467b48Spatrick 
101809467b48Spatrick     AllocaInst *Alloca = CreateEntryBlockAlloca(TheFunction, VarName);
101973471bf0Spatrick     Builder->CreateStore(InitVal, Alloca);
102009467b48Spatrick 
102109467b48Spatrick     // Remember the old variable binding so that we can restore the binding when
102209467b48Spatrick     // we unrecurse.
102309467b48Spatrick     OldBindings.push_back(NamedValues[VarName]);
102409467b48Spatrick 
102509467b48Spatrick     // Remember this binding.
102609467b48Spatrick     NamedValues[VarName] = Alloca;
102709467b48Spatrick   }
102809467b48Spatrick 
102909467b48Spatrick   // Codegen the body, now that all vars are in scope.
103009467b48Spatrick   Value *BodyVal = Body->codegen();
103109467b48Spatrick   if (!BodyVal)
103209467b48Spatrick     return nullptr;
103309467b48Spatrick 
103409467b48Spatrick   // Pop all our variables from scope.
103509467b48Spatrick   for (unsigned i = 0, e = VarNames.size(); i != e; ++i)
103609467b48Spatrick     NamedValues[VarNames[i].first] = OldBindings[i];
103709467b48Spatrick 
103809467b48Spatrick   // Return the body computation.
103909467b48Spatrick   return BodyVal;
104009467b48Spatrick }
104109467b48Spatrick 
codegen()104209467b48Spatrick Function *PrototypeAST::codegen() {
104309467b48Spatrick   // Make the function type:  double(double,double) etc.
104473471bf0Spatrick   std::vector<Type *> Doubles(Args.size(), Type::getDoubleTy(*TheContext));
104509467b48Spatrick   FunctionType *FT =
104673471bf0Spatrick       FunctionType::get(Type::getDoubleTy(*TheContext), Doubles, false);
104709467b48Spatrick 
104809467b48Spatrick   Function *F =
104909467b48Spatrick       Function::Create(FT, Function::ExternalLinkage, Name, TheModule.get());
105009467b48Spatrick 
105109467b48Spatrick   // Set names for all arguments.
105209467b48Spatrick   unsigned Idx = 0;
105309467b48Spatrick   for (auto &Arg : F->args())
105409467b48Spatrick     Arg.setName(Args[Idx++]);
105509467b48Spatrick 
105609467b48Spatrick   return F;
105709467b48Spatrick }
105809467b48Spatrick 
codegen()105909467b48Spatrick Function *FunctionAST::codegen() {
106009467b48Spatrick   // Transfer ownership of the prototype to the FunctionProtos map, but keep a
106109467b48Spatrick   // reference to it for use below.
106209467b48Spatrick   auto &P = *Proto;
106309467b48Spatrick   FunctionProtos[Proto->getName()] = std::move(Proto);
106409467b48Spatrick   Function *TheFunction = getFunction(P.getName());
106509467b48Spatrick   if (!TheFunction)
106609467b48Spatrick     return nullptr;
106709467b48Spatrick 
106809467b48Spatrick   // If this is an operator, install it.
106909467b48Spatrick   if (P.isBinaryOp())
107009467b48Spatrick     BinopPrecedence[P.getOperatorName()] = P.getBinaryPrecedence();
107109467b48Spatrick 
107209467b48Spatrick   // Create a new basic block to start insertion into.
107373471bf0Spatrick   BasicBlock *BB = BasicBlock::Create(*TheContext, "entry", TheFunction);
107473471bf0Spatrick   Builder->SetInsertPoint(BB);
107509467b48Spatrick 
107609467b48Spatrick   // Record the function arguments in the NamedValues map.
107709467b48Spatrick   NamedValues.clear();
107809467b48Spatrick   for (auto &Arg : TheFunction->args()) {
107909467b48Spatrick     // Create an alloca for this variable.
108009467b48Spatrick     AllocaInst *Alloca = CreateEntryBlockAlloca(TheFunction, Arg.getName());
108109467b48Spatrick 
108209467b48Spatrick     // Store the initial value into the alloca.
108373471bf0Spatrick     Builder->CreateStore(&Arg, Alloca);
108409467b48Spatrick 
108509467b48Spatrick     // Add arguments to variable symbol table.
1086097a140dSpatrick     NamedValues[std::string(Arg.getName())] = Alloca;
108709467b48Spatrick   }
108809467b48Spatrick 
108909467b48Spatrick   if (Value *RetVal = Body->codegen()) {
109009467b48Spatrick     // Finish off the function.
109173471bf0Spatrick     Builder->CreateRet(RetVal);
109209467b48Spatrick 
109309467b48Spatrick     // Validate the generated code, checking for consistency.
109409467b48Spatrick     verifyFunction(*TheFunction);
109509467b48Spatrick 
109609467b48Spatrick     // Run the optimizer on the function.
109709467b48Spatrick     TheFPM->run(*TheFunction);
109809467b48Spatrick 
109909467b48Spatrick     return TheFunction;
110009467b48Spatrick   }
110109467b48Spatrick 
110209467b48Spatrick   // Error reading body, remove function.
110309467b48Spatrick   TheFunction->eraseFromParent();
110409467b48Spatrick 
110509467b48Spatrick   if (P.isBinaryOp())
110609467b48Spatrick     BinopPrecedence.erase(P.getOperatorName());
110709467b48Spatrick   return nullptr;
110809467b48Spatrick }
110909467b48Spatrick 
111009467b48Spatrick //===----------------------------------------------------------------------===//
111109467b48Spatrick // Top-Level parsing and JIT Driver
111209467b48Spatrick //===----------------------------------------------------------------------===//
111309467b48Spatrick 
InitializeModuleAndPassManager()111409467b48Spatrick static void InitializeModuleAndPassManager() {
111509467b48Spatrick   // Open a new module.
111673471bf0Spatrick   TheContext = std::make_unique<LLVMContext>();
111773471bf0Spatrick   TheModule = std::make_unique<Module>("my cool jit", *TheContext);
111873471bf0Spatrick   TheModule->setDataLayout(TheJIT->getDataLayout());
111973471bf0Spatrick 
112073471bf0Spatrick   // Create a new builder for the module.
112173471bf0Spatrick   Builder = std::make_unique<IRBuilder<>>(*TheContext);
112209467b48Spatrick 
112309467b48Spatrick   // Create a new pass manager attached to it.
112409467b48Spatrick   TheFPM = std::make_unique<legacy::FunctionPassManager>(TheModule.get());
112509467b48Spatrick 
112609467b48Spatrick   // Promote allocas to registers.
112709467b48Spatrick   TheFPM->add(createPromoteMemoryToRegisterPass());
112809467b48Spatrick   // Do simple "peephole" optimizations and bit-twiddling optzns.
112909467b48Spatrick   TheFPM->add(createInstructionCombiningPass());
113009467b48Spatrick   // Reassociate expressions.
113109467b48Spatrick   TheFPM->add(createReassociatePass());
113209467b48Spatrick   // Eliminate Common SubExpressions.
113309467b48Spatrick   TheFPM->add(createGVNPass());
113409467b48Spatrick   // Simplify the control flow graph (deleting unreachable blocks, etc).
113509467b48Spatrick   TheFPM->add(createCFGSimplificationPass());
113609467b48Spatrick 
113709467b48Spatrick   TheFPM->doInitialization();
113809467b48Spatrick }
113909467b48Spatrick 
HandleDefinition()114009467b48Spatrick static void HandleDefinition() {
114109467b48Spatrick   if (auto FnAST = ParseDefinition()) {
114209467b48Spatrick     if (auto *FnIR = FnAST->codegen()) {
114309467b48Spatrick       fprintf(stderr, "Read function definition:");
114409467b48Spatrick       FnIR->print(errs());
114509467b48Spatrick       fprintf(stderr, "\n");
114673471bf0Spatrick       ExitOnErr(TheJIT->addModule(
114773471bf0Spatrick           ThreadSafeModule(std::move(TheModule), std::move(TheContext))));
114809467b48Spatrick       InitializeModuleAndPassManager();
114909467b48Spatrick     }
115009467b48Spatrick   } else {
115109467b48Spatrick     // Skip token for error recovery.
115209467b48Spatrick     getNextToken();
115309467b48Spatrick   }
115409467b48Spatrick }
115509467b48Spatrick 
HandleExtern()115609467b48Spatrick static void HandleExtern() {
115709467b48Spatrick   if (auto ProtoAST = ParseExtern()) {
115809467b48Spatrick     if (auto *FnIR = ProtoAST->codegen()) {
115909467b48Spatrick       fprintf(stderr, "Read extern: ");
116009467b48Spatrick       FnIR->print(errs());
116109467b48Spatrick       fprintf(stderr, "\n");
116209467b48Spatrick       FunctionProtos[ProtoAST->getName()] = std::move(ProtoAST);
116309467b48Spatrick     }
116409467b48Spatrick   } else {
116509467b48Spatrick     // Skip token for error recovery.
116609467b48Spatrick     getNextToken();
116709467b48Spatrick   }
116809467b48Spatrick }
116909467b48Spatrick 
HandleTopLevelExpression()117009467b48Spatrick static void HandleTopLevelExpression() {
117109467b48Spatrick   // Evaluate a top-level expression into an anonymous function.
117209467b48Spatrick   if (auto FnAST = ParseTopLevelExpr()) {
117309467b48Spatrick     if (FnAST->codegen()) {
117473471bf0Spatrick       // Create a ResourceTracker to track JIT'd memory allocated to our
117573471bf0Spatrick       // anonymous expression -- that way we can free it after executing.
117673471bf0Spatrick       auto RT = TheJIT->getMainJITDylib().createResourceTracker();
117773471bf0Spatrick 
117873471bf0Spatrick       auto TSM = ThreadSafeModule(std::move(TheModule), std::move(TheContext));
117973471bf0Spatrick       ExitOnErr(TheJIT->addModule(std::move(TSM), RT));
118009467b48Spatrick       InitializeModuleAndPassManager();
118109467b48Spatrick 
118209467b48Spatrick       // Search the JIT for the __anon_expr symbol.
118373471bf0Spatrick       auto ExprSymbol = ExitOnErr(TheJIT->lookup("__anon_expr"));
118409467b48Spatrick 
118509467b48Spatrick       // Get the symbol's address and cast it to the right type (takes no
118609467b48Spatrick       // arguments, returns a double) so we can call it as a native function.
118773471bf0Spatrick       double (*FP)() = (double (*)())(intptr_t)ExprSymbol.getAddress();
118809467b48Spatrick       fprintf(stderr, "Evaluated to %f\n", FP());
118909467b48Spatrick 
119009467b48Spatrick       // Delete the anonymous expression module from the JIT.
119173471bf0Spatrick       ExitOnErr(RT->remove());
119209467b48Spatrick     }
119309467b48Spatrick   } else {
119409467b48Spatrick     // Skip token for error recovery.
119509467b48Spatrick     getNextToken();
119609467b48Spatrick   }
119709467b48Spatrick }
119809467b48Spatrick 
119909467b48Spatrick /// top ::= definition | external | expression | ';'
MainLoop()120009467b48Spatrick static void MainLoop() {
120109467b48Spatrick   while (true) {
120209467b48Spatrick     fprintf(stderr, "ready> ");
120309467b48Spatrick     switch (CurTok) {
120409467b48Spatrick     case tok_eof:
120509467b48Spatrick       return;
120609467b48Spatrick     case ';': // ignore top-level semicolons.
120709467b48Spatrick       getNextToken();
120809467b48Spatrick       break;
120909467b48Spatrick     case tok_def:
121009467b48Spatrick       HandleDefinition();
121109467b48Spatrick       break;
121209467b48Spatrick     case tok_extern:
121309467b48Spatrick       HandleExtern();
121409467b48Spatrick       break;
121509467b48Spatrick     default:
121609467b48Spatrick       HandleTopLevelExpression();
121709467b48Spatrick       break;
121809467b48Spatrick     }
121909467b48Spatrick   }
122009467b48Spatrick }
122109467b48Spatrick 
122209467b48Spatrick //===----------------------------------------------------------------------===//
122309467b48Spatrick // "Library" functions that can be "extern'd" from user code.
122409467b48Spatrick //===----------------------------------------------------------------------===//
122509467b48Spatrick 
122609467b48Spatrick #ifdef _WIN32
122709467b48Spatrick #define DLLEXPORT __declspec(dllexport)
122809467b48Spatrick #else
122909467b48Spatrick #define DLLEXPORT
123009467b48Spatrick #endif
123109467b48Spatrick 
123209467b48Spatrick /// putchard - putchar that takes a double and returns 0.
putchard(double X)123309467b48Spatrick extern "C" DLLEXPORT double putchard(double X) {
123409467b48Spatrick   fputc((char)X, stderr);
123509467b48Spatrick   return 0;
123609467b48Spatrick }
123709467b48Spatrick 
123809467b48Spatrick /// printd - printf that takes a double prints it as "%f\n", returning 0.
printd(double X)123909467b48Spatrick extern "C" DLLEXPORT double printd(double X) {
124009467b48Spatrick   fprintf(stderr, "%f\n", X);
124109467b48Spatrick   return 0;
124209467b48Spatrick }
124309467b48Spatrick 
124409467b48Spatrick //===----------------------------------------------------------------------===//
124509467b48Spatrick // Main driver code.
124609467b48Spatrick //===----------------------------------------------------------------------===//
124709467b48Spatrick 
main()124809467b48Spatrick int main() {
124909467b48Spatrick   InitializeNativeTarget();
125009467b48Spatrick   InitializeNativeTargetAsmPrinter();
125109467b48Spatrick   InitializeNativeTargetAsmParser();
125209467b48Spatrick 
125309467b48Spatrick   // Install standard binary operators.
125409467b48Spatrick   // 1 is lowest precedence.
125509467b48Spatrick   BinopPrecedence['='] = 2;
125609467b48Spatrick   BinopPrecedence['<'] = 10;
125709467b48Spatrick   BinopPrecedence['+'] = 20;
125809467b48Spatrick   BinopPrecedence['-'] = 20;
125909467b48Spatrick   BinopPrecedence['*'] = 40; // highest.
126009467b48Spatrick 
126109467b48Spatrick   // Prime the first token.
126209467b48Spatrick   fprintf(stderr, "ready> ");
126309467b48Spatrick   getNextToken();
126409467b48Spatrick 
126573471bf0Spatrick   TheJIT = ExitOnErr(KaleidoscopeJIT::Create());
126609467b48Spatrick 
126709467b48Spatrick   InitializeModuleAndPassManager();
126809467b48Spatrick 
126909467b48Spatrick   // Run the main "interpreter loop" now.
127009467b48Spatrick   MainLoop();
127109467b48Spatrick 
127209467b48Spatrick   return 0;
127309467b48Spatrick }
1274