| 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 | |
| 27 | using namespace llvm; |
| 28 | |
| 29 | //===----------------------------------------------------------------------===// |
| 30 | // Command-line options |
| 31 | //===----------------------------------------------------------------------===// |
| 32 | |
| 33 | namespace { |
| 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. |
| 69 | enum 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 | |
| 89 | static std::string IdentifierStr; // Filled in if tok_identifier |
| 90 | static double NumVal; // Filled in if tok_number |
| 91 | |
| 92 | /// gettok - Return the next token from standard input. |
| 93 | static 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. |
| 153 | class ExprAST { |
| 154 | public: |
| 155 | virtual ~ExprAST() {} |
| 156 | virtual Value *Codegen() = 0; |
| 157 | }; |
| 158 | |
| 159 | /// NumberExprAST - Expression class for numeric literals like "1.0". |
| 160 | class NumberExprAST : public ExprAST { |
| 161 | double Val; |
| 162 | public: |
| 163 | NumberExprAST(double val) : Val(val) {} |
| 164 | virtual Value *Codegen(); |
| 165 | }; |
| 166 | |
| 167 | /// VariableExprAST - Expression class for referencing a variable, like "a". |
| 168 | class VariableExprAST : public ExprAST { |
| 169 | std::string Name; |
| 170 | public: |
| 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. |
| 177 | class UnaryExprAST : public ExprAST { |
| 178 | char Opcode; |
| 179 | ExprAST *Operand; |
| 180 | public: |
| 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. |
| 187 | class BinaryExprAST : public ExprAST { |
| 188 | char Op; |
| 189 | ExprAST *LHS, *RHS; |
| 190 | public: |
| 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. |
| 197 | class CallExprAST : public ExprAST { |
| 198 | std::string Callee; |
| 199 | std::vector<ExprAST*> Args; |
| 200 | public: |
| 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. |
| 207 | class IfExprAST : public ExprAST { |
| 208 | ExprAST *Cond, *Then, *Else; |
| 209 | public: |
| 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. |
| 216 | class ForExprAST : public ExprAST { |
| 217 | std::string VarName; |
| 218 | ExprAST *Start, *End, *Step, *Body; |
| 219 | public: |
| 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 |
| 227 | class VarExprAST : public ExprAST { |
| 228 | std::vector<std::pair<std::string, ExprAST*> > VarNames; |
| 229 | ExprAST *Body; |
| 230 | public: |
| 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. |
| 240 | class PrototypeAST { |
| 241 | std::string Name; |
| 242 | std::vector<std::string> Args; |
| 243 | bool isOperator; |
| 244 | unsigned Precedence; // Precedence if a binary op. |
| 245 | public: |
| 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. |
| 266 | class FunctionAST { |
| 267 | PrototypeAST *Proto; |
| 268 | ExprAST *Body; |
| 269 | public: |
| 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. |
| 283 | static int CurTok; |
| 284 | static int getNextToken() { |
| 285 | return CurTok = gettok(); |
| 286 | } |
| 287 | |
| 288 | /// BinopPrecedence - This holds the precedence for each binary operator that is |
| 289 | /// defined. |
| 290 | static std::map<char, int> BinopPrecedence; |
| 291 | |
| 292 | /// GetTokPrecedence - Get the precedence of the pending binary operator token. |
| 293 | static 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. |
| 304 | ExprAST *Error(const char *Str) { fprintf(stderr, "Error: %s\n", Str);return 0;} |
| 305 | PrototypeAST *ErrorP(const char *Str) { Error(Str); return 0; } |
| 306 | FunctionAST *ErrorF(const char *Str) { Error(Str); return 0; } |
| 307 | |
| 308 | static ExprAST *ParseExpression(); |
| 309 | |
| 310 | /// identifierexpr |
| 311 | /// ::= identifier |
| 312 | /// ::= identifier '(' expression* ')' |
| 313 | static 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 |
| 345 | static ExprAST *ParseNumberExpr() { |
| 346 | ExprAST *Result = new NumberExprAST(NumVal); |
| 347 | getNextToken(); // consume the number |
| 348 | return Result; |
| 349 | } |
| 350 | |
| 351 | /// parenexpr ::= '(' expression ')' |
| 352 | static 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 |
| 364 | static 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 |
| 390 | static 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 |
| 433 | static 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 |
| 483 | static 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 |
| 498 | static 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)* |
| 513 | static 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 | /// |
| 547 | static 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) |
| 558 | static 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 |
| 620 | static 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 |
| 631 | static 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 |
| 641 | static 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 |
| 651 | std::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 | |
| 660 | std::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 | |
| 691 | class MCJITObjectCache : public ObjectCache { |
| 692 | public: |
| 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 | |
| 749 | private: |
| 750 | SmallString<128> CacheDir; |
| 751 | }; |
| 752 | |
| 753 | //===----------------------------------------------------------------------===// |
| 754 | // IR input file handler |
| 755 | //===----------------------------------------------------------------------===// |
| 756 | |
| 757 | Module* 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 | |
| 775 | class BaseHelper |
| 776 | { |
| 777 | public: |
| 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 | |
| 794 | class MCJITHelper : public BaseHelper |
| 795 | { |
| 796 | public: |
| 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 | |
| 815 | protected: |
| 816 | ExecutionEngine *compileModule(Module *M); |
| 817 | |
| 818 | private: |
| 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 | |
| 831 | class HelpingMemoryManager : public SectionMemoryManager |
| 832 | { |
| 833 | HelpingMemoryManager(const HelpingMemoryManager&) = delete; |
| 834 | void operator=(const HelpingMemoryManager&) = delete; |
| 835 | |
| 836 | public: |
| 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); |
| 850 | private: |
| 851 | MCJITHelper *MasterHelper; |
| 852 | }; |
| 853 | |
| 854 | void *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 | |
| 869 | MCJITHelper::~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 | |
| 887 | Function *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 | |
| 919 | Module *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 | |
| 933 | ExecutionEngine *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 | |
| 998 | void *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 | |
| 1023 | void MCJITHelper::closeCurrentModule() { |
| 1024 | // If we have an open module (and we should), pack it up |
| 1025 | if (CurrentModule) { |
| 1026 | CurrentModule = NULL; |
| 1027 | } |
| 1028 | } |
| 1029 | |
| 1030 | void *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 | |
| 1055 | void 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 | |
| 1068 | static BaseHelper *TheHelper; |
| 1069 | static LLVMContext TheContext; |
| 1070 | static IRBuilder<> Builder(TheContext); |
| 1071 | static std::map<std::string, AllocaInst*> NamedValues; |
| 1072 | |
| 1073 | Value *ErrorV(const char *Str) { Error(Str); return 0; } |
| 1074 | |
| 1075 | /// CreateEntryBlockAlloca - Create an alloca instruction in the entry block of |
| 1076 | /// the function. This is used for mutable variables etc. |
| 1077 | static AllocaInst *CreateEntryBlockAlloca(Function *TheFunction, |
| 1078 | const std::string &VarName) { |
| 1079 | IRBuilder<> TmpB(&TheFunction->getEntryBlock(), |
| 1080 | TheFunction->getEntryBlock().begin()); |
| 1081 | return TmpB.CreateAlloca(Type::getDoubleTy(TheContext), 0, VarName.c_str()); |
| 1082 | } |
| 1083 | |
| 1084 | Value *NumberExprAST::Codegen() { |
| 1085 | return ConstantFP::get(TheContext, APFloat(Val)); |
| 1086 | } |
| 1087 | |
| 1088 | Value *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 | |
| 1097 | Value *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 | |
| 1109 | Value *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(TheContext), "booltmp"); |
| 1144 | default: break; |
| 1145 | } |
| 1146 | |
| 1147 | // If it wasn't a builtin binary operator, it must be a user defined one. Emit |
| 1148 | // a call to it. |
| 1149 | Function *F; |
| 1150 | F = TheHelper->getFunction(MakeLegalFunctionName(std::string("binary")+Op)); |
| 1151 | assert(F && "binary operator not found!"); |
| 1152 | |
| 1153 | Value *Ops[] = { L, R }; |
| 1154 | return Builder.CreateCall(F, Ops, "binop"); |
| 1155 | } |
| 1156 | |
| 1157 | Value *CallExprAST::Codegen() { |
| 1158 | // Look up the name in the global module table. |
| 1159 | Function *CalleeF = TheHelper->getFunction(Callee); |
| 1160 | if (CalleeF == 0) { |
| 1161 | char error_str[64]; |
| 1162 | sprintf(error_str, "Unknown function referenced %s", Callee.c_str()); |
| 1163 | return ErrorV(error_str); |
| 1164 | } |
| 1165 | |
| 1166 | // If argument mismatch error. |
| 1167 | if (CalleeF->arg_size() != Args.size()) |
| 1168 | return ErrorV("Incorrect # arguments passed"); |
| 1169 | |
| 1170 | std::vector<Value*> ArgsV; |
| 1171 | for (unsigned i = 0, e = Args.size(); i != e; ++i) { |
| 1172 | ArgsV.push_back(Args[i]->Codegen()); |
| 1173 | if (ArgsV.back() == 0) return 0; |
| 1174 | } |
| 1175 | |
| 1176 | return Builder.CreateCall(CalleeF, ArgsV, "calltmp"); |
| 1177 | } |
| 1178 | |
| 1179 | Value *IfExprAST::Codegen() { |
| 1180 | Value *CondV = Cond->Codegen(); |
| 1181 | if (CondV == 0) return 0; |
| 1182 | |
| 1183 | // Convert condition to a bool by comparing equal to 0.0. |
| 1184 | CondV = Builder.CreateFCmpONE( |
| 1185 | CondV, ConstantFP::get(TheContext, APFloat(0.0)), "ifcond"); |
| 1186 | |
| 1187 | Function *TheFunction = Builder.GetInsertBlock()->getParent(); |
| 1188 | |
| 1189 | // Create blocks for the then and else cases. Insert the 'then' block at the |
| 1190 | // end of the function. |
| 1191 | BasicBlock *ThenBB = BasicBlock::Create(TheContext, "then", TheFunction); |
| 1192 | BasicBlock *ElseBB = BasicBlock::Create(TheContext, "else"); |
| 1193 | BasicBlock *MergeBB = BasicBlock::Create(TheContext, "ifcont"); |
| 1194 | |
| 1195 | Builder.CreateCondBr(CondV, ThenBB, ElseBB); |
| 1196 | |
| 1197 | // Emit then value. |
| 1198 | Builder.SetInsertPoint(ThenBB); |
| 1199 | |
| 1200 | Value *ThenV = Then->Codegen(); |
| 1201 | if (ThenV == 0) return 0; |
| 1202 | |
| 1203 | Builder.CreateBr(MergeBB); |
| 1204 | // Codegen of 'Then' can change the current block, update ThenBB for the PHI. |
| 1205 | ThenBB = Builder.GetInsertBlock(); |
| 1206 | |
| 1207 | // Emit else block. |
| 1208 | TheFunction->insert(TheFunction->end(), ElseBB); |
| 1209 | Builder.SetInsertPoint(ElseBB); |
| 1210 | |
| 1211 | Value *ElseV = Else->Codegen(); |
| 1212 | if (ElseV == 0) return 0; |
| 1213 | |
| 1214 | Builder.CreateBr(MergeBB); |
| 1215 | // Codegen of 'Else' can change the current block, update ElseBB for the PHI. |
| 1216 | ElseBB = Builder.GetInsertBlock(); |
| 1217 | |
| 1218 | // Emit merge block. |
| 1219 | TheFunction->insert(TheFunction->end(), MergeBB); |
| 1220 | Builder.SetInsertPoint(MergeBB); |
| 1221 | PHINode *PN = Builder.CreatePHI(Type::getDoubleTy(TheContext), 2, "iftmp"); |
| 1222 | |
| 1223 | PN->addIncoming(ThenV, ThenBB); |
| 1224 | PN->addIncoming(ElseV, ElseBB); |
| 1225 | return PN; |
| 1226 | } |
| 1227 | |
| 1228 | Value *ForExprAST::Codegen() { |
| 1229 | // Output this as: |
| 1230 | // var = alloca double |
| 1231 | // ... |
| 1232 | // start = startexpr |
| 1233 | // store start -> var |
| 1234 | // goto loop |
| 1235 | // loop: |
| 1236 | // ... |
| 1237 | // bodyexpr |
| 1238 | // ... |
| 1239 | // loopend: |
| 1240 | // step = stepexpr |
| 1241 | // endcond = endexpr |
| 1242 | // |
| 1243 | // curvar = load var |
| 1244 | // nextvar = curvar + step |
| 1245 | // store nextvar -> var |
| 1246 | // br endcond, loop, endloop |
| 1247 | // outloop: |
| 1248 | |
| 1249 | Function *TheFunction = Builder.GetInsertBlock()->getParent(); |
| 1250 | |
| 1251 | // Create an alloca for the variable in the entry block. |
| 1252 | AllocaInst *Alloca = CreateEntryBlockAlloca(TheFunction, VarName); |
| 1253 | |
| 1254 | // Emit the start code first, without 'variable' in scope. |
| 1255 | Value *StartVal = Start->Codegen(); |
| 1256 | if (StartVal == 0) return 0; |
| 1257 | |
| 1258 | // Store the value into the alloca. |
| 1259 | Builder.CreateStore(StartVal, Alloca); |
| 1260 | |
| 1261 | // Make the new basic block for the loop header, inserting after current |
| 1262 | // block. |
| 1263 | BasicBlock *LoopBB = BasicBlock::Create(TheContext, "loop", TheFunction); |
| 1264 | |
| 1265 | // Insert an explicit fall through from the current block to the LoopBB. |
| 1266 | Builder.CreateBr(LoopBB); |
| 1267 | |
| 1268 | // Start insertion in LoopBB. |
| 1269 | Builder.SetInsertPoint(LoopBB); |
| 1270 | |
| 1271 | // Within the loop, the variable is defined equal to the PHI node. If it |
| 1272 | // shadows an existing variable, we have to restore it, so save it now. |
| 1273 | AllocaInst *OldVal = NamedValues[VarName]; |
| 1274 | NamedValues[VarName] = Alloca; |
| 1275 | |
| 1276 | // Emit the body of the loop. This, like any other expr, can change the |
| 1277 | // current BB. Note that we ignore the value computed by the body, but don't |
| 1278 | // allow an error. |
| 1279 | if (Body->Codegen() == 0) |
| 1280 | return 0; |
| 1281 | |
| 1282 | // Emit the step value. |
| 1283 | Value *StepVal; |
| 1284 | if (Step) { |
| 1285 | StepVal = Step->Codegen(); |
| 1286 | if (StepVal == 0) return 0; |
| 1287 | } else { |
| 1288 | // If not specified, use 1.0. |
| 1289 | StepVal = ConstantFP::get(TheContext, APFloat(1.0)); |
| 1290 | } |
| 1291 | |
| 1292 | // Compute the end condition. |
| 1293 | Value *EndCond = End->Codegen(); |
| 1294 | if (EndCond == 0) return EndCond; |
| 1295 | |
| 1296 | // Reload, increment, and restore the alloca. This handles the case where |
| 1297 | // the body of the loop mutates the variable. |
| 1298 | Value *CurVar = Builder.CreateLoad(Alloca, VarName.c_str()); |
| 1299 | Value *NextVar = Builder.CreateFAdd(CurVar, StepVal, "nextvar"); |
| 1300 | Builder.CreateStore(NextVar, Alloca); |
| 1301 | |
| 1302 | // Convert condition to a bool by comparing equal to 0.0. |
| 1303 | EndCond = Builder.CreateFCmpONE( |
| 1304 | EndCond, ConstantFP::get(TheContext, APFloat(0.0)), "loopcond"); |
| 1305 | |
| 1306 | // Create the "after loop" block and insert it. |
| 1307 | BasicBlock *AfterBB = |
| 1308 | BasicBlock::Create(TheContext, "afterloop", TheFunction); |
| 1309 | |
| 1310 | // Insert the conditional branch into the end of LoopEndBB. |
| 1311 | Builder.CreateCondBr(EndCond, LoopBB, AfterBB); |
| 1312 | |
| 1313 | // Any new code will be inserted in AfterBB. |
| 1314 | Builder.SetInsertPoint(AfterBB); |
| 1315 | |
| 1316 | // Restore the unshadowed variable. |
| 1317 | if (OldVal) |
| 1318 | NamedValues[VarName] = OldVal; |
| 1319 | else |
| 1320 | NamedValues.erase(VarName); |
| 1321 | |
| 1322 | |
| 1323 | // for expr always returns 0.0. |
| 1324 | return Constant::getNullValue(Type::getDoubleTy(TheContext)); |
| 1325 | } |
| 1326 | |
| 1327 | Value *VarExprAST::Codegen() { |
| 1328 | std::vector<AllocaInst *> OldBindings; |
| 1329 | |
| 1330 | Function *TheFunction = Builder.GetInsertBlock()->getParent(); |
| 1331 | |
| 1332 | // Register all variables and emit their initializer. |
| 1333 | for (unsigned i = 0, e = VarNames.size(); i != e; ++i) { |
| 1334 | const std::string &VarName = VarNames[i].first; |
| 1335 | ExprAST *Init = VarNames[i].second; |
| 1336 | |
| 1337 | // Emit the initializer before adding the variable to scope, this prevents |
| 1338 | // the initializer from referencing the variable itself, and permits stuff |
| 1339 | // like this: |
| 1340 | // var a = 1 in |
| 1341 | // var a = a in ... # refers to outer 'a'. |
| 1342 | Value *InitVal; |
| 1343 | if (Init) { |
| 1344 | InitVal = Init->Codegen(); |
| 1345 | if (InitVal == 0) return 0; |
| 1346 | } else { // If not specified, use 0.0. |
| 1347 | InitVal = ConstantFP::get(TheContext, APFloat(0.0)); |
| 1348 | } |
| 1349 | |
| 1350 | AllocaInst *Alloca = CreateEntryBlockAlloca(TheFunction, VarName); |
| 1351 | Builder.CreateStore(InitVal, Alloca); |
| 1352 | |
| 1353 | // Remember the old variable binding so that we can restore the binding when |
| 1354 | // we unrecurse. |
| 1355 | OldBindings.push_back(NamedValues[VarName]); |
| 1356 | |
| 1357 | // Remember this binding. |
| 1358 | NamedValues[VarName] = Alloca; |
| 1359 | } |
| 1360 | |
| 1361 | // Codegen the body, now that all vars are in scope. |
| 1362 | Value *BodyVal = Body->Codegen(); |
| 1363 | if (BodyVal == 0) return 0; |
| 1364 | |
| 1365 | // Pop all our variables from scope. |
| 1366 | for (unsigned i = 0, e = VarNames.size(); i != e; ++i) |
| 1367 | NamedValues[VarNames[i].first] = OldBindings[i]; |
| 1368 | |
| 1369 | // Return the body computation. |
| 1370 | return BodyVal; |
| 1371 | } |
| 1372 | |
| 1373 | Function *PrototypeAST::Codegen() { |
| 1374 | // Make the function type: double(double,double) etc. |
| 1375 | std::vector<Type *> Doubles(Args.size(), Type::getDoubleTy(TheContext)); |
| 1376 | FunctionType *FT = |
| 1377 | FunctionType::get(Type::getDoubleTy(TheContext), Doubles, false); |
| 1378 | |
| 1379 | std::string FnName; |
| 1380 | FnName = MakeLegalFunctionName(Name); |
| 1381 | |
| 1382 | Module* M = TheHelper->getModuleForNewFunction(); |
| 1383 | Function *F = Function::Create(FT, Function::ExternalLinkage, FnName, M); |
| 1384 | |
| 1385 | // FIXME: Implement duplicate function detection. |
| 1386 | // The check below will only work if the duplicate is in the open module. |
| 1387 | // If F conflicted, there was already something named 'Name'. If it has a |
| 1388 | // body, don't allow redefinition or reextern. |
| 1389 | if (F->getName() != FnName) { |
| 1390 | // Delete the one we just made and get the existing one. |
| 1391 | F->eraseFromParent(); |
| 1392 | F = M->getFunction(FnName); |
| 1393 | // If F already has a body, reject this. |
| 1394 | if (!F->empty()) { |
| 1395 | ErrorF("redefinition of function"); |
| 1396 | return 0; |
| 1397 | } |
| 1398 | // If F took a different number of args, reject. |
| 1399 | if (F->arg_size() != Args.size()) { |
| 1400 | ErrorF("redefinition of function with different # args"); |
| 1401 | return 0; |
| 1402 | } |
| 1403 | } |
| 1404 | |
| 1405 | // Set names for all arguments. |
| 1406 | unsigned Idx = 0; |
| 1407 | for (Function::arg_iterator AI = F->arg_begin(); Idx != Args.size(); |
| 1408 | ++AI, ++Idx) |
| 1409 | AI->setName(Args[Idx]); |
| 1410 | |
| 1411 | return F; |
| 1412 | } |
| 1413 | |
| 1414 | /// CreateArgumentAllocas - Create an alloca for each argument and register the |
| 1415 | /// argument in the symbol table so that references to it will succeed. |
| 1416 | void PrototypeAST::CreateArgumentAllocas(Function *F) { |
| 1417 | Function::arg_iterator AI = F->arg_begin(); |
| 1418 | for (unsigned Idx = 0, e = Args.size(); Idx != e; ++Idx, ++AI) { |
| 1419 | // Create an alloca for this variable. |
| 1420 | AllocaInst *Alloca = CreateEntryBlockAlloca(F, Args[Idx]); |
| 1421 | |
| 1422 | // Store the initial value into the alloca. |
| 1423 | Builder.CreateStore(AI, Alloca); |
| 1424 | |
| 1425 | // Add arguments to variable symbol table. |
| 1426 | NamedValues[Args[Idx]] = Alloca; |
| 1427 | } |
| 1428 | } |
| 1429 | |
| 1430 | Function *FunctionAST::Codegen() { |
| 1431 | NamedValues.clear(); |
| 1432 | |
| 1433 | Function *TheFunction = Proto->Codegen(); |
| 1434 | if (TheFunction == 0) |
| 1435 | return 0; |
| 1436 | |
| 1437 | // If this is an operator, install it. |
| 1438 | if (Proto->isBinaryOp()) |
| 1439 | BinopPrecedence[Proto->getOperatorName()] = Proto->getBinaryPrecedence(); |
| 1440 | |
| 1441 | // Create a new basic block to start insertion into. |
| 1442 | BasicBlock *BB = BasicBlock::Create(TheContext, "entry", TheFunction); |
| 1443 | Builder.SetInsertPoint(BB); |
| 1444 | |
| 1445 | // Add all arguments to the symbol table and create their allocas. |
| 1446 | Proto->CreateArgumentAllocas(TheFunction); |
| 1447 | |
| 1448 | if (Value *RetVal = Body->Codegen()) { |
| 1449 | // Finish off the function. |
| 1450 | Builder.CreateRet(RetVal); |
| 1451 | |
| 1452 | // Validate the generated code, checking for consistency. |
| 1453 | verifyFunction(*TheFunction); |
| 1454 | |
| 1455 | return TheFunction; |
| 1456 | } |
| 1457 | |
| 1458 | // Error reading body, remove function. |
| 1459 | TheFunction->eraseFromParent(); |
| 1460 | |
| 1461 | if (Proto->isBinaryOp()) |
| 1462 | BinopPrecedence.erase(Proto->getOperatorName()); |
| 1463 | return 0; |
| 1464 | } |
| 1465 | |
| 1466 | //===----------------------------------------------------------------------===// |
| 1467 | // Top-Level parsing and JIT Driver |
| 1468 | //===----------------------------------------------------------------------===// |
| 1469 | |
| 1470 | static void HandleDefinition() { |
| 1471 | if (FunctionAST *F = ParseDefinition()) { |
| 1472 | if (EnableLazyCompilation) |
| 1473 | TheHelper->closeCurrentModule(); |
| 1474 | Function *LF = F->Codegen(); |
| 1475 | if (LF && VerboseOutput) { |
| 1476 | fprintf(stderr, "Read function definition:"); |
| 1477 | LF->print(errs()); |
| 1478 | fprintf(stderr, "\n"); |
| 1479 | } |
| 1480 | } else { |
| 1481 | // Skip token for error recovery. |
| 1482 | getNextToken(); |
| 1483 | } |
| 1484 | } |
| 1485 | |
| 1486 | static void HandleExtern() { |
| 1487 | if (PrototypeAST *P = ParseExtern()) { |
| 1488 | Function *F = P->Codegen(); |
| 1489 | if (F && VerboseOutput) { |
| 1490 | fprintf(stderr, "Read extern: "); |
| 1491 | F->print(errs()); |
| 1492 | fprintf(stderr, "\n"); |
| 1493 | } |
| 1494 | } else { |
| 1495 | // Skip token for error recovery. |
| 1496 | getNextToken(); |
| 1497 | } |
| 1498 | } |
| 1499 | |
| 1500 | static void HandleTopLevelExpression() { |
| 1501 | // Evaluate a top-level expression into an anonymous function. |
| 1502 | if (FunctionAST *F = ParseTopLevelExpr()) { |
| 1503 | if (Function *LF = F->Codegen()) { |
| 1504 | // JIT the function, returning a function pointer. |
| 1505 | void *FPtr = TheHelper->getPointerToFunction(LF); |
| 1506 | // Cast it to the right type (takes no arguments, returns a double) so we |
| 1507 | // can call it as a native function. |
| 1508 | double (*FP)() = (double (*)())(intptr_t)FPtr; |
| 1509 | double Result = FP(); |
| 1510 | if (VerboseOutput) |
| 1511 | fprintf(stderr, "Evaluated to %f\n", Result); |
| 1512 | } |
| 1513 | } else { |
| 1514 | // Skip token for error recovery. |
| 1515 | getNextToken(); |
| 1516 | } |
| 1517 | } |
| 1518 | |
| 1519 | /// top ::= definition | external | expression | ';' |
| 1520 | static void MainLoop() { |
| 1521 | while (1) { |
| 1522 | if (!SuppressPrompts) |
| 1523 | fprintf(stderr, "ready> "); |
| 1524 | switch (CurTok) { |
| 1525 | case tok_eof: return; |
| 1526 | case ';': getNextToken(); break; // ignore top-level semicolons. |
| 1527 | case tok_def: HandleDefinition(); break; |
| 1528 | case tok_extern: HandleExtern(); break; |
| 1529 | default: HandleTopLevelExpression(); break; |
| 1530 | } |
| 1531 | } |
| 1532 | } |
| 1533 | |
| 1534 | //===----------------------------------------------------------------------===// |
| 1535 | // "Library" functions that can be "extern'd" from user code. |
| 1536 | //===----------------------------------------------------------------------===// |
| 1537 | |
| 1538 | /// putchard - putchar that takes a double and returns 0. |
| 1539 | extern "C" |
| 1540 | double putchard(double X) { |
| 1541 | putchar((char)X); |
| 1542 | return 0; |
| 1543 | } |
| 1544 | |
| 1545 | /// printd - printf that takes a double prints it as "%f\n", returning 0. |
| 1546 | extern "C" |
| 1547 | double printd(double X) { |
| 1548 | printf("%f", X); |
| 1549 | return 0; |
| 1550 | } |
| 1551 | |
| 1552 | extern "C" |
| 1553 | double printlf() { |
| 1554 | printf("\n"); |
| 1555 | return 0; |
| 1556 | } |
| 1557 | |
| 1558 | //===----------------------------------------------------------------------===// |
| 1559 | // Main driver code. |
| 1560 | //===----------------------------------------------------------------------===// |
| 1561 | |
| 1562 | int main(int argc, char **argv) { |
| 1563 | InitializeNativeTarget(); |
| 1564 | InitializeNativeTargetAsmPrinter(); |
| 1565 | InitializeNativeTargetAsmParser(); |
| 1566 | LLVMContext &Context = TheContext; |
| 1567 | |
| 1568 | cl::ParseCommandLineOptions(argc, argv, |
| 1569 | "Kaleidoscope example program\n"); |
| 1570 | |
| 1571 | // Install standard binary operators. |
| 1572 | // 1 is lowest precedence. |
| 1573 | BinopPrecedence['='] = 2; |
| 1574 | BinopPrecedence['<'] = 10; |
| 1575 | BinopPrecedence['+'] = 20; |
| 1576 | BinopPrecedence['-'] = 20; |
| 1577 | BinopPrecedence['/'] = 40; |
| 1578 | BinopPrecedence['*'] = 40; // highest. |
| 1579 | |
| 1580 | // Make the Helper, which holds all the code. |
| 1581 | TheHelper = new MCJITHelper(Context); |
| 1582 | |
| 1583 | // Prime the first token. |
| 1584 | if (!SuppressPrompts) |
| 1585 | fprintf(stderr, "ready> "); |
| 1586 | getNextToken(); |
| 1587 | |
| 1588 | // Run the main "interpreter loop" now. |
| 1589 | MainLoop(); |
| 1590 | |
| 1591 | // Print out all of the generated code. |
| 1592 | if (DumpModulesOnExit) |
| 1593 | TheHelper->dump(); |
| 1594 | |
| 1595 | return 0; |
| 1596 | } |