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