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