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