[LV] Ignore some costs when loop gets fully unrolled (#106699)
[llvm-project.git] / llvm / examples / Kaleidoscope / MCJIT / complete / toy.cpp
blobbc2267fe1829328e144547316c0f0114aa7fe116
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>
27 using namespace llvm;
29 //===----------------------------------------------------------------------===//
30 // Command-line options
31 //===----------------------------------------------------------------------===//
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"));
39 cl::opt<bool>
40 VerboseOutput("verbose",
41 cl::desc("Enable verbose output (results, IR, etc.) to stderr"),
42 cl::init(false));
44 cl::opt<bool>
45 SuppressPrompts("suppress-prompts",
46 cl::desc("Disable printing the 'ready' prompt"),
47 cl::init(false));
49 cl::opt<bool>
50 DumpModulesOnExit("dump-modules",
51 cl::desc("Dump IR from modules to stderr on shutdown"),
52 cl::init(false));
54 cl::opt<bool> EnableLazyCompilation(
55 "enable-lazy-compilation", cl::desc("Enable lazy compilation when using the MCJIT engine"),
56 cl::init(true));
58 cl::opt<bool> UseObjectCache(
59 "use-object-cache", cl::desc("Enable use of the MCJIT object caching"),
60 cl::init(false));
61 } // namespace
63 //===----------------------------------------------------------------------===//
64 // Lexer
65 //===----------------------------------------------------------------------===//
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,
72 // commands
73 tok_def = -2, tok_extern = -3,
75 // primary
76 tok_identifier = -4, tok_number = -5,
78 // control
79 tok_if = -6, tok_then = -7, tok_else = -8,
80 tok_for = -9, tok_in = -10,
82 // operators
83 tok_binary = -11, tok_unary = -12,
85 // var definition
86 tok_var = -13
89 static std::string IdentifierStr; // Filled in if tok_identifier
90 static double NumVal; // Filled in if tok_number
92 /// gettok - Return the next token from standard input.
93 static int gettok() {
94 static int LastChar = ' ';
96 // Skip any whitespace.
97 while (isspace(LastChar))
98 LastChar = getchar();
100 if (isalpha(LastChar)) { // identifier: [a-zA-Z][a-zA-Z0-9]*
101 IdentifierStr = LastChar;
102 while (isalnum((LastChar = getchar())))
103 IdentifierStr += LastChar;
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;
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 == '.');
125 NumVal = strtod(NumStr.c_str(), 0);
126 return tok_number;
129 if (LastChar == '#') {
130 // Comment until end of line.
131 do LastChar = getchar();
132 while (LastChar != EOF && LastChar != '\n' && LastChar != '\r');
134 if (LastChar != EOF)
135 return gettok();
138 // Check for end of file. Don't eat the EOF.
139 if (LastChar == EOF)
140 return tok_eof;
142 // Otherwise, just return the character as its ascii value.
143 int ThisChar = LastChar;
144 LastChar = getchar();
145 return ThisChar;
148 //===----------------------------------------------------------------------===//
149 // Abstract Syntax Tree (aka Parse Tree)
150 //===----------------------------------------------------------------------===//
152 /// ExprAST - Base class for all expression nodes.
153 class ExprAST {
154 public:
155 virtual ~ExprAST() {}
156 virtual Value *Codegen() = 0;
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();
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();
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();
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();
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();
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();
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();
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) {}
235 virtual Value *Codegen();
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) {}
250 bool isUnaryOp() const { return isOperator && Args.size() == 1; }
251 bool isBinaryOp() const { return isOperator && Args.size() == 2; }
253 char getOperatorName() const {
254 assert(isUnaryOp() || isBinaryOp());
255 return Name[Name.size()-1];
258 unsigned getBinaryPrecedence() const { return Precedence; }
260 Function *Codegen();
262 void CreateArgumentAllocas(Function *F);
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) {}
273 Function *Codegen();
276 //===----------------------------------------------------------------------===//
277 // Parser
278 //===----------------------------------------------------------------------===//
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();
288 /// BinopPrecedence - This holds the precedence for each binary operator that is
289 /// defined.
290 static std::map<char, int> BinopPrecedence;
292 /// GetTokPrecedence - Get the precedence of the pending binary operator token.
293 static int GetTokPrecedence() {
294 if (!isascii(CurTok))
295 return -1;
297 // Make sure it's a declared binop.
298 int TokPrec = BinopPrecedence[CurTok];
299 if (TokPrec <= 0) return -1;
300 return TokPrec;
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; }
308 static ExprAST *ParseExpression();
310 /// identifierexpr
311 /// ::= identifier
312 /// ::= identifier '(' expression* ')'
313 static ExprAST *ParseIdentifierExpr() {
314 std::string IdName = IdentifierStr;
316 getNextToken(); // eat identifier.
318 if (CurTok != '(') // Simple variable ref.
319 return new VariableExprAST(IdName);
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);
330 if (CurTok == ')') break;
332 if (CurTok != ',')
333 return Error("Expected ')' or ',' in argument list");
334 getNextToken();
338 // Eat the ')'.
339 getNextToken();
341 return new CallExprAST(IdName, Args);
344 /// numberexpr ::= number
345 static ExprAST *ParseNumberExpr() {
346 ExprAST *Result = new NumberExprAST(NumVal);
347 getNextToken(); // consume the number
348 return Result;
351 /// parenexpr ::= '(' expression ')'
352 static ExprAST *ParseParenExpr() {
353 getNextToken(); // eat (.
354 ExprAST *V = ParseExpression();
355 if (!V) return 0;
357 if (CurTok != ')')
358 return Error("expected ')'");
359 getNextToken(); // eat ).
360 return V;
363 /// ifexpr ::= 'if' expression 'then' expression 'else' expression
364 static ExprAST *ParseIfExpr() {
365 getNextToken(); // eat the if.
367 // condition.
368 ExprAST *Cond = ParseExpression();
369 if (!Cond) return 0;
371 if (CurTok != tok_then)
372 return Error("expected then");
373 getNextToken(); // eat the then
375 ExprAST *Then = ParseExpression();
376 if (Then == 0) return 0;
378 if (CurTok != tok_else)
379 return Error("expected else");
381 getNextToken();
383 ExprAST *Else = ParseExpression();
384 if (!Else) return 0;
386 return new IfExprAST(Cond, Then, Else);
389 /// forexpr ::= 'for' identifier '=' expr ',' expr (',' expr)? 'in' expression
390 static ExprAST *ParseForExpr() {
391 getNextToken(); // eat the for.
393 if (CurTok != tok_identifier)
394 return Error("expected identifier after for");
396 std::string IdName = IdentifierStr;
397 getNextToken(); // eat identifier.
399 if (CurTok != '=')
400 return Error("expected '=' after for");
401 getNextToken(); // eat '='.
404 ExprAST *Start = ParseExpression();
405 if (Start == 0) return 0;
406 if (CurTok != ',')
407 return Error("expected ',' after for start value");
408 getNextToken();
410 ExprAST *End = ParseExpression();
411 if (End == 0) return 0;
413 // The step value is optional.
414 ExprAST *Step = 0;
415 if (CurTok == ',') {
416 getNextToken();
417 Step = ParseExpression();
418 if (Step == 0) return 0;
421 if (CurTok != tok_in)
422 return Error("expected 'in' after for");
423 getNextToken(); // eat 'in'.
425 ExprAST *Body = ParseExpression();
426 if (Body == 0) return 0;
428 return new ForExprAST(IdName, Start, End, Step, Body);
431 /// varexpr ::= 'var' identifier ('=' expression)?
432 // (',' identifier ('=' expression)?)* 'in' expression
433 static ExprAST *ParseVarExpr() {
434 getNextToken(); // eat the var.
436 std::vector<std::pair<std::string, ExprAST*> > VarNames;
438 // At least one variable name is required.
439 if (CurTok != tok_identifier)
440 return Error("expected identifier after var");
442 while (1) {
443 std::string Name = IdentifierStr;
444 getNextToken(); // eat identifier.
446 // Read the optional initializer.
447 ExprAST *Init = 0;
448 if (CurTok == '=') {
449 getNextToken(); // eat the '='.
451 Init = ParseExpression();
452 if (Init == 0) return 0;
455 VarNames.push_back(std::make_pair(Name, Init));
457 // End of var list, exit loop.
458 if (CurTok != ',') break;
459 getNextToken(); // eat the ','.
461 if (CurTok != tok_identifier)
462 return Error("expected identifier list after var");
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'.
470 ExprAST *Body = ParseExpression();
471 if (Body == 0) return 0;
473 return new VarExprAST(VarNames, Body);
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();
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();
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;
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();
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;
523 // Okay, we know this is a binop.
524 int BinOp = CurTok;
525 getNextToken(); // eat binop
527 // Parse the unary expression after the binary operator.
528 ExprAST *RHS = ParseUnary();
529 if (!RHS) return 0;
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;
539 // Merge LHS/RHS.
540 LHS = new BinaryExprAST(BinOp, LHS, RHS);
544 /// expression
545 /// ::= unary binoprhs
547 static ExprAST *ParseExpression() {
548 ExprAST *LHS = ParseUnary();
549 if (!LHS) return 0;
551 return ParseBinOpRHS(0, LHS);
554 /// prototype
555 /// ::= id '(' id* ')'
556 /// ::= binary LETTER number? (id, id)
557 /// ::= unary LETTER (id)
558 static PrototypeAST *ParsePrototype() {
559 std::string FnName;
561 unsigned Kind = 0; // 0 = identifier, 1 = unary, 2 = binary.
562 unsigned BinaryPrecedence = 30;
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();
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();
597 break;
600 if (CurTok != '(')
601 return ErrorP("Expected '(' in prototype");
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");
609 // success.
610 getNextToken(); // eat ')'.
612 // Verify right number of names for operator.
613 if (Kind && ArgNames.size() != Kind)
614 return ErrorP("Invalid number of operands for operator");
616 return new PrototypeAST(FnName, ArgNames, Kind != 0, BinaryPrecedence);
619 /// definition ::= 'def' prototype expression
620 static FunctionAST *ParseDefinition() {
621 getNextToken(); // eat def.
622 PrototypeAST *Proto = ParsePrototype();
623 if (Proto == 0) return 0;
625 if (ExprAST *E = ParseExpression())
626 return new FunctionAST(Proto, E);
627 return 0;
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);
637 return 0;
640 /// external ::= 'extern' prototype
641 static PrototypeAST *ParseExtern() {
642 getNextToken(); // eat extern.
643 return ParsePrototype();
646 //===----------------------------------------------------------------------===//
647 // Quick and dirty hack
648 //===----------------------------------------------------------------------===//
650 // FIXME: Obviously we can do better than this
651 std::string GenerateUniqueName(const char *root)
653 static int i = 0;
654 char s[16];
655 sprintf(s, "%s%d", root, i++);
656 std::string S = s;
657 return S;
660 std::string MakeLegalFunctionName(std::string Name)
662 std::string NewName;
663 if (!Name.length())
664 return GenerateUniqueName("anon_func_");
666 // Start with what we have
667 NewName = Name;
669 // Look for a numberic first character
670 if (NewName.find_first_of("0123456789") == 0) {
671 NewName.insert(0, 1, 'n');
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);
684 return NewName;
687 //===----------------------------------------------------------------------===//
688 // MCJIT object cache class
689 //===----------------------------------------------------------------------===//
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");
699 virtual ~MCJITObjectCache() {
702 virtual void notifyObjectCompiled(const Module *M, const MemoryBuffer *Obj) {
703 // Get the ModuleID
704 const std::string ModuleID = M->getModuleIdentifier();
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;
715 std::string ErrStr;
716 raw_fd_ostream IRObjectFile(IRCacheFile.c_str(), ErrStr, raw_fd_ostream::F_Binary);
717 IRObjectFile << Obj->getBuffer();
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();
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;
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());
746 return NULL;
749 private:
750 SmallString<128> CacheDir;
753 //===----------------------------------------------------------------------===//
754 // IR input file handler
755 //===----------------------------------------------------------------------===//
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;
765 char ModID[256];
766 sprintf(ModID, "IR:%s", InputFile.c_str());
767 M->setModuleIdentifier(ModID);
768 return M;
771 //===----------------------------------------------------------------------===//
772 // Helper class for execution engine abstraction
773 //===----------------------------------------------------------------------===//
775 class BaseHelper
777 public:
778 BaseHelper() {}
779 virtual ~BaseHelper() {}
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();
790 //===----------------------------------------------------------------------===//
791 // MCJIT helper class
792 //===----------------------------------------------------------------------===//
794 class MCJITHelper : public BaseHelper
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);
805 ~MCJITHelper();
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();
815 protected:
816 ExecutionEngine *compileModule(Module *M);
818 private:
819 typedef std::vector<Module*> ModuleVector;
821 MCJITObjectCache OurObjectCache;
823 LLVMContext &Context;
824 ModuleVector Modules;
826 std::map<Module *, ExecutionEngine *> EngineMap;
828 Module *CurrentModule;
831 class HelpingMemoryManager : public SectionMemoryManager
833 HelpingMemoryManager(const HelpingMemoryManager&) = delete;
834 void operator=(const HelpingMemoryManager&) = delete;
836 public:
837 HelpingMemoryManager(MCJITHelper *Helper) : MasterHelper(Helper) {}
838 virtual ~HelpingMemoryManager() {}
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.
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;
854 void *HelpingMemoryManager::getPointerToNamedFunction(const std::string &Name,
855 bool AbortOnFailure)
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;
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;
869 MCJITHelper::~MCJITHelper()
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;
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;
897 assert(CurrentModule != NULL);
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;
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;
916 return NULL;
919 Module *MCJITHelper::getModuleForNewFunction() {
920 // If we have a Module that hasn't been JITed, use that.
921 if (CurrentModule)
922 return CurrentModule;
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;
930 return M;
933 ExecutionEngine *MCJITHelper::compileModule(Module *M) {
934 assert(EngineMap.find(M) == EngineMap.end());
936 if (M == CurrentModule)
937 closeCurrentModule();
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);
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();
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;
958 // Create a FPM for this module
959 FPM = new FunctionPassManager(M);
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());
977 FPM->doInitialization();
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);
987 delete FPM;
990 EE->finalizeObject();
992 // Store this engine
993 EngineMap[M] = EE;
995 return EE;
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;
1020 return NULL;
1023 void MCJITHelper::closeCurrentModule() {
1024 // If we have an open module (and we should), pack it up
1025 if (CurrentModule) {
1026 CurrentModule = NULL;
1030 void *MCJITHelper::getPointerToNamedFunction(const std::string &Name)
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;
1052 return NULL;
1055 void MCJITHelper::dump()
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();
1064 //===----------------------------------------------------------------------===//
1065 // Code Generation
1066 //===----------------------------------------------------------------------===//
1068 static BaseHelper *TheHelper;
1069 static LLVMContext TheContext;
1070 static IRBuilder<> Builder(TheContext);
1071 static std::map<std::string, AllocaInst*> NamedValues;
1073 Value *ErrorV(const char *Str) { Error(Str); return 0; }
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());
1084 Value *NumberExprAST::Codegen() {
1085 return ConstantFP::get(TheContext, APFloat(Val));
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");
1093 // Load the value.
1094 return Builder.CreateLoad(V, Name.c_str());
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");
1106 return Builder.CreateCall(F, OperandV, "unop");
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;
1123 // Look up the name.
1124 Value *Variable = NamedValues[LHSE->getName()];
1125 if (Variable == 0) return ErrorV("Unknown variable name");
1127 Builder.CreateStore(Val, Variable);
1128 return Val;
1131 Value *L = LHS->Codegen();
1132 Value *R = RHS->Codegen();
1133 if (L == 0 || R == 0) return 0;
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;
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!");
1153 Value *Ops[] = { L, R };
1154 return Builder.CreateCall(F, Ops, "binop");
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);
1166 // If argument mismatch error.
1167 if (CalleeF->arg_size() != Args.size())
1168 return ErrorV("Incorrect # arguments passed");
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;
1176 return Builder.CreateCall(CalleeF, ArgsV, "calltmp");
1179 Value *IfExprAST::Codegen() {
1180 Value *CondV = Cond->Codegen();
1181 if (CondV == 0) return 0;
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");
1187 Function *TheFunction = Builder.GetInsertBlock()->getParent();
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");
1195 Builder.CreateCondBr(CondV, ThenBB, ElseBB);
1197 // Emit then value.
1198 Builder.SetInsertPoint(ThenBB);
1200 Value *ThenV = Then->Codegen();
1201 if (ThenV == 0) return 0;
1203 Builder.CreateBr(MergeBB);
1204 // Codegen of 'Then' can change the current block, update ThenBB for the PHI.
1205 ThenBB = Builder.GetInsertBlock();
1207 // Emit else block.
1208 TheFunction->insert(TheFunction->end(), ElseBB);
1209 Builder.SetInsertPoint(ElseBB);
1211 Value *ElseV = Else->Codegen();
1212 if (ElseV == 0) return 0;
1214 Builder.CreateBr(MergeBB);
1215 // Codegen of 'Else' can change the current block, update ElseBB for the PHI.
1216 ElseBB = Builder.GetInsertBlock();
1218 // Emit merge block.
1219 TheFunction->insert(TheFunction->end(), MergeBB);
1220 Builder.SetInsertPoint(MergeBB);
1221 PHINode *PN = Builder.CreatePHI(Type::getDoubleTy(TheContext), 2, "iftmp");
1223 PN->addIncoming(ThenV, ThenBB);
1224 PN->addIncoming(ElseV, ElseBB);
1225 return PN;
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
1243 // curvar = load var
1244 // nextvar = curvar + step
1245 // store nextvar -> var
1246 // br endcond, loop, endloop
1247 // outloop:
1249 Function *TheFunction = Builder.GetInsertBlock()->getParent();
1251 // Create an alloca for the variable in the entry block.
1252 AllocaInst *Alloca = CreateEntryBlockAlloca(TheFunction, VarName);
1254 // Emit the start code first, without 'variable' in scope.
1255 Value *StartVal = Start->Codegen();
1256 if (StartVal == 0) return 0;
1258 // Store the value into the alloca.
1259 Builder.CreateStore(StartVal, Alloca);
1261 // Make the new basic block for the loop header, inserting after current
1262 // block.
1263 BasicBlock *LoopBB = BasicBlock::Create(TheContext, "loop", TheFunction);
1265 // Insert an explicit fall through from the current block to the LoopBB.
1266 Builder.CreateBr(LoopBB);
1268 // Start insertion in LoopBB.
1269 Builder.SetInsertPoint(LoopBB);
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;
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;
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));
1292 // Compute the end condition.
1293 Value *EndCond = End->Codegen();
1294 if (EndCond == 0) return EndCond;
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);
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");
1306 // Create the "after loop" block and insert it.
1307 BasicBlock *AfterBB =
1308 BasicBlock::Create(TheContext, "afterloop", TheFunction);
1310 // Insert the conditional branch into the end of LoopEndBB.
1311 Builder.CreateCondBr(EndCond, LoopBB, AfterBB);
1313 // Any new code will be inserted in AfterBB.
1314 Builder.SetInsertPoint(AfterBB);
1316 // Restore the unshadowed variable.
1317 if (OldVal)
1318 NamedValues[VarName] = OldVal;
1319 else
1320 NamedValues.erase(VarName);
1323 // for expr always returns 0.0.
1324 return Constant::getNullValue(Type::getDoubleTy(TheContext));
1327 Value *VarExprAST::Codegen() {
1328 std::vector<AllocaInst *> OldBindings;
1330 Function *TheFunction = Builder.GetInsertBlock()->getParent();
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;
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));
1350 AllocaInst *Alloca = CreateEntryBlockAlloca(TheFunction, VarName);
1351 Builder.CreateStore(InitVal, Alloca);
1353 // Remember the old variable binding so that we can restore the binding when
1354 // we unrecurse.
1355 OldBindings.push_back(NamedValues[VarName]);
1357 // Remember this binding.
1358 NamedValues[VarName] = Alloca;
1361 // Codegen the body, now that all vars are in scope.
1362 Value *BodyVal = Body->Codegen();
1363 if (BodyVal == 0) return 0;
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];
1369 // Return the body computation.
1370 return BodyVal;
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);
1379 std::string FnName;
1380 FnName = MakeLegalFunctionName(Name);
1382 Module* M = TheHelper->getModuleForNewFunction();
1383 Function *F = Function::Create(FT, Function::ExternalLinkage, FnName, M);
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;
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;
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]);
1411 return F;
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]);
1422 // Store the initial value into the alloca.
1423 Builder.CreateStore(AI, Alloca);
1425 // Add arguments to variable symbol table.
1426 NamedValues[Args[Idx]] = Alloca;
1430 Function *FunctionAST::Codegen() {
1431 NamedValues.clear();
1433 Function *TheFunction = Proto->Codegen();
1434 if (TheFunction == 0)
1435 return 0;
1437 // If this is an operator, install it.
1438 if (Proto->isBinaryOp())
1439 BinopPrecedence[Proto->getOperatorName()] = Proto->getBinaryPrecedence();
1441 // Create a new basic block to start insertion into.
1442 BasicBlock *BB = BasicBlock::Create(TheContext, "entry", TheFunction);
1443 Builder.SetInsertPoint(BB);
1445 // Add all arguments to the symbol table and create their allocas.
1446 Proto->CreateArgumentAllocas(TheFunction);
1448 if (Value *RetVal = Body->Codegen()) {
1449 // Finish off the function.
1450 Builder.CreateRet(RetVal);
1452 // Validate the generated code, checking for consistency.
1453 verifyFunction(*TheFunction);
1455 return TheFunction;
1458 // Error reading body, remove function.
1459 TheFunction->eraseFromParent();
1461 if (Proto->isBinaryOp())
1462 BinopPrecedence.erase(Proto->getOperatorName());
1463 return 0;
1466 //===----------------------------------------------------------------------===//
1467 // Top-Level parsing and JIT Driver
1468 //===----------------------------------------------------------------------===//
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");
1480 } else {
1481 // Skip token for error recovery.
1482 getNextToken();
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");
1494 } else {
1495 // Skip token for error recovery.
1496 getNextToken();
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);
1513 } else {
1514 // Skip token for error recovery.
1515 getNextToken();
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;
1534 //===----------------------------------------------------------------------===//
1535 // "Library" functions that can be "extern'd" from user code.
1536 //===----------------------------------------------------------------------===//
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;
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;
1552 extern "C"
1553 double printlf() {
1554 printf("\n");
1555 return 0;
1558 //===----------------------------------------------------------------------===//
1559 // Main driver code.
1560 //===----------------------------------------------------------------------===//
1562 int main(int argc, char **argv) {
1563 InitializeNativeTarget();
1564 InitializeNativeTargetAsmPrinter();
1565 InitializeNativeTargetAsmParser();
1566 LLVMContext &Context = TheContext;
1568 cl::ParseCommandLineOptions(argc, argv,
1569 "Kaleidoscope example program\n");
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.
1580 // Make the Helper, which holds all the code.
1581 TheHelper = new MCJITHelper(Context);
1583 // Prime the first token.
1584 if (!SuppressPrompts)
1585 fprintf(stderr, "ready> ");
1586 getNextToken();
1588 // Run the main "interpreter loop" now.
1589 MainLoop();
1591 // Print out all of the generated code.
1592 if (DumpModulesOnExit)
1593 TheHelper->dump();
1595 return 0;