| 1 | #define MINIMAL_STDERR_OUTPUT |
| 2 | |
| 3 | #include "llvm/Analysis/Passes.h" |
| 4 | #include "llvm/ExecutionEngine/ExecutionEngine.h" |
| 5 | #include "llvm/ExecutionEngine/MCJIT.h" |
| 6 | #include "llvm/ExecutionEngine/ObjectCache.h" |
| 7 | #include "llvm/ExecutionEngine/SectionMemoryManager.h" |
| 8 | #include "llvm/IR/DataLayout.h" |
| 9 | #include "llvm/IR/DerivedTypes.h" |
| 10 | #include "llvm/IR/IRBuilder.h" |
| 11 | #include "llvm/IR/LLVMContext.h" |
| 12 | #include "llvm/IR/LegacyPassManager.h" |
| 13 | #include "llvm/IR/Module.h" |
| 14 | #include "llvm/IR/Verifier.h" |
| 15 | #include "llvm/IRReader/IRReader.h" |
| 16 | #include "llvm/Support/CommandLine.h" |
| 17 | #include "llvm/Support/FileSystem.h" |
| 18 | #include "llvm/Support/Path.h" |
| 19 | #include "llvm/Support/SourceMgr.h" |
| 20 | #include "llvm/Support/TargetSelect.h" |
| 21 | #include "llvm/Support/raw_ostream.h" |
| 22 | #include "llvm/Transforms/Scalar.h" |
| 23 | #include <cctype> |
| 24 | #include <cstdio> |
| 25 | #include <map> |
| 26 | #include <string> |
| 27 | #include <vector> |
| 28 | using namespace llvm; |
| 29 | |
| 30 | //===----------------------------------------------------------------------===// |
| 31 | // Command-line options |
| 32 | //===----------------------------------------------------------------------===// |
| 33 | |
| 34 | cl::opt<std::string> |
| 35 | InputIR("input-IR", |
| 36 | cl::desc("Specify the name of an IR file to load for function definitions"), |
| 37 | cl::value_desc("input IR file name")); |
| 38 | |
| 39 | cl::opt<bool> |
| 40 | UseObjectCache("use-object-cache", |
| 41 | cl::desc("Enable use of the MCJIT object caching"), |
| 42 | cl::init(false)); |
| 43 | |
| 44 | //===----------------------------------------------------------------------===// |
| 45 | // Lexer |
| 46 | //===----------------------------------------------------------------------===// |
| 47 | |
| 48 | // The lexer returns tokens [0-255] if it is an unknown character, otherwise one |
| 49 | // of these for known things. |
| 50 | enum Token { |
| 51 | tok_eof = -1, |
| 52 | |
| 53 | // commands |
| 54 | tok_def = -2, tok_extern = -3, |
| 55 | |
| 56 | // primary |
| 57 | tok_identifier = -4, tok_number = -5, |
| 58 | |
| 59 | // control |
| 60 | tok_if = -6, tok_then = -7, tok_else = -8, |
| 61 | tok_for = -9, tok_in = -10, |
| 62 | |
| 63 | // operators |
| 64 | tok_binary = -11, tok_unary = -12, |
| 65 | |
| 66 | // var definition |
| 67 | tok_var = -13 |
| 68 | }; |
| 69 | |
| 70 | static std::string IdentifierStr; // Filled in if tok_identifier |
| 71 | static double NumVal; // Filled in if tok_number |
| 72 | |
| 73 | /// gettok - Return the next token from standard input. |
| 74 | static int gettok() { |
| 75 | static int LastChar = ' '; |
| 76 | |
| 77 | // Skip any whitespace. |
| 78 | while (isspace(LastChar)) |
| 79 | LastChar = getchar(); |
| 80 | |
| 81 | if (isalpha(LastChar)) { // identifier: [a-zA-Z][a-zA-Z0-9]* |
| 82 | IdentifierStr = LastChar; |
| 83 | while (isalnum((LastChar = getchar()))) |
| 84 | IdentifierStr += LastChar; |
| 85 | |
| 86 | if (IdentifierStr == "def") return tok_def; |
| 87 | if (IdentifierStr == "extern") return tok_extern; |
| 88 | if (IdentifierStr == "if") return tok_if; |
| 89 | if (IdentifierStr == "then") return tok_then; |
| 90 | if (IdentifierStr == "else") return tok_else; |
| 91 | if (IdentifierStr == "for") return tok_for; |
| 92 | if (IdentifierStr == "in") return tok_in; |
| 93 | if (IdentifierStr == "binary") return tok_binary; |
| 94 | if (IdentifierStr == "unary") return tok_unary; |
| 95 | if (IdentifierStr == "var") return tok_var; |
| 96 | return tok_identifier; |
| 97 | } |
| 98 | |
| 99 | if (isdigit(LastChar) || LastChar == '.') { // Number: [0-9.]+ |
| 100 | std::string NumStr; |
| 101 | do { |
| 102 | NumStr += LastChar; |
| 103 | LastChar = getchar(); |
| 104 | } while (isdigit(LastChar) || LastChar == '.'); |
| 105 | |
| 106 | NumVal = strtod(NumStr.c_str(), 0); |
| 107 | return tok_number; |
| 108 | } |
| 109 | |
| 110 | if (LastChar == '#') { |
| 111 | // Comment until end of line. |
| 112 | do LastChar = getchar(); |
| 113 | while (LastChar != EOF && LastChar != '\n' && LastChar != '\r'); |
| 114 | |
| 115 | if (LastChar != EOF) |
| 116 | return gettok(); |
| 117 | } |
| 118 | |
| 119 | // Check for end of file. Don't eat the EOF. |
| 120 | if (LastChar == EOF) |
| 121 | return tok_eof; |
| 122 | |
| 123 | // Otherwise, just return the character as its ascii value. |
| 124 | int ThisChar = LastChar; |
| 125 | LastChar = getchar(); |
| 126 | return ThisChar; |
| 127 | } |
| 128 | |
| 129 | //===----------------------------------------------------------------------===// |
| 130 | // Abstract Syntax Tree (aka Parse Tree) |
| 131 | //===----------------------------------------------------------------------===// |
| 132 | |
| 133 | /// ExprAST - Base class for all expression nodes. |
| 134 | class ExprAST { |
| 135 | public: |
| 136 | virtual ~ExprAST() {} |
| 137 | virtual Value *Codegen() = 0; |
| 138 | }; |
| 139 | |
| 140 | /// NumberExprAST - Expression class for numeric literals like "1.0". |
| 141 | class NumberExprAST : public ExprAST { |
| 142 | double Val; |
| 143 | public: |
| 144 | NumberExprAST(double val) : Val(val) {} |
| 145 | virtual Value *Codegen(); |
| 146 | }; |
| 147 | |
| 148 | /// VariableExprAST - Expression class for referencing a variable, like "a". |
| 149 | class VariableExprAST : public ExprAST { |
| 150 | std::string Name; |
| 151 | public: |
| 152 | VariableExprAST(const std::string &name) : Name(name) {} |
| 153 | const std::string &getName() const { return Name; } |
| 154 | virtual Value *Codegen(); |
| 155 | }; |
| 156 | |
| 157 | /// UnaryExprAST - Expression class for a unary operator. |
| 158 | class UnaryExprAST : public ExprAST { |
| 159 | char Opcode; |
| 160 | ExprAST *Operand; |
| 161 | public: |
| 162 | UnaryExprAST(char opcode, ExprAST *operand) |
| 163 | : Opcode(opcode), Operand(operand) {} |
| 164 | virtual Value *Codegen(); |
| 165 | }; |
| 166 | |
| 167 | /// BinaryExprAST - Expression class for a binary operator. |
| 168 | class BinaryExprAST : public ExprAST { |
| 169 | char Op; |
| 170 | ExprAST *LHS, *RHS; |
| 171 | public: |
| 172 | BinaryExprAST(char op, ExprAST *lhs, ExprAST *rhs) |
| 173 | : Op(op), LHS(lhs), RHS(rhs) {} |
| 174 | virtual Value *Codegen(); |
| 175 | }; |
| 176 | |
| 177 | /// CallExprAST - Expression class for function calls. |
| 178 | class CallExprAST : public ExprAST { |
| 179 | std::string Callee; |
| 180 | std::vector<ExprAST*> Args; |
| 181 | public: |
| 182 | CallExprAST(const std::string &callee, std::vector<ExprAST*> &args) |
| 183 | : Callee(callee), Args(args) {} |
| 184 | virtual Value *Codegen(); |
| 185 | }; |
| 186 | |
| 187 | /// IfExprAST - Expression class for if/then/else. |
| 188 | class IfExprAST : public ExprAST { |
| 189 | ExprAST *Cond, *Then, *Else; |
| 190 | public: |
| 191 | IfExprAST(ExprAST *cond, ExprAST *then, ExprAST *_else) |
| 192 | : Cond(cond), Then(then), Else(_else) {} |
| 193 | virtual Value *Codegen(); |
| 194 | }; |
| 195 | |
| 196 | /// ForExprAST - Expression class for for/in. |
| 197 | class ForExprAST : public ExprAST { |
| 198 | std::string VarName; |
| 199 | ExprAST *Start, *End, *Step, *Body; |
| 200 | public: |
| 201 | ForExprAST(const std::string &varname, ExprAST *start, ExprAST *end, |
| 202 | ExprAST *step, ExprAST *body) |
| 203 | : VarName(varname), Start(start), End(end), Step(step), Body(body) {} |
| 204 | virtual Value *Codegen(); |
| 205 | }; |
| 206 | |
| 207 | /// VarExprAST - Expression class for var/in |
| 208 | class VarExprAST : public ExprAST { |
| 209 | std::vector<std::pair<std::string, ExprAST*> > VarNames; |
| 210 | ExprAST *Body; |
| 211 | public: |
| 212 | VarExprAST(const std::vector<std::pair<std::string, ExprAST*> > &varnames, |
| 213 | ExprAST *body) |
| 214 | : VarNames(varnames), Body(body) {} |
| 215 | |
| 216 | virtual Value *Codegen(); |
| 217 | }; |
| 218 | |
| 219 | /// PrototypeAST - This class represents the "prototype" for a function, |
| 220 | /// which captures its argument names as well as if it is an operator. |
| 221 | class PrototypeAST { |
| 222 | std::string Name; |
| 223 | std::vector<std::string> Args; |
| 224 | bool isOperator; |
| 225 | unsigned Precedence; // Precedence if a binary op. |
| 226 | public: |
| 227 | PrototypeAST(const std::string &name, const std::vector<std::string> &args, |
| 228 | bool isoperator = false, unsigned prec = 0) |
| 229 | : Name(name), Args(args), isOperator(isoperator), Precedence(prec) {} |
| 230 | |
| 231 | bool isUnaryOp() const { return isOperator && Args.size() == 1; } |
| 232 | bool isBinaryOp() const { return isOperator && Args.size() == 2; } |
| 233 | |
| 234 | char getOperatorName() const { |
| 235 | assert(isUnaryOp() || isBinaryOp()); |
| 236 | return Name[Name.size()-1]; |
| 237 | } |
| 238 | |
| 239 | unsigned getBinaryPrecedence() const { return Precedence; } |
| 240 | |
| 241 | Function *Codegen(); |
| 242 | |
| 243 | void CreateArgumentAllocas(Function *F); |
| 244 | }; |
| 245 | |
| 246 | /// FunctionAST - This class represents a function definition itself. |
| 247 | class FunctionAST { |
| 248 | PrototypeAST *Proto; |
| 249 | ExprAST *Body; |
| 250 | public: |
| 251 | FunctionAST(PrototypeAST *proto, ExprAST *body) |
| 252 | : Proto(proto), Body(body) {} |
| 253 | |
| 254 | Function *Codegen(); |
| 255 | }; |
| 256 | |
| 257 | //===----------------------------------------------------------------------===// |
| 258 | // Parser |
| 259 | //===----------------------------------------------------------------------===// |
| 260 | |
| 261 | /// CurTok/getNextToken - Provide a simple token buffer. CurTok is the current |
| 262 | /// token the parser is looking at. getNextToken reads another token from the |
| 263 | /// lexer and updates CurTok with its results. |
| 264 | static int CurTok; |
| 265 | static int getNextToken() { |
| 266 | return CurTok = gettok(); |
| 267 | } |
| 268 | |
| 269 | /// BinopPrecedence - This holds the precedence for each binary operator that is |
| 270 | /// defined. |
| 271 | static std::map<char, int> BinopPrecedence; |
| 272 | |
| 273 | /// GetTokPrecedence - Get the precedence of the pending binary operator token. |
| 274 | static int GetTokPrecedence() { |
| 275 | if (!isascii(CurTok)) |
| 276 | return -1; |
| 277 | |
| 278 | // Make sure it's a declared binop. |
| 279 | int TokPrec = BinopPrecedence[CurTok]; |
| 280 | if (TokPrec <= 0) return -1; |
| 281 | return TokPrec; |
| 282 | } |
| 283 | |
| 284 | /// Error* - These are little helper functions for error handling. |
| 285 | ExprAST *Error(const char *Str) { fprintf(stderr, "Error: %s\n", Str);return 0;} |
| 286 | PrototypeAST *ErrorP(const char *Str) { Error(Str); return 0; } |
| 287 | FunctionAST *ErrorF(const char *Str) { Error(Str); return 0; } |
| 288 | |
| 289 | static ExprAST *ParseExpression(); |
| 290 | |
| 291 | /// identifierexpr |
| 292 | /// ::= identifier |
| 293 | /// ::= identifier '(' expression* ')' |
| 294 | static ExprAST *ParseIdentifierExpr() { |
| 295 | std::string IdName = IdentifierStr; |
| 296 | |
| 297 | getNextToken(); // eat identifier. |
| 298 | |
| 299 | if (CurTok != '(') // Simple variable ref. |
| 300 | return new VariableExprAST(IdName); |
| 301 | |
| 302 | // Call. |
| 303 | getNextToken(); // eat ( |
| 304 | std::vector<ExprAST*> Args; |
| 305 | if (CurTok != ')') { |
| 306 | while (1) { |
| 307 | ExprAST *Arg = ParseExpression(); |
| 308 | if (!Arg) return 0; |
| 309 | Args.push_back(Arg); |
| 310 | |
| 311 | if (CurTok == ')') break; |
| 312 | |
| 313 | if (CurTok != ',') |
| 314 | return Error("Expected ')' or ',' in argument list"); |
| 315 | getNextToken(); |
| 316 | } |
| 317 | } |
| 318 | |
| 319 | // Eat the ')'. |
| 320 | getNextToken(); |
| 321 | |
| 322 | return new CallExprAST(IdName, Args); |
| 323 | } |
| 324 | |
| 325 | /// numberexpr ::= number |
| 326 | static ExprAST *ParseNumberExpr() { |
| 327 | ExprAST *Result = new NumberExprAST(NumVal); |
| 328 | getNextToken(); // consume the number |
| 329 | return Result; |
| 330 | } |
| 331 | |
| 332 | /// parenexpr ::= '(' expression ')' |
| 333 | static ExprAST *ParseParenExpr() { |
| 334 | getNextToken(); // eat (. |
| 335 | ExprAST *V = ParseExpression(); |
| 336 | if (!V) return 0; |
| 337 | |
| 338 | if (CurTok != ')') |
| 339 | return Error("expected ')'"); |
| 340 | getNextToken(); // eat ). |
| 341 | return V; |
| 342 | } |
| 343 | |
| 344 | /// ifexpr ::= 'if' expression 'then' expression 'else' expression |
| 345 | static ExprAST *ParseIfExpr() { |
| 346 | getNextToken(); // eat the if. |
| 347 | |
| 348 | // condition. |
| 349 | ExprAST *Cond = ParseExpression(); |
| 350 | if (!Cond) return 0; |
| 351 | |
| 352 | if (CurTok != tok_then) |
| 353 | return Error("expected then"); |
| 354 | getNextToken(); // eat the then |
| 355 | |
| 356 | ExprAST *Then = ParseExpression(); |
| 357 | if (Then == 0) return 0; |
| 358 | |
| 359 | if (CurTok != tok_else) |
| 360 | return Error("expected else"); |
| 361 | |
| 362 | getNextToken(); |
| 363 | |
| 364 | ExprAST *Else = ParseExpression(); |
| 365 | if (!Else) return 0; |
| 366 | |
| 367 | return new IfExprAST(Cond, Then, Else); |
| 368 | } |
| 369 | |
| 370 | /// forexpr ::= 'for' identifier '=' expr ',' expr (',' expr)? 'in' expression |
| 371 | static ExprAST *ParseForExpr() { |
| 372 | getNextToken(); // eat the for. |
| 373 | |
| 374 | if (CurTok != tok_identifier) |
| 375 | return Error("expected identifier after for"); |
| 376 | |
| 377 | std::string IdName = IdentifierStr; |
| 378 | getNextToken(); // eat identifier. |
| 379 | |
| 380 | if (CurTok != '=') |
| 381 | return Error("expected '=' after for"); |
| 382 | getNextToken(); // eat '='. |
| 383 | |
| 384 | |
| 385 | ExprAST *Start = ParseExpression(); |
| 386 | if (Start == 0) return 0; |
| 387 | if (CurTok != ',') |
| 388 | return Error("expected ',' after for start value"); |
| 389 | getNextToken(); |
| 390 | |
| 391 | ExprAST *End = ParseExpression(); |
| 392 | if (End == 0) return 0; |
| 393 | |
| 394 | // The step value is optional. |
| 395 | ExprAST *Step = 0; |
| 396 | if (CurTok == ',') { |
| 397 | getNextToken(); |
| 398 | Step = ParseExpression(); |
| 399 | if (Step == 0) return 0; |
| 400 | } |
| 401 | |
| 402 | if (CurTok != tok_in) |
| 403 | return Error("expected 'in' after for"); |
| 404 | getNextToken(); // eat 'in'. |
| 405 | |
| 406 | ExprAST *Body = ParseExpression(); |
| 407 | if (Body == 0) return 0; |
| 408 | |
| 409 | return new ForExprAST(IdName, Start, End, Step, Body); |
| 410 | } |
| 411 | |
| 412 | /// varexpr ::= 'var' identifier ('=' expression)? |
| 413 | // (',' identifier ('=' expression)?)* 'in' expression |
| 414 | static ExprAST *ParseVarExpr() { |
| 415 | getNextToken(); // eat the var. |
| 416 | |
| 417 | std::vector<std::pair<std::string, ExprAST*> > VarNames; |
| 418 | |
| 419 | // At least one variable name is required. |
| 420 | if (CurTok != tok_identifier) |
| 421 | return Error("expected identifier after var"); |
| 422 | |
| 423 | while (1) { |
| 424 | std::string Name = IdentifierStr; |
| 425 | getNextToken(); // eat identifier. |
| 426 | |
| 427 | // Read the optional initializer. |
| 428 | ExprAST *Init = 0; |
| 429 | if (CurTok == '=') { |
| 430 | getNextToken(); // eat the '='. |
| 431 | |
| 432 | Init = ParseExpression(); |
| 433 | if (Init == 0) return 0; |
| 434 | } |
| 435 | |
| 436 | VarNames.push_back(std::make_pair(Name, Init)); |
| 437 | |
| 438 | // End of var list, exit loop. |
| 439 | if (CurTok != ',') break; |
| 440 | getNextToken(); // eat the ','. |
| 441 | |
| 442 | if (CurTok != tok_identifier) |
| 443 | return Error("expected identifier list after var"); |
| 444 | } |
| 445 | |
| 446 | // At this point, we have to have 'in'. |
| 447 | if (CurTok != tok_in) |
| 448 | return Error("expected 'in' keyword after 'var'"); |
| 449 | getNextToken(); // eat 'in'. |
| 450 | |
| 451 | ExprAST *Body = ParseExpression(); |
| 452 | if (Body == 0) return 0; |
| 453 | |
| 454 | return new VarExprAST(VarNames, Body); |
| 455 | } |
| 456 | |
| 457 | /// primary |
| 458 | /// ::= identifierexpr |
| 459 | /// ::= numberexpr |
| 460 | /// ::= parenexpr |
| 461 | /// ::= ifexpr |
| 462 | /// ::= forexpr |
| 463 | /// ::= varexpr |
| 464 | static ExprAST *ParsePrimary() { |
| 465 | switch (CurTok) { |
| 466 | default: return Error("unknown token when expecting an expression"); |
| 467 | case tok_identifier: return ParseIdentifierExpr(); |
| 468 | case tok_number: return ParseNumberExpr(); |
| 469 | case '(': return ParseParenExpr(); |
| 470 | case tok_if: return ParseIfExpr(); |
| 471 | case tok_for: return ParseForExpr(); |
| 472 | case tok_var: return ParseVarExpr(); |
| 473 | } |
| 474 | } |
| 475 | |
| 476 | /// unary |
| 477 | /// ::= primary |
| 478 | /// ::= '!' unary |
| 479 | static ExprAST *ParseUnary() { |
| 480 | // If the current token is not an operator, it must be a primary expr. |
| 481 | if (!isascii(CurTok) || CurTok == '(' || CurTok == ',') |
| 482 | return ParsePrimary(); |
| 483 | |
| 484 | // If this is a unary operator, read it. |
| 485 | int Opc = CurTok; |
| 486 | getNextToken(); |
| 487 | if (ExprAST *Operand = ParseUnary()) |
| 488 | return new UnaryExprAST(Opc, Operand); |
| 489 | return 0; |
| 490 | } |
| 491 | |
| 492 | /// binoprhs |
| 493 | /// ::= ('+' unary)* |
| 494 | static ExprAST *ParseBinOpRHS(int ExprPrec, ExprAST *LHS) { |
| 495 | // If this is a binop, find its precedence. |
| 496 | while (1) { |
| 497 | int TokPrec = GetTokPrecedence(); |
| 498 | |
| 499 | // If this is a binop that binds at least as tightly as the current binop, |
| 500 | // consume it, otherwise we are done. |
| 501 | if (TokPrec < ExprPrec) |
| 502 | return LHS; |
| 503 | |
| 504 | // Okay, we know this is a binop. |
| 505 | int BinOp = CurTok; |
| 506 | getNextToken(); // eat binop |
| 507 | |
| 508 | // Parse the unary expression after the binary operator. |
| 509 | ExprAST *RHS = ParseUnary(); |
| 510 | if (!RHS) return 0; |
| 511 | |
| 512 | // If BinOp binds less tightly with RHS than the operator after RHS, let |
| 513 | // the pending operator take RHS as its LHS. |
| 514 | int NextPrec = GetTokPrecedence(); |
| 515 | if (TokPrec < NextPrec) { |
| 516 | RHS = ParseBinOpRHS(TokPrec+1, RHS); |
| 517 | if (RHS == 0) return 0; |
| 518 | } |
| 519 | |
| 520 | // Merge LHS/RHS. |
| 521 | LHS = new BinaryExprAST(BinOp, LHS, RHS); |
| 522 | } |
| 523 | } |
| 524 | |
| 525 | /// expression |
| 526 | /// ::= unary binoprhs |
| 527 | /// |
| 528 | static ExprAST *ParseExpression() { |
| 529 | ExprAST *LHS = ParseUnary(); |
| 530 | if (!LHS) return 0; |
| 531 | |
| 532 | return ParseBinOpRHS(0, LHS); |
| 533 | } |
| 534 | |
| 535 | /// prototype |
| 536 | /// ::= id '(' id* ')' |
| 537 | /// ::= binary LETTER number? (id, id) |
| 538 | /// ::= unary LETTER (id) |
| 539 | static PrototypeAST *ParsePrototype() { |
| 540 | std::string FnName; |
| 541 | |
| 542 | unsigned Kind = 0; // 0 = identifier, 1 = unary, 2 = binary. |
| 543 | unsigned BinaryPrecedence = 30; |
| 544 | |
| 545 | switch (CurTok) { |
| 546 | default: |
| 547 | return ErrorP("Expected function name in prototype"); |
| 548 | case tok_identifier: |
| 549 | FnName = IdentifierStr; |
| 550 | Kind = 0; |
| 551 | getNextToken(); |
| 552 | break; |
| 553 | case tok_unary: |
| 554 | getNextToken(); |
| 555 | if (!isascii(CurTok)) |
| 556 | return ErrorP("Expected unary operator"); |
| 557 | FnName = "unary"; |
| 558 | FnName += (char)CurTok; |
| 559 | Kind = 1; |
| 560 | getNextToken(); |
| 561 | break; |
| 562 | case tok_binary: |
| 563 | getNextToken(); |
| 564 | if (!isascii(CurTok)) |
| 565 | return ErrorP("Expected binary operator"); |
| 566 | FnName = "binary"; |
| 567 | FnName += (char)CurTok; |
| 568 | Kind = 2; |
| 569 | getNextToken(); |
| 570 | |
| 571 | // Read the precedence if present. |
| 572 | if (CurTok == tok_number) { |
| 573 | if (NumVal < 1 || NumVal > 100) |
| 574 | return ErrorP("Invalid precedecnce: must be 1..100"); |
| 575 | BinaryPrecedence = (unsigned)NumVal; |
| 576 | getNextToken(); |
| 577 | } |
| 578 | break; |
| 579 | } |
| 580 | |
| 581 | if (CurTok != '(') |
| 582 | return ErrorP("Expected '(' in prototype"); |
| 583 | |
| 584 | std::vector<std::string> ArgNames; |
| 585 | while (getNextToken() == tok_identifier) |
| 586 | ArgNames.push_back(IdentifierStr); |
| 587 | if (CurTok != ')') |
| 588 | return ErrorP("Expected ')' in prototype"); |
| 589 | |
| 590 | // success. |
| 591 | getNextToken(); // eat ')'. |
| 592 | |
| 593 | // Verify right number of names for operator. |
| 594 | if (Kind && ArgNames.size() != Kind) |
| 595 | return ErrorP("Invalid number of operands for operator"); |
| 596 | |
| 597 | return new PrototypeAST(FnName, ArgNames, Kind != 0, BinaryPrecedence); |
| 598 | } |
| 599 | |
| 600 | /// definition ::= 'def' prototype expression |
| 601 | static FunctionAST *ParseDefinition() { |
| 602 | getNextToken(); // eat def. |
| 603 | PrototypeAST *Proto = ParsePrototype(); |
| 604 | if (Proto == 0) return 0; |
| 605 | |
| 606 | if (ExprAST *E = ParseExpression()) |
| 607 | return new FunctionAST(Proto, E); |
| 608 | return 0; |
| 609 | } |
| 610 | |
| 611 | /// toplevelexpr ::= expression |
| 612 | static FunctionAST *ParseTopLevelExpr() { |
| 613 | if (ExprAST *E = ParseExpression()) { |
| 614 | // Make an anonymous proto. |
| 615 | PrototypeAST *Proto = new PrototypeAST("", std::vector<std::string>()); |
| 616 | return new FunctionAST(Proto, E); |
| 617 | } |
| 618 | return 0; |
| 619 | } |
| 620 | |
| 621 | /// external ::= 'extern' prototype |
| 622 | static PrototypeAST *ParseExtern() { |
| 623 | getNextToken(); // eat extern. |
| 624 | return ParsePrototype(); |
| 625 | } |
| 626 | |
| 627 | //===----------------------------------------------------------------------===// |
| 628 | // Quick and dirty hack |
| 629 | //===----------------------------------------------------------------------===// |
| 630 | |
| 631 | // FIXME: Obviously we can do better than this |
| 632 | std::string GenerateUniqueName(const char *root) |
| 633 | { |
| 634 | static int i = 0; |
| 635 | char s[16]; |
| 636 | sprintf(s, "%s%d", root, i++); |
| 637 | std::string S = s; |
| 638 | return S; |
| 639 | } |
| 640 | |
| 641 | std::string MakeLegalFunctionName(std::string Name) |
| 642 | { |
| 643 | std::string NewName; |
| 644 | if (!Name.length()) |
| 645 | return GenerateUniqueName("anon_func_"); |
| 646 | |
| 647 | // Start with what we have |
| 648 | NewName = Name; |
| 649 | |
| 650 | // Look for a numberic first character |
| 651 | if (NewName.find_first_of("0123456789") == 0) { |
| 652 | NewName.insert(0, 1, 'n'); |
| 653 | } |
| 654 | |
| 655 | // Replace illegal characters with their ASCII equivalent |
| 656 | std::string legal_elements = "_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; |
| 657 | size_t pos; |
| 658 | while ((pos = NewName.find_first_not_of(legal_elements)) != std::string::npos) { |
| 659 | char old_c = NewName.at(pos); |
| 660 | char new_str[16]; |
| 661 | sprintf(new_str, "%d", (int)old_c); |
| 662 | NewName = NewName.replace(pos, 1, new_str); |
| 663 | } |
| 664 | |
| 665 | return NewName; |
| 666 | } |
| 667 | |
| 668 | //===----------------------------------------------------------------------===// |
| 669 | // MCJIT object cache class |
| 670 | //===----------------------------------------------------------------------===// |
| 671 | |
| 672 | class MCJITObjectCache : public ObjectCache { |
| 673 | public: |
| 674 | MCJITObjectCache() { |
| 675 | // Set IR cache directory |
| 676 | sys::fs::current_path(CacheDir); |
| 677 | sys::path::append(CacheDir, "toy_object_cache"); |
| 678 | } |
| 679 | |
| 680 | virtual ~MCJITObjectCache() { |
| 681 | } |
| 682 | |
| 683 | virtual void notifyObjectCompiled(const Module *M, const MemoryBuffer *Obj) { |
| 684 | // Get the ModuleID |
| 685 | const std::string ModuleID = M->getModuleIdentifier(); |
| 686 | |
| 687 | // If we've flagged this as an IR file, cache it |
| 688 | if (0 == ModuleID.compare(0, 3, "IR:")) { |
| 689 | std::string IRFileName = ModuleID.substr(3); |
| 690 | SmallString<128>IRCacheFile = CacheDir; |
| 691 | sys::path::append(IRCacheFile, IRFileName); |
| 692 | if (!sys::fs::exists(CacheDir.str()) && sys::fs::create_directory(CacheDir.str())) { |
| 693 | fprintf(stderr, "Unable to create cache directory\n"); |
| 694 | return; |
| 695 | } |
| 696 | std::string ErrStr; |
| 697 | raw_fd_ostream IRObjectFile(IRCacheFile.c_str(), ErrStr, raw_fd_ostream::F_Binary); |
| 698 | IRObjectFile << Obj->getBuffer(); |
| 699 | } |
| 700 | } |
| 701 | |
| 702 | // MCJIT will call this function before compiling any module |
| 703 | // MCJIT takes ownership of both the MemoryBuffer object and the memory |
| 704 | // to which it refers. |
| 705 | virtual MemoryBuffer* getObject(const Module* M) { |
| 706 | // Get the ModuleID |
| 707 | const std::string ModuleID = M->getModuleIdentifier(); |
| 708 | |
| 709 | // If we've flagged this as an IR file, cache it |
| 710 | if (0 == ModuleID.compare(0, 3, "IR:")) { |
| 711 | std::string IRFileName = ModuleID.substr(3); |
| 712 | SmallString<128> IRCacheFile = CacheDir; |
| 713 | sys::path::append(IRCacheFile, IRFileName); |
| 714 | if (!sys::fs::exists(IRCacheFile.str())) { |
| 715 | // This file isn't in our cache |
| 716 | return NULL; |
| 717 | } |
| 718 | std::unique_ptr<MemoryBuffer> IRObjectBuffer; |
| 719 | MemoryBuffer::getFile(IRCacheFile.c_str(), IRObjectBuffer, -1, false); |
| 720 | // MCJIT will want to write into this buffer, and we don't want that |
| 721 | // because the file has probably just been mmapped. Instead we make |
| 722 | // a copy. The filed-based buffer will be released when it goes |
| 723 | // out of scope. |
| 724 | return MemoryBuffer::getMemBufferCopy(IRObjectBuffer->getBuffer()); |
| 725 | } |
| 726 | |
| 727 | return NULL; |
| 728 | } |
| 729 | |
| 730 | private: |
| 731 | SmallString<128> CacheDir; |
| 732 | }; |
| 733 | |
| 734 | //===----------------------------------------------------------------------===// |
| 735 | // MCJIT helper class |
| 736 | //===----------------------------------------------------------------------===// |
| 737 | |
| 738 | class MCJITHelper |
| 739 | { |
| 740 | public: |
| 741 | MCJITHelper(LLVMContext& C) : Context(C), OpenModule(NULL) {} |
| 742 | ~MCJITHelper(); |
| 743 | |
| 744 | Function *getFunction(const std::string FnName); |
| 745 | Module *getModuleForNewFunction(); |
| 746 | void *getPointerToFunction(Function* F); |
| 747 | void *getPointerToNamedFunction(const std::string &Name); |
| 748 | ExecutionEngine *compileModule(Module *M); |
| 749 | void closeCurrentModule(); |
| 750 | void addModule(Module *M); |
| 751 | void dump(); |
| 752 | |
| 753 | private: |
| 754 | typedef std::vector<Module*> ModuleVector; |
| 755 | |
| 756 | LLVMContext &Context; |
| 757 | Module *OpenModule; |
| 758 | ModuleVector Modules; |
| 759 | std::map<Module *, ExecutionEngine *> EngineMap; |
| 760 | MCJITObjectCache OurObjectCache; |
| 761 | }; |
| 762 | |
| 763 | class HelpingMemoryManager : public SectionMemoryManager |
| 764 | { |
| 765 | HelpingMemoryManager(const HelpingMemoryManager&) = delete; |
| 766 | void operator=(const HelpingMemoryManager&) = delete; |
| 767 | |
| 768 | public: |
| 769 | HelpingMemoryManager(MCJITHelper *Helper) : MasterHelper(Helper) {} |
| 770 | virtual ~HelpingMemoryManager() {} |
| 771 | |
| 772 | /// This method returns the address of the specified function. |
| 773 | /// Our implementation will attempt to find functions in other |
| 774 | /// modules associated with the MCJITHelper to cross link functions |
| 775 | /// from one generated module to another. |
| 776 | /// |
| 777 | /// If \p AbortOnFailure is false and no function with the given name is |
| 778 | /// found, this function returns a null pointer. Otherwise, it prints a |
| 779 | /// message to stderr and aborts. |
| 780 | virtual void *getPointerToNamedFunction(const std::string &Name, |
| 781 | bool AbortOnFailure = true); |
| 782 | private: |
| 783 | MCJITHelper *MasterHelper; |
| 784 | }; |
| 785 | |
| 786 | void *HelpingMemoryManager::getPointerToNamedFunction(const std::string &Name, |
| 787 | bool AbortOnFailure) |
| 788 | { |
| 789 | // Try the standard symbol resolution first, but ask it not to abort. |
| 790 | void *pfn = SectionMemoryManager::getPointerToNamedFunction(Name, false); |
| 791 | if (pfn) |
| 792 | return pfn; |
| 793 | |
| 794 | pfn = MasterHelper->getPointerToNamedFunction(Name); |
| 795 | if (!pfn && AbortOnFailure) |
| 796 | report_fatal_error("Program used external function '" + Name + |
| 797 | "' which could not be resolved!"); |
| 798 | return pfn; |
| 799 | } |
| 800 | |
| 801 | MCJITHelper::~MCJITHelper() |
| 802 | { |
| 803 | // Walk the vector of modules. |
| 804 | ModuleVector::iterator it, end; |
| 805 | for (it = Modules.begin(), end = Modules.end(); |
| 806 | it != end; ++it) { |
| 807 | // See if we have an execution engine for this module. |
| 808 | std::map<Module*, ExecutionEngine*>::iterator mapIt = EngineMap.find(*it); |
| 809 | // If we have an EE, the EE owns the module so just delete the EE. |
| 810 | if (mapIt != EngineMap.end()) { |
| 811 | delete mapIt->second; |
| 812 | } else { |
| 813 | // Otherwise, we still own the module. Delete it now. |
| 814 | delete *it; |
| 815 | } |
| 816 | } |
| 817 | } |
| 818 | |
| 819 | Function *MCJITHelper::getFunction(const std::string FnName) { |
| 820 | ModuleVector::iterator begin = Modules.begin(); |
| 821 | ModuleVector::iterator end = Modules.end(); |
| 822 | ModuleVector::iterator it; |
| 823 | for (it = begin; it != end; ++it) { |
| 824 | Function *F = (*it)->getFunction(FnName); |
| 825 | if (F) { |
| 826 | if (*it == OpenModule) |
| 827 | return F; |
| 828 | |
| 829 | assert(OpenModule != NULL); |
| 830 | |
| 831 | // This function is in a module that has already been JITed. |
| 832 | // We need to generate a new prototype for external linkage. |
| 833 | Function *PF = OpenModule->getFunction(FnName); |
| 834 | if (PF && !PF->empty()) { |
| 835 | ErrorF("redefinition of function across modules"); |
| 836 | return 0; |
| 837 | } |
| 838 | |
| 839 | // If we don't have a prototype yet, create one. |
| 840 | if (!PF) |
| 841 | PF = Function::Create(F->getFunctionType(), |
| 842 | Function::ExternalLinkage, |
| 843 | FnName, |
| 844 | OpenModule); |
| 845 | return PF; |
| 846 | } |
| 847 | } |
| 848 | return NULL; |
| 849 | } |
| 850 | |
| 851 | Module *MCJITHelper::getModuleForNewFunction() { |
| 852 | // If we have a Module that hasn't been JITed, use that. |
| 853 | if (OpenModule) |
| 854 | return OpenModule; |
| 855 | |
| 856 | // Otherwise create a new Module. |
| 857 | std::string ModName = GenerateUniqueName("mcjit_module_"); |
| 858 | Module *M = new Module(ModName, Context); |
| 859 | Modules.push_back(M); |
| 860 | OpenModule = M; |
| 861 | return M; |
| 862 | } |
| 863 | |
| 864 | void *MCJITHelper::getPointerToFunction(Function* F) { |
| 865 | // Look for this function in an existing module |
| 866 | ModuleVector::iterator begin = Modules.begin(); |
| 867 | ModuleVector::iterator end = Modules.end(); |
| 868 | ModuleVector::iterator it; |
| 869 | std::string FnName = F->getName(); |
| 870 | for (it = begin; it != end; ++it) { |
| 871 | Function *MF = (*it)->getFunction(FnName); |
| 872 | if (MF == F) { |
| 873 | std::map<Module*, ExecutionEngine*>::iterator eeIt = EngineMap.find(*it); |
| 874 | if (eeIt != EngineMap.end()) { |
| 875 | void *P = eeIt->second->getPointerToFunction(F); |
| 876 | if (P) |
| 877 | return P; |
| 878 | } else { |
| 879 | ExecutionEngine *EE = compileModule(*it); |
| 880 | void *P = EE->getPointerToFunction(F); |
| 881 | if (P) |
| 882 | return P; |
| 883 | } |
| 884 | } |
| 885 | } |
| 886 | return NULL; |
| 887 | } |
| 888 | |
| 889 | void MCJITHelper::closeCurrentModule() { |
| 890 | OpenModule = NULL; |
| 891 | } |
| 892 | |
| 893 | ExecutionEngine *MCJITHelper::compileModule(Module *M) { |
| 894 | if (M == OpenModule) |
| 895 | closeCurrentModule(); |
| 896 | |
| 897 | std::string ErrStr; |
| 898 | ExecutionEngine *NewEngine = EngineBuilder(M) |
| 899 | .setErrorStr(&ErrStr) |
| 900 | .setMCJITMemoryManager(new HelpingMemoryManager(this)) |
| 901 | .create(); |
| 902 | if (!NewEngine) { |
| 903 | fprintf(stderr, "Could not create ExecutionEngine: %s\n", ErrStr.c_str()); |
| 904 | exit(1); |
| 905 | } |
| 906 | |
| 907 | if (UseObjectCache) |
| 908 | NewEngine->setObjectCache(&OurObjectCache); |
| 909 | |
| 910 | // Get the ModuleID so we can identify IR input files |
| 911 | const std::string ModuleID = M->getModuleIdentifier(); |
| 912 | |
| 913 | // If we've flagged this as an IR file, it doesn't need function passes run. |
| 914 | if (0 != ModuleID.compare(0, 3, "IR:")) { |
| 915 | // Create a function pass manager for this engine |
| 916 | FunctionPassManager *FPM = new FunctionPassManager(M); |
| 917 | |
| 918 | // Set up the optimizer pipeline. Start with registering info about how the |
| 919 | // target lays out data structures. |
| 920 | FPM->add(new DataLayout(*NewEngine->getDataLayout())); |
| 921 | // Provide basic AliasAnalysis support for GVN. |
| 922 | FPM->add(createBasicAliasAnalysisPass()); |
| 923 | // Promote allocas to registers. |
| 924 | FPM->add(createPromoteMemoryToRegisterPass()); |
| 925 | // Do simple "peephole" optimizations and bit-twiddling optzns. |
| 926 | FPM->add(createInstructionCombiningPass()); |
| 927 | // Reassociate expressions. |
| 928 | FPM->add(createReassociatePass()); |
| 929 | // Eliminate Common SubExpressions. |
| 930 | FPM->add(createGVNPass()); |
| 931 | // Simplify the control flow graph (deleting unreachable blocks, etc). |
| 932 | FPM->add(createCFGSimplificationPass()); |
| 933 | FPM->doInitialization(); |
| 934 | |
| 935 | // For each function in the module |
| 936 | Module::iterator it; |
| 937 | Module::iterator end = M->end(); |
| 938 | for (it = M->begin(); it != end; ++it) { |
| 939 | // Run the FPM on this function |
| 940 | FPM->run(*it); |
| 941 | } |
| 942 | |
| 943 | // We don't need this anymore |
| 944 | delete FPM; |
| 945 | } |
| 946 | |
| 947 | // Store this engine |
| 948 | EngineMap[M] = NewEngine; |
| 949 | NewEngine->finalizeObject(); |
| 950 | |
| 951 | return NewEngine; |
| 952 | } |
| 953 | |
| 954 | void *MCJITHelper::getPointerToNamedFunction(const std::string &Name) |
| 955 | { |
| 956 | // Look for the functions in our modules, compiling only as necessary |
| 957 | ModuleVector::iterator begin = Modules.begin(); |
| 958 | ModuleVector::iterator end = Modules.end(); |
| 959 | ModuleVector::iterator it; |
| 960 | for (it = begin; it != end; ++it) { |
| 961 | Function *F = (*it)->getFunction(Name); |
| 962 | if (F && !F->empty()) { |
| 963 | std::map<Module*, ExecutionEngine*>::iterator eeIt = EngineMap.find(*it); |
| 964 | if (eeIt != EngineMap.end()) { |
| 965 | void *P = eeIt->second->getPointerToFunction(F); |
| 966 | if (P) |
| 967 | return P; |
| 968 | } else { |
| 969 | ExecutionEngine *EE = compileModule(*it); |
| 970 | void *P = EE->getPointerToFunction(F); |
| 971 | if (P) |
| 972 | return P; |
| 973 | } |
| 974 | } |
| 975 | } |
| 976 | return NULL; |
| 977 | } |
| 978 | |
| 979 | void MCJITHelper::addModule(Module* M) { |
| 980 | Modules.push_back(M); |
| 981 | } |
| 982 | |
| 983 | void MCJITHelper::dump() |
| 984 | { |
| 985 | ModuleVector::iterator begin = Modules.begin(); |
| 986 | ModuleVector::iterator end = Modules.end(); |
| 987 | ModuleVector::iterator it; |
| 988 | for (it = begin; it != end; ++it) |
| 989 | (*it)->dump(); |
| 990 | } |
| 991 | |
| 992 | //===----------------------------------------------------------------------===// |
| 993 | // Code Generation |
| 994 | //===----------------------------------------------------------------------===// |
| 995 | |
| 996 | static MCJITHelper *TheHelper; |
| 997 | static LLVMContext TheContext; |
| 998 | static IRBuilder<> Builder(TheContext); |
| 999 | static std::map<std::string, AllocaInst*> NamedValues; |
| 1000 | |
| 1001 | Value *ErrorV(const char *Str) { Error(Str); return 0; } |
| 1002 | |
| 1003 | /// CreateEntryBlockAlloca - Create an alloca instruction in the entry block of |
| 1004 | /// the function. This is used for mutable variables etc. |
| 1005 | static AllocaInst *CreateEntryBlockAlloca(Function *TheFunction, |
| 1006 | const std::string &VarName) { |
| 1007 | IRBuilder<> TmpB(&TheFunction->getEntryBlock(), |
| 1008 | TheFunction->getEntryBlock().begin()); |
| 1009 | return TmpB.CreateAlloca(Type::getDoubleTy(TheContext), 0, VarName.c_str()); |
| 1010 | } |
| 1011 | |
| 1012 | Value *NumberExprAST::Codegen() { |
| 1013 | return ConstantFP::get(TheContext, APFloat(Val)); |
| 1014 | } |
| 1015 | |
| 1016 | Value *VariableExprAST::Codegen() { |
| 1017 | // Look this variable up in the function. |
| 1018 | Value *V = NamedValues[Name]; |
| 1019 | char ErrStr[256]; |
| 1020 | sprintf(ErrStr, "Unknown variable name %s", Name.c_str()); |
| 1021 | if (V == 0) return ErrorV(ErrStr); |
| 1022 | |
| 1023 | // Load the value. |
| 1024 | return Builder.CreateLoad(V, Name.c_str()); |
| 1025 | } |
| 1026 | |
| 1027 | Value *UnaryExprAST::Codegen() { |
| 1028 | Value *OperandV = Operand->Codegen(); |
| 1029 | if (OperandV == 0) return 0; |
| 1030 | |
| 1031 | Function *F = TheHelper->getFunction(MakeLegalFunctionName(std::string("unary")+Opcode)); |
| 1032 | if (F == 0) |
| 1033 | return ErrorV("Unknown unary operator"); |
| 1034 | |
| 1035 | return Builder.CreateCall(F, OperandV, "unop"); |
| 1036 | } |
| 1037 | |
| 1038 | Value *BinaryExprAST::Codegen() { |
| 1039 | // Special case '=' because we don't want to emit the LHS as an expression. |
| 1040 | if (Op == '=') { |
| 1041 | // Assignment requires the LHS to be an identifier. |
| 1042 | VariableExprAST *LHSE = static_cast<VariableExprAST*>(LHS); |
| 1043 | if (!LHSE) |
| 1044 | return ErrorV("destination of '=' must be a variable"); |
| 1045 | // Codegen the RHS. |
| 1046 | Value *Val = RHS->Codegen(); |
| 1047 | if (Val == 0) return 0; |
| 1048 | |
| 1049 | // Look up the name. |
| 1050 | Value *Variable = NamedValues[LHSE->getName()]; |
| 1051 | if (Variable == 0) return ErrorV("Unknown variable name"); |
| 1052 | |
| 1053 | Builder.CreateStore(Val, Variable); |
| 1054 | return Val; |
| 1055 | } |
| 1056 | |
| 1057 | Value *L = LHS->Codegen(); |
| 1058 | Value *R = RHS->Codegen(); |
| 1059 | if (L == 0 || R == 0) return 0; |
| 1060 | |
| 1061 | switch (Op) { |
| 1062 | case '+': return Builder.CreateFAdd(L, R, "addtmp"); |
| 1063 | case '-': return Builder.CreateFSub(L, R, "subtmp"); |
| 1064 | case '*': return Builder.CreateFMul(L, R, "multmp"); |
| 1065 | case '/': return Builder.CreateFDiv(L, R, "divtmp"); |
| 1066 | case '<': |
| 1067 | L = Builder.CreateFCmpULT(L, R, "cmptmp"); |
| 1068 | // Convert bool 0/1 to double 0.0 or 1.0 |
| 1069 | return Builder.CreateUIToFP(L, Type::getDoubleTy(TheContext), "booltmp"); |
| 1070 | default: break; |
| 1071 | } |
| 1072 | |
| 1073 | // If it wasn't a builtin binary operator, it must be a user defined one. Emit |
| 1074 | // a call to it. |
| 1075 | Function *F = TheHelper->getFunction(MakeLegalFunctionName(std::string("binary")+Op)); |
| 1076 | assert(F && "binary operator not found!"); |
| 1077 | |
| 1078 | Value *Ops[] = { L, R }; |
| 1079 | return Builder.CreateCall(F, Ops, "binop"); |
| 1080 | } |
| 1081 | |
| 1082 | Value *CallExprAST::Codegen() { |
| 1083 | // Look up the name in the global module table. |
| 1084 | Function *CalleeF = TheHelper->getFunction(Callee); |
| 1085 | if (CalleeF == 0) |
| 1086 | return ErrorV("Unknown function referenced"); |
| 1087 | |
| 1088 | // If argument mismatch error. |
| 1089 | if (CalleeF->arg_size() != Args.size()) |
| 1090 | return ErrorV("Incorrect # arguments passed"); |
| 1091 | |
| 1092 | std::vector<Value*> ArgsV; |
| 1093 | for (unsigned i = 0, e = Args.size(); i != e; ++i) { |
| 1094 | ArgsV.push_back(Args[i]->Codegen()); |
| 1095 | if (ArgsV.back() == 0) return 0; |
| 1096 | } |
| 1097 | |
| 1098 | return Builder.CreateCall(CalleeF, ArgsV, "calltmp"); |
| 1099 | } |
| 1100 | |
| 1101 | Value *IfExprAST::Codegen() { |
| 1102 | Value *CondV = Cond->Codegen(); |
| 1103 | if (CondV == 0) return 0; |
| 1104 | |
| 1105 | // Convert condition to a bool by comparing equal to 0.0. |
| 1106 | CondV = Builder.CreateFCmpONE( |
| 1107 | CondV, ConstantFP::get(TheContext, APFloat(0.0)), "ifcond"); |
| 1108 | |
| 1109 | Function *TheFunction = Builder.GetInsertBlock()->getParent(); |
| 1110 | |
| 1111 | // Create blocks for the then and else cases. Insert the 'then' block at the |
| 1112 | // end of the function. |
| 1113 | BasicBlock *ThenBB = BasicBlock::Create(TheContext, "then", TheFunction); |
| 1114 | BasicBlock *ElseBB = BasicBlock::Create(TheContext, "else"); |
| 1115 | BasicBlock *MergeBB = BasicBlock::Create(TheContext, "ifcont"); |
| 1116 | |
| 1117 | Builder.CreateCondBr(CondV, ThenBB, ElseBB); |
| 1118 | |
| 1119 | // Emit then value. |
| 1120 | Builder.SetInsertPoint(ThenBB); |
| 1121 | |
| 1122 | Value *ThenV = Then->Codegen(); |
| 1123 | if (ThenV == 0) return 0; |
| 1124 | |
| 1125 | Builder.CreateBr(MergeBB); |
| 1126 | // Codegen of 'Then' can change the current block, update ThenBB for the PHI. |
| 1127 | ThenBB = Builder.GetInsertBlock(); |
| 1128 | |
| 1129 | // Emit else block. |
| 1130 | TheFunction->insert(TheFunction->end(), ElseBB); |
| 1131 | Builder.SetInsertPoint(ElseBB); |
| 1132 | |
| 1133 | Value *ElseV = Else->Codegen(); |
| 1134 | if (ElseV == 0) return 0; |
| 1135 | |
| 1136 | Builder.CreateBr(MergeBB); |
| 1137 | // Codegen of 'Else' can change the current block, update ElseBB for the PHI. |
| 1138 | ElseBB = Builder.GetInsertBlock(); |
| 1139 | |
| 1140 | // Emit merge block. |
| 1141 | TheFunction->insert(TheFunction->end(), MergeBB); |
| 1142 | Builder.SetInsertPoint(MergeBB); |
| 1143 | PHINode *PN = Builder.CreatePHI(Type::getDoubleTy(TheContext), 2, "iftmp"); |
| 1144 | |
| 1145 | PN->addIncoming(ThenV, ThenBB); |
| 1146 | PN->addIncoming(ElseV, ElseBB); |
| 1147 | return PN; |
| 1148 | } |
| 1149 | |
| 1150 | Value *ForExprAST::Codegen() { |
| 1151 | // Output this as: |
| 1152 | // var = alloca double |
| 1153 | // ... |
| 1154 | // start = startexpr |
| 1155 | // store start -> var |
| 1156 | // goto loop |
| 1157 | // loop: |
| 1158 | // ... |
| 1159 | // bodyexpr |
| 1160 | // ... |
| 1161 | // loopend: |
| 1162 | // step = stepexpr |
| 1163 | // endcond = endexpr |
| 1164 | // |
| 1165 | // curvar = load var |
| 1166 | // nextvar = curvar + step |
| 1167 | // store nextvar -> var |
| 1168 | // br endcond, loop, endloop |
| 1169 | // outloop: |
| 1170 | |
| 1171 | Function *TheFunction = Builder.GetInsertBlock()->getParent(); |
| 1172 | |
| 1173 | // Create an alloca for the variable in the entry block. |
| 1174 | AllocaInst *Alloca = CreateEntryBlockAlloca(TheFunction, VarName); |
| 1175 | |
| 1176 | // Emit the start code first, without 'variable' in scope. |
| 1177 | Value *StartVal = Start->Codegen(); |
| 1178 | if (StartVal == 0) return 0; |
| 1179 | |
| 1180 | // Store the value into the alloca. |
| 1181 | Builder.CreateStore(StartVal, Alloca); |
| 1182 | |
| 1183 | // Make the new basic block for the loop header, inserting after current |
| 1184 | // block. |
| 1185 | BasicBlock *LoopBB = BasicBlock::Create(TheContext, "loop", TheFunction); |
| 1186 | |
| 1187 | // Insert an explicit fall through from the current block to the LoopBB. |
| 1188 | Builder.CreateBr(LoopBB); |
| 1189 | |
| 1190 | // Start insertion in LoopBB. |
| 1191 | Builder.SetInsertPoint(LoopBB); |
| 1192 | |
| 1193 | // Within the loop, the variable is defined equal to the PHI node. If it |
| 1194 | // shadows an existing variable, we have to restore it, so save it now. |
| 1195 | AllocaInst *OldVal = NamedValues[VarName]; |
| 1196 | NamedValues[VarName] = Alloca; |
| 1197 | |
| 1198 | // Emit the body of the loop. This, like any other expr, can change the |
| 1199 | // current BB. Note that we ignore the value computed by the body, but don't |
| 1200 | // allow an error. |
| 1201 | if (Body->Codegen() == 0) |
| 1202 | return 0; |
| 1203 | |
| 1204 | // Emit the step value. |
| 1205 | Value *StepVal; |
| 1206 | if (Step) { |
| 1207 | StepVal = Step->Codegen(); |
| 1208 | if (StepVal == 0) return 0; |
| 1209 | } else { |
| 1210 | // If not specified, use 1.0. |
| 1211 | StepVal = ConstantFP::get(TheContext, APFloat(1.0)); |
| 1212 | } |
| 1213 | |
| 1214 | // Compute the end condition. |
| 1215 | Value *EndCond = End->Codegen(); |
| 1216 | if (EndCond == 0) return EndCond; |
| 1217 | |
| 1218 | // Reload, increment, and restore the alloca. This handles the case where |
| 1219 | // the body of the loop mutates the variable. |
| 1220 | Value *CurVar = Builder.CreateLoad(Alloca, VarName.c_str()); |
| 1221 | Value *NextVar = Builder.CreateFAdd(CurVar, StepVal, "nextvar"); |
| 1222 | Builder.CreateStore(NextVar, Alloca); |
| 1223 | |
| 1224 | // Convert condition to a bool by comparing equal to 0.0. |
| 1225 | EndCond = Builder.CreateFCmpONE( |
| 1226 | EndCond, ConstantFP::get(TheContext, APFloat(0.0)), "loopcond"); |
| 1227 | |
| 1228 | // Create the "after loop" block and insert it. |
| 1229 | BasicBlock *AfterBB = |
| 1230 | BasicBlock::Create(TheContext, "afterloop", TheFunction); |
| 1231 | |
| 1232 | // Insert the conditional branch into the end of LoopEndBB. |
| 1233 | Builder.CreateCondBr(EndCond, LoopBB, AfterBB); |
| 1234 | |
| 1235 | // Any new code will be inserted in AfterBB. |
| 1236 | Builder.SetInsertPoint(AfterBB); |
| 1237 | |
| 1238 | // Restore the unshadowed variable. |
| 1239 | if (OldVal) |
| 1240 | NamedValues[VarName] = OldVal; |
| 1241 | else |
| 1242 | NamedValues.erase(VarName); |
| 1243 | |
| 1244 | |
| 1245 | // for expr always returns 0.0. |
| 1246 | return Constant::getNullValue(Type::getDoubleTy(TheContext)); |
| 1247 | } |
| 1248 | |
| 1249 | Value *VarExprAST::Codegen() { |
| 1250 | std::vector<AllocaInst *> OldBindings; |
| 1251 | |
| 1252 | Function *TheFunction = Builder.GetInsertBlock()->getParent(); |
| 1253 | |
| 1254 | // Register all variables and emit their initializer. |
| 1255 | for (unsigned i = 0, e = VarNames.size(); i != e; ++i) { |
| 1256 | const std::string &VarName = VarNames[i].first; |
| 1257 | ExprAST *Init = VarNames[i].second; |
| 1258 | |
| 1259 | // Emit the initializer before adding the variable to scope, this prevents |
| 1260 | // the initializer from referencing the variable itself, and permits stuff |
| 1261 | // like this: |
| 1262 | // var a = 1 in |
| 1263 | // var a = a in ... # refers to outer 'a'. |
| 1264 | Value *InitVal; |
| 1265 | if (Init) { |
| 1266 | InitVal = Init->Codegen(); |
| 1267 | if (InitVal == 0) return 0; |
| 1268 | } else { // If not specified, use 0.0. |
| 1269 | InitVal = ConstantFP::get(TheContext, APFloat(0.0)); |
| 1270 | } |
| 1271 | |
| 1272 | AllocaInst *Alloca = CreateEntryBlockAlloca(TheFunction, VarName); |
| 1273 | Builder.CreateStore(InitVal, Alloca); |
| 1274 | |
| 1275 | // Remember the old variable binding so that we can restore the binding when |
| 1276 | // we unrecurse. |
| 1277 | OldBindings.push_back(NamedValues[VarName]); |
| 1278 | |
| 1279 | // Remember this binding. |
| 1280 | NamedValues[VarName] = Alloca; |
| 1281 | } |
| 1282 | |
| 1283 | // Codegen the body, now that all vars are in scope. |
| 1284 | Value *BodyVal = Body->Codegen(); |
| 1285 | if (BodyVal == 0) return 0; |
| 1286 | |
| 1287 | // Pop all our variables from scope. |
| 1288 | for (unsigned i = 0, e = VarNames.size(); i != e; ++i) |
| 1289 | NamedValues[VarNames[i].first] = OldBindings[i]; |
| 1290 | |
| 1291 | // Return the body computation. |
| 1292 | return BodyVal; |
| 1293 | } |
| 1294 | |
| 1295 | Function *PrototypeAST::Codegen() { |
| 1296 | // Make the function type: double(double,double) etc. |
| 1297 | std::vector<Type *> Doubles(Args.size(), Type::getDoubleTy(TheContext)); |
| 1298 | FunctionType *FT = |
| 1299 | FunctionType::get(Type::getDoubleTy(TheContext), Doubles, false); |
| 1300 | |
| 1301 | std::string FnName = MakeLegalFunctionName(Name); |
| 1302 | |
| 1303 | Module* M = TheHelper->getModuleForNewFunction(); |
| 1304 | |
| 1305 | Function *F = Function::Create(FT, Function::ExternalLinkage, FnName, M); |
| 1306 | |
| 1307 | // If F conflicted, there was already something named 'FnName'. If it has a |
| 1308 | // body, don't allow redefinition or reextern. |
| 1309 | if (F->getName() != FnName) { |
| 1310 | // Delete the one we just made and get the existing one. |
| 1311 | F->eraseFromParent(); |
| 1312 | F = M->getFunction(Name); |
| 1313 | |
| 1314 | // If F already has a body, reject this. |
| 1315 | if (!F->empty()) { |
| 1316 | ErrorF("redefinition of function"); |
| 1317 | return 0; |
| 1318 | } |
| 1319 | |
| 1320 | // If F took a different number of args, reject. |
| 1321 | if (F->arg_size() != Args.size()) { |
| 1322 | ErrorF("redefinition of function with different # args"); |
| 1323 | return 0; |
| 1324 | } |
| 1325 | } |
| 1326 | |
| 1327 | // Set names for all arguments. |
| 1328 | unsigned Idx = 0; |
| 1329 | for (Function::arg_iterator AI = F->arg_begin(); Idx != Args.size(); |
| 1330 | ++AI, ++Idx) |
| 1331 | AI->setName(Args[Idx]); |
| 1332 | |
| 1333 | return F; |
| 1334 | } |
| 1335 | |
| 1336 | /// CreateArgumentAllocas - Create an alloca for each argument and register the |
| 1337 | /// argument in the symbol table so that references to it will succeed. |
| 1338 | void PrototypeAST::CreateArgumentAllocas(Function *F) { |
| 1339 | Function::arg_iterator AI = F->arg_begin(); |
| 1340 | for (unsigned Idx = 0, e = Args.size(); Idx != e; ++Idx, ++AI) { |
| 1341 | // Create an alloca for this variable. |
| 1342 | AllocaInst *Alloca = CreateEntryBlockAlloca(F, Args[Idx]); |
| 1343 | |
| 1344 | // Store the initial value into the alloca. |
| 1345 | Builder.CreateStore(AI, Alloca); |
| 1346 | |
| 1347 | // Add arguments to variable symbol table. |
| 1348 | NamedValues[Args[Idx]] = Alloca; |
| 1349 | } |
| 1350 | } |
| 1351 | |
| 1352 | Function *FunctionAST::Codegen() { |
| 1353 | NamedValues.clear(); |
| 1354 | |
| 1355 | Function *TheFunction = Proto->Codegen(); |
| 1356 | if (TheFunction == 0) |
| 1357 | return 0; |
| 1358 | |
| 1359 | // If this is an operator, install it. |
| 1360 | if (Proto->isBinaryOp()) |
| 1361 | BinopPrecedence[Proto->getOperatorName()] = Proto->getBinaryPrecedence(); |
| 1362 | |
| 1363 | // Create a new basic block to start insertion into. |
| 1364 | BasicBlock *BB = BasicBlock::Create(TheContext, "entry", TheFunction); |
| 1365 | Builder.SetInsertPoint(BB); |
| 1366 | |
| 1367 | // Add all arguments to the symbol table and create their allocas. |
| 1368 | Proto->CreateArgumentAllocas(TheFunction); |
| 1369 | |
| 1370 | if (Value *RetVal = Body->Codegen()) { |
| 1371 | // Finish off the function. |
| 1372 | Builder.CreateRet(RetVal); |
| 1373 | |
| 1374 | // Validate the generated code, checking for consistency. |
| 1375 | verifyFunction(*TheFunction); |
| 1376 | |
| 1377 | return TheFunction; |
| 1378 | } |
| 1379 | |
| 1380 | // Error reading body, remove function. |
| 1381 | TheFunction->eraseFromParent(); |
| 1382 | |
| 1383 | if (Proto->isBinaryOp()) |
| 1384 | BinopPrecedence.erase(Proto->getOperatorName()); |
| 1385 | return 0; |
| 1386 | } |
| 1387 | |
| 1388 | //===----------------------------------------------------------------------===// |
| 1389 | // Top-Level parsing and JIT Driver |
| 1390 | //===----------------------------------------------------------------------===// |
| 1391 | |
| 1392 | static void HandleDefinition() { |
| 1393 | if (FunctionAST *F = ParseDefinition()) { |
| 1394 | TheHelper->closeCurrentModule(); |
| 1395 | if (Function *LF = F->Codegen()) { |
| 1396 | #ifndef MINIMAL_STDERR_OUTPUT |
| 1397 | fprintf(stderr, "Read function definition:"); |
| 1398 | LF->print(errs()); |
| 1399 | fprintf(stderr, "\n"); |
| 1400 | #endif |
| 1401 | } |
| 1402 | } else { |
| 1403 | // Skip token for error recovery. |
| 1404 | getNextToken(); |
| 1405 | } |
| 1406 | } |
| 1407 | |
| 1408 | static void HandleExtern() { |
| 1409 | if (PrototypeAST *P = ParseExtern()) { |
| 1410 | if (Function *F = P->Codegen()) { |
| 1411 | #ifndef MINIMAL_STDERR_OUTPUT |
| 1412 | fprintf(stderr, "Read extern: "); |
| 1413 | F->print(errs()); |
| 1414 | fprintf(stderr, "\n"); |
| 1415 | #endif |
| 1416 | } |
| 1417 | } else { |
| 1418 | // Skip token for error recovery. |
| 1419 | getNextToken(); |
| 1420 | } |
| 1421 | } |
| 1422 | |
| 1423 | static void HandleTopLevelExpression() { |
| 1424 | // Evaluate a top-level expression into an anonymous function. |
| 1425 | if (FunctionAST *F = ParseTopLevelExpr()) { |
| 1426 | if (Function *LF = F->Codegen()) { |
| 1427 | // JIT the function, returning a function pointer. |
| 1428 | void *FPtr = TheHelper->getPointerToFunction(LF); |
| 1429 | |
| 1430 | // Cast it to the right type (takes no arguments, returns a double) so we |
| 1431 | // can call it as a native function. |
| 1432 | double (*FP)() = (double (*)())(intptr_t)FPtr; |
| 1433 | #ifdef MINIMAL_STDERR_OUTPUT |
| 1434 | FP(); |
| 1435 | #else |
| 1436 | fprintf(stderr, "Evaluated to %f\n", FP()); |
| 1437 | #endif |
| 1438 | } |
| 1439 | } else { |
| 1440 | // Skip token for error recovery. |
| 1441 | getNextToken(); |
| 1442 | } |
| 1443 | } |
| 1444 | |
| 1445 | /// top ::= definition | external | expression | ';' |
| 1446 | static void MainLoop() { |
| 1447 | while (1) { |
| 1448 | #ifndef MINIMAL_STDERR_OUTPUT |
| 1449 | fprintf(stderr, "ready> "); |
| 1450 | #endif |
| 1451 | switch (CurTok) { |
| 1452 | case tok_eof: return; |
| 1453 | case ';': getNextToken(); break; // ignore top-level semicolons. |
| 1454 | case tok_def: HandleDefinition(); break; |
| 1455 | case tok_extern: HandleExtern(); break; |
| 1456 | default: HandleTopLevelExpression(); break; |
| 1457 | } |
| 1458 | } |
| 1459 | } |
| 1460 | |
| 1461 | //===----------------------------------------------------------------------===// |
| 1462 | // "Library" functions that can be "extern'd" from user code. |
| 1463 | //===----------------------------------------------------------------------===// |
| 1464 | |
| 1465 | /// putchard - putchar that takes a double and returns 0. |
| 1466 | extern "C" |
| 1467 | double putchard(double X) { |
| 1468 | putchar((char)X); |
| 1469 | return 0; |
| 1470 | } |
| 1471 | |
| 1472 | /// printd - printf that takes a double prints it as "%f\n", returning 0. |
| 1473 | extern "C" |
| 1474 | double printd(double X) { |
| 1475 | printf("%f", X); |
| 1476 | return 0; |
| 1477 | } |
| 1478 | |
| 1479 | extern "C" |
| 1480 | double printlf() { |
| 1481 | printf("\n"); |
| 1482 | return 0; |
| 1483 | } |
| 1484 | |
| 1485 | //===----------------------------------------------------------------------===// |
| 1486 | // Command line input file handler |
| 1487 | //===----------------------------------------------------------------------===// |
| 1488 | |
| 1489 | Module* parseInputIR(std::string InputFile) { |
| 1490 | SMDiagnostic Err; |
| 1491 | Module *M = ParseIRFile(InputFile, Err, TheContext); |
| 1492 | if (!M) { |
| 1493 | Err.print("IR parsing failed: ", errs()); |
| 1494 | return NULL; |
| 1495 | } |
| 1496 | |
| 1497 | char ModID[256]; |
| 1498 | sprintf(ModID, "IR:%s", InputFile.c_str()); |
| 1499 | M->setModuleIdentifier(ModID); |
| 1500 | |
| 1501 | TheHelper->addModule(M); |
| 1502 | return M; |
| 1503 | } |
| 1504 | |
| 1505 | //===----------------------------------------------------------------------===// |
| 1506 | // Main driver code. |
| 1507 | //===----------------------------------------------------------------------===// |
| 1508 | |
| 1509 | int main(int argc, char **argv) { |
| 1510 | InitializeNativeTarget(); |
| 1511 | InitializeNativeTargetAsmPrinter(); |
| 1512 | InitializeNativeTargetAsmParser(); |
| 1513 | LLVMContext &Context = TheContext; |
| 1514 | |
| 1515 | cl::ParseCommandLineOptions(argc, argv, |
| 1516 | "Kaleidoscope example program\n"); |
| 1517 | |
| 1518 | // Install standard binary operators. |
| 1519 | // 1 is lowest precedence. |
| 1520 | BinopPrecedence['='] = 2; |
| 1521 | BinopPrecedence['<'] = 10; |
| 1522 | BinopPrecedence['+'] = 20; |
| 1523 | BinopPrecedence['-'] = 20; |
| 1524 | BinopPrecedence['/'] = 40; |
| 1525 | BinopPrecedence['*'] = 40; // highest. |
| 1526 | |
| 1527 | // Prime the first token. |
| 1528 | #ifndef MINIMAL_STDERR_OUTPUT |
| 1529 | fprintf(stderr, "ready> "); |
| 1530 | #endif |
| 1531 | getNextToken(); |
| 1532 | |
| 1533 | // Make the helper, which holds all the code. |
| 1534 | TheHelper = new MCJITHelper(Context); |
| 1535 | |
| 1536 | if (!InputIR.empty()) { |
| 1537 | parseInputIR(InputIR); |
| 1538 | } |
| 1539 | |
| 1540 | // Run the main "interpreter loop" now. |
| 1541 | MainLoop(); |
| 1542 | |
| 1543 | #ifndef MINIMAL_STDERR_OUTPUT |
| 1544 | // Print out all of the generated code. |
| 1545 | TheHelper->print(errs()); |
| 1546 | #endif |
| 1547 | |
| 1548 | return 0; |
| 1549 | } |