Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +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" |
Will Dietz | 981af00 | 2013-10-12 00:55:57 +0000 | [diff] [blame] | 10 | #include <cctype> |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 11 | #include <cstdio> |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 12 | #include <map> |
Chandler Carruth | 605e30e | 2012-12-04 10:16:57 +0000 | [diff] [blame] | 13 | #include <string> |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 14 | #include <vector> |
Lang Hames | 2d789c3 | 2015-08-26 03:07:41 +0000 | [diff] [blame] | 15 | #include "../include/KaleidoscopeJIT.h" |
| 16 | |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 17 | using namespace llvm; |
Lang Hames | 2d789c3 | 2015-08-26 03:07:41 +0000 | [diff] [blame] | 18 | using namespace llvm::orc; |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 19 | |
| 20 | //===----------------------------------------------------------------------===// |
| 21 | // Lexer |
| 22 | //===----------------------------------------------------------------------===// |
| 23 | |
| 24 | // The lexer returns tokens [0-255] if it is an unknown character, otherwise one |
| 25 | // of these for known things. |
| 26 | enum Token { |
| 27 | tok_eof = -1, |
| 28 | |
| 29 | // commands |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 30 | tok_def = -2, |
| 31 | tok_extern = -3, |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 32 | |
| 33 | // primary |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 34 | tok_identifier = -4, |
| 35 | tok_number = -5 |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 36 | }; |
| 37 | |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 38 | static std::string IdentifierStr; // Filled in if tok_identifier |
| 39 | static double NumVal; // Filled in if tok_number |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 40 | |
| 41 | /// gettok - Return the next token from standard input. |
| 42 | static int gettok() { |
| 43 | static int LastChar = ' '; |
| 44 | |
| 45 | // Skip any whitespace. |
| 46 | while (isspace(LastChar)) |
| 47 | LastChar = getchar(); |
| 48 | |
| 49 | if (isalpha(LastChar)) { // identifier: [a-zA-Z][a-zA-Z0-9]* |
| 50 | IdentifierStr = LastChar; |
| 51 | while (isalnum((LastChar = getchar()))) |
| 52 | IdentifierStr += LastChar; |
| 53 | |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 54 | if (IdentifierStr == "def") |
| 55 | return tok_def; |
| 56 | if (IdentifierStr == "extern") |
| 57 | return tok_extern; |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 58 | return tok_identifier; |
| 59 | } |
| 60 | |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 61 | if (isdigit(LastChar) || LastChar == '.') { // Number: [0-9.]+ |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 62 | std::string NumStr; |
| 63 | do { |
| 64 | NumStr += LastChar; |
| 65 | LastChar = getchar(); |
| 66 | } while (isdigit(LastChar) || LastChar == '.'); |
| 67 | |
| 68 | NumVal = strtod(NumStr.c_str(), 0); |
| 69 | return tok_number; |
| 70 | } |
| 71 | |
| 72 | if (LastChar == '#') { |
| 73 | // Comment until end of line. |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 74 | do |
| 75 | LastChar = getchar(); |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 76 | while (LastChar != EOF && LastChar != '\n' && LastChar != '\r'); |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 77 | |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 78 | if (LastChar != EOF) |
| 79 | return gettok(); |
| 80 | } |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 81 | |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 82 | // Check for end of file. Don't eat the EOF. |
| 83 | if (LastChar == EOF) |
| 84 | return tok_eof; |
| 85 | |
| 86 | // Otherwise, just return the character as its ascii value. |
| 87 | int ThisChar = LastChar; |
| 88 | LastChar = getchar(); |
| 89 | return ThisChar; |
| 90 | } |
| 91 | |
| 92 | //===----------------------------------------------------------------------===// |
| 93 | // Abstract Syntax Tree (aka Parse Tree) |
| 94 | //===----------------------------------------------------------------------===// |
Juergen Ributzka | 05c5a93 | 2013-11-19 03:08:35 +0000 | [diff] [blame] | 95 | namespace { |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 96 | /// ExprAST - Base class for all expression nodes. |
| 97 | class ExprAST { |
| 98 | public: |
Juergen Ributzka | 05c5a93 | 2013-11-19 03:08:35 +0000 | [diff] [blame] | 99 | virtual ~ExprAST() {} |
Lang Hames | 2d789c3 | 2015-08-26 03:07:41 +0000 | [diff] [blame] | 100 | virtual Value *codegen() = 0; |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 101 | }; |
| 102 | |
| 103 | /// NumberExprAST - Expression class for numeric literals like "1.0". |
| 104 | class NumberExprAST : public ExprAST { |
| 105 | double Val; |
Lang Hames | 59b0da8 | 2015-08-19 18:15:58 +0000 | [diff] [blame] | 106 | |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 107 | public: |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 108 | NumberExprAST(double Val) : Val(Val) {} |
Lang Hames | 2d789c3 | 2015-08-26 03:07:41 +0000 | [diff] [blame] | 109 | Value *codegen() override; |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 110 | }; |
| 111 | |
| 112 | /// VariableExprAST - Expression class for referencing a variable, like "a". |
| 113 | class VariableExprAST : public ExprAST { |
| 114 | std::string Name; |
Lang Hames | 59b0da8 | 2015-08-19 18:15:58 +0000 | [diff] [blame] | 115 | |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 116 | public: |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 117 | VariableExprAST(const std::string &Name) : Name(Name) {} |
Lang Hames | 2d789c3 | 2015-08-26 03:07:41 +0000 | [diff] [blame] | 118 | Value *codegen() override; |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 119 | }; |
| 120 | |
| 121 | /// BinaryExprAST - Expression class for a binary operator. |
| 122 | class BinaryExprAST : public ExprAST { |
| 123 | char Op; |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 124 | std::unique_ptr<ExprAST> LHS, RHS; |
Lang Hames | 59b0da8 | 2015-08-19 18:15:58 +0000 | [diff] [blame] | 125 | |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 126 | public: |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 127 | BinaryExprAST(char Op, std::unique_ptr<ExprAST> LHS, |
| 128 | std::unique_ptr<ExprAST> RHS) |
| 129 | : Op(Op), LHS(std::move(LHS)), RHS(std::move(RHS)) {} |
Lang Hames | 2d789c3 | 2015-08-26 03:07:41 +0000 | [diff] [blame] | 130 | Value *codegen() override; |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 131 | }; |
| 132 | |
| 133 | /// CallExprAST - Expression class for function calls. |
| 134 | class CallExprAST : public ExprAST { |
| 135 | std::string Callee; |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 136 | std::vector<std::unique_ptr<ExprAST>> Args; |
Lang Hames | 59b0da8 | 2015-08-19 18:15:58 +0000 | [diff] [blame] | 137 | |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 138 | public: |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 139 | CallExprAST(const std::string &Callee, |
| 140 | std::vector<std::unique_ptr<ExprAST>> Args) |
| 141 | : Callee(Callee), Args(std::move(Args)) {} |
Lang Hames | 2d789c3 | 2015-08-26 03:07:41 +0000 | [diff] [blame] | 142 | Value *codegen() override; |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 143 | }; |
| 144 | |
| 145 | /// PrototypeAST - This class represents the "prototype" for a function, |
| 146 | /// which captures its name, and its argument names (thus implicitly the number |
| 147 | /// of arguments the function takes). |
| 148 | class PrototypeAST { |
| 149 | std::string Name; |
| 150 | std::vector<std::string> Args; |
Lang Hames | 59b0da8 | 2015-08-19 18:15:58 +0000 | [diff] [blame] | 151 | |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 152 | public: |
Lang Hames | 59b0da8 | 2015-08-19 18:15:58 +0000 | [diff] [blame] | 153 | PrototypeAST(const std::string &Name, std::vector<std::string> Args) |
| 154 | : Name(Name), Args(std::move(Args)) {} |
Lang Hames | 2d789c3 | 2015-08-26 03:07:41 +0000 | [diff] [blame] | 155 | Function *codegen(); |
| 156 | const std::string &getName() const { return Name; } |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 157 | }; |
| 158 | |
| 159 | /// FunctionAST - This class represents a function definition itself. |
| 160 | class FunctionAST { |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 161 | std::unique_ptr<PrototypeAST> Proto; |
| 162 | std::unique_ptr<ExprAST> Body; |
Lang Hames | 59b0da8 | 2015-08-19 18:15:58 +0000 | [diff] [blame] | 163 | |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 164 | public: |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 165 | FunctionAST(std::unique_ptr<PrototypeAST> Proto, |
| 166 | std::unique_ptr<ExprAST> Body) |
Lang Hames | 59b0da8 | 2015-08-19 18:15:58 +0000 | [diff] [blame] | 167 | : Proto(std::move(Proto)), Body(std::move(Body)) {} |
Lang Hames | 2d789c3 | 2015-08-26 03:07:41 +0000 | [diff] [blame] | 168 | Function *codegen(); |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 169 | }; |
Juergen Ributzka | 05c5a93 | 2013-11-19 03:08:35 +0000 | [diff] [blame] | 170 | } // end anonymous namespace |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 171 | |
| 172 | //===----------------------------------------------------------------------===// |
| 173 | // Parser |
| 174 | //===----------------------------------------------------------------------===// |
| 175 | |
| 176 | /// CurTok/getNextToken - Provide a simple token buffer. CurTok is the current |
| 177 | /// token the parser is looking at. getNextToken reads another token from the |
| 178 | /// lexer and updates CurTok with its results. |
| 179 | static int CurTok; |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 180 | static int getNextToken() { return CurTok = gettok(); } |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 181 | |
| 182 | /// BinopPrecedence - This holds the precedence for each binary operator that is |
| 183 | /// defined. |
| 184 | static std::map<char, int> BinopPrecedence; |
| 185 | |
| 186 | /// GetTokPrecedence - Get the precedence of the pending binary operator token. |
| 187 | static int GetTokPrecedence() { |
| 188 | if (!isascii(CurTok)) |
| 189 | return -1; |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 190 | |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 191 | // Make sure it's a declared binop. |
| 192 | int TokPrec = BinopPrecedence[CurTok]; |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 193 | if (TokPrec <= 0) |
| 194 | return -1; |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 195 | return TokPrec; |
| 196 | } |
| 197 | |
| 198 | /// Error* - These are little helper functions for error handling. |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 199 | std::unique_ptr<ExprAST> Error(const char *Str) { |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 200 | fprintf(stderr, "Error: %s\n", Str); |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 201 | return nullptr; |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 202 | } |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 203 | std::unique_ptr<PrototypeAST> ErrorP(const char *Str) { |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 204 | Error(Str); |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 205 | return nullptr; |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 206 | } |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 207 | |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 208 | static std::unique_ptr<ExprAST> ParseExpression(); |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 209 | |
Lang Hames | 59b0da8 | 2015-08-19 18:15:58 +0000 | [diff] [blame] | 210 | /// numberexpr ::= number |
| 211 | static std::unique_ptr<ExprAST> ParseNumberExpr() { |
| 212 | auto Result = llvm::make_unique<NumberExprAST>(NumVal); |
| 213 | getNextToken(); // consume the number |
| 214 | return std::move(Result); |
| 215 | } |
| 216 | |
| 217 | /// parenexpr ::= '(' expression ')' |
| 218 | static std::unique_ptr<ExprAST> ParseParenExpr() { |
| 219 | getNextToken(); // eat (. |
| 220 | auto V = ParseExpression(); |
| 221 | if (!V) |
| 222 | return nullptr; |
| 223 | |
| 224 | if (CurTok != ')') |
| 225 | return Error("expected ')'"); |
| 226 | getNextToken(); // eat ). |
| 227 | return V; |
| 228 | } |
| 229 | |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 230 | /// identifierexpr |
| 231 | /// ::= identifier |
| 232 | /// ::= identifier '(' expression* ')' |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 233 | static std::unique_ptr<ExprAST> ParseIdentifierExpr() { |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 234 | std::string IdName = IdentifierStr; |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 235 | |
| 236 | getNextToken(); // eat identifier. |
| 237 | |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 238 | if (CurTok != '(') // Simple variable ref. |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 239 | return llvm::make_unique<VariableExprAST>(IdName); |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 240 | |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 241 | // Call. |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 242 | getNextToken(); // eat ( |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 243 | std::vector<std::unique_ptr<ExprAST>> Args; |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 244 | if (CurTok != ')') { |
| 245 | while (1) { |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 246 | if (auto Arg = ParseExpression()) |
| 247 | Args.push_back(std::move(Arg)); |
| 248 | else |
| 249 | return nullptr; |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 250 | |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 251 | if (CurTok == ')') |
| 252 | break; |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 253 | |
| 254 | if (CurTok != ',') |
| 255 | return Error("Expected ')' or ',' in argument list"); |
| 256 | getNextToken(); |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | // Eat the ')'. |
| 261 | getNextToken(); |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 262 | |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 263 | return llvm::make_unique<CallExprAST>(IdName, std::move(Args)); |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 264 | } |
| 265 | |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 266 | /// primary |
| 267 | /// ::= identifierexpr |
| 268 | /// ::= numberexpr |
| 269 | /// ::= parenexpr |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 270 | static std::unique_ptr<ExprAST> ParsePrimary() { |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 271 | switch (CurTok) { |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 272 | default: |
| 273 | return Error("unknown token when expecting an expression"); |
| 274 | case tok_identifier: |
| 275 | return ParseIdentifierExpr(); |
| 276 | case tok_number: |
| 277 | return ParseNumberExpr(); |
| 278 | case '(': |
| 279 | return ParseParenExpr(); |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 280 | } |
| 281 | } |
| 282 | |
| 283 | /// binoprhs |
| 284 | /// ::= ('+' primary)* |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 285 | static std::unique_ptr<ExprAST> ParseBinOpRHS(int ExprPrec, |
| 286 | std::unique_ptr<ExprAST> LHS) { |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 287 | // If this is a binop, find its precedence. |
| 288 | while (1) { |
| 289 | int TokPrec = GetTokPrecedence(); |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 290 | |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 291 | // If this is a binop that binds at least as tightly as the current binop, |
| 292 | // consume it, otherwise we are done. |
| 293 | if (TokPrec < ExprPrec) |
| 294 | return LHS; |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 295 | |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 296 | // Okay, we know this is a binop. |
| 297 | int BinOp = CurTok; |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 298 | getNextToken(); // eat binop |
| 299 | |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 300 | // Parse the primary expression after the binary operator. |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 301 | auto RHS = ParsePrimary(); |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 302 | if (!RHS) |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 303 | return nullptr; |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 304 | |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 305 | // If BinOp binds less tightly with RHS than the operator after RHS, let |
| 306 | // the pending operator take RHS as its LHS. |
| 307 | int NextPrec = GetTokPrecedence(); |
| 308 | if (TokPrec < NextPrec) { |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 309 | RHS = ParseBinOpRHS(TokPrec + 1, std::move(RHS)); |
| 310 | if (!RHS) |
| 311 | return nullptr; |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 312 | } |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 313 | |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 314 | // Merge LHS/RHS. |
Lang Hames | 59b0da8 | 2015-08-19 18:15:58 +0000 | [diff] [blame] | 315 | LHS = |
| 316 | llvm::make_unique<BinaryExprAST>(BinOp, std::move(LHS), std::move(RHS)); |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 317 | } |
| 318 | } |
| 319 | |
| 320 | /// expression |
| 321 | /// ::= primary binoprhs |
| 322 | /// |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 323 | static std::unique_ptr<ExprAST> ParseExpression() { |
| 324 | auto LHS = ParsePrimary(); |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 325 | if (!LHS) |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 326 | return nullptr; |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 327 | |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 328 | return ParseBinOpRHS(0, std::move(LHS)); |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | /// prototype |
| 332 | /// ::= id '(' id* ')' |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 333 | static std::unique_ptr<PrototypeAST> ParsePrototype() { |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 334 | if (CurTok != tok_identifier) |
| 335 | return ErrorP("Expected function name in prototype"); |
| 336 | |
| 337 | std::string FnName = IdentifierStr; |
| 338 | getNextToken(); |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 339 | |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 340 | if (CurTok != '(') |
| 341 | return ErrorP("Expected '(' in prototype"); |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 342 | |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 343 | std::vector<std::string> ArgNames; |
| 344 | while (getNextToken() == tok_identifier) |
| 345 | ArgNames.push_back(IdentifierStr); |
| 346 | if (CurTok != ')') |
| 347 | return ErrorP("Expected ')' in prototype"); |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 348 | |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 349 | // success. |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 350 | getNextToken(); // eat ')'. |
| 351 | |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 352 | return llvm::make_unique<PrototypeAST>(FnName, std::move(ArgNames)); |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | /// definition ::= 'def' prototype expression |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 356 | static std::unique_ptr<FunctionAST> ParseDefinition() { |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 357 | getNextToken(); // eat def. |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 358 | auto Proto = ParsePrototype(); |
| 359 | if (!Proto) |
| 360 | return nullptr; |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 361 | |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 362 | if (auto E = ParseExpression()) |
| 363 | return llvm::make_unique<FunctionAST>(std::move(Proto), std::move(E)); |
| 364 | return nullptr; |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | /// toplevelexpr ::= expression |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 368 | static std::unique_ptr<FunctionAST> ParseTopLevelExpr() { |
| 369 | if (auto E = ParseExpression()) { |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 370 | // Make an anonymous proto. |
Lang Hames | 2d789c3 | 2015-08-26 03:07:41 +0000 | [diff] [blame] | 371 | auto Proto = llvm::make_unique<PrototypeAST>("__anon_expr", |
| 372 | std::vector<std::string>()); |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 373 | return llvm::make_unique<FunctionAST>(std::move(Proto), std::move(E)); |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 374 | } |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 375 | return nullptr; |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | /// external ::= 'extern' prototype |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 379 | static std::unique_ptr<PrototypeAST> ParseExtern() { |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 380 | getNextToken(); // eat extern. |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 381 | return ParsePrototype(); |
| 382 | } |
| 383 | |
| 384 | //===----------------------------------------------------------------------===// |
| 385 | // Code Generation |
| 386 | //===----------------------------------------------------------------------===// |
| 387 | |
Lang Hames | 2d789c3 | 2015-08-26 03:07:41 +0000 | [diff] [blame] | 388 | static std::unique_ptr<Module> TheModule; |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 389 | static IRBuilder<> Builder(getGlobalContext()); |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 390 | static std::map<std::string, Value *> NamedValues; |
Lang Hames | 2d789c3 | 2015-08-26 03:07:41 +0000 | [diff] [blame] | 391 | static std::unique_ptr<legacy::FunctionPassManager> TheFPM; |
| 392 | static std::unique_ptr<KaleidoscopeJIT> TheJIT; |
| 393 | static std::map<std::string, std::unique_ptr<PrototypeAST>> FunctionProtos; |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 394 | |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 395 | Value *ErrorV(const char *Str) { |
| 396 | Error(Str); |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 397 | return nullptr; |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 398 | } |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 399 | |
Lang Hames | 2d789c3 | 2015-08-26 03:07:41 +0000 | [diff] [blame] | 400 | Function *getFunction(std::string Name) { |
| 401 | // First, see if the function has already been added to the current module. |
| 402 | if (auto *F = TheModule->getFunction(Name)) |
| 403 | return F; |
| 404 | |
| 405 | // If not, check whether we can codegen the declaration from some existing |
| 406 | // prototype. |
| 407 | auto FI = FunctionProtos.find(Name); |
| 408 | if (FI != FunctionProtos.end()) |
| 409 | return FI->second->codegen(); |
| 410 | |
| 411 | // If no existing prototype exists, return null. |
| 412 | return nullptr; |
| 413 | } |
| 414 | |
| 415 | Value *NumberExprAST::codegen() { |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 416 | return ConstantFP::get(getGlobalContext(), APFloat(Val)); |
| 417 | } |
| 418 | |
Lang Hames | 2d789c3 | 2015-08-26 03:07:41 +0000 | [diff] [blame] | 419 | Value *VariableExprAST::codegen() { |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 420 | // Look this variable up in the function. |
| 421 | Value *V = NamedValues[Name]; |
Lang Hames | 596aec9 | 2015-08-19 18:32:58 +0000 | [diff] [blame] | 422 | if (!V) |
| 423 | return ErrorV("Unknown variable name"); |
| 424 | return V; |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 425 | } |
| 426 | |
Lang Hames | 2d789c3 | 2015-08-26 03:07:41 +0000 | [diff] [blame] | 427 | Value *BinaryExprAST::codegen() { |
| 428 | Value *L = LHS->codegen(); |
| 429 | Value *R = RHS->codegen(); |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 430 | if (!L || !R) |
| 431 | return nullptr; |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 432 | |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 433 | switch (Op) { |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 434 | case '+': |
| 435 | return Builder.CreateFAdd(L, R, "addtmp"); |
| 436 | case '-': |
| 437 | return Builder.CreateFSub(L, R, "subtmp"); |
| 438 | case '*': |
| 439 | return Builder.CreateFMul(L, R, "multmp"); |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 440 | case '<': |
| 441 | L = Builder.CreateFCmpULT(L, R, "cmptmp"); |
| 442 | // Convert bool 0/1 to double 0.0 or 1.0 |
| 443 | return Builder.CreateUIToFP(L, Type::getDoubleTy(getGlobalContext()), |
| 444 | "booltmp"); |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 445 | default: |
| 446 | return ErrorV("invalid binary operator"); |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 447 | } |
| 448 | } |
| 449 | |
Lang Hames | 2d789c3 | 2015-08-26 03:07:41 +0000 | [diff] [blame] | 450 | Value *CallExprAST::codegen() { |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 451 | // Look up the name in the global module table. |
Lang Hames | 2d789c3 | 2015-08-26 03:07:41 +0000 | [diff] [blame] | 452 | Function *CalleeF = getFunction(Callee); |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 453 | if (!CalleeF) |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 454 | return ErrorV("Unknown function referenced"); |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 455 | |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 456 | // If argument mismatch error. |
| 457 | if (CalleeF->arg_size() != Args.size()) |
| 458 | return ErrorV("Incorrect # arguments passed"); |
| 459 | |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 460 | std::vector<Value *> ArgsV; |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 461 | for (unsigned i = 0, e = Args.size(); i != e; ++i) { |
Lang Hames | 2d789c3 | 2015-08-26 03:07:41 +0000 | [diff] [blame] | 462 | ArgsV.push_back(Args[i]->codegen()); |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 463 | if (!ArgsV.back()) |
| 464 | return nullptr; |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 465 | } |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 466 | |
Francois Pichet | c5d1050 | 2011-07-15 10:59:52 +0000 | [diff] [blame] | 467 | return Builder.CreateCall(CalleeF, ArgsV, "calltmp"); |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 468 | } |
| 469 | |
Lang Hames | 2d789c3 | 2015-08-26 03:07:41 +0000 | [diff] [blame] | 470 | Function *PrototypeAST::codegen() { |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 471 | // Make the function type: double(double,double) etc. |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 472 | std::vector<Type *> Doubles(Args.size(), |
| 473 | Type::getDoubleTy(getGlobalContext())); |
| 474 | FunctionType *FT = |
| 475 | FunctionType::get(Type::getDoubleTy(getGlobalContext()), Doubles, false); |
| 476 | |
Lang Hames | 2d789c3 | 2015-08-26 03:07:41 +0000 | [diff] [blame] | 477 | Function *F = |
| 478 | Function::Create(FT, Function::ExternalLinkage, Name, TheModule.get()); |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 479 | |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 480 | // Set names for all arguments. |
| 481 | unsigned Idx = 0; |
Lang Hames | 2d789c3 | 2015-08-26 03:07:41 +0000 | [diff] [blame] | 482 | for (auto &Arg : F->args()) |
| 483 | Arg.setName(Args[Idx++]); |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 484 | |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 485 | return F; |
| 486 | } |
| 487 | |
Lang Hames | 2d789c3 | 2015-08-26 03:07:41 +0000 | [diff] [blame] | 488 | Function *FunctionAST::codegen() { |
| 489 | // Transfer ownership of the prototype to the FunctionProtos map, but keep a |
| 490 | // reference to it for use below. |
| 491 | auto &P = *Proto; |
| 492 | FunctionProtos[Proto->getName()] = std::move(Proto); |
| 493 | Function *TheFunction = getFunction(P.getName()); |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 494 | if (!TheFunction) |
| 495 | return nullptr; |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 496 | |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 497 | // Create a new basic block to start insertion into. |
| 498 | BasicBlock *BB = BasicBlock::Create(getGlobalContext(), "entry", TheFunction); |
| 499 | Builder.SetInsertPoint(BB); |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 500 | |
Lang Hames | 2d789c3 | 2015-08-26 03:07:41 +0000 | [diff] [blame] | 501 | // Record the function arguments in the NamedValues map. |
| 502 | NamedValues.clear(); |
| 503 | for (auto &Arg : TheFunction->args()) |
| 504 | NamedValues[Arg.getName()] = &Arg; |
| 505 | |
| 506 | if (Value *RetVal = Body->codegen()) { |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 507 | // Finish off the function. |
| 508 | Builder.CreateRet(RetVal); |
| 509 | |
| 510 | // Validate the generated code, checking for consistency. |
| 511 | verifyFunction(*TheFunction); |
| 512 | |
Lang Hames | 2d789c3 | 2015-08-26 03:07:41 +0000 | [diff] [blame] | 513 | // Run the optimizer on the function. |
| 514 | TheFPM->run(*TheFunction); |
| 515 | |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 516 | return TheFunction; |
| 517 | } |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 518 | |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 519 | // Error reading body, remove function. |
| 520 | TheFunction->eraseFromParent(); |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 521 | return nullptr; |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 522 | } |
| 523 | |
| 524 | //===----------------------------------------------------------------------===// |
| 525 | // Top-Level parsing and JIT Driver |
| 526 | //===----------------------------------------------------------------------===// |
| 527 | |
Lang Hames | 2d789c3 | 2015-08-26 03:07:41 +0000 | [diff] [blame] | 528 | static void InitializeModuleAndPassManager() { |
| 529 | // Open a new module. |
| 530 | TheModule = llvm::make_unique<Module>("my cool jit", getGlobalContext()); |
| 531 | TheModule->setDataLayout(TheJIT->getTargetMachine().createDataLayout()); |
| 532 | |
| 533 | // Create a new pass manager attached to it. |
| 534 | TheFPM = llvm::make_unique<legacy::FunctionPassManager>(TheModule.get()); |
| 535 | |
Lang Hames | 2d789c3 | 2015-08-26 03:07:41 +0000 | [diff] [blame] | 536 | // Do simple "peephole" optimizations and bit-twiddling optzns. |
| 537 | TheFPM->add(createInstructionCombiningPass()); |
| 538 | // Reassociate expressions. |
| 539 | TheFPM->add(createReassociatePass()); |
| 540 | // Eliminate Common SubExpressions. |
| 541 | TheFPM->add(createGVNPass()); |
| 542 | // Simplify the control flow graph (deleting unreachable blocks, etc). |
| 543 | TheFPM->add(createCFGSimplificationPass()); |
| 544 | |
| 545 | TheFPM->doInitialization(); |
| 546 | } |
| 547 | |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 548 | static void HandleDefinition() { |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 549 | if (auto FnAST = ParseDefinition()) { |
Lang Hames | 2d789c3 | 2015-08-26 03:07:41 +0000 | [diff] [blame] | 550 | if (auto *FnIR = FnAST->codegen()) { |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 551 | fprintf(stderr, "Read function definition:"); |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 552 | FnIR->dump(); |
Lang Hames | 2d789c3 | 2015-08-26 03:07:41 +0000 | [diff] [blame] | 553 | TheJIT->addModule(std::move(TheModule)); |
| 554 | InitializeModuleAndPassManager(); |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 555 | } |
| 556 | } else { |
| 557 | // Skip token for error recovery. |
| 558 | getNextToken(); |
| 559 | } |
| 560 | } |
| 561 | |
| 562 | static void HandleExtern() { |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 563 | if (auto ProtoAST = ParseExtern()) { |
Lang Hames | 2d789c3 | 2015-08-26 03:07:41 +0000 | [diff] [blame] | 564 | if (auto *FnIR = ProtoAST->codegen()) { |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 565 | fprintf(stderr, "Read extern: "); |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 566 | FnIR->dump(); |
Lang Hames | 2d789c3 | 2015-08-26 03:07:41 +0000 | [diff] [blame] | 567 | FunctionProtos[ProtoAST->getName()] = std::move(ProtoAST); |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 568 | } |
| 569 | } else { |
| 570 | // Skip token for error recovery. |
| 571 | getNextToken(); |
| 572 | } |
| 573 | } |
| 574 | |
| 575 | static void HandleTopLevelExpression() { |
| 576 | // Evaluate a top-level expression into an anonymous function. |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 577 | if (auto FnAST = ParseTopLevelExpr()) { |
Lang Hames | 2d789c3 | 2015-08-26 03:07:41 +0000 | [diff] [blame] | 578 | if (FnAST->codegen()) { |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 579 | |
Lang Hames | 2d789c3 | 2015-08-26 03:07:41 +0000 | [diff] [blame] | 580 | // JIT the module containing the anonymous expression, keeping a handle so |
| 581 | // we can free it later. |
| 582 | auto H = TheJIT->addModule(std::move(TheModule)); |
| 583 | InitializeModuleAndPassManager(); |
| 584 | |
| 585 | // Search the JIT for the __anon_expr symbol. |
| 586 | auto ExprSymbol = TheJIT->findSymbol("__anon_expr"); |
| 587 | assert(ExprSymbol && "Function not found"); |
| 588 | |
| 589 | // Get the symbol's address and cast it to the right type (takes no |
| 590 | // arguments, returns a double) so we can call it as a native function. |
| 591 | double (*FP)() = (double (*)())(intptr_t)ExprSymbol.getAddress(); |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 592 | fprintf(stderr, "Evaluated to %f\n", FP()); |
Lang Hames | 2d789c3 | 2015-08-26 03:07:41 +0000 | [diff] [blame] | 593 | |
| 594 | // Delete the anonymous expression module from the JIT. |
| 595 | TheJIT->removeModule(H); |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 596 | } |
| 597 | } else { |
| 598 | // Skip token for error recovery. |
| 599 | getNextToken(); |
| 600 | } |
| 601 | } |
| 602 | |
| 603 | /// top ::= definition | external | expression | ';' |
| 604 | static void MainLoop() { |
| 605 | while (1) { |
| 606 | fprintf(stderr, "ready> "); |
| 607 | switch (CurTok) { |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 608 | case tok_eof: |
| 609 | return; |
Lang Hames | 59b0da8 | 2015-08-19 18:15:58 +0000 | [diff] [blame] | 610 | case ';': // ignore top-level semicolons. |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 611 | getNextToken(); |
Lang Hames | 59b0da8 | 2015-08-19 18:15:58 +0000 | [diff] [blame] | 612 | break; |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 613 | case tok_def: |
| 614 | HandleDefinition(); |
| 615 | break; |
| 616 | case tok_extern: |
| 617 | HandleExtern(); |
| 618 | break; |
| 619 | default: |
| 620 | HandleTopLevelExpression(); |
| 621 | break; |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 622 | } |
| 623 | } |
| 624 | } |
| 625 | |
| 626 | //===----------------------------------------------------------------------===// |
| 627 | // "Library" functions that can be "extern'd" from user code. |
| 628 | //===----------------------------------------------------------------------===// |
| 629 | |
| 630 | /// putchard - putchar that takes a double and returns 0. |
NAKAMURA Takumi | ac9d373 | 2015-08-28 03:34:33 +0000 | [diff] [blame] | 631 | extern "C" double putchard(double X) { |
Lang Hames | d76e067 | 2015-08-27 20:31:44 +0000 | [diff] [blame] | 632 | fputc((char)X, stderr); |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 633 | return 0; |
| 634 | } |
| 635 | |
Lang Hames | 59b0da8 | 2015-08-19 18:15:58 +0000 | [diff] [blame] | 636 | /// 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] | 637 | extern "C" double printd(double X) { |
Lang Hames | d76e067 | 2015-08-27 20:31:44 +0000 | [diff] [blame] | 638 | fprintf(stderr, "%f\n", X); |
Lang Hames | 59b0da8 | 2015-08-19 18:15:58 +0000 | [diff] [blame] | 639 | return 0; |
| 640 | } |
| 641 | |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 642 | //===----------------------------------------------------------------------===// |
| 643 | // Main driver code. |
| 644 | //===----------------------------------------------------------------------===// |
| 645 | |
| 646 | int main() { |
| 647 | InitializeNativeTarget(); |
Eric Christopher | 1b74b65 | 2014-12-08 18:00:38 +0000 | [diff] [blame] | 648 | InitializeNativeTargetAsmPrinter(); |
| 649 | InitializeNativeTargetAsmParser(); |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 650 | |
| 651 | // Install standard binary operators. |
| 652 | // 1 is lowest precedence. |
| 653 | BinopPrecedence['<'] = 10; |
| 654 | BinopPrecedence['+'] = 20; |
| 655 | BinopPrecedence['-'] = 20; |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 656 | BinopPrecedence['*'] = 40; // highest. |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 657 | |
| 658 | // Prime the first token. |
| 659 | fprintf(stderr, "ready> "); |
| 660 | getNextToken(); |
| 661 | |
Lang Hames | 2d789c3 | 2015-08-26 03:07:41 +0000 | [diff] [blame] | 662 | TheJIT = llvm::make_unique<KaleidoscopeJIT>(); |
| 663 | |
| 664 | InitializeModuleAndPassManager(); |
| 665 | |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 666 | // Run the main "interpreter loop" now. |
| 667 | MainLoop(); |
| 668 | |
Erick Tryzelaar | 21e83ea | 2009-09-22 21:15:19 +0000 | [diff] [blame] | 669 | return 0; |
| 670 | } |