NAKAMURA Takumi | 85c9bac | 2015-03-02 01:04:34 +0000 | [diff] [blame] | 1 | #include "llvm/ADT/STLExtras.h" |
Chandler Carruth | 605e30e | 2012-12-04 10:16:57 +0000 | [diff] [blame] | 2 | #include "llvm/Analysis/Passes.h" |
Chandler Carruth | 005f27a | 2013-01-02 11:56:33 +0000 | [diff] [blame] | 3 | #include "llvm/IR/IRBuilder.h" |
| 4 | #include "llvm/IR/LLVMContext.h" |
Chandler Carruth | 30d69c2 | 2015-02-13 10:01:29 +0000 | [diff] [blame] | 5 | #include "llvm/IR/LegacyPassManager.h" |
Chandler Carruth | 005f27a | 2013-01-02 11:56:33 +0000 | [diff] [blame] | 6 | #include "llvm/IR/Module.h" |
Chandler Carruth | 20d4e6b | 2014-01-13 09:58:03 +0000 | [diff] [blame] | 7 | #include "llvm/IR/Verifier.h" |
Evan Cheng | 2bb4035 | 2011-08-24 18:08:43 +0000 | [diff] [blame] | 8 | #include "llvm/Support/TargetSelect.h" |
Chandler Carruth | 605e30e | 2012-12-04 10:16:57 +0000 | [diff] [blame] | 9 | #include "llvm/Transforms/Scalar.h" |
Chandler Carruth | ec5872b | 2016-03-11 12:10:15 +0000 | [diff] [blame^] | 10 | #include "llvm/Transforms/Scalar/GVN.h" |
Will Dietz | 981af00 | 2013-10-12 00:55:57 +0000 | [diff] [blame] | 11 | #include <cctype> |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 12 | #include <cstdio> |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 13 | #include <map> |
Chandler Carruth | 605e30e | 2012-12-04 10:16:57 +0000 | [diff] [blame] | 14 | #include <string> |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 15 | #include <vector> |
Lang Hames | 2d789c3 | 2015-08-26 03:07:41 +0000 | [diff] [blame] | 16 | #include "../include/KaleidoscopeJIT.h" |
| 17 | |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 18 | using namespace llvm; |
Lang Hames | 2d789c3 | 2015-08-26 03:07:41 +0000 | [diff] [blame] | 19 | using namespace llvm::orc; |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 20 | |
| 21 | //===----------------------------------------------------------------------===// |
| 22 | // Lexer |
| 23 | //===----------------------------------------------------------------------===// |
| 24 | |
| 25 | // The lexer returns tokens [0-255] if it is an unknown character, otherwise one |
| 26 | // of these for known things. |
| 27 | enum Token { |
| 28 | tok_eof = -1, |
| 29 | |
| 30 | // commands |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 31 | tok_def = -2, |
| 32 | tok_extern = -3, |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 33 | |
| 34 | // primary |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 35 | tok_identifier = -4, |
| 36 | tok_number = -5, |
| 37 | |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 38 | // control |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 39 | tok_if = -6, |
| 40 | tok_then = -7, |
| 41 | tok_else = -8, |
| 42 | tok_for = -9, |
| 43 | tok_in = -10 |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 44 | }; |
| 45 | |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 46 | static std::string IdentifierStr; // Filled in if tok_identifier |
| 47 | static double NumVal; // Filled in if tok_number |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 48 | |
| 49 | /// gettok - Return the next token from standard input. |
| 50 | static int gettok() { |
| 51 | static int LastChar = ' '; |
| 52 | |
| 53 | // Skip any whitespace. |
| 54 | while (isspace(LastChar)) |
| 55 | LastChar = getchar(); |
| 56 | |
| 57 | if (isalpha(LastChar)) { // identifier: [a-zA-Z][a-zA-Z0-9]* |
| 58 | IdentifierStr = LastChar; |
| 59 | while (isalnum((LastChar = getchar()))) |
| 60 | IdentifierStr += LastChar; |
| 61 | |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 62 | if (IdentifierStr == "def") |
| 63 | return tok_def; |
| 64 | if (IdentifierStr == "extern") |
| 65 | return tok_extern; |
| 66 | if (IdentifierStr == "if") |
| 67 | return tok_if; |
| 68 | if (IdentifierStr == "then") |
| 69 | return tok_then; |
| 70 | if (IdentifierStr == "else") |
| 71 | return tok_else; |
| 72 | if (IdentifierStr == "for") |
| 73 | return tok_for; |
| 74 | if (IdentifierStr == "in") |
| 75 | return tok_in; |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 76 | return tok_identifier; |
| 77 | } |
| 78 | |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 79 | if (isdigit(LastChar) || LastChar == '.') { // Number: [0-9.]+ |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 80 | std::string NumStr; |
| 81 | do { |
| 82 | NumStr += LastChar; |
| 83 | LastChar = getchar(); |
| 84 | } while (isdigit(LastChar) || LastChar == '.'); |
| 85 | |
Hans Wennborg | cc9deb4 | 2015-09-29 18:02:48 +0000 | [diff] [blame] | 86 | NumVal = strtod(NumStr.c_str(), nullptr); |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 87 | return tok_number; |
| 88 | } |
| 89 | |
| 90 | if (LastChar == '#') { |
| 91 | // Comment until end of line. |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 92 | do |
| 93 | LastChar = getchar(); |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 94 | while (LastChar != EOF && LastChar != '\n' && LastChar != '\r'); |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 95 | |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 96 | if (LastChar != EOF) |
| 97 | return gettok(); |
| 98 | } |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 99 | |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 100 | // Check for end of file. Don't eat the EOF. |
| 101 | if (LastChar == EOF) |
| 102 | return tok_eof; |
| 103 | |
| 104 | // Otherwise, just return the character as its ascii value. |
| 105 | int ThisChar = LastChar; |
| 106 | LastChar = getchar(); |
| 107 | return ThisChar; |
| 108 | } |
| 109 | |
| 110 | //===----------------------------------------------------------------------===// |
| 111 | // Abstract Syntax Tree (aka Parse Tree) |
| 112 | //===----------------------------------------------------------------------===// |
Juergen Ributzka | 05c5a93 | 2013-11-19 03:08:35 +0000 | [diff] [blame] | 113 | namespace { |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 114 | /// ExprAST - Base class for all expression nodes. |
| 115 | class ExprAST { |
| 116 | public: |
Juergen Ributzka | 05c5a93 | 2013-11-19 03:08:35 +0000 | [diff] [blame] | 117 | virtual ~ExprAST() {} |
Lang Hames | 2d789c3 | 2015-08-26 03:07:41 +0000 | [diff] [blame] | 118 | virtual Value *codegen() = 0; |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 119 | }; |
| 120 | |
| 121 | /// NumberExprAST - Expression class for numeric literals like "1.0". |
| 122 | class NumberExprAST : public ExprAST { |
| 123 | double Val; |
Lang Hames | 59b0da8 | 2015-08-19 18:15:58 +0000 | [diff] [blame] | 124 | |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 125 | public: |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 126 | NumberExprAST(double Val) : Val(Val) {} |
Lang Hames | 2d789c3 | 2015-08-26 03:07:41 +0000 | [diff] [blame] | 127 | Value *codegen() override; |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 128 | }; |
| 129 | |
| 130 | /// VariableExprAST - Expression class for referencing a variable, like "a". |
| 131 | class VariableExprAST : public ExprAST { |
| 132 | std::string Name; |
Lang Hames | 59b0da8 | 2015-08-19 18:15:58 +0000 | [diff] [blame] | 133 | |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 134 | public: |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 135 | VariableExprAST(const std::string &Name) : Name(Name) {} |
Lang Hames | 2d789c3 | 2015-08-26 03:07:41 +0000 | [diff] [blame] | 136 | Value *codegen() override; |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 137 | }; |
| 138 | |
| 139 | /// BinaryExprAST - Expression class for a binary operator. |
| 140 | class BinaryExprAST : public ExprAST { |
| 141 | char Op; |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 142 | std::unique_ptr<ExprAST> LHS, RHS; |
Lang Hames | 59b0da8 | 2015-08-19 18:15:58 +0000 | [diff] [blame] | 143 | |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 144 | public: |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 145 | BinaryExprAST(char Op, std::unique_ptr<ExprAST> LHS, |
| 146 | std::unique_ptr<ExprAST> RHS) |
| 147 | : Op(Op), LHS(std::move(LHS)), RHS(std::move(RHS)) {} |
Lang Hames | 2d789c3 | 2015-08-26 03:07:41 +0000 | [diff] [blame] | 148 | Value *codegen() override; |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 149 | }; |
| 150 | |
| 151 | /// CallExprAST - Expression class for function calls. |
| 152 | class CallExprAST : public ExprAST { |
| 153 | std::string Callee; |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 154 | std::vector<std::unique_ptr<ExprAST>> Args; |
Lang Hames | 59b0da8 | 2015-08-19 18:15:58 +0000 | [diff] [blame] | 155 | |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 156 | public: |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 157 | CallExprAST(const std::string &Callee, |
| 158 | std::vector<std::unique_ptr<ExprAST>> Args) |
| 159 | : Callee(Callee), Args(std::move(Args)) {} |
Lang Hames | 2d789c3 | 2015-08-26 03:07:41 +0000 | [diff] [blame] | 160 | Value *codegen() override; |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 161 | }; |
| 162 | |
| 163 | /// IfExprAST - Expression class for if/then/else. |
| 164 | class IfExprAST : public ExprAST { |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 165 | std::unique_ptr<ExprAST> Cond, Then, Else; |
Lang Hames | 59b0da8 | 2015-08-19 18:15:58 +0000 | [diff] [blame] | 166 | |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 167 | public: |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 168 | IfExprAST(std::unique_ptr<ExprAST> Cond, std::unique_ptr<ExprAST> Then, |
| 169 | std::unique_ptr<ExprAST> Else) |
| 170 | : Cond(std::move(Cond)), Then(std::move(Then)), Else(std::move(Else)) {} |
Lang Hames | 2d789c3 | 2015-08-26 03:07:41 +0000 | [diff] [blame] | 171 | Value *codegen() override; |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 172 | }; |
| 173 | |
| 174 | /// ForExprAST - Expression class for for/in. |
| 175 | class ForExprAST : public ExprAST { |
| 176 | std::string VarName; |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 177 | std::unique_ptr<ExprAST> Start, End, Step, Body; |
Lang Hames | 59b0da8 | 2015-08-19 18:15:58 +0000 | [diff] [blame] | 178 | |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 179 | public: |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 180 | ForExprAST(const std::string &VarName, std::unique_ptr<ExprAST> Start, |
| 181 | std::unique_ptr<ExprAST> End, std::unique_ptr<ExprAST> Step, |
| 182 | std::unique_ptr<ExprAST> Body) |
| 183 | : VarName(VarName), Start(std::move(Start)), End(std::move(End)), |
| 184 | Step(std::move(Step)), Body(std::move(Body)) {} |
Lang Hames | 2d789c3 | 2015-08-26 03:07:41 +0000 | [diff] [blame] | 185 | Value *codegen() override; |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 186 | }; |
| 187 | |
| 188 | /// PrototypeAST - This class represents the "prototype" for a function, |
| 189 | /// which captures its name, and its argument names (thus implicitly the number |
| 190 | /// of arguments the function takes). |
| 191 | class PrototypeAST { |
| 192 | std::string Name; |
| 193 | std::vector<std::string> Args; |
Lang Hames | 59b0da8 | 2015-08-19 18:15:58 +0000 | [diff] [blame] | 194 | |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 195 | public: |
Lang Hames | 59b0da8 | 2015-08-19 18:15:58 +0000 | [diff] [blame] | 196 | PrototypeAST(const std::string &Name, std::vector<std::string> Args) |
| 197 | : Name(Name), Args(std::move(Args)) {} |
Lang Hames | 2d789c3 | 2015-08-26 03:07:41 +0000 | [diff] [blame] | 198 | Function *codegen(); |
| 199 | const std::string &getName() const { return Name; } |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 200 | }; |
| 201 | |
| 202 | /// FunctionAST - This class represents a function definition itself. |
| 203 | class FunctionAST { |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 204 | std::unique_ptr<PrototypeAST> Proto; |
| 205 | std::unique_ptr<ExprAST> Body; |
Lang Hames | 59b0da8 | 2015-08-19 18:15:58 +0000 | [diff] [blame] | 206 | |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 207 | public: |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 208 | FunctionAST(std::unique_ptr<PrototypeAST> Proto, |
| 209 | std::unique_ptr<ExprAST> Body) |
Lang Hames | 59b0da8 | 2015-08-19 18:15:58 +0000 | [diff] [blame] | 210 | : Proto(std::move(Proto)), Body(std::move(Body)) {} |
Lang Hames | 2d789c3 | 2015-08-26 03:07:41 +0000 | [diff] [blame] | 211 | Function *codegen(); |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 212 | }; |
Juergen Ributzka | 05c5a93 | 2013-11-19 03:08:35 +0000 | [diff] [blame] | 213 | } // end anonymous namespace |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 214 | |
| 215 | //===----------------------------------------------------------------------===// |
| 216 | // Parser |
| 217 | //===----------------------------------------------------------------------===// |
| 218 | |
| 219 | /// CurTok/getNextToken - Provide a simple token buffer. CurTok is the current |
| 220 | /// token the parser is looking at. getNextToken reads another token from the |
| 221 | /// lexer and updates CurTok with its results. |
| 222 | static int CurTok; |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 223 | static int getNextToken() { return CurTok = gettok(); } |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 224 | |
| 225 | /// BinopPrecedence - This holds the precedence for each binary operator that is |
| 226 | /// defined. |
| 227 | static std::map<char, int> BinopPrecedence; |
| 228 | |
| 229 | /// GetTokPrecedence - Get the precedence of the pending binary operator token. |
| 230 | static int GetTokPrecedence() { |
| 231 | if (!isascii(CurTok)) |
| 232 | return -1; |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 233 | |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 234 | // Make sure it's a declared binop. |
| 235 | int TokPrec = BinopPrecedence[CurTok]; |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 236 | if (TokPrec <= 0) |
| 237 | return -1; |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 238 | return TokPrec; |
| 239 | } |
| 240 | |
| 241 | /// Error* - These are little helper functions for error handling. |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 242 | std::unique_ptr<ExprAST> Error(const char *Str) { |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 243 | fprintf(stderr, "Error: %s\n", Str); |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 244 | return nullptr; |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 245 | } |
Hans Wennborg | cc9deb4 | 2015-09-29 18:02:48 +0000 | [diff] [blame] | 246 | |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 247 | std::unique_ptr<PrototypeAST> ErrorP(const char *Str) { |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 248 | Error(Str); |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 249 | return nullptr; |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 250 | } |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 251 | |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 252 | static std::unique_ptr<ExprAST> ParseExpression(); |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 253 | |
Lang Hames | 59b0da8 | 2015-08-19 18:15:58 +0000 | [diff] [blame] | 254 | /// numberexpr ::= number |
| 255 | static std::unique_ptr<ExprAST> ParseNumberExpr() { |
| 256 | auto Result = llvm::make_unique<NumberExprAST>(NumVal); |
| 257 | getNextToken(); // consume the number |
| 258 | return std::move(Result); |
| 259 | } |
| 260 | |
| 261 | /// parenexpr ::= '(' expression ')' |
| 262 | static std::unique_ptr<ExprAST> ParseParenExpr() { |
| 263 | getNextToken(); // eat (. |
| 264 | auto V = ParseExpression(); |
| 265 | if (!V) |
| 266 | return nullptr; |
| 267 | |
| 268 | if (CurTok != ')') |
| 269 | return Error("expected ')'"); |
| 270 | getNextToken(); // eat ). |
| 271 | return V; |
| 272 | } |
| 273 | |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 274 | /// identifierexpr |
| 275 | /// ::= identifier |
| 276 | /// ::= identifier '(' expression* ')' |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 277 | static std::unique_ptr<ExprAST> ParseIdentifierExpr() { |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 278 | std::string IdName = IdentifierStr; |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 279 | |
| 280 | getNextToken(); // eat identifier. |
| 281 | |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 282 | if (CurTok != '(') // Simple variable ref. |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 283 | return llvm::make_unique<VariableExprAST>(IdName); |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 284 | |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 285 | // Call. |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 286 | getNextToken(); // eat ( |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 287 | std::vector<std::unique_ptr<ExprAST>> Args; |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 288 | if (CurTok != ')') { |
| 289 | while (1) { |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 290 | if (auto Arg = ParseExpression()) |
| 291 | Args.push_back(std::move(Arg)); |
| 292 | else |
| 293 | return nullptr; |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 294 | |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 295 | if (CurTok == ')') |
| 296 | break; |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 297 | |
| 298 | if (CurTok != ',') |
| 299 | return Error("Expected ')' or ',' in argument list"); |
| 300 | getNextToken(); |
| 301 | } |
| 302 | } |
| 303 | |
| 304 | // Eat the ')'. |
| 305 | getNextToken(); |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 306 | |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 307 | return llvm::make_unique<CallExprAST>(IdName, std::move(Args)); |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 308 | } |
| 309 | |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 310 | /// ifexpr ::= 'if' expression 'then' expression 'else' expression |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 311 | static std::unique_ptr<ExprAST> ParseIfExpr() { |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 312 | getNextToken(); // eat the if. |
| 313 | |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 314 | // condition. |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 315 | auto Cond = ParseExpression(); |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 316 | if (!Cond) |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 317 | return nullptr; |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 318 | |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 319 | if (CurTok != tok_then) |
| 320 | return Error("expected then"); |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 321 | getNextToken(); // eat the then |
| 322 | |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 323 | auto Then = ParseExpression(); |
| 324 | if (!Then) |
| 325 | return nullptr; |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 326 | |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 327 | if (CurTok != tok_else) |
| 328 | return Error("expected else"); |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 329 | |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 330 | getNextToken(); |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 331 | |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 332 | auto Else = ParseExpression(); |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 333 | if (!Else) |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 334 | return nullptr; |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 335 | |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 336 | return llvm::make_unique<IfExprAST>(std::move(Cond), std::move(Then), |
| 337 | std::move(Else)); |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | /// forexpr ::= 'for' identifier '=' expr ',' expr (',' expr)? 'in' expression |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 341 | static std::unique_ptr<ExprAST> ParseForExpr() { |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 342 | getNextToken(); // eat the for. |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 343 | |
| 344 | if (CurTok != tok_identifier) |
| 345 | return Error("expected identifier after for"); |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 346 | |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 347 | std::string IdName = IdentifierStr; |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 348 | getNextToken(); // eat identifier. |
| 349 | |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 350 | if (CurTok != '=') |
| 351 | return Error("expected '=' after for"); |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 352 | getNextToken(); // eat '='. |
| 353 | |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 354 | auto Start = ParseExpression(); |
| 355 | if (!Start) |
| 356 | return nullptr; |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 357 | if (CurTok != ',') |
| 358 | return Error("expected ',' after for start value"); |
| 359 | getNextToken(); |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 360 | |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 361 | auto End = ParseExpression(); |
| 362 | if (!End) |
| 363 | return nullptr; |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 364 | |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 365 | // The step value is optional. |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 366 | std::unique_ptr<ExprAST> Step; |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 367 | if (CurTok == ',') { |
| 368 | getNextToken(); |
| 369 | Step = ParseExpression(); |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 370 | if (!Step) |
| 371 | return nullptr; |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 372 | } |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 373 | |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 374 | if (CurTok != tok_in) |
| 375 | return Error("expected 'in' after for"); |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 376 | getNextToken(); // eat 'in'. |
| 377 | |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 378 | auto Body = ParseExpression(); |
| 379 | if (!Body) |
| 380 | return nullptr; |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 381 | |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 382 | return llvm::make_unique<ForExprAST>(IdName, std::move(Start), std::move(End), |
| 383 | std::move(Step), std::move(Body)); |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 384 | } |
| 385 | |
| 386 | /// primary |
| 387 | /// ::= identifierexpr |
| 388 | /// ::= numberexpr |
| 389 | /// ::= parenexpr |
| 390 | /// ::= ifexpr |
| 391 | /// ::= forexpr |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 392 | static std::unique_ptr<ExprAST> ParsePrimary() { |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 393 | switch (CurTok) { |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 394 | default: |
| 395 | return Error("unknown token when expecting an expression"); |
| 396 | case tok_identifier: |
| 397 | return ParseIdentifierExpr(); |
| 398 | case tok_number: |
| 399 | return ParseNumberExpr(); |
| 400 | case '(': |
| 401 | return ParseParenExpr(); |
| 402 | case tok_if: |
| 403 | return ParseIfExpr(); |
| 404 | case tok_for: |
| 405 | return ParseForExpr(); |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 406 | } |
| 407 | } |
| 408 | |
| 409 | /// binoprhs |
| 410 | /// ::= ('+' primary)* |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 411 | static std::unique_ptr<ExprAST> ParseBinOpRHS(int ExprPrec, |
| 412 | std::unique_ptr<ExprAST> LHS) { |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 413 | // If this is a binop, find its precedence. |
| 414 | while (1) { |
| 415 | int TokPrec = GetTokPrecedence(); |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 416 | |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 417 | // If this is a binop that binds at least as tightly as the current binop, |
| 418 | // consume it, otherwise we are done. |
| 419 | if (TokPrec < ExprPrec) |
| 420 | return LHS; |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 421 | |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 422 | // Okay, we know this is a binop. |
| 423 | int BinOp = CurTok; |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 424 | getNextToken(); // eat binop |
| 425 | |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 426 | // Parse the primary expression after the binary operator. |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 427 | auto RHS = ParsePrimary(); |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 428 | if (!RHS) |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 429 | return nullptr; |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 430 | |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 431 | // If BinOp binds less tightly with RHS than the operator after RHS, let |
| 432 | // the pending operator take RHS as its LHS. |
| 433 | int NextPrec = GetTokPrecedence(); |
| 434 | if (TokPrec < NextPrec) { |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 435 | RHS = ParseBinOpRHS(TokPrec + 1, std::move(RHS)); |
| 436 | if (!RHS) |
| 437 | return nullptr; |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 438 | } |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 439 | |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 440 | // Merge LHS/RHS. |
Lang Hames | 59b0da8 | 2015-08-19 18:15:58 +0000 | [diff] [blame] | 441 | LHS = |
| 442 | llvm::make_unique<BinaryExprAST>(BinOp, std::move(LHS), std::move(RHS)); |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 443 | } |
| 444 | } |
| 445 | |
| 446 | /// expression |
| 447 | /// ::= primary binoprhs |
| 448 | /// |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 449 | static std::unique_ptr<ExprAST> ParseExpression() { |
| 450 | auto LHS = ParsePrimary(); |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 451 | if (!LHS) |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 452 | return nullptr; |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 453 | |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 454 | return ParseBinOpRHS(0, std::move(LHS)); |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 455 | } |
| 456 | |
| 457 | /// prototype |
| 458 | /// ::= id '(' id* ')' |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 459 | static std::unique_ptr<PrototypeAST> ParsePrototype() { |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 460 | if (CurTok != tok_identifier) |
| 461 | return ErrorP("Expected function name in prototype"); |
| 462 | |
| 463 | std::string FnName = IdentifierStr; |
| 464 | getNextToken(); |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 465 | |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 466 | if (CurTok != '(') |
| 467 | return ErrorP("Expected '(' in prototype"); |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 468 | |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 469 | std::vector<std::string> ArgNames; |
| 470 | while (getNextToken() == tok_identifier) |
| 471 | ArgNames.push_back(IdentifierStr); |
| 472 | if (CurTok != ')') |
| 473 | return ErrorP("Expected ')' in prototype"); |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 474 | |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 475 | // success. |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 476 | getNextToken(); // eat ')'. |
| 477 | |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 478 | return llvm::make_unique<PrototypeAST>(FnName, std::move(ArgNames)); |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 479 | } |
| 480 | |
| 481 | /// definition ::= 'def' prototype expression |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 482 | static std::unique_ptr<FunctionAST> ParseDefinition() { |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 483 | getNextToken(); // eat def. |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 484 | auto Proto = ParsePrototype(); |
| 485 | if (!Proto) |
| 486 | return nullptr; |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 487 | |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 488 | if (auto E = ParseExpression()) |
| 489 | return llvm::make_unique<FunctionAST>(std::move(Proto), std::move(E)); |
| 490 | return nullptr; |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 491 | } |
| 492 | |
| 493 | /// toplevelexpr ::= expression |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 494 | static std::unique_ptr<FunctionAST> ParseTopLevelExpr() { |
| 495 | if (auto E = ParseExpression()) { |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 496 | // Make an anonymous proto. |
Lang Hames | 2d789c3 | 2015-08-26 03:07:41 +0000 | [diff] [blame] | 497 | auto Proto = llvm::make_unique<PrototypeAST>("__anon_expr", |
| 498 | std::vector<std::string>()); |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 499 | return llvm::make_unique<FunctionAST>(std::move(Proto), std::move(E)); |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 500 | } |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 501 | return nullptr; |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 502 | } |
| 503 | |
| 504 | /// external ::= 'extern' prototype |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 505 | static std::unique_ptr<PrototypeAST> ParseExtern() { |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 506 | getNextToken(); // eat extern. |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 507 | return ParsePrototype(); |
| 508 | } |
| 509 | |
| 510 | //===----------------------------------------------------------------------===// |
| 511 | // Code Generation |
| 512 | //===----------------------------------------------------------------------===// |
| 513 | |
Lang Hames | 2d789c3 | 2015-08-26 03:07:41 +0000 | [diff] [blame] | 514 | static std::unique_ptr<Module> TheModule; |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 515 | static IRBuilder<> Builder(getGlobalContext()); |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 516 | static std::map<std::string, Value *> NamedValues; |
Lang Hames | 2d789c3 | 2015-08-26 03:07:41 +0000 | [diff] [blame] | 517 | static std::unique_ptr<legacy::FunctionPassManager> TheFPM; |
| 518 | static std::unique_ptr<KaleidoscopeJIT> TheJIT; |
| 519 | static std::map<std::string, std::unique_ptr<PrototypeAST>> FunctionProtos; |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 520 | |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 521 | Value *ErrorV(const char *Str) { |
| 522 | Error(Str); |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 523 | return nullptr; |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 524 | } |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 525 | |
Lang Hames | 2d789c3 | 2015-08-26 03:07:41 +0000 | [diff] [blame] | 526 | Function *getFunction(std::string Name) { |
| 527 | // First, see if the function has already been added to the current module. |
| 528 | if (auto *F = TheModule->getFunction(Name)) |
| 529 | return F; |
| 530 | |
| 531 | // If not, check whether we can codegen the declaration from some existing |
| 532 | // prototype. |
| 533 | auto FI = FunctionProtos.find(Name); |
| 534 | if (FI != FunctionProtos.end()) |
| 535 | return FI->second->codegen(); |
| 536 | |
| 537 | // If no existing prototype exists, return null. |
| 538 | return nullptr; |
| 539 | } |
| 540 | |
| 541 | Value *NumberExprAST::codegen() { |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 542 | return ConstantFP::get(getGlobalContext(), APFloat(Val)); |
| 543 | } |
| 544 | |
Lang Hames | 2d789c3 | 2015-08-26 03:07:41 +0000 | [diff] [blame] | 545 | Value *VariableExprAST::codegen() { |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 546 | // Look this variable up in the function. |
| 547 | Value *V = NamedValues[Name]; |
Lang Hames | 596aec9 | 2015-08-19 18:32:58 +0000 | [diff] [blame] | 548 | if (!V) |
| 549 | return ErrorV("Unknown variable name"); |
| 550 | return V; |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 551 | } |
| 552 | |
Lang Hames | 2d789c3 | 2015-08-26 03:07:41 +0000 | [diff] [blame] | 553 | Value *BinaryExprAST::codegen() { |
| 554 | Value *L = LHS->codegen(); |
| 555 | Value *R = RHS->codegen(); |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 556 | if (!L || !R) |
| 557 | return nullptr; |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 558 | |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 559 | switch (Op) { |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 560 | case '+': |
| 561 | return Builder.CreateFAdd(L, R, "addtmp"); |
| 562 | case '-': |
| 563 | return Builder.CreateFSub(L, R, "subtmp"); |
| 564 | case '*': |
| 565 | return Builder.CreateFMul(L, R, "multmp"); |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 566 | case '<': |
| 567 | L = Builder.CreateFCmpULT(L, R, "cmptmp"); |
| 568 | // Convert bool 0/1 to double 0.0 or 1.0 |
| 569 | return Builder.CreateUIToFP(L, Type::getDoubleTy(getGlobalContext()), |
| 570 | "booltmp"); |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 571 | default: |
| 572 | return ErrorV("invalid binary operator"); |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 573 | } |
| 574 | } |
| 575 | |
Lang Hames | 2d789c3 | 2015-08-26 03:07:41 +0000 | [diff] [blame] | 576 | Value *CallExprAST::codegen() { |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 577 | // Look up the name in the global module table. |
Lang Hames | 2d789c3 | 2015-08-26 03:07:41 +0000 | [diff] [blame] | 578 | Function *CalleeF = getFunction(Callee); |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 579 | if (!CalleeF) |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 580 | return ErrorV("Unknown function referenced"); |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 581 | |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 582 | // If argument mismatch error. |
| 583 | if (CalleeF->arg_size() != Args.size()) |
| 584 | return ErrorV("Incorrect # arguments passed"); |
| 585 | |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 586 | std::vector<Value *> ArgsV; |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 587 | for (unsigned i = 0, e = Args.size(); i != e; ++i) { |
Lang Hames | 2d789c3 | 2015-08-26 03:07:41 +0000 | [diff] [blame] | 588 | ArgsV.push_back(Args[i]->codegen()); |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 589 | if (!ArgsV.back()) |
| 590 | return nullptr; |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 591 | } |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 592 | |
Francois Pichet | c5d1050 | 2011-07-15 10:59:52 +0000 | [diff] [blame] | 593 | return Builder.CreateCall(CalleeF, ArgsV, "calltmp"); |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 594 | } |
| 595 | |
Lang Hames | 2d789c3 | 2015-08-26 03:07:41 +0000 | [diff] [blame] | 596 | Value *IfExprAST::codegen() { |
| 597 | Value *CondV = Cond->codegen(); |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 598 | if (!CondV) |
| 599 | return nullptr; |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 600 | |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 601 | // Convert condition to a bool by comparing equal to 0.0. |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 602 | CondV = Builder.CreateFCmpONE( |
| 603 | CondV, ConstantFP::get(getGlobalContext(), APFloat(0.0)), "ifcond"); |
| 604 | |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 605 | Function *TheFunction = Builder.GetInsertBlock()->getParent(); |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 606 | |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 607 | // Create blocks for the then and else cases. Insert the 'then' block at the |
| 608 | // end of the function. |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 609 | BasicBlock *ThenBB = |
| 610 | BasicBlock::Create(getGlobalContext(), "then", TheFunction); |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 611 | BasicBlock *ElseBB = BasicBlock::Create(getGlobalContext(), "else"); |
| 612 | BasicBlock *MergeBB = BasicBlock::Create(getGlobalContext(), "ifcont"); |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 613 | |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 614 | Builder.CreateCondBr(CondV, ThenBB, ElseBB); |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 615 | |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 616 | // Emit then value. |
| 617 | Builder.SetInsertPoint(ThenBB); |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 618 | |
Lang Hames | 2d789c3 | 2015-08-26 03:07:41 +0000 | [diff] [blame] | 619 | Value *ThenV = Then->codegen(); |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 620 | if (!ThenV) |
| 621 | return nullptr; |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 622 | |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 623 | Builder.CreateBr(MergeBB); |
| 624 | // Codegen of 'Then' can change the current block, update ThenBB for the PHI. |
| 625 | ThenBB = Builder.GetInsertBlock(); |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 626 | |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 627 | // Emit else block. |
| 628 | TheFunction->getBasicBlockList().push_back(ElseBB); |
| 629 | Builder.SetInsertPoint(ElseBB); |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 630 | |
Lang Hames | 2d789c3 | 2015-08-26 03:07:41 +0000 | [diff] [blame] | 631 | Value *ElseV = Else->codegen(); |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 632 | if (!ElseV) |
| 633 | return nullptr; |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 634 | |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 635 | Builder.CreateBr(MergeBB); |
| 636 | // Codegen of 'Else' can change the current block, update ElseBB for the PHI. |
| 637 | ElseBB = Builder.GetInsertBlock(); |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 638 | |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 639 | // Emit merge block. |
| 640 | TheFunction->getBasicBlockList().push_back(MergeBB); |
| 641 | Builder.SetInsertPoint(MergeBB); |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 642 | PHINode *PN = |
| 643 | Builder.CreatePHI(Type::getDoubleTy(getGlobalContext()), 2, "iftmp"); |
| 644 | |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 645 | PN->addIncoming(ThenV, ThenBB); |
| 646 | PN->addIncoming(ElseV, ElseBB); |
| 647 | return PN; |
| 648 | } |
| 649 | |
Lang Hames | 59b0da8 | 2015-08-19 18:15:58 +0000 | [diff] [blame] | 650 | // Output for-loop as: |
| 651 | // ... |
| 652 | // start = startexpr |
| 653 | // goto loop |
| 654 | // loop: |
| 655 | // variable = phi [start, loopheader], [nextvariable, loopend] |
| 656 | // ... |
| 657 | // bodyexpr |
| 658 | // ... |
| 659 | // loopend: |
| 660 | // step = stepexpr |
| 661 | // nextvariable = variable + step |
| 662 | // endcond = endexpr |
| 663 | // br endcond, loop, endloop |
| 664 | // outloop: |
Lang Hames | 2d789c3 | 2015-08-26 03:07:41 +0000 | [diff] [blame] | 665 | Value *ForExprAST::codegen() { |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 666 | // Emit the start code first, without 'variable' in scope. |
Lang Hames | 2d789c3 | 2015-08-26 03:07:41 +0000 | [diff] [blame] | 667 | Value *StartVal = Start->codegen(); |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 668 | if (!StartVal) |
| 669 | return nullptr; |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 670 | |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 671 | // Make the new basic block for the loop header, inserting after current |
| 672 | // block. |
| 673 | Function *TheFunction = Builder.GetInsertBlock()->getParent(); |
| 674 | BasicBlock *PreheaderBB = Builder.GetInsertBlock(); |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 675 | BasicBlock *LoopBB = |
| 676 | BasicBlock::Create(getGlobalContext(), "loop", TheFunction); |
| 677 | |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 678 | // Insert an explicit fall through from the current block to the LoopBB. |
| 679 | Builder.CreateBr(LoopBB); |
| 680 | |
| 681 | // Start insertion in LoopBB. |
| 682 | Builder.SetInsertPoint(LoopBB); |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 683 | |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 684 | // Start the PHI node with an entry for Start. |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 685 | PHINode *Variable = Builder.CreatePHI(Type::getDoubleTy(getGlobalContext()), |
| 686 | 2, VarName.c_str()); |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 687 | Variable->addIncoming(StartVal, PreheaderBB); |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 688 | |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 689 | // Within the loop, the variable is defined equal to the PHI node. If it |
| 690 | // shadows an existing variable, we have to restore it, so save it now. |
| 691 | Value *OldVal = NamedValues[VarName]; |
| 692 | NamedValues[VarName] = Variable; |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 693 | |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 694 | // Emit the body of the loop. This, like any other expr, can change the |
| 695 | // current BB. Note that we ignore the value computed by the body, but don't |
| 696 | // allow an error. |
Lang Hames | 2d789c3 | 2015-08-26 03:07:41 +0000 | [diff] [blame] | 697 | if (!Body->codegen()) |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 698 | return nullptr; |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 699 | |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 700 | // Emit the step value. |
Lang Hames | 59b0da8 | 2015-08-19 18:15:58 +0000 | [diff] [blame] | 701 | Value *StepVal = nullptr; |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 702 | if (Step) { |
Lang Hames | 2d789c3 | 2015-08-26 03:07:41 +0000 | [diff] [blame] | 703 | StepVal = Step->codegen(); |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 704 | if (!StepVal) |
| 705 | return nullptr; |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 706 | } else { |
| 707 | // If not specified, use 1.0. |
| 708 | StepVal = ConstantFP::get(getGlobalContext(), APFloat(1.0)); |
| 709 | } |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 710 | |
Chris Lattner | 26d7950 | 2010-06-21 22:51:14 +0000 | [diff] [blame] | 711 | Value *NextVar = Builder.CreateFAdd(Variable, StepVal, "nextvar"); |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 712 | |
| 713 | // Compute the end condition. |
Lang Hames | 2d789c3 | 2015-08-26 03:07:41 +0000 | [diff] [blame] | 714 | Value *EndCond = End->codegen(); |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 715 | if (!EndCond) |
Lang Hames | 59b0da8 | 2015-08-19 18:15:58 +0000 | [diff] [blame] | 716 | return nullptr; |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 717 | |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 718 | // Convert condition to a bool by comparing equal to 0.0. |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 719 | EndCond = Builder.CreateFCmpONE( |
| 720 | EndCond, ConstantFP::get(getGlobalContext(), APFloat(0.0)), "loopcond"); |
| 721 | |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 722 | // Create the "after loop" block and insert it. |
| 723 | BasicBlock *LoopEndBB = Builder.GetInsertBlock(); |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 724 | BasicBlock *AfterBB = |
| 725 | BasicBlock::Create(getGlobalContext(), "afterloop", TheFunction); |
| 726 | |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 727 | // Insert the conditional branch into the end of LoopEndBB. |
| 728 | Builder.CreateCondBr(EndCond, LoopBB, AfterBB); |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 729 | |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 730 | // Any new code will be inserted in AfterBB. |
| 731 | Builder.SetInsertPoint(AfterBB); |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 732 | |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 733 | // Add a new entry to the PHI node for the backedge. |
| 734 | Variable->addIncoming(NextVar, LoopEndBB); |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 735 | |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 736 | // Restore the unshadowed variable. |
| 737 | if (OldVal) |
| 738 | NamedValues[VarName] = OldVal; |
| 739 | else |
| 740 | NamedValues.erase(VarName); |
| 741 | |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 742 | // for expr always returns 0.0. |
| 743 | return Constant::getNullValue(Type::getDoubleTy(getGlobalContext())); |
| 744 | } |
| 745 | |
Lang Hames | 2d789c3 | 2015-08-26 03:07:41 +0000 | [diff] [blame] | 746 | Function *PrototypeAST::codegen() { |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 747 | // Make the function type: double(double,double) etc. |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 748 | std::vector<Type *> Doubles(Args.size(), |
| 749 | Type::getDoubleTy(getGlobalContext())); |
| 750 | FunctionType *FT = |
| 751 | FunctionType::get(Type::getDoubleTy(getGlobalContext()), Doubles, false); |
| 752 | |
| 753 | Function *F = |
Lang Hames | 2d789c3 | 2015-08-26 03:07:41 +0000 | [diff] [blame] | 754 | Function::Create(FT, Function::ExternalLinkage, Name, TheModule.get()); |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 755 | |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 756 | // Set names for all arguments. |
| 757 | unsigned Idx = 0; |
Lang Hames | 2d789c3 | 2015-08-26 03:07:41 +0000 | [diff] [blame] | 758 | for (auto &Arg : F->args()) |
| 759 | Arg.setName(Args[Idx++]); |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 760 | |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 761 | return F; |
| 762 | } |
| 763 | |
Lang Hames | 2d789c3 | 2015-08-26 03:07:41 +0000 | [diff] [blame] | 764 | Function *FunctionAST::codegen() { |
| 765 | // Transfer ownership of the prototype to the FunctionProtos map, but keep a |
| 766 | // reference to it for use below. |
| 767 | auto &P = *Proto; |
| 768 | FunctionProtos[Proto->getName()] = std::move(Proto); |
| 769 | Function *TheFunction = getFunction(P.getName()); |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 770 | if (!TheFunction) |
| 771 | return nullptr; |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 772 | |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 773 | // Create a new basic block to start insertion into. |
| 774 | BasicBlock *BB = BasicBlock::Create(getGlobalContext(), "entry", TheFunction); |
| 775 | Builder.SetInsertPoint(BB); |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 776 | |
Lang Hames | 2d789c3 | 2015-08-26 03:07:41 +0000 | [diff] [blame] | 777 | // Record the function arguments in the NamedValues map. |
| 778 | NamedValues.clear(); |
| 779 | for (auto &Arg : TheFunction->args()) |
| 780 | NamedValues[Arg.getName()] = &Arg; |
| 781 | |
| 782 | if (Value *RetVal = Body->codegen()) { |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 783 | // Finish off the function. |
| 784 | Builder.CreateRet(RetVal); |
| 785 | |
| 786 | // Validate the generated code, checking for consistency. |
| 787 | verifyFunction(*TheFunction); |
| 788 | |
Lang Hames | 2d789c3 | 2015-08-26 03:07:41 +0000 | [diff] [blame] | 789 | // Run the optimizer on the function. |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 790 | TheFPM->run(*TheFunction); |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 791 | |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 792 | return TheFunction; |
| 793 | } |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 794 | |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 795 | // Error reading body, remove function. |
| 796 | TheFunction->eraseFromParent(); |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 797 | return nullptr; |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 798 | } |
| 799 | |
| 800 | //===----------------------------------------------------------------------===// |
| 801 | // Top-Level parsing and JIT Driver |
| 802 | //===----------------------------------------------------------------------===// |
| 803 | |
Lang Hames | 2d789c3 | 2015-08-26 03:07:41 +0000 | [diff] [blame] | 804 | static void InitializeModuleAndPassManager() { |
| 805 | // Open a new module. |
| 806 | TheModule = llvm::make_unique<Module>("my cool jit", getGlobalContext()); |
| 807 | TheModule->setDataLayout(TheJIT->getTargetMachine().createDataLayout()); |
| 808 | |
| 809 | // Create a new pass manager attached to it. |
| 810 | TheFPM = llvm::make_unique<legacy::FunctionPassManager>(TheModule.get()); |
| 811 | |
Lang Hames | 2d789c3 | 2015-08-26 03:07:41 +0000 | [diff] [blame] | 812 | // Do simple "peephole" optimizations and bit-twiddling optzns. |
| 813 | TheFPM->add(createInstructionCombiningPass()); |
| 814 | // Reassociate expressions. |
| 815 | TheFPM->add(createReassociatePass()); |
| 816 | // Eliminate Common SubExpressions. |
| 817 | TheFPM->add(createGVNPass()); |
| 818 | // Simplify the control flow graph (deleting unreachable blocks, etc). |
| 819 | TheFPM->add(createCFGSimplificationPass()); |
| 820 | |
| 821 | TheFPM->doInitialization(); |
| 822 | } |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 823 | |
| 824 | static void HandleDefinition() { |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 825 | if (auto FnAST = ParseDefinition()) { |
Lang Hames | 2d789c3 | 2015-08-26 03:07:41 +0000 | [diff] [blame] | 826 | if (auto *FnIR = FnAST->codegen()) { |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 827 | fprintf(stderr, "Read function definition:"); |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 828 | FnIR->dump(); |
Lang Hames | 2d789c3 | 2015-08-26 03:07:41 +0000 | [diff] [blame] | 829 | TheJIT->addModule(std::move(TheModule)); |
| 830 | InitializeModuleAndPassManager(); |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 831 | } |
| 832 | } else { |
| 833 | // Skip token for error recovery. |
| 834 | getNextToken(); |
| 835 | } |
| 836 | } |
| 837 | |
| 838 | static void HandleExtern() { |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 839 | if (auto ProtoAST = ParseExtern()) { |
Lang Hames | 2d789c3 | 2015-08-26 03:07:41 +0000 | [diff] [blame] | 840 | if (auto *FnIR = ProtoAST->codegen()) { |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 841 | fprintf(stderr, "Read extern: "); |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 842 | FnIR->dump(); |
Lang Hames | 2d789c3 | 2015-08-26 03:07:41 +0000 | [diff] [blame] | 843 | FunctionProtos[ProtoAST->getName()] = std::move(ProtoAST); |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 844 | } |
| 845 | } else { |
| 846 | // Skip token for error recovery. |
| 847 | getNextToken(); |
| 848 | } |
| 849 | } |
| 850 | |
| 851 | static void HandleTopLevelExpression() { |
| 852 | // Evaluate a top-level expression into an anonymous function. |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 853 | if (auto FnAST = ParseTopLevelExpr()) { |
Lang Hames | 2d789c3 | 2015-08-26 03:07:41 +0000 | [diff] [blame] | 854 | if (FnAST->codegen()) { |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 855 | |
Lang Hames | 2d789c3 | 2015-08-26 03:07:41 +0000 | [diff] [blame] | 856 | // JIT the module containing the anonymous expression, keeping a handle so |
| 857 | // we can free it later. |
| 858 | auto H = TheJIT->addModule(std::move(TheModule)); |
| 859 | InitializeModuleAndPassManager(); |
| 860 | |
| 861 | // Search the JIT for the __anon_expr symbol. |
| 862 | auto ExprSymbol = TheJIT->findSymbol("__anon_expr"); |
| 863 | assert(ExprSymbol && "Function not found"); |
| 864 | |
| 865 | // Get the symbol's address and cast it to the right type (takes no |
| 866 | // arguments, returns a double) so we can call it as a native function. |
| 867 | double (*FP)() = (double (*)())(intptr_t)ExprSymbol.getAddress(); |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 868 | fprintf(stderr, "Evaluated to %f\n", FP()); |
Lang Hames | 2d789c3 | 2015-08-26 03:07:41 +0000 | [diff] [blame] | 869 | |
| 870 | // Delete the anonymous expression module from the JIT. |
| 871 | TheJIT->removeModule(H); |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 872 | } |
| 873 | } else { |
| 874 | // Skip token for error recovery. |
| 875 | getNextToken(); |
| 876 | } |
| 877 | } |
| 878 | |
| 879 | /// top ::= definition | external | expression | ';' |
| 880 | static void MainLoop() { |
| 881 | while (1) { |
| 882 | fprintf(stderr, "ready> "); |
| 883 | switch (CurTok) { |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 884 | case tok_eof: |
| 885 | return; |
Lang Hames | 59b0da8 | 2015-08-19 18:15:58 +0000 | [diff] [blame] | 886 | case ';': // ignore top-level semicolons. |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 887 | getNextToken(); |
Lang Hames | 59b0da8 | 2015-08-19 18:15:58 +0000 | [diff] [blame] | 888 | break; |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 889 | case tok_def: |
| 890 | HandleDefinition(); |
| 891 | break; |
| 892 | case tok_extern: |
| 893 | HandleExtern(); |
| 894 | break; |
| 895 | default: |
| 896 | HandleTopLevelExpression(); |
| 897 | break; |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 898 | } |
| 899 | } |
| 900 | } |
| 901 | |
| 902 | //===----------------------------------------------------------------------===// |
| 903 | // "Library" functions that can be "extern'd" from user code. |
| 904 | //===----------------------------------------------------------------------===// |
| 905 | |
| 906 | /// putchard - putchar that takes a double and returns 0. |
NAKAMURA Takumi | ac9d373 | 2015-08-28 03:34:33 +0000 | [diff] [blame] | 907 | extern "C" double putchard(double X) { |
Lang Hames | d76e067 | 2015-08-27 20:31:44 +0000 | [diff] [blame] | 908 | fputc((char)X, stderr); |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 909 | return 0; |
| 910 | } |
| 911 | |
Lang Hames | 59b0da8 | 2015-08-19 18:15:58 +0000 | [diff] [blame] | 912 | /// printd - printf that takes a double prints it as "%f\n", returning 0. |
NAKAMURA Takumi | ac9d373 | 2015-08-28 03:34:33 +0000 | [diff] [blame] | 913 | extern "C" double printd(double X) { |
Lang Hames | d76e067 | 2015-08-27 20:31:44 +0000 | [diff] [blame] | 914 | fprintf(stderr, "%f\n", X); |
Lang Hames | 59b0da8 | 2015-08-19 18:15:58 +0000 | [diff] [blame] | 915 | return 0; |
| 916 | } |
| 917 | |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 918 | //===----------------------------------------------------------------------===// |
| 919 | // Main driver code. |
| 920 | //===----------------------------------------------------------------------===// |
| 921 | |
| 922 | int main() { |
| 923 | InitializeNativeTarget(); |
Eric Christopher | 1b74b65 | 2014-12-08 18:00:38 +0000 | [diff] [blame] | 924 | InitializeNativeTargetAsmPrinter(); |
| 925 | InitializeNativeTargetAsmParser(); |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 926 | |
| 927 | // Install standard binary operators. |
| 928 | // 1 is lowest precedence. |
| 929 | BinopPrecedence['<'] = 10; |
| 930 | BinopPrecedence['+'] = 20; |
| 931 | BinopPrecedence['-'] = 20; |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 932 | BinopPrecedence['*'] = 40; // highest. |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 933 | |
| 934 | // Prime the first token. |
| 935 | fprintf(stderr, "ready> "); |
| 936 | getNextToken(); |
| 937 | |
Lang Hames | 2d789c3 | 2015-08-26 03:07:41 +0000 | [diff] [blame] | 938 | TheJIT = llvm::make_unique<KaleidoscopeJIT>(); |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 939 | |
Lang Hames | 2d789c3 | 2015-08-26 03:07:41 +0000 | [diff] [blame] | 940 | InitializeModuleAndPassManager(); |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 941 | |
| 942 | // Run the main "interpreter loop" now. |
| 943 | MainLoop(); |
| 944 | |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 945 | return 0; |
| 946 | } |