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" |
Lang Hames | 2d789c3 | 2015-08-26 03:07:41 +0000 | [diff] [blame] | 18 | #include "../include/KaleidoscopeJIT.h" |
Eugene Zelenko | ae7ac95 | 2016-11-18 21:57:58 +0000 | [diff] [blame^] | 19 | #include <algorithm> |
Eugene Zelenko | f981ec4 | 2016-05-19 01:08:04 +0000 | [diff] [blame] | 20 | #include <cassert> |
| 21 | #include <cctype> |
| 22 | #include <cstdint> |
| 23 | #include <cstdio> |
| 24 | #include <cstdlib> |
| 25 | #include <map> |
| 26 | #include <memory> |
| 27 | #include <string> |
| 28 | #include <utility> |
| 29 | #include <vector> |
Lang Hames | 2d789c3 | 2015-08-26 03:07:41 +0000 | [diff] [blame] | 30 | |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 31 | using namespace llvm; |
Lang Hames | 2d789c3 | 2015-08-26 03:07:41 +0000 | [diff] [blame] | 32 | using namespace llvm::orc; |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 33 | |
| 34 | //===----------------------------------------------------------------------===// |
| 35 | // Lexer |
| 36 | //===----------------------------------------------------------------------===// |
| 37 | |
| 38 | // The lexer returns tokens [0-255] if it is an unknown character, otherwise one |
| 39 | // of these for known things. |
| 40 | enum Token { |
| 41 | tok_eof = -1, |
| 42 | |
| 43 | // commands |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 44 | tok_def = -2, |
| 45 | tok_extern = -3, |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 46 | |
| 47 | // primary |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 48 | tok_identifier = -4, |
| 49 | tok_number = -5, |
| 50 | |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 51 | // control |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 52 | tok_if = -6, |
| 53 | tok_then = -7, |
| 54 | tok_else = -8, |
| 55 | tok_for = -9, |
| 56 | tok_in = -10, |
| 57 | |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 58 | // operators |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 59 | tok_binary = -11, |
| 60 | tok_unary = -12, |
| 61 | |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 62 | // var definition |
| 63 | tok_var = -13 |
| 64 | }; |
| 65 | |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 66 | static std::string IdentifierStr; // Filled in if tok_identifier |
| 67 | static double NumVal; // Filled in if tok_number |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 68 | |
| 69 | /// gettok - Return the next token from standard input. |
| 70 | static int gettok() { |
| 71 | static int LastChar = ' '; |
| 72 | |
| 73 | // Skip any whitespace. |
| 74 | while (isspace(LastChar)) |
| 75 | LastChar = getchar(); |
| 76 | |
| 77 | if (isalpha(LastChar)) { // identifier: [a-zA-Z][a-zA-Z0-9]* |
| 78 | IdentifierStr = LastChar; |
| 79 | while (isalnum((LastChar = getchar()))) |
| 80 | IdentifierStr += LastChar; |
| 81 | |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 82 | if (IdentifierStr == "def") |
| 83 | return tok_def; |
| 84 | if (IdentifierStr == "extern") |
| 85 | return tok_extern; |
| 86 | if (IdentifierStr == "if") |
| 87 | return tok_if; |
| 88 | if (IdentifierStr == "then") |
| 89 | return tok_then; |
| 90 | if (IdentifierStr == "else") |
| 91 | return tok_else; |
| 92 | if (IdentifierStr == "for") |
| 93 | return tok_for; |
| 94 | if (IdentifierStr == "in") |
| 95 | return tok_in; |
| 96 | if (IdentifierStr == "binary") |
| 97 | return tok_binary; |
| 98 | if (IdentifierStr == "unary") |
| 99 | return tok_unary; |
| 100 | if (IdentifierStr == "var") |
| 101 | return tok_var; |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 102 | return tok_identifier; |
| 103 | } |
| 104 | |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 105 | if (isdigit(LastChar) || LastChar == '.') { // Number: [0-9.]+ |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 106 | std::string NumStr; |
| 107 | do { |
| 108 | NumStr += LastChar; |
| 109 | LastChar = getchar(); |
| 110 | } while (isdigit(LastChar) || LastChar == '.'); |
| 111 | |
Hans Wennborg | cc9deb4 | 2015-09-29 18:02:48 +0000 | [diff] [blame] | 112 | NumVal = strtod(NumStr.c_str(), nullptr); |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 113 | return tok_number; |
| 114 | } |
| 115 | |
| 116 | if (LastChar == '#') { |
| 117 | // Comment until end of line. |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 118 | do |
| 119 | LastChar = getchar(); |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 120 | while (LastChar != EOF && LastChar != '\n' && LastChar != '\r'); |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 121 | |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 122 | if (LastChar != EOF) |
| 123 | return gettok(); |
| 124 | } |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 125 | |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 126 | // Check for end of file. Don't eat the EOF. |
| 127 | if (LastChar == EOF) |
| 128 | return tok_eof; |
| 129 | |
| 130 | // Otherwise, just return the character as its ascii value. |
| 131 | int ThisChar = LastChar; |
| 132 | LastChar = getchar(); |
| 133 | return ThisChar; |
| 134 | } |
| 135 | |
| 136 | //===----------------------------------------------------------------------===// |
| 137 | // Abstract Syntax Tree (aka Parse Tree) |
| 138 | //===----------------------------------------------------------------------===// |
Eugene Zelenko | ae7ac95 | 2016-11-18 21:57:58 +0000 | [diff] [blame^] | 139 | |
Juergen Ributzka | 05c5a93 | 2013-11-19 03:08:35 +0000 | [diff] [blame] | 140 | namespace { |
Eugene Zelenko | ae7ac95 | 2016-11-18 21:57:58 +0000 | [diff] [blame^] | 141 | |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 142 | /// ExprAST - Base class for all expression nodes. |
| 143 | class ExprAST { |
| 144 | public: |
Eugene Zelenko | ae7ac95 | 2016-11-18 21:57:58 +0000 | [diff] [blame^] | 145 | virtual ~ExprAST() = default; |
| 146 | |
Lang Hames | 2d789c3 | 2015-08-26 03:07:41 +0000 | [diff] [blame] | 147 | virtual Value *codegen() = 0; |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 148 | }; |
| 149 | |
| 150 | /// NumberExprAST - Expression class for numeric literals like "1.0". |
| 151 | class NumberExprAST : public ExprAST { |
| 152 | double Val; |
Lang Hames | 59b0da8 | 2015-08-19 18:15:58 +0000 | [diff] [blame] | 153 | |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 154 | public: |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 155 | NumberExprAST(double Val) : Val(Val) {} |
Eugene Zelenko | ae7ac95 | 2016-11-18 21:57:58 +0000 | [diff] [blame^] | 156 | |
Lang Hames | 2d789c3 | 2015-08-26 03:07:41 +0000 | [diff] [blame] | 157 | Value *codegen() override; |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 158 | }; |
| 159 | |
| 160 | /// VariableExprAST - Expression class for referencing a variable, like "a". |
| 161 | class VariableExprAST : public ExprAST { |
| 162 | std::string Name; |
Lang Hames | 59b0da8 | 2015-08-19 18:15:58 +0000 | [diff] [blame] | 163 | |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 164 | public: |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 165 | VariableExprAST(const std::string &Name) : Name(Name) {} |
Eugene Zelenko | ae7ac95 | 2016-11-18 21:57:58 +0000 | [diff] [blame^] | 166 | |
Lang Hames | 2d789c3 | 2015-08-26 03:07:41 +0000 | [diff] [blame] | 167 | Value *codegen() override; |
Eugene Zelenko | ae7ac95 | 2016-11-18 21:57:58 +0000 | [diff] [blame^] | 168 | const std::string &getName() const { return Name; } |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 169 | }; |
| 170 | |
| 171 | /// UnaryExprAST - Expression class for a unary operator. |
| 172 | class UnaryExprAST : public ExprAST { |
| 173 | char Opcode; |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 174 | std::unique_ptr<ExprAST> Operand; |
Lang Hames | 59b0da8 | 2015-08-19 18:15:58 +0000 | [diff] [blame] | 175 | |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 176 | public: |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 177 | UnaryExprAST(char Opcode, std::unique_ptr<ExprAST> Operand) |
| 178 | : Opcode(Opcode), Operand(std::move(Operand)) {} |
Eugene Zelenko | ae7ac95 | 2016-11-18 21:57:58 +0000 | [diff] [blame^] | 179 | |
Lang Hames | 2d789c3 | 2015-08-26 03:07:41 +0000 | [diff] [blame] | 180 | Value *codegen() override; |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 181 | }; |
| 182 | |
| 183 | /// BinaryExprAST - Expression class for a binary operator. |
| 184 | class BinaryExprAST : public ExprAST { |
| 185 | char Op; |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 186 | std::unique_ptr<ExprAST> LHS, RHS; |
Lang Hames | 59b0da8 | 2015-08-19 18:15:58 +0000 | [diff] [blame] | 187 | |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 188 | public: |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 189 | BinaryExprAST(char Op, std::unique_ptr<ExprAST> LHS, |
| 190 | std::unique_ptr<ExprAST> RHS) |
| 191 | : Op(Op), LHS(std::move(LHS)), RHS(std::move(RHS)) {} |
Eugene Zelenko | ae7ac95 | 2016-11-18 21:57:58 +0000 | [diff] [blame^] | 192 | |
Lang Hames | 2d789c3 | 2015-08-26 03:07:41 +0000 | [diff] [blame] | 193 | Value *codegen() override; |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 194 | }; |
| 195 | |
| 196 | /// CallExprAST - Expression class for function calls. |
| 197 | class CallExprAST : public ExprAST { |
| 198 | std::string Callee; |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 199 | std::vector<std::unique_ptr<ExprAST>> Args; |
Lang Hames | 59b0da8 | 2015-08-19 18:15:58 +0000 | [diff] [blame] | 200 | |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 201 | public: |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 202 | CallExprAST(const std::string &Callee, |
| 203 | std::vector<std::unique_ptr<ExprAST>> Args) |
| 204 | : Callee(Callee), Args(std::move(Args)) {} |
Eugene Zelenko | ae7ac95 | 2016-11-18 21:57:58 +0000 | [diff] [blame^] | 205 | |
Lang Hames | 2d789c3 | 2015-08-26 03:07:41 +0000 | [diff] [blame] | 206 | Value *codegen() override; |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 207 | }; |
| 208 | |
| 209 | /// IfExprAST - Expression class for if/then/else. |
| 210 | class IfExprAST : public ExprAST { |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 211 | std::unique_ptr<ExprAST> Cond, Then, Else; |
Lang Hames | 59b0da8 | 2015-08-19 18:15:58 +0000 | [diff] [blame] | 212 | |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 213 | public: |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 214 | IfExprAST(std::unique_ptr<ExprAST> Cond, std::unique_ptr<ExprAST> Then, |
| 215 | std::unique_ptr<ExprAST> Else) |
| 216 | : Cond(std::move(Cond)), Then(std::move(Then)), Else(std::move(Else)) {} |
Eugene Zelenko | ae7ac95 | 2016-11-18 21:57:58 +0000 | [diff] [blame^] | 217 | |
Lang Hames | 2d789c3 | 2015-08-26 03:07:41 +0000 | [diff] [blame] | 218 | Value *codegen() override; |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 219 | }; |
| 220 | |
| 221 | /// ForExprAST - Expression class for for/in. |
| 222 | class ForExprAST : public ExprAST { |
| 223 | std::string VarName; |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 224 | std::unique_ptr<ExprAST> Start, End, Step, Body; |
Lang Hames | 59b0da8 | 2015-08-19 18:15:58 +0000 | [diff] [blame] | 225 | |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 226 | public: |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 227 | ForExprAST(const std::string &VarName, std::unique_ptr<ExprAST> Start, |
| 228 | std::unique_ptr<ExprAST> End, std::unique_ptr<ExprAST> Step, |
| 229 | std::unique_ptr<ExprAST> Body) |
| 230 | : VarName(VarName), Start(std::move(Start)), End(std::move(End)), |
| 231 | Step(std::move(Step)), Body(std::move(Body)) {} |
Eugene Zelenko | ae7ac95 | 2016-11-18 21:57:58 +0000 | [diff] [blame^] | 232 | |
Lang Hames | 2d789c3 | 2015-08-26 03:07:41 +0000 | [diff] [blame] | 233 | Value *codegen() override; |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 234 | }; |
| 235 | |
| 236 | /// VarExprAST - Expression class for var/in |
| 237 | class VarExprAST : public ExprAST { |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 238 | std::vector<std::pair<std::string, std::unique_ptr<ExprAST>>> VarNames; |
| 239 | std::unique_ptr<ExprAST> Body; |
Lang Hames | 59b0da8 | 2015-08-19 18:15:58 +0000 | [diff] [blame] | 240 | |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 241 | public: |
Lang Hames | 59b0da8 | 2015-08-19 18:15:58 +0000 | [diff] [blame] | 242 | VarExprAST( |
| 243 | std::vector<std::pair<std::string, std::unique_ptr<ExprAST>>> VarNames, |
| 244 | std::unique_ptr<ExprAST> Body) |
| 245 | : VarNames(std::move(VarNames)), Body(std::move(Body)) {} |
Eugene Zelenko | ae7ac95 | 2016-11-18 21:57:58 +0000 | [diff] [blame^] | 246 | |
Lang Hames | 2d789c3 | 2015-08-26 03:07:41 +0000 | [diff] [blame] | 247 | Value *codegen() override; |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 248 | }; |
| 249 | |
| 250 | /// PrototypeAST - This class represents the "prototype" for a function, |
Lang Hames | 59b0da8 | 2015-08-19 18:15:58 +0000 | [diff] [blame] | 251 | /// which captures its name, and its argument names (thus implicitly the number |
| 252 | /// of arguments the function takes), as well as if it is an operator. |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 253 | class PrototypeAST { |
| 254 | std::string Name; |
| 255 | std::vector<std::string> Args; |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 256 | bool IsOperator; |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 257 | unsigned Precedence; // Precedence if a binary op. |
Lang Hames | 59b0da8 | 2015-08-19 18:15:58 +0000 | [diff] [blame] | 258 | |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 259 | public: |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 260 | PrototypeAST(const std::string &Name, std::vector<std::string> Args, |
| 261 | bool IsOperator = false, unsigned Prec = 0) |
Lang Hames | 59b0da8 | 2015-08-19 18:15:58 +0000 | [diff] [blame] | 262 | : Name(Name), Args(std::move(Args)), IsOperator(IsOperator), |
| 263 | Precedence(Prec) {} |
Eugene Zelenko | ae7ac95 | 2016-11-18 21:57:58 +0000 | [diff] [blame^] | 264 | |
Lang Hames | 2d789c3 | 2015-08-26 03:07:41 +0000 | [diff] [blame] | 265 | Function *codegen(); |
| 266 | const std::string &getName() const { return Name; } |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 267 | |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 268 | bool isUnaryOp() const { return IsOperator && Args.size() == 1; } |
| 269 | bool isBinaryOp() const { return IsOperator && Args.size() == 2; } |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 270 | |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 271 | char getOperatorName() const { |
| 272 | assert(isUnaryOp() || isBinaryOp()); |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 273 | return Name[Name.size() - 1]; |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 274 | } |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 275 | |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 276 | unsigned getBinaryPrecedence() const { return Precedence; } |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 277 | }; |
| 278 | |
| 279 | /// FunctionAST - This class represents a function definition itself. |
| 280 | class FunctionAST { |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 281 | std::unique_ptr<PrototypeAST> Proto; |
| 282 | std::unique_ptr<ExprAST> Body; |
Lang Hames | 59b0da8 | 2015-08-19 18:15:58 +0000 | [diff] [blame] | 283 | |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 284 | public: |
Lang Hames | 59b0da8 | 2015-08-19 18:15:58 +0000 | [diff] [blame] | 285 | FunctionAST(std::unique_ptr<PrototypeAST> Proto, |
| 286 | std::unique_ptr<ExprAST> Body) |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 287 | : Proto(std::move(Proto)), Body(std::move(Body)) {} |
Eugene Zelenko | ae7ac95 | 2016-11-18 21:57:58 +0000 | [diff] [blame^] | 288 | |
Lang Hames | 2d789c3 | 2015-08-26 03:07:41 +0000 | [diff] [blame] | 289 | Function *codegen(); |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 290 | }; |
Eugene Zelenko | ae7ac95 | 2016-11-18 21:57:58 +0000 | [diff] [blame^] | 291 | |
Juergen Ributzka | 05c5a93 | 2013-11-19 03:08:35 +0000 | [diff] [blame] | 292 | } // end anonymous namespace |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 293 | |
| 294 | //===----------------------------------------------------------------------===// |
| 295 | // Parser |
| 296 | //===----------------------------------------------------------------------===// |
| 297 | |
| 298 | /// CurTok/getNextToken - Provide a simple token buffer. CurTok is the current |
Erick Tryzelaar | 6e2b34bc | 2009-09-22 21:14:49 +0000 | [diff] [blame] | 299 | /// token the parser is looking at. getNextToken reads another token from the |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 300 | /// lexer and updates CurTok with its results. |
| 301 | static int CurTok; |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 302 | static int getNextToken() { return CurTok = gettok(); } |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 303 | |
| 304 | /// BinopPrecedence - This holds the precedence for each binary operator that is |
| 305 | /// defined. |
| 306 | static std::map<char, int> BinopPrecedence; |
| 307 | |
| 308 | /// GetTokPrecedence - Get the precedence of the pending binary operator token. |
| 309 | static int GetTokPrecedence() { |
| 310 | if (!isascii(CurTok)) |
| 311 | return -1; |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 312 | |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 313 | // Make sure it's a declared binop. |
| 314 | int TokPrec = BinopPrecedence[CurTok]; |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 315 | if (TokPrec <= 0) |
| 316 | return -1; |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 317 | return TokPrec; |
| 318 | } |
| 319 | |
Lang Hames | f9878c5 | 2016-03-25 17:33:32 +0000 | [diff] [blame] | 320 | /// LogError* - These are little helper functions for error handling. |
| 321 | std::unique_ptr<ExprAST> LogError(const char *Str) { |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 322 | fprintf(stderr, "Error: %s\n", Str); |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 323 | return nullptr; |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 324 | } |
Hans Wennborg | cc9deb4 | 2015-09-29 18:02:48 +0000 | [diff] [blame] | 325 | |
Lang Hames | f9878c5 | 2016-03-25 17:33:32 +0000 | [diff] [blame] | 326 | std::unique_ptr<PrototypeAST> LogErrorP(const char *Str) { |
| 327 | LogError(Str); |
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 | } |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 330 | |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 331 | static std::unique_ptr<ExprAST> ParseExpression(); |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 332 | |
Lang Hames | 59b0da8 | 2015-08-19 18:15:58 +0000 | [diff] [blame] | 333 | /// numberexpr ::= number |
| 334 | static std::unique_ptr<ExprAST> ParseNumberExpr() { |
| 335 | auto Result = llvm::make_unique<NumberExprAST>(NumVal); |
| 336 | getNextToken(); // consume the number |
| 337 | return std::move(Result); |
| 338 | } |
| 339 | |
| 340 | /// parenexpr ::= '(' expression ')' |
| 341 | static std::unique_ptr<ExprAST> ParseParenExpr() { |
| 342 | getNextToken(); // eat (. |
| 343 | auto V = ParseExpression(); |
| 344 | if (!V) |
| 345 | return nullptr; |
| 346 | |
| 347 | if (CurTok != ')') |
Lang Hames | f9878c5 | 2016-03-25 17:33:32 +0000 | [diff] [blame] | 348 | return LogError("expected ')'"); |
Lang Hames | 59b0da8 | 2015-08-19 18:15:58 +0000 | [diff] [blame] | 349 | getNextToken(); // eat ). |
| 350 | return V; |
| 351 | } |
| 352 | |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 353 | /// identifierexpr |
| 354 | /// ::= identifier |
| 355 | /// ::= identifier '(' expression* ')' |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 356 | static std::unique_ptr<ExprAST> ParseIdentifierExpr() { |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 357 | std::string IdName = IdentifierStr; |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 358 | |
| 359 | getNextToken(); // eat identifier. |
| 360 | |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 361 | if (CurTok != '(') // Simple variable ref. |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 362 | return llvm::make_unique<VariableExprAST>(IdName); |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 363 | |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 364 | // Call. |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 365 | getNextToken(); // eat ( |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 366 | std::vector<std::unique_ptr<ExprAST>> Args; |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 367 | if (CurTok != ')') { |
Eugene Zelenko | f981ec4 | 2016-05-19 01:08:04 +0000 | [diff] [blame] | 368 | while (true) { |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 369 | if (auto Arg = ParseExpression()) |
| 370 | Args.push_back(std::move(Arg)); |
| 371 | else |
| 372 | return nullptr; |
Erick Tryzelaar | 6e2b34bc | 2009-09-22 21:14:49 +0000 | [diff] [blame] | 373 | |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 374 | if (CurTok == ')') |
| 375 | break; |
Erick Tryzelaar | 6e2b34bc | 2009-09-22 21:14:49 +0000 | [diff] [blame] | 376 | |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 377 | if (CurTok != ',') |
Lang Hames | f9878c5 | 2016-03-25 17:33:32 +0000 | [diff] [blame] | 378 | return LogError("Expected ')' or ',' in argument list"); |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 379 | getNextToken(); |
| 380 | } |
| 381 | } |
| 382 | |
| 383 | // Eat the ')'. |
| 384 | getNextToken(); |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 385 | |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 386 | return llvm::make_unique<CallExprAST>(IdName, std::move(Args)); |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 387 | } |
| 388 | |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 389 | /// ifexpr ::= 'if' expression 'then' expression 'else' expression |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 390 | static std::unique_ptr<ExprAST> ParseIfExpr() { |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 391 | getNextToken(); // eat the if. |
| 392 | |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 393 | // condition. |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 394 | auto Cond = ParseExpression(); |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 395 | if (!Cond) |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 396 | return nullptr; |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 397 | |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 398 | if (CurTok != tok_then) |
Lang Hames | f9878c5 | 2016-03-25 17:33:32 +0000 | [diff] [blame] | 399 | return LogError("expected then"); |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 400 | getNextToken(); // eat the then |
| 401 | |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 402 | auto Then = ParseExpression(); |
| 403 | if (!Then) |
| 404 | return nullptr; |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 405 | |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 406 | if (CurTok != tok_else) |
Lang Hames | f9878c5 | 2016-03-25 17:33:32 +0000 | [diff] [blame] | 407 | return LogError("expected else"); |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 408 | |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 409 | getNextToken(); |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 410 | |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 411 | auto Else = ParseExpression(); |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 412 | if (!Else) |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 413 | return nullptr; |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 414 | |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 415 | return llvm::make_unique<IfExprAST>(std::move(Cond), std::move(Then), |
| 416 | std::move(Else)); |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | /// forexpr ::= 'for' identifier '=' expr ',' expr (',' expr)? 'in' expression |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 420 | static std::unique_ptr<ExprAST> ParseForExpr() { |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 421 | getNextToken(); // eat the for. |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 422 | |
| 423 | if (CurTok != tok_identifier) |
Lang Hames | f9878c5 | 2016-03-25 17:33:32 +0000 | [diff] [blame] | 424 | return LogError("expected identifier after for"); |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 425 | |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 426 | std::string IdName = IdentifierStr; |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 427 | getNextToken(); // eat identifier. |
| 428 | |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 429 | if (CurTok != '=') |
Lang Hames | f9878c5 | 2016-03-25 17:33:32 +0000 | [diff] [blame] | 430 | return LogError("expected '=' after for"); |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 431 | getNextToken(); // eat '='. |
| 432 | |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 433 | auto Start = ParseExpression(); |
| 434 | if (!Start) |
| 435 | return nullptr; |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 436 | if (CurTok != ',') |
Lang Hames | f9878c5 | 2016-03-25 17:33:32 +0000 | [diff] [blame] | 437 | return LogError("expected ',' after for start value"); |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 438 | getNextToken(); |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 439 | |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 440 | auto End = ParseExpression(); |
| 441 | if (!End) |
| 442 | return nullptr; |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 443 | |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 444 | // The step value is optional. |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 445 | std::unique_ptr<ExprAST> Step; |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 446 | if (CurTok == ',') { |
| 447 | getNextToken(); |
| 448 | Step = ParseExpression(); |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 449 | if (!Step) |
| 450 | return nullptr; |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 451 | } |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 452 | |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 453 | if (CurTok != tok_in) |
Lang Hames | f9878c5 | 2016-03-25 17:33:32 +0000 | [diff] [blame] | 454 | return LogError("expected 'in' after for"); |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 455 | getNextToken(); // eat 'in'. |
| 456 | |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 457 | auto Body = ParseExpression(); |
| 458 | if (!Body) |
| 459 | return nullptr; |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 460 | |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 461 | return llvm::make_unique<ForExprAST>(IdName, std::move(Start), std::move(End), |
| 462 | std::move(Step), std::move(Body)); |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 463 | } |
| 464 | |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 465 | /// varexpr ::= 'var' identifier ('=' expression)? |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 466 | // (',' identifier ('=' expression)?)* 'in' expression |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 467 | static std::unique_ptr<ExprAST> ParseVarExpr() { |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 468 | getNextToken(); // eat the var. |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 469 | |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 470 | std::vector<std::pair<std::string, std::unique_ptr<ExprAST>>> VarNames; |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 471 | |
| 472 | // At least one variable name is required. |
| 473 | if (CurTok != tok_identifier) |
Lang Hames | f9878c5 | 2016-03-25 17:33:32 +0000 | [diff] [blame] | 474 | return LogError("expected identifier after var"); |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 475 | |
Eugene Zelenko | f981ec4 | 2016-05-19 01:08:04 +0000 | [diff] [blame] | 476 | while (true) { |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 477 | std::string Name = IdentifierStr; |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 478 | getNextToken(); // eat identifier. |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 479 | |
| 480 | // Read the optional initializer. |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 481 | std::unique_ptr<ExprAST> Init = nullptr; |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 482 | if (CurTok == '=') { |
| 483 | getNextToken(); // eat the '='. |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 484 | |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 485 | Init = ParseExpression(); |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 486 | if (!Init) |
| 487 | return nullptr; |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 488 | } |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 489 | |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 490 | VarNames.push_back(std::make_pair(Name, std::move(Init))); |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 491 | |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 492 | // End of var list, exit loop. |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 493 | if (CurTok != ',') |
| 494 | break; |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 495 | getNextToken(); // eat the ','. |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 496 | |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 497 | if (CurTok != tok_identifier) |
Lang Hames | f9878c5 | 2016-03-25 17:33:32 +0000 | [diff] [blame] | 498 | return LogError("expected identifier list after var"); |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 499 | } |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 500 | |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 501 | // At this point, we have to have 'in'. |
| 502 | if (CurTok != tok_in) |
Lang Hames | f9878c5 | 2016-03-25 17:33:32 +0000 | [diff] [blame] | 503 | return LogError("expected 'in' keyword after 'var'"); |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 504 | getNextToken(); // eat 'in'. |
| 505 | |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 506 | auto Body = ParseExpression(); |
| 507 | if (!Body) |
| 508 | return nullptr; |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 509 | |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 510 | return llvm::make_unique<VarExprAST>(std::move(VarNames), std::move(Body)); |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 511 | } |
| 512 | |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 513 | /// primary |
| 514 | /// ::= identifierexpr |
| 515 | /// ::= numberexpr |
| 516 | /// ::= parenexpr |
| 517 | /// ::= ifexpr |
| 518 | /// ::= forexpr |
| 519 | /// ::= varexpr |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 520 | static std::unique_ptr<ExprAST> ParsePrimary() { |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 521 | switch (CurTok) { |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 522 | default: |
Lang Hames | f9878c5 | 2016-03-25 17:33:32 +0000 | [diff] [blame] | 523 | return LogError("unknown token when expecting an expression"); |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 524 | case tok_identifier: |
| 525 | return ParseIdentifierExpr(); |
| 526 | case tok_number: |
| 527 | return ParseNumberExpr(); |
| 528 | case '(': |
| 529 | return ParseParenExpr(); |
| 530 | case tok_if: |
| 531 | return ParseIfExpr(); |
| 532 | case tok_for: |
| 533 | return ParseForExpr(); |
| 534 | case tok_var: |
| 535 | return ParseVarExpr(); |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 536 | } |
| 537 | } |
| 538 | |
| 539 | /// unary |
| 540 | /// ::= primary |
| 541 | /// ::= '!' unary |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 542 | static std::unique_ptr<ExprAST> ParseUnary() { |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 543 | // If the current token is not an operator, it must be a primary expr. |
| 544 | if (!isascii(CurTok) || CurTok == '(' || CurTok == ',') |
| 545 | return ParsePrimary(); |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 546 | |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 547 | // If this is a unary operator, read it. |
| 548 | int Opc = CurTok; |
| 549 | getNextToken(); |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 550 | if (auto Operand = ParseUnary()) |
| 551 | return llvm::make_unique<UnaryExprAST>(Opc, std::move(Operand)); |
| 552 | return nullptr; |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 553 | } |
| 554 | |
| 555 | /// binoprhs |
| 556 | /// ::= ('+' unary)* |
Lang Hames | 59b0da8 | 2015-08-19 18:15:58 +0000 | [diff] [blame] | 557 | static std::unique_ptr<ExprAST> ParseBinOpRHS(int ExprPrec, |
| 558 | std::unique_ptr<ExprAST> LHS) { |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 559 | // If this is a binop, find its precedence. |
Eugene Zelenko | f981ec4 | 2016-05-19 01:08:04 +0000 | [diff] [blame] | 560 | while (true) { |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 561 | int TokPrec = GetTokPrecedence(); |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 562 | |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 563 | // If this is a binop that binds at least as tightly as the current binop, |
| 564 | // consume it, otherwise we are done. |
| 565 | if (TokPrec < ExprPrec) |
| 566 | return LHS; |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 567 | |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 568 | // Okay, we know this is a binop. |
| 569 | int BinOp = CurTok; |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 570 | getNextToken(); // eat binop |
| 571 | |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 572 | // Parse the unary expression after the binary operator. |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 573 | auto RHS = ParseUnary(); |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 574 | if (!RHS) |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 575 | return nullptr; |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 576 | |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 577 | // If BinOp binds less tightly with RHS than the operator after RHS, let |
| 578 | // the pending operator take RHS as its LHS. |
| 579 | int NextPrec = GetTokPrecedence(); |
| 580 | if (TokPrec < NextPrec) { |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 581 | RHS = ParseBinOpRHS(TokPrec + 1, std::move(RHS)); |
| 582 | if (!RHS) |
| 583 | return nullptr; |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 584 | } |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 585 | |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 586 | // Merge LHS/RHS. |
Lang Hames | 59b0da8 | 2015-08-19 18:15:58 +0000 | [diff] [blame] | 587 | LHS = |
| 588 | llvm::make_unique<BinaryExprAST>(BinOp, std::move(LHS), std::move(RHS)); |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 589 | } |
| 590 | } |
| 591 | |
| 592 | /// expression |
| 593 | /// ::= unary binoprhs |
| 594 | /// |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 595 | static std::unique_ptr<ExprAST> ParseExpression() { |
| 596 | auto LHS = ParseUnary(); |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 597 | if (!LHS) |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 598 | return nullptr; |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 599 | |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 600 | return ParseBinOpRHS(0, std::move(LHS)); |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 601 | } |
| 602 | |
| 603 | /// prototype |
| 604 | /// ::= id '(' id* ')' |
| 605 | /// ::= binary LETTER number? (id, id) |
| 606 | /// ::= unary LETTER (id) |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 607 | static std::unique_ptr<PrototypeAST> ParsePrototype() { |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 608 | std::string FnName; |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 609 | |
Erick Tryzelaar | 6e2b34bc | 2009-09-22 21:14:49 +0000 | [diff] [blame] | 610 | unsigned Kind = 0; // 0 = identifier, 1 = unary, 2 = binary. |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 611 | unsigned BinaryPrecedence = 30; |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 612 | |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 613 | switch (CurTok) { |
| 614 | default: |
Lang Hames | f9878c5 | 2016-03-25 17:33:32 +0000 | [diff] [blame] | 615 | return LogErrorP("Expected function name in prototype"); |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 616 | case tok_identifier: |
| 617 | FnName = IdentifierStr; |
| 618 | Kind = 0; |
| 619 | getNextToken(); |
| 620 | break; |
| 621 | case tok_unary: |
| 622 | getNextToken(); |
| 623 | if (!isascii(CurTok)) |
Lang Hames | f9878c5 | 2016-03-25 17:33:32 +0000 | [diff] [blame] | 624 | return LogErrorP("Expected unary operator"); |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 625 | FnName = "unary"; |
| 626 | FnName += (char)CurTok; |
| 627 | Kind = 1; |
| 628 | getNextToken(); |
| 629 | break; |
| 630 | case tok_binary: |
| 631 | getNextToken(); |
| 632 | if (!isascii(CurTok)) |
Lang Hames | f9878c5 | 2016-03-25 17:33:32 +0000 | [diff] [blame] | 633 | return LogErrorP("Expected binary operator"); |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 634 | FnName = "binary"; |
| 635 | FnName += (char)CurTok; |
| 636 | Kind = 2; |
| 637 | getNextToken(); |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 638 | |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 639 | // Read the precedence if present. |
| 640 | if (CurTok == tok_number) { |
| 641 | if (NumVal < 1 || NumVal > 100) |
Lang Hames | f9878c5 | 2016-03-25 17:33:32 +0000 | [diff] [blame] | 642 | return LogErrorP("Invalid precedecnce: must be 1..100"); |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 643 | BinaryPrecedence = (unsigned)NumVal; |
| 644 | getNextToken(); |
| 645 | } |
| 646 | break; |
| 647 | } |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 648 | |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 649 | if (CurTok != '(') |
Lang Hames | f9878c5 | 2016-03-25 17:33:32 +0000 | [diff] [blame] | 650 | return LogErrorP("Expected '(' in prototype"); |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 651 | |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 652 | std::vector<std::string> ArgNames; |
| 653 | while (getNextToken() == tok_identifier) |
| 654 | ArgNames.push_back(IdentifierStr); |
| 655 | if (CurTok != ')') |
Lang Hames | f9878c5 | 2016-03-25 17:33:32 +0000 | [diff] [blame] | 656 | return LogErrorP("Expected ')' in prototype"); |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 657 | |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 658 | // success. |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 659 | getNextToken(); // eat ')'. |
| 660 | |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 661 | // Verify right number of names for operator. |
| 662 | if (Kind && ArgNames.size() != Kind) |
Lang Hames | f9878c5 | 2016-03-25 17:33:32 +0000 | [diff] [blame] | 663 | return LogErrorP("Invalid number of operands for operator"); |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 664 | |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 665 | return llvm::make_unique<PrototypeAST>(FnName, ArgNames, Kind != 0, |
| 666 | BinaryPrecedence); |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 667 | } |
| 668 | |
| 669 | /// definition ::= 'def' prototype expression |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 670 | static std::unique_ptr<FunctionAST> ParseDefinition() { |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 671 | getNextToken(); // eat def. |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 672 | auto Proto = ParsePrototype(); |
| 673 | if (!Proto) |
| 674 | return nullptr; |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 675 | |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 676 | if (auto E = ParseExpression()) |
| 677 | return llvm::make_unique<FunctionAST>(std::move(Proto), std::move(E)); |
| 678 | return nullptr; |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 679 | } |
| 680 | |
| 681 | /// toplevelexpr ::= expression |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 682 | static std::unique_ptr<FunctionAST> ParseTopLevelExpr() { |
| 683 | if (auto E = ParseExpression()) { |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 684 | // Make an anonymous proto. |
Lang Hames | 2d789c3 | 2015-08-26 03:07:41 +0000 | [diff] [blame] | 685 | auto Proto = llvm::make_unique<PrototypeAST>("__anon_expr", |
| 686 | std::vector<std::string>()); |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 687 | return llvm::make_unique<FunctionAST>(std::move(Proto), std::move(E)); |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 688 | } |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 689 | return nullptr; |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 690 | } |
| 691 | |
| 692 | /// external ::= 'extern' prototype |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 693 | static std::unique_ptr<PrototypeAST> ParseExtern() { |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 694 | getNextToken(); // eat extern. |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 695 | return ParsePrototype(); |
| 696 | } |
| 697 | |
| 698 | //===----------------------------------------------------------------------===// |
| 699 | // Code Generation |
| 700 | //===----------------------------------------------------------------------===// |
| 701 | |
Mehdi Amini | 03b42e4 | 2016-04-14 21:59:01 +0000 | [diff] [blame] | 702 | static LLVMContext TheContext; |
| 703 | static IRBuilder<> Builder(TheContext); |
Lang Hames | 2479680 | 2016-05-22 22:48:36 +0000 | [diff] [blame] | 704 | static std::unique_ptr<Module> TheModule; |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 705 | static std::map<std::string, AllocaInst *> NamedValues; |
Lang Hames | 2d789c3 | 2015-08-26 03:07:41 +0000 | [diff] [blame] | 706 | static std::unique_ptr<legacy::FunctionPassManager> TheFPM; |
| 707 | static std::unique_ptr<KaleidoscopeJIT> TheJIT; |
| 708 | static std::map<std::string, std::unique_ptr<PrototypeAST>> FunctionProtos; |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 709 | |
Lang Hames | f9878c5 | 2016-03-25 17:33:32 +0000 | [diff] [blame] | 710 | Value *LogErrorV(const char *Str) { |
| 711 | LogError(Str); |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 712 | return nullptr; |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 713 | } |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 714 | |
Lang Hames | 2d789c3 | 2015-08-26 03:07:41 +0000 | [diff] [blame] | 715 | Function *getFunction(std::string Name) { |
| 716 | // First, see if the function has already been added to the current module. |
| 717 | if (auto *F = TheModule->getFunction(Name)) |
| 718 | return F; |
| 719 | |
| 720 | // If not, check whether we can codegen the declaration from some existing |
| 721 | // prototype. |
| 722 | auto FI = FunctionProtos.find(Name); |
| 723 | if (FI != FunctionProtos.end()) |
| 724 | return FI->second->codegen(); |
| 725 | |
| 726 | // If no existing prototype exists, return null. |
| 727 | return nullptr; |
| 728 | } |
| 729 | |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 730 | /// CreateEntryBlockAlloca - Create an alloca instruction in the entry block of |
| 731 | /// the function. This is used for mutable variables etc. |
| 732 | static AllocaInst *CreateEntryBlockAlloca(Function *TheFunction, |
| 733 | const std::string &VarName) { |
| 734 | IRBuilder<> TmpB(&TheFunction->getEntryBlock(), |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 735 | TheFunction->getEntryBlock().begin()); |
Eugene Zelenko | f981ec4 | 2016-05-19 01:08:04 +0000 | [diff] [blame] | 736 | return TmpB.CreateAlloca(Type::getDoubleTy(TheContext), nullptr, VarName); |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 737 | } |
| 738 | |
Lang Hames | 2d789c3 | 2015-08-26 03:07:41 +0000 | [diff] [blame] | 739 | Value *NumberExprAST::codegen() { |
Mehdi Amini | 03b42e4 | 2016-04-14 21:59:01 +0000 | [diff] [blame] | 740 | return ConstantFP::get(TheContext, APFloat(Val)); |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 741 | } |
| 742 | |
Lang Hames | 2d789c3 | 2015-08-26 03:07:41 +0000 | [diff] [blame] | 743 | Value *VariableExprAST::codegen() { |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 744 | // Look this variable up in the function. |
| 745 | Value *V = NamedValues[Name]; |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 746 | if (!V) |
Lang Hames | f9878c5 | 2016-03-25 17:33:32 +0000 | [diff] [blame] | 747 | return LogErrorV("Unknown variable name"); |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 748 | |
| 749 | // Load the value. |
| 750 | return Builder.CreateLoad(V, Name.c_str()); |
| 751 | } |
| 752 | |
Lang Hames | 2d789c3 | 2015-08-26 03:07:41 +0000 | [diff] [blame] | 753 | Value *UnaryExprAST::codegen() { |
| 754 | Value *OperandV = Operand->codegen(); |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 755 | if (!OperandV) |
| 756 | return nullptr; |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 757 | |
Lang Hames | 2d789c3 | 2015-08-26 03:07:41 +0000 | [diff] [blame] | 758 | Function *F = getFunction(std::string("unary") + Opcode); |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 759 | if (!F) |
Lang Hames | f9878c5 | 2016-03-25 17:33:32 +0000 | [diff] [blame] | 760 | return LogErrorV("Unknown unary operator"); |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 761 | |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 762 | return Builder.CreateCall(F, OperandV, "unop"); |
| 763 | } |
| 764 | |
Lang Hames | 2d789c3 | 2015-08-26 03:07:41 +0000 | [diff] [blame] | 765 | Value *BinaryExprAST::codegen() { |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 766 | // Special case '=' because we don't want to emit the LHS as an expression. |
| 767 | if (Op == '=') { |
| 768 | // Assignment requires the LHS to be an identifier. |
Lang Hames | e7c28bc | 2015-04-22 20:41:34 +0000 | [diff] [blame] | 769 | // This assume we're building without RTTI because LLVM builds that way by |
| 770 | // default. If you build LLVM with RTTI this can be changed to a |
| 771 | // dynamic_cast for automatic error checking. |
Lang Hames | 59b0da8 | 2015-08-19 18:15:58 +0000 | [diff] [blame] | 772 | VariableExprAST *LHSE = static_cast<VariableExprAST *>(LHS.get()); |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 773 | if (!LHSE) |
Lang Hames | f9878c5 | 2016-03-25 17:33:32 +0000 | [diff] [blame] | 774 | return LogErrorV("destination of '=' must be a variable"); |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 775 | // Codegen the RHS. |
Lang Hames | 2d789c3 | 2015-08-26 03:07:41 +0000 | [diff] [blame] | 776 | Value *Val = RHS->codegen(); |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 777 | if (!Val) |
| 778 | return nullptr; |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 779 | |
| 780 | // Look up the name. |
| 781 | Value *Variable = NamedValues[LHSE->getName()]; |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 782 | if (!Variable) |
Lang Hames | f9878c5 | 2016-03-25 17:33:32 +0000 | [diff] [blame] | 783 | return LogErrorV("Unknown variable name"); |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 784 | |
| 785 | Builder.CreateStore(Val, Variable); |
| 786 | return Val; |
| 787 | } |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 788 | |
Lang Hames | 2d789c3 | 2015-08-26 03:07:41 +0000 | [diff] [blame] | 789 | Value *L = LHS->codegen(); |
| 790 | Value *R = RHS->codegen(); |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 791 | if (!L || !R) |
| 792 | return nullptr; |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 793 | |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 794 | switch (Op) { |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 795 | case '+': |
| 796 | return Builder.CreateFAdd(L, R, "addtmp"); |
| 797 | case '-': |
| 798 | return Builder.CreateFSub(L, R, "subtmp"); |
| 799 | case '*': |
| 800 | return Builder.CreateFMul(L, R, "multmp"); |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 801 | case '<': |
| 802 | L = Builder.CreateFCmpULT(L, R, "cmptmp"); |
| 803 | // Convert bool 0/1 to double 0.0 or 1.0 |
Mehdi Amini | 03b42e4 | 2016-04-14 21:59:01 +0000 | [diff] [blame] | 804 | return Builder.CreateUIToFP(L, Type::getDoubleTy(TheContext), "booltmp"); |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 805 | default: |
| 806 | break; |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 807 | } |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 808 | |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 809 | // If it wasn't a builtin binary operator, it must be a user defined one. Emit |
| 810 | // a call to it. |
Lang Hames | 2d789c3 | 2015-08-26 03:07:41 +0000 | [diff] [blame] | 811 | Function *F = getFunction(std::string("binary") + Op); |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 812 | assert(F && "binary operator not found!"); |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 813 | |
Lang Hames | 59b0da8 | 2015-08-19 18:15:58 +0000 | [diff] [blame] | 814 | Value *Ops[] = {L, R}; |
Francois Pichet | c5d1050 | 2011-07-15 10:59:52 +0000 | [diff] [blame] | 815 | return Builder.CreateCall(F, Ops, "binop"); |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 816 | } |
| 817 | |
Lang Hames | 2d789c3 | 2015-08-26 03:07:41 +0000 | [diff] [blame] | 818 | Value *CallExprAST::codegen() { |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 819 | // Look up the name in the global module table. |
Lang Hames | 2d789c3 | 2015-08-26 03:07:41 +0000 | [diff] [blame] | 820 | Function *CalleeF = getFunction(Callee); |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 821 | if (!CalleeF) |
Lang Hames | f9878c5 | 2016-03-25 17:33:32 +0000 | [diff] [blame] | 822 | return LogErrorV("Unknown function referenced"); |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 823 | |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 824 | // If argument mismatch error. |
| 825 | if (CalleeF->arg_size() != Args.size()) |
Lang Hames | f9878c5 | 2016-03-25 17:33:32 +0000 | [diff] [blame] | 826 | return LogErrorV("Incorrect # arguments passed"); |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 827 | |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 828 | std::vector<Value *> ArgsV; |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 829 | for (unsigned i = 0, e = Args.size(); i != e; ++i) { |
Lang Hames | 2d789c3 | 2015-08-26 03:07:41 +0000 | [diff] [blame] | 830 | ArgsV.push_back(Args[i]->codegen()); |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 831 | if (!ArgsV.back()) |
| 832 | return nullptr; |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 833 | } |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 834 | |
Francois Pichet | c5d1050 | 2011-07-15 10:59:52 +0000 | [diff] [blame] | 835 | return Builder.CreateCall(CalleeF, ArgsV, "calltmp"); |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 836 | } |
| 837 | |
Lang Hames | 2d789c3 | 2015-08-26 03:07:41 +0000 | [diff] [blame] | 838 | Value *IfExprAST::codegen() { |
| 839 | Value *CondV = Cond->codegen(); |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 840 | if (!CondV) |
| 841 | return nullptr; |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 842 | |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 843 | // Convert condition to a bool by comparing equal to 0.0. |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 844 | CondV = Builder.CreateFCmpONE( |
Mehdi Amini | 03b42e4 | 2016-04-14 21:59:01 +0000 | [diff] [blame] | 845 | CondV, ConstantFP::get(TheContext, APFloat(0.0)), "ifcond"); |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 846 | |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 847 | Function *TheFunction = Builder.GetInsertBlock()->getParent(); |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 848 | |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 849 | // Create blocks for the then and else cases. Insert the 'then' block at the |
| 850 | // end of the function. |
Mehdi Amini | 03b42e4 | 2016-04-14 21:59:01 +0000 | [diff] [blame] | 851 | BasicBlock *ThenBB = BasicBlock::Create(TheContext, "then", TheFunction); |
| 852 | BasicBlock *ElseBB = BasicBlock::Create(TheContext, "else"); |
| 853 | BasicBlock *MergeBB = BasicBlock::Create(TheContext, "ifcont"); |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 854 | |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 855 | Builder.CreateCondBr(CondV, ThenBB, ElseBB); |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 856 | |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 857 | // Emit then value. |
| 858 | Builder.SetInsertPoint(ThenBB); |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 859 | |
Lang Hames | 2d789c3 | 2015-08-26 03:07:41 +0000 | [diff] [blame] | 860 | Value *ThenV = Then->codegen(); |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 861 | if (!ThenV) |
| 862 | return nullptr; |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 863 | |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 864 | Builder.CreateBr(MergeBB); |
| 865 | // Codegen of 'Then' can change the current block, update ThenBB for the PHI. |
| 866 | ThenBB = Builder.GetInsertBlock(); |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 867 | |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 868 | // Emit else block. |
| 869 | TheFunction->getBasicBlockList().push_back(ElseBB); |
| 870 | Builder.SetInsertPoint(ElseBB); |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 871 | |
Lang Hames | 2d789c3 | 2015-08-26 03:07:41 +0000 | [diff] [blame] | 872 | Value *ElseV = Else->codegen(); |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 873 | if (!ElseV) |
| 874 | return nullptr; |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 875 | |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 876 | Builder.CreateBr(MergeBB); |
| 877 | // Codegen of 'Else' can change the current block, update ElseBB for the PHI. |
| 878 | ElseBB = Builder.GetInsertBlock(); |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 879 | |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 880 | // Emit merge block. |
| 881 | TheFunction->getBasicBlockList().push_back(MergeBB); |
| 882 | Builder.SetInsertPoint(MergeBB); |
Mehdi Amini | 03b42e4 | 2016-04-14 21:59:01 +0000 | [diff] [blame] | 883 | PHINode *PN = Builder.CreatePHI(Type::getDoubleTy(TheContext), 2, "iftmp"); |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 884 | |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 885 | PN->addIncoming(ThenV, ThenBB); |
| 886 | PN->addIncoming(ElseV, ElseBB); |
| 887 | return PN; |
| 888 | } |
| 889 | |
Lang Hames | 59b0da8 | 2015-08-19 18:15:58 +0000 | [diff] [blame] | 890 | // Output for-loop as: |
| 891 | // var = alloca double |
| 892 | // ... |
| 893 | // start = startexpr |
| 894 | // store start -> var |
| 895 | // goto loop |
| 896 | // loop: |
| 897 | // ... |
| 898 | // bodyexpr |
| 899 | // ... |
| 900 | // loopend: |
| 901 | // step = stepexpr |
| 902 | // endcond = endexpr |
| 903 | // |
| 904 | // curvar = load var |
| 905 | // nextvar = curvar + step |
| 906 | // store nextvar -> var |
| 907 | // br endcond, loop, endloop |
| 908 | // outloop: |
Lang Hames | 2d789c3 | 2015-08-26 03:07:41 +0000 | [diff] [blame] | 909 | Value *ForExprAST::codegen() { |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 910 | Function *TheFunction = Builder.GetInsertBlock()->getParent(); |
| 911 | |
| 912 | // Create an alloca for the variable in the entry block. |
| 913 | AllocaInst *Alloca = CreateEntryBlockAlloca(TheFunction, VarName); |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 914 | |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 915 | // Emit the start code first, without 'variable' in scope. |
Lang Hames | 2d789c3 | 2015-08-26 03:07:41 +0000 | [diff] [blame] | 916 | Value *StartVal = Start->codegen(); |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 917 | if (!StartVal) |
| 918 | return nullptr; |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 919 | |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 920 | // Store the value into the alloca. |
| 921 | Builder.CreateStore(StartVal, Alloca); |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 922 | |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 923 | // Make the new basic block for the loop header, inserting after current |
| 924 | // block. |
Mehdi Amini | 03b42e4 | 2016-04-14 21:59:01 +0000 | [diff] [blame] | 925 | BasicBlock *LoopBB = BasicBlock::Create(TheContext, "loop", TheFunction); |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 926 | |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 927 | // Insert an explicit fall through from the current block to the LoopBB. |
| 928 | Builder.CreateBr(LoopBB); |
| 929 | |
| 930 | // Start insertion in LoopBB. |
| 931 | Builder.SetInsertPoint(LoopBB); |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 932 | |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 933 | // Within the loop, the variable is defined equal to the PHI node. If it |
| 934 | // shadows an existing variable, we have to restore it, so save it now. |
| 935 | AllocaInst *OldVal = NamedValues[VarName]; |
| 936 | NamedValues[VarName] = Alloca; |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 937 | |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 938 | // Emit the body of the loop. This, like any other expr, can change the |
| 939 | // current BB. Note that we ignore the value computed by the body, but don't |
| 940 | // allow an error. |
Lang Hames | 2d789c3 | 2015-08-26 03:07:41 +0000 | [diff] [blame] | 941 | if (!Body->codegen()) |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 942 | return nullptr; |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 943 | |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 944 | // Emit the step value. |
Lang Hames | 59b0da8 | 2015-08-19 18:15:58 +0000 | [diff] [blame] | 945 | Value *StepVal = nullptr; |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 946 | if (Step) { |
Lang Hames | 2d789c3 | 2015-08-26 03:07:41 +0000 | [diff] [blame] | 947 | StepVal = Step->codegen(); |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 948 | if (!StepVal) |
| 949 | return nullptr; |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 950 | } else { |
| 951 | // If not specified, use 1.0. |
Mehdi Amini | 03b42e4 | 2016-04-14 21:59:01 +0000 | [diff] [blame] | 952 | StepVal = ConstantFP::get(TheContext, APFloat(1.0)); |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 953 | } |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 954 | |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 955 | // Compute the end condition. |
Lang Hames | 2d789c3 | 2015-08-26 03:07:41 +0000 | [diff] [blame] | 956 | Value *EndCond = End->codegen(); |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 957 | if (!EndCond) |
Lang Hames | 59b0da8 | 2015-08-19 18:15:58 +0000 | [diff] [blame] | 958 | return nullptr; |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 959 | |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 960 | // Reload, increment, and restore the alloca. This handles the case where |
| 961 | // the body of the loop mutates the variable. |
| 962 | Value *CurVar = Builder.CreateLoad(Alloca, VarName.c_str()); |
Chris Lattner | 26d7950 | 2010-06-21 22:51:14 +0000 | [diff] [blame] | 963 | Value *NextVar = Builder.CreateFAdd(CurVar, StepVal, "nextvar"); |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 964 | Builder.CreateStore(NextVar, Alloca); |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 965 | |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 966 | // Convert condition to a bool by comparing equal to 0.0. |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 967 | EndCond = Builder.CreateFCmpONE( |
Mehdi Amini | 03b42e4 | 2016-04-14 21:59:01 +0000 | [diff] [blame] | 968 | EndCond, ConstantFP::get(TheContext, APFloat(0.0)), "loopcond"); |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 969 | |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 970 | // Create the "after loop" block and insert it. |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 971 | BasicBlock *AfterBB = |
Mehdi Amini | 03b42e4 | 2016-04-14 21:59:01 +0000 | [diff] [blame] | 972 | BasicBlock::Create(TheContext, "afterloop", TheFunction); |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 973 | |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 974 | // Insert the conditional branch into the end of LoopEndBB. |
| 975 | Builder.CreateCondBr(EndCond, LoopBB, AfterBB); |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 976 | |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 977 | // Any new code will be inserted in AfterBB. |
| 978 | Builder.SetInsertPoint(AfterBB); |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 979 | |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 980 | // Restore the unshadowed variable. |
| 981 | if (OldVal) |
| 982 | NamedValues[VarName] = OldVal; |
| 983 | else |
| 984 | NamedValues.erase(VarName); |
| 985 | |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 986 | // for expr always returns 0.0. |
Mehdi Amini | 03b42e4 | 2016-04-14 21:59:01 +0000 | [diff] [blame] | 987 | return Constant::getNullValue(Type::getDoubleTy(TheContext)); |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 988 | } |
| 989 | |
Lang Hames | 2d789c3 | 2015-08-26 03:07:41 +0000 | [diff] [blame] | 990 | Value *VarExprAST::codegen() { |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 991 | std::vector<AllocaInst *> OldBindings; |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 992 | |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 993 | Function *TheFunction = Builder.GetInsertBlock()->getParent(); |
| 994 | |
| 995 | // Register all variables and emit their initializer. |
| 996 | for (unsigned i = 0, e = VarNames.size(); i != e; ++i) { |
| 997 | const std::string &VarName = VarNames[i].first; |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 998 | ExprAST *Init = VarNames[i].second.get(); |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 999 | |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 1000 | // Emit the initializer before adding the variable to scope, this prevents |
| 1001 | // the initializer from referencing the variable itself, and permits stuff |
| 1002 | // like this: |
| 1003 | // var a = 1 in |
| 1004 | // var a = a in ... # refers to outer 'a'. |
| 1005 | Value *InitVal; |
| 1006 | if (Init) { |
Lang Hames | 2d789c3 | 2015-08-26 03:07:41 +0000 | [diff] [blame] | 1007 | InitVal = Init->codegen(); |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 1008 | if (!InitVal) |
| 1009 | return nullptr; |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 1010 | } else { // If not specified, use 0.0. |
Mehdi Amini | 03b42e4 | 2016-04-14 21:59:01 +0000 | [diff] [blame] | 1011 | InitVal = ConstantFP::get(TheContext, APFloat(0.0)); |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 1012 | } |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 1013 | |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 1014 | AllocaInst *Alloca = CreateEntryBlockAlloca(TheFunction, VarName); |
| 1015 | Builder.CreateStore(InitVal, Alloca); |
| 1016 | |
| 1017 | // Remember the old variable binding so that we can restore the binding when |
| 1018 | // we unrecurse. |
| 1019 | OldBindings.push_back(NamedValues[VarName]); |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 1020 | |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 1021 | // Remember this binding. |
| 1022 | NamedValues[VarName] = Alloca; |
| 1023 | } |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 1024 | |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 1025 | // Codegen the body, now that all vars are in scope. |
Lang Hames | 2d789c3 | 2015-08-26 03:07:41 +0000 | [diff] [blame] | 1026 | Value *BodyVal = Body->codegen(); |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 1027 | if (!BodyVal) |
| 1028 | return nullptr; |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 1029 | |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 1030 | // Pop all our variables from scope. |
| 1031 | for (unsigned i = 0, e = VarNames.size(); i != e; ++i) |
| 1032 | NamedValues[VarNames[i].first] = OldBindings[i]; |
| 1033 | |
| 1034 | // Return the body computation. |
| 1035 | return BodyVal; |
| 1036 | } |
| 1037 | |
Lang Hames | 2d789c3 | 2015-08-26 03:07:41 +0000 | [diff] [blame] | 1038 | Function *PrototypeAST::codegen() { |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 1039 | // Make the function type: double(double,double) etc. |
Mehdi Amini | 03b42e4 | 2016-04-14 21:59:01 +0000 | [diff] [blame] | 1040 | std::vector<Type *> Doubles(Args.size(), Type::getDoubleTy(TheContext)); |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 1041 | FunctionType *FT = |
Mehdi Amini | 03b42e4 | 2016-04-14 21:59:01 +0000 | [diff] [blame] | 1042 | FunctionType::get(Type::getDoubleTy(TheContext), Doubles, false); |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 1043 | |
| 1044 | Function *F = |
Lang Hames | 2d789c3 | 2015-08-26 03:07:41 +0000 | [diff] [blame] | 1045 | Function::Create(FT, Function::ExternalLinkage, Name, TheModule.get()); |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 1046 | |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 1047 | // Set names for all arguments. |
| 1048 | unsigned Idx = 0; |
Lang Hames | 2d789c3 | 2015-08-26 03:07:41 +0000 | [diff] [blame] | 1049 | for (auto &Arg : F->args()) |
| 1050 | Arg.setName(Args[Idx++]); |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 1051 | |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 1052 | return F; |
| 1053 | } |
| 1054 | |
Lang Hames | 2d789c3 | 2015-08-26 03:07:41 +0000 | [diff] [blame] | 1055 | Function *FunctionAST::codegen() { |
| 1056 | // Transfer ownership of the prototype to the FunctionProtos map, but keep a |
| 1057 | // reference to it for use below. |
| 1058 | auto &P = *Proto; |
| 1059 | FunctionProtos[Proto->getName()] = std::move(Proto); |
| 1060 | Function *TheFunction = getFunction(P.getName()); |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 1061 | if (!TheFunction) |
| 1062 | return nullptr; |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 1063 | |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 1064 | // If this is an operator, install it. |
Lang Hames | 2d789c3 | 2015-08-26 03:07:41 +0000 | [diff] [blame] | 1065 | if (P.isBinaryOp()) |
| 1066 | BinopPrecedence[P.getOperatorName()] = P.getBinaryPrecedence(); |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 1067 | |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 1068 | // Create a new basic block to start insertion into. |
Mehdi Amini | 03b42e4 | 2016-04-14 21:59:01 +0000 | [diff] [blame] | 1069 | BasicBlock *BB = BasicBlock::Create(TheContext, "entry", TheFunction); |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 1070 | Builder.SetInsertPoint(BB); |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 1071 | |
Lang Hames | 2d789c3 | 2015-08-26 03:07:41 +0000 | [diff] [blame] | 1072 | // Record the function arguments in the NamedValues map. |
| 1073 | NamedValues.clear(); |
| 1074 | for (auto &Arg : TheFunction->args()) { |
| 1075 | // Create an alloca for this variable. |
| 1076 | AllocaInst *Alloca = CreateEntryBlockAlloca(TheFunction, Arg.getName()); |
Erick Tryzelaar | 6e2b34bc | 2009-09-22 21:14:49 +0000 | [diff] [blame] | 1077 | |
Lang Hames | 2d789c3 | 2015-08-26 03:07:41 +0000 | [diff] [blame] | 1078 | // Store the initial value into the alloca. |
| 1079 | Builder.CreateStore(&Arg, Alloca); |
| 1080 | |
| 1081 | // Add arguments to variable symbol table. |
| 1082 | NamedValues[Arg.getName()] = Alloca; |
| 1083 | } |
| 1084 | |
| 1085 | if (Value *RetVal = Body->codegen()) { |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 1086 | // Finish off the function. |
| 1087 | Builder.CreateRet(RetVal); |
| 1088 | |
| 1089 | // Validate the generated code, checking for consistency. |
| 1090 | verifyFunction(*TheFunction); |
| 1091 | |
Lang Hames | 2d789c3 | 2015-08-26 03:07:41 +0000 | [diff] [blame] | 1092 | // Run the optimizer on the function. |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 1093 | TheFPM->run(*TheFunction); |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 1094 | |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 1095 | return TheFunction; |
| 1096 | } |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 1097 | |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 1098 | // Error reading body, remove function. |
| 1099 | TheFunction->eraseFromParent(); |
| 1100 | |
Lang Hames | 2d789c3 | 2015-08-26 03:07:41 +0000 | [diff] [blame] | 1101 | if (P.isBinaryOp()) |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 1102 | BinopPrecedence.erase(Proto->getOperatorName()); |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 1103 | return nullptr; |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 1104 | } |
| 1105 | |
| 1106 | //===----------------------------------------------------------------------===// |
| 1107 | // Top-Level parsing and JIT Driver |
| 1108 | //===----------------------------------------------------------------------===// |
| 1109 | |
Lang Hames | 2d789c3 | 2015-08-26 03:07:41 +0000 | [diff] [blame] | 1110 | static void InitializeModuleAndPassManager() { |
| 1111 | // Open a new module. |
Mehdi Amini | 03b42e4 | 2016-04-14 21:59:01 +0000 | [diff] [blame] | 1112 | TheModule = llvm::make_unique<Module>("my cool jit", TheContext); |
Lang Hames | 2d789c3 | 2015-08-26 03:07:41 +0000 | [diff] [blame] | 1113 | TheModule->setDataLayout(TheJIT->getTargetMachine().createDataLayout()); |
| 1114 | |
| 1115 | // Create a new pass manager attached to it. |
| 1116 | TheFPM = llvm::make_unique<legacy::FunctionPassManager>(TheModule.get()); |
| 1117 | |
Lang Hames | 2d789c3 | 2015-08-26 03:07:41 +0000 | [diff] [blame] | 1118 | // Do simple "peephole" optimizations and bit-twiddling optzns. |
| 1119 | TheFPM->add(createInstructionCombiningPass()); |
| 1120 | // Reassociate expressions. |
| 1121 | TheFPM->add(createReassociatePass()); |
| 1122 | // Eliminate Common SubExpressions. |
| 1123 | TheFPM->add(createGVNPass()); |
| 1124 | // Simplify the control flow graph (deleting unreachable blocks, etc). |
| 1125 | TheFPM->add(createCFGSimplificationPass()); |
| 1126 | |
| 1127 | TheFPM->doInitialization(); |
| 1128 | } |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 1129 | |
| 1130 | static void HandleDefinition() { |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 1131 | if (auto FnAST = ParseDefinition()) { |
Lang Hames | 2d789c3 | 2015-08-26 03:07:41 +0000 | [diff] [blame] | 1132 | if (auto *FnIR = FnAST->codegen()) { |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 1133 | fprintf(stderr, "Read function definition:"); |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 1134 | FnIR->dump(); |
Lang Hames | 2d789c3 | 2015-08-26 03:07:41 +0000 | [diff] [blame] | 1135 | TheJIT->addModule(std::move(TheModule)); |
| 1136 | InitializeModuleAndPassManager(); |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 1137 | } |
| 1138 | } else { |
| 1139 | // Skip token for error recovery. |
| 1140 | getNextToken(); |
| 1141 | } |
| 1142 | } |
| 1143 | |
| 1144 | static void HandleExtern() { |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 1145 | if (auto ProtoAST = ParseExtern()) { |
Lang Hames | 2d789c3 | 2015-08-26 03:07:41 +0000 | [diff] [blame] | 1146 | if (auto *FnIR = ProtoAST->codegen()) { |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 1147 | fprintf(stderr, "Read extern: "); |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 1148 | FnIR->dump(); |
Lang Hames | 2d789c3 | 2015-08-26 03:07:41 +0000 | [diff] [blame] | 1149 | FunctionProtos[ProtoAST->getName()] = std::move(ProtoAST); |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 1150 | } |
| 1151 | } else { |
| 1152 | // Skip token for error recovery. |
| 1153 | getNextToken(); |
| 1154 | } |
| 1155 | } |
| 1156 | |
| 1157 | static void HandleTopLevelExpression() { |
Erick Tryzelaar | 6e2b34bc | 2009-09-22 21:14:49 +0000 | [diff] [blame] | 1158 | // Evaluate a top-level expression into an anonymous function. |
Lang Hames | 09bf4c1 | 2015-08-18 18:11:06 +0000 | [diff] [blame] | 1159 | if (auto FnAST = ParseTopLevelExpr()) { |
Lang Hames | 2d789c3 | 2015-08-26 03:07:41 +0000 | [diff] [blame] | 1160 | if (FnAST->codegen()) { |
Lang Hames | 2d789c3 | 2015-08-26 03:07:41 +0000 | [diff] [blame] | 1161 | // JIT the module containing the anonymous expression, keeping a handle so |
| 1162 | // we can free it later. |
| 1163 | auto H = TheJIT->addModule(std::move(TheModule)); |
| 1164 | InitializeModuleAndPassManager(); |
| 1165 | |
| 1166 | // Search the JIT for the __anon_expr symbol. |
| 1167 | auto ExprSymbol = TheJIT->findSymbol("__anon_expr"); |
| 1168 | assert(ExprSymbol && "Function not found"); |
| 1169 | |
| 1170 | // Get the symbol's address and cast it to the right type (takes no |
| 1171 | // arguments, returns a double) so we can call it as a native function. |
| 1172 | double (*FP)() = (double (*)())(intptr_t)ExprSymbol.getAddress(); |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 1173 | fprintf(stderr, "Evaluated to %f\n", FP()); |
Lang Hames | 2d789c3 | 2015-08-26 03:07:41 +0000 | [diff] [blame] | 1174 | |
| 1175 | // Delete the anonymous expression module from the JIT. |
| 1176 | TheJIT->removeModule(H); |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 1177 | } |
| 1178 | } else { |
| 1179 | // Skip token for error recovery. |
| 1180 | getNextToken(); |
| 1181 | } |
| 1182 | } |
| 1183 | |
| 1184 | /// top ::= definition | external | expression | ';' |
| 1185 | static void MainLoop() { |
Eugene Zelenko | f981ec4 | 2016-05-19 01:08:04 +0000 | [diff] [blame] | 1186 | while (true) { |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 1187 | fprintf(stderr, "ready> "); |
| 1188 | switch (CurTok) { |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 1189 | case tok_eof: |
| 1190 | return; |
Lang Hames | 59b0da8 | 2015-08-19 18:15:58 +0000 | [diff] [blame] | 1191 | case ';': // ignore top-level semicolons. |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 1192 | getNextToken(); |
Lang Hames | 59b0da8 | 2015-08-19 18:15:58 +0000 | [diff] [blame] | 1193 | break; |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 1194 | case tok_def: |
| 1195 | HandleDefinition(); |
| 1196 | break; |
| 1197 | case tok_extern: |
| 1198 | HandleExtern(); |
| 1199 | break; |
| 1200 | default: |
| 1201 | HandleTopLevelExpression(); |
| 1202 | break; |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 1203 | } |
| 1204 | } |
| 1205 | } |
| 1206 | |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 1207 | //===----------------------------------------------------------------------===// |
| 1208 | // "Library" functions that can be "extern'd" from user code. |
| 1209 | //===----------------------------------------------------------------------===// |
| 1210 | |
| 1211 | /// putchard - putchar that takes a double and returns 0. |
NAKAMURA Takumi | ac9d373 | 2015-08-28 03:34:33 +0000 | [diff] [blame] | 1212 | extern "C" double putchard(double X) { |
Lang Hames | d76e067 | 2015-08-27 20:31:44 +0000 | [diff] [blame] | 1213 | fputc((char)X, stderr); |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 1214 | return 0; |
| 1215 | } |
| 1216 | |
| 1217 | /// 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] | 1218 | extern "C" double printd(double X) { |
Lang Hames | d76e067 | 2015-08-27 20:31:44 +0000 | [diff] [blame] | 1219 | fprintf(stderr, "%f\n", X); |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 1220 | return 0; |
| 1221 | } |
| 1222 | |
| 1223 | //===----------------------------------------------------------------------===// |
| 1224 | // Main driver code. |
| 1225 | //===----------------------------------------------------------------------===// |
| 1226 | |
| 1227 | int main() { |
Chris Lattner | d24df24 | 2009-06-17 16:48:44 +0000 | [diff] [blame] | 1228 | InitializeNativeTarget(); |
Eric Christopher | 1b74b65 | 2014-12-08 18:00:38 +0000 | [diff] [blame] | 1229 | InitializeNativeTargetAsmPrinter(); |
| 1230 | InitializeNativeTargetAsmParser(); |
Erick Tryzelaar | 6e2b34bc | 2009-09-22 21:14:49 +0000 | [diff] [blame] | 1231 | |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 1232 | // Install standard binary operators. |
| 1233 | // 1 is lowest precedence. |
| 1234 | BinopPrecedence['='] = 2; |
| 1235 | BinopPrecedence['<'] = 10; |
| 1236 | BinopPrecedence['+'] = 20; |
| 1237 | BinopPrecedence['-'] = 20; |
Eric Christopher | c023936 | 2014-12-08 18:12:28 +0000 | [diff] [blame] | 1238 | BinopPrecedence['*'] = 40; // highest. |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 1239 | |
| 1240 | // Prime the first token. |
| 1241 | fprintf(stderr, "ready> "); |
| 1242 | getNextToken(); |
| 1243 | |
Lang Hames | 2d789c3 | 2015-08-26 03:07:41 +0000 | [diff] [blame] | 1244 | TheJIT = llvm::make_unique<KaleidoscopeJIT>(); |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 1245 | |
Lang Hames | 2d789c3 | 2015-08-26 03:07:41 +0000 | [diff] [blame] | 1246 | InitializeModuleAndPassManager(); |
Reid Kleckner | ab77004 | 2009-08-26 20:58:25 +0000 | [diff] [blame] | 1247 | |
| 1248 | // Run the main "interpreter loop" now. |
| 1249 | MainLoop(); |
| 1250 | |
Nick Lewycky | 109af62 | 2009-04-12 20:47:23 +0000 | [diff] [blame] | 1251 | return 0; |
| 1252 | } |