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