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