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