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