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