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