1 #include "llvm/Analysis/Passes.h"
2 #include "llvm/ExecutionEngine/ExecutionEngine.h"
3 #include "llvm/ExecutionEngine/MCJIT.h"
4 #include "llvm/ExecutionEngine/SectionMemoryManager.h"
5 #include "llvm/IR/DataLayout.h"
6 #include "llvm/IR/DerivedTypes.h"
7 #include "llvm/IR/IRBuilder.h"
8 #include "llvm/IR/LLVMContext.h"
9 #include "llvm/IR/LegacyPassManager.h"
10 #include "llvm/IR/Module.h"
11 #include "llvm/IR/Verifier.h"
12 #include "llvm/Support/TargetSelect.h"
13 #include "llvm/Transforms/Scalar.h"
21 //===----------------------------------------------------------------------===//
23 //===----------------------------------------------------------------------===//
25 // The lexer returns tokens [0-255] if it is an unknown character, otherwise one
26 // of these for known things.
31 tok_def
= -2, tok_extern
= -3,
34 tok_identifier
= -4, tok_number
= -5,
37 tok_if
= -6, tok_then
= -7, tok_else
= -8,
38 tok_for
= -9, tok_in
= -10,
41 tok_binary
= -11, tok_unary
= -12,
47 static std::string IdentifierStr
; // Filled in if tok_identifier
48 static double NumVal
; // Filled in if tok_number
50 /// gettok - Return the next token from standard input.
52 static int LastChar
= ' ';
54 // Skip any whitespace.
55 while (isspace(LastChar
))
58 if (isalpha(LastChar
)) { // identifier: [a-zA-Z][a-zA-Z0-9]*
59 IdentifierStr
= LastChar
;
60 while (isalnum((LastChar
= getchar())))
61 IdentifierStr
+= LastChar
;
63 if (IdentifierStr
== "def") return tok_def
;
64 if (IdentifierStr
== "extern") return tok_extern
;
65 if (IdentifierStr
== "if") return tok_if
;
66 if (IdentifierStr
== "then") return tok_then
;
67 if (IdentifierStr
== "else") return tok_else
;
68 if (IdentifierStr
== "for") return tok_for
;
69 if (IdentifierStr
== "in") return tok_in
;
70 if (IdentifierStr
== "binary") return tok_binary
;
71 if (IdentifierStr
== "unary") return tok_unary
;
72 if (IdentifierStr
== "var") return tok_var
;
73 return tok_identifier
;
76 if (isdigit(LastChar
) || LastChar
== '.') { // Number: [0-9.]+
81 } while (isdigit(LastChar
) || LastChar
== '.');
83 NumVal
= strtod(NumStr
.c_str(), 0);
87 if (LastChar
== '#') {
88 // Comment until end of line.
89 do LastChar
= getchar();
90 while (LastChar
!= EOF
&& LastChar
!= '\n' && LastChar
!= '\r');
96 // Check for end of file. Don't eat the EOF.
100 // Otherwise, just return the character as its ascii value.
101 int ThisChar
= LastChar
;
102 LastChar
= getchar();
106 //===----------------------------------------------------------------------===//
107 // Abstract Syntax Tree (aka Parse Tree)
108 //===----------------------------------------------------------------------===//
110 /// ExprAST - Base class for all expression nodes.
113 virtual ~ExprAST() {}
114 virtual Value
*Codegen() = 0;
117 /// NumberExprAST - Expression class for numeric literals like "1.0".
118 class NumberExprAST
: public ExprAST
{
121 NumberExprAST(double val
) : Val(val
) {}
122 virtual Value
*Codegen();
125 /// VariableExprAST - Expression class for referencing a variable, like "a".
126 class VariableExprAST
: public ExprAST
{
129 VariableExprAST(const std::string
&name
) : Name(name
) {}
130 const std::string
&getName() const { return Name
; }
131 virtual Value
*Codegen();
134 /// UnaryExprAST - Expression class for a unary operator.
135 class UnaryExprAST
: public ExprAST
{
139 UnaryExprAST(char opcode
, ExprAST
*operand
)
140 : Opcode(opcode
), Operand(operand
) {}
141 virtual Value
*Codegen();
144 /// BinaryExprAST - Expression class for a binary operator.
145 class BinaryExprAST
: public ExprAST
{
149 BinaryExprAST(char op
, ExprAST
*lhs
, ExprAST
*rhs
)
150 : Op(op
), LHS(lhs
), RHS(rhs
) {}
151 virtual Value
*Codegen();
154 /// CallExprAST - Expression class for function calls.
155 class CallExprAST
: public ExprAST
{
157 std::vector
<ExprAST
*> Args
;
159 CallExprAST(const std::string
&callee
, std::vector
<ExprAST
*> &args
)
160 : Callee(callee
), Args(args
) {}
161 virtual Value
*Codegen();
164 /// IfExprAST - Expression class for if/then/else.
165 class IfExprAST
: public ExprAST
{
166 ExprAST
*Cond
, *Then
, *Else
;
168 IfExprAST(ExprAST
*cond
, ExprAST
*then
, ExprAST
*_else
)
169 : Cond(cond
), Then(then
), Else(_else
) {}
170 virtual Value
*Codegen();
173 /// ForExprAST - Expression class for for/in.
174 class ForExprAST
: public ExprAST
{
176 ExprAST
*Start
, *End
, *Step
, *Body
;
178 ForExprAST(const std::string
&varname
, ExprAST
*start
, ExprAST
*end
,
179 ExprAST
*step
, ExprAST
*body
)
180 : VarName(varname
), Start(start
), End(end
), Step(step
), Body(body
) {}
181 virtual Value
*Codegen();
184 /// VarExprAST - Expression class for var/in
185 class VarExprAST
: public ExprAST
{
186 std::vector
<std::pair
<std::string
, ExprAST
*> > VarNames
;
189 VarExprAST(const std::vector
<std::pair
<std::string
, ExprAST
*> > &varnames
,
191 : VarNames(varnames
), Body(body
) {}
193 virtual Value
*Codegen();
196 /// PrototypeAST - This class represents the "prototype" for a function,
197 /// which captures its argument names as well as if it is an operator.
200 std::vector
<std::string
> Args
;
202 unsigned Precedence
; // Precedence if a binary op.
204 PrototypeAST(const std::string
&name
, const std::vector
<std::string
> &args
,
205 bool isoperator
= false, unsigned prec
= 0)
206 : Name(name
), Args(args
), isOperator(isoperator
), Precedence(prec
) {}
208 bool isUnaryOp() const { return isOperator
&& Args
.size() == 1; }
209 bool isBinaryOp() const { return isOperator
&& Args
.size() == 2; }
211 char getOperatorName() const {
212 assert(isUnaryOp() || isBinaryOp());
213 return Name
[Name
.size()-1];
216 unsigned getBinaryPrecedence() const { return Precedence
; }
220 void CreateArgumentAllocas(Function
*F
);
223 /// FunctionAST - This class represents a function definition itself.
228 FunctionAST(PrototypeAST
*proto
, ExprAST
*body
)
229 : Proto(proto
), Body(body
) {}
234 //===----------------------------------------------------------------------===//
236 //===----------------------------------------------------------------------===//
238 /// CurTok/getNextToken - Provide a simple token buffer. CurTok is the current
239 /// token the parser is looking at. getNextToken reads another token from the
240 /// lexer and updates CurTok with its results.
242 static int getNextToken() {
243 return CurTok
= gettok();
246 /// BinopPrecedence - This holds the precedence for each binary operator that is
248 static std::map
<char, int> BinopPrecedence
;
250 /// GetTokPrecedence - Get the precedence of the pending binary operator token.
251 static int GetTokPrecedence() {
252 if (!isascii(CurTok
))
255 // Make sure it's a declared binop.
256 int TokPrec
= BinopPrecedence
[CurTok
];
257 if (TokPrec
<= 0) return -1;
261 /// Error* - These are little helper functions for error handling.
262 ExprAST
*Error(const char *Str
) { fprintf(stderr
, "Error: %s\n", Str
);return 0;}
263 PrototypeAST
*ErrorP(const char *Str
) { Error(Str
); return 0; }
264 FunctionAST
*ErrorF(const char *Str
) { Error(Str
); return 0; }
266 static ExprAST
*ParseExpression();
270 /// ::= identifier '(' expression* ')'
271 static ExprAST
*ParseIdentifierExpr() {
272 std::string IdName
= IdentifierStr
;
274 getNextToken(); // eat identifier.
276 if (CurTok
!= '(') // Simple variable ref.
277 return new VariableExprAST(IdName
);
280 getNextToken(); // eat (
281 std::vector
<ExprAST
*> Args
;
284 ExprAST
*Arg
= ParseExpression();
288 if (CurTok
== ')') break;
291 return Error("Expected ')' or ',' in argument list");
299 return new CallExprAST(IdName
, Args
);
302 /// numberexpr ::= number
303 static ExprAST
*ParseNumberExpr() {
304 ExprAST
*Result
= new NumberExprAST(NumVal
);
305 getNextToken(); // consume the number
309 /// parenexpr ::= '(' expression ')'
310 static ExprAST
*ParseParenExpr() {
311 getNextToken(); // eat (.
312 ExprAST
*V
= ParseExpression();
316 return Error("expected ')'");
317 getNextToken(); // eat ).
321 /// ifexpr ::= 'if' expression 'then' expression 'else' expression
322 static ExprAST
*ParseIfExpr() {
323 getNextToken(); // eat the if.
326 ExprAST
*Cond
= ParseExpression();
329 if (CurTok
!= tok_then
)
330 return Error("expected then");
331 getNextToken(); // eat the then
333 ExprAST
*Then
= ParseExpression();
334 if (Then
== 0) return 0;
336 if (CurTok
!= tok_else
)
337 return Error("expected else");
341 ExprAST
*Else
= ParseExpression();
344 return new IfExprAST(Cond
, Then
, Else
);
347 /// forexpr ::= 'for' identifier '=' expr ',' expr (',' expr)? 'in' expression
348 static ExprAST
*ParseForExpr() {
349 getNextToken(); // eat the for.
351 if (CurTok
!= tok_identifier
)
352 return Error("expected identifier after for");
354 std::string IdName
= IdentifierStr
;
355 getNextToken(); // eat identifier.
358 return Error("expected '=' after for");
359 getNextToken(); // eat '='.
362 ExprAST
*Start
= ParseExpression();
363 if (Start
== 0) return 0;
365 return Error("expected ',' after for start value");
368 ExprAST
*End
= ParseExpression();
369 if (End
== 0) return 0;
371 // The step value is optional.
375 Step
= ParseExpression();
376 if (Step
== 0) return 0;
379 if (CurTok
!= tok_in
)
380 return Error("expected 'in' after for");
381 getNextToken(); // eat 'in'.
383 ExprAST
*Body
= ParseExpression();
384 if (Body
== 0) return 0;
386 return new ForExprAST(IdName
, Start
, End
, Step
, Body
);
389 /// varexpr ::= 'var' identifier ('=' expression)?
390 // (',' identifier ('=' expression)?)* 'in' expression
391 static ExprAST
*ParseVarExpr() {
392 getNextToken(); // eat the var.
394 std::vector
<std::pair
<std::string
, ExprAST
*> > VarNames
;
396 // At least one variable name is required.
397 if (CurTok
!= tok_identifier
)
398 return Error("expected identifier after var");
401 std::string Name
= IdentifierStr
;
402 getNextToken(); // eat identifier.
404 // Read the optional initializer.
407 getNextToken(); // eat the '='.
409 Init
= ParseExpression();
410 if (Init
== 0) return 0;
413 VarNames
.push_back(std::make_pair(Name
, Init
));
415 // End of var list, exit loop.
416 if (CurTok
!= ',') break;
417 getNextToken(); // eat the ','.
419 if (CurTok
!= tok_identifier
)
420 return Error("expected identifier list after var");
423 // At this point, we have to have 'in'.
424 if (CurTok
!= tok_in
)
425 return Error("expected 'in' keyword after 'var'");
426 getNextToken(); // eat 'in'.
428 ExprAST
*Body
= ParseExpression();
429 if (Body
== 0) return 0;
431 return new VarExprAST(VarNames
, Body
);
435 /// ::= identifierexpr
441 static ExprAST
*ParsePrimary() {
443 default: return Error("unknown token when expecting an expression");
444 case tok_identifier
: return ParseIdentifierExpr();
445 case tok_number
: return ParseNumberExpr();
446 case '(': return ParseParenExpr();
447 case tok_if
: return ParseIfExpr();
448 case tok_for
: return ParseForExpr();
449 case tok_var
: return ParseVarExpr();
456 static ExprAST
*ParseUnary() {
457 // If the current token is not an operator, it must be a primary expr.
458 if (!isascii(CurTok
) || CurTok
== '(' || CurTok
== ',')
459 return ParsePrimary();
461 // If this is a unary operator, read it.
464 if (ExprAST
*Operand
= ParseUnary())
465 return new UnaryExprAST(Opc
, Operand
);
471 static ExprAST
*ParseBinOpRHS(int ExprPrec
, ExprAST
*LHS
) {
472 // If this is a binop, find its precedence.
474 int TokPrec
= GetTokPrecedence();
476 // If this is a binop that binds at least as tightly as the current binop,
477 // consume it, otherwise we are done.
478 if (TokPrec
< ExprPrec
)
481 // Okay, we know this is a binop.
483 getNextToken(); // eat binop
485 // Parse the unary expression after the binary operator.
486 ExprAST
*RHS
= ParseUnary();
489 // If BinOp binds less tightly with RHS than the operator after RHS, let
490 // the pending operator take RHS as its LHS.
491 int NextPrec
= GetTokPrecedence();
492 if (TokPrec
< NextPrec
) {
493 RHS
= ParseBinOpRHS(TokPrec
+1, RHS
);
494 if (RHS
== 0) return 0;
498 LHS
= new BinaryExprAST(BinOp
, LHS
, RHS
);
503 /// ::= unary binoprhs
505 static ExprAST
*ParseExpression() {
506 ExprAST
*LHS
= ParseUnary();
509 return ParseBinOpRHS(0, LHS
);
513 /// ::= id '(' id* ')'
514 /// ::= binary LETTER number? (id, id)
515 /// ::= unary LETTER (id)
516 static PrototypeAST
*ParsePrototype() {
519 unsigned Kind
= 0; // 0 = identifier, 1 = unary, 2 = binary.
520 unsigned BinaryPrecedence
= 30;
524 return ErrorP("Expected function name in prototype");
526 FnName
= IdentifierStr
;
532 if (!isascii(CurTok
))
533 return ErrorP("Expected unary operator");
535 FnName
+= (char)CurTok
;
541 if (!isascii(CurTok
))
542 return ErrorP("Expected binary operator");
544 FnName
+= (char)CurTok
;
548 // Read the precedence if present.
549 if (CurTok
== tok_number
) {
550 if (NumVal
< 1 || NumVal
> 100)
551 return ErrorP("Invalid precedecnce: must be 1..100");
552 BinaryPrecedence
= (unsigned)NumVal
;
559 return ErrorP("Expected '(' in prototype");
561 std::vector
<std::string
> ArgNames
;
562 while (getNextToken() == tok_identifier
)
563 ArgNames
.push_back(IdentifierStr
);
565 return ErrorP("Expected ')' in prototype");
568 getNextToken(); // eat ')'.
570 // Verify right number of names for operator.
571 if (Kind
&& ArgNames
.size() != Kind
)
572 return ErrorP("Invalid number of operands for operator");
574 return new PrototypeAST(FnName
, ArgNames
, Kind
!= 0, BinaryPrecedence
);
577 /// definition ::= 'def' prototype expression
578 static FunctionAST
*ParseDefinition() {
579 getNextToken(); // eat def.
580 PrototypeAST
*Proto
= ParsePrototype();
581 if (Proto
== 0) return 0;
583 if (ExprAST
*E
= ParseExpression())
584 return new FunctionAST(Proto
, E
);
588 /// toplevelexpr ::= expression
589 static FunctionAST
*ParseTopLevelExpr() {
590 if (ExprAST
*E
= ParseExpression()) {
591 // Make an anonymous proto.
592 PrototypeAST
*Proto
= new PrototypeAST("", std::vector
<std::string
>());
593 return new FunctionAST(Proto
, E
);
598 /// external ::= 'extern' prototype
599 static PrototypeAST
*ParseExtern() {
600 getNextToken(); // eat extern.
601 return ParsePrototype();
604 //===----------------------------------------------------------------------===//
605 // Quick and dirty hack
606 //===----------------------------------------------------------------------===//
608 // FIXME: Obviously we can do better than this
609 std::string
GenerateUniqueName(const char *root
)
613 sprintf(s
, "%s%d", root
, i
++);
618 std::string
MakeLegalFunctionName(std::string Name
)
622 return GenerateUniqueName("anon_func_");
624 // Start with what we have
627 // Look for a numberic first character
628 if (NewName
.find_first_of("0123456789") == 0) {
629 NewName
.insert(0, 1, 'n');
632 // Replace illegal characters with their ASCII equivalent
633 std::string legal_elements
= "_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
635 while ((pos
= NewName
.find_first_not_of(legal_elements
)) != std::string::npos
) {
636 char old_c
= NewName
.at(pos
);
638 sprintf(new_str
, "%d", (int)old_c
);
639 NewName
= NewName
.replace(pos
, 1, new_str
);
645 //===----------------------------------------------------------------------===//
646 // MCJIT helper class
647 //===----------------------------------------------------------------------===//
652 MCJITHelper(LLVMContext
& C
) : Context(C
), OpenModule(NULL
) {}
655 Function
*getFunction(const std::string FnName
);
656 Module
*getModuleForNewFunction();
657 void *getPointerToFunction(Function
* F
);
658 void *getPointerToNamedFunction(const std::string
&Name
);
662 typedef std::vector
<Module
*> ModuleVector
;
663 typedef std::vector
<ExecutionEngine
*> EngineVector
;
665 LLVMContext
&Context
;
667 ModuleVector Modules
;
668 EngineVector Engines
;
671 class HelpingMemoryManager
: public SectionMemoryManager
673 HelpingMemoryManager(const HelpingMemoryManager
&) = delete;
674 void operator=(const HelpingMemoryManager
&) = delete;
677 HelpingMemoryManager(MCJITHelper
*Helper
) : MasterHelper(Helper
) {}
678 virtual ~HelpingMemoryManager() {}
680 /// This method returns the address of the specified function.
681 /// Our implementation will attempt to find functions in other
682 /// modules associated with the MCJITHelper to cross link functions
683 /// from one generated module to another.
685 /// If \p AbortOnFailure is false and no function with the given name is
686 /// found, this function returns a null pointer. Otherwise, it prints a
687 /// message to stderr and aborts.
688 virtual void *getPointerToNamedFunction(const std::string
&Name
,
689 bool AbortOnFailure
= true);
691 MCJITHelper
*MasterHelper
;
694 void *HelpingMemoryManager::getPointerToNamedFunction(const std::string
&Name
,
697 // Try the standard symbol resolution first, but ask it not to abort.
698 void *pfn
= SectionMemoryManager::getPointerToNamedFunction(Name
, false);
702 pfn
= MasterHelper
->getPointerToNamedFunction(Name
);
703 if (!pfn
&& AbortOnFailure
)
704 report_fatal_error("Program used external function '" + Name
+
705 "' which could not be resolved!");
709 MCJITHelper::~MCJITHelper()
713 EngineVector::iterator begin
= Engines
.begin();
714 EngineVector::iterator end
= Engines
.end();
715 EngineVector::iterator it
;
716 for (it
= begin
; it
!= end
; ++it
)
720 Function
*MCJITHelper::getFunction(const std::string FnName
) {
721 ModuleVector::iterator begin
= Modules
.begin();
722 ModuleVector::iterator end
= Modules
.end();
723 ModuleVector::iterator it
;
724 for (it
= begin
; it
!= end
; ++it
) {
725 Function
*F
= (*it
)->getFunction(FnName
);
727 if (*it
== OpenModule
)
730 assert(OpenModule
!= NULL
);
732 // This function is in a module that has already been JITed.
733 // We need to generate a new prototype for external linkage.
734 Function
*PF
= OpenModule
->getFunction(FnName
);
735 if (PF
&& !PF
->empty()) {
736 ErrorF("redefinition of function across modules");
740 // If we don't have a prototype yet, create one.
742 PF
= Function::Create(F
->getFunctionType(),
743 Function::ExternalLinkage
,
752 Module
*MCJITHelper::getModuleForNewFunction() {
753 // If we have a Module that hasn't been JITed, use that.
757 // Otherwise create a new Module.
758 std::string ModName
= GenerateUniqueName("mcjit_module_");
759 Module
*M
= new Module(ModName
, Context
);
760 Modules
.push_back(M
);
765 void *MCJITHelper::getPointerToFunction(Function
* F
) {
766 // See if an existing instance of MCJIT has this function.
767 EngineVector::iterator begin
= Engines
.begin();
768 EngineVector::iterator end
= Engines
.end();
769 EngineVector::iterator it
;
770 for (it
= begin
; it
!= end
; ++it
) {
771 void *P
= (*it
)->getPointerToFunction(F
);
776 // If we didn't find the function, see if we can generate it.
779 ExecutionEngine
*NewEngine
= EngineBuilder(OpenModule
)
780 .setErrorStr(&ErrStr
)
781 .setMCJITMemoryManager(new HelpingMemoryManager(this))
784 fprintf(stderr
, "Could not create ExecutionEngine: %s\n", ErrStr
.c_str());
788 // Create a function pass manager for this engine
789 FunctionPassManager
*FPM
= new FunctionPassManager(OpenModule
);
791 // Set up the optimizer pipeline. Start with registering info about how the
792 // target lays out data structures.
793 FPM
->add(new DataLayout(*NewEngine
->getDataLayout()));
794 // Provide basic AliasAnalysis support for GVN.
795 FPM
->add(createBasicAliasAnalysisPass());
796 // Promote allocas to registers.
797 FPM
->add(createPromoteMemoryToRegisterPass());
798 // Do simple "peephole" optimizations and bit-twiddling optzns.
799 FPM
->add(createInstructionCombiningPass());
800 // Reassociate expressions.
801 FPM
->add(createReassociatePass());
802 // Eliminate Common SubExpressions.
803 FPM
->add(createGVNPass());
804 // Simplify the control flow graph (deleting unreachable blocks, etc).
805 FPM
->add(createCFGSimplificationPass());
806 FPM
->doInitialization();
808 // For each function in the module
810 Module::iterator end
= OpenModule
->end();
811 for (it
= OpenModule
->begin(); it
!= end
; ++it
) {
812 // Run the FPM on this function
816 // We don't need this anymore
820 Engines
.push_back(NewEngine
);
821 NewEngine
->finalizeObject();
822 return NewEngine
->getPointerToFunction(F
);
827 void *MCJITHelper::getPointerToNamedFunction(const std::string
&Name
)
829 // Look for the function in each of our execution engines.
830 EngineVector::iterator begin
= Engines
.begin();
831 EngineVector::iterator end
= Engines
.end();
832 EngineVector::iterator it
;
833 for (it
= begin
; it
!= end
; ++it
) {
834 if (Function
*F
= (*it
)->FindFunctionNamed(Name
.c_str()))
835 return (*it
)->getPointerToFunction(F
);
841 void MCJITHelper::dump()
843 ModuleVector::iterator begin
= Modules
.begin();
844 ModuleVector::iterator end
= Modules
.end();
845 ModuleVector::iterator it
;
846 for (it
= begin
; it
!= end
; ++it
)
850 //===----------------------------------------------------------------------===//
852 //===----------------------------------------------------------------------===//
854 static MCJITHelper
*TheHelper
;
855 static LLVMContext TheContext
;
856 static IRBuilder
<> Builder(TheContext
);
857 static std::map
<std::string
, AllocaInst
*> NamedValues
;
859 Value
*ErrorV(const char *Str
) { Error(Str
); return 0; }
861 /// CreateEntryBlockAlloca - Create an alloca instruction in the entry block of
862 /// the function. This is used for mutable variables etc.
863 static AllocaInst
*CreateEntryBlockAlloca(Function
*TheFunction
,
864 const std::string
&VarName
) {
865 IRBuilder
<> TmpB(&TheFunction
->getEntryBlock(),
866 TheFunction
->getEntryBlock().begin());
867 return TmpB
.CreateAlloca(Type::getDoubleTy(TheContext
), 0, VarName
.c_str());
870 Value
*NumberExprAST::Codegen() {
871 return ConstantFP::get(TheContext
, APFloat(Val
));
874 Value
*VariableExprAST::Codegen() {
875 // Look this variable up in the function.
876 Value
*V
= NamedValues
[Name
];
878 sprintf(ErrStr
, "Unknown variable name %s", Name
.c_str());
879 if (V
== 0) return ErrorV(ErrStr
);
882 return Builder
.CreateLoad(V
, Name
.c_str());
885 Value
*UnaryExprAST::Codegen() {
886 Value
*OperandV
= Operand
->Codegen();
887 if (OperandV
== 0) return 0;
889 Function
*F
= TheHelper
->getFunction(MakeLegalFunctionName(std::string("unary")+Opcode
));
891 return ErrorV("Unknown unary operator");
893 return Builder
.CreateCall(F
, OperandV
, "unop");
896 Value
*BinaryExprAST::Codegen() {
897 // Special case '=' because we don't want to emit the LHS as an expression.
899 // Assignment requires the LHS to be an identifier.
900 VariableExprAST
*LHSE
= static_cast<VariableExprAST
*>(LHS
);
902 return ErrorV("destination of '=' must be a variable");
904 Value
*Val
= RHS
->Codegen();
905 if (Val
== 0) return 0;
908 Value
*Variable
= NamedValues
[LHSE
->getName()];
909 if (Variable
== 0) return ErrorV("Unknown variable name");
911 Builder
.CreateStore(Val
, Variable
);
915 Value
*L
= LHS
->Codegen();
916 Value
*R
= RHS
->Codegen();
917 if (L
== 0 || R
== 0) return 0;
920 case '+': return Builder
.CreateFAdd(L
, R
, "addtmp");
921 case '-': return Builder
.CreateFSub(L
, R
, "subtmp");
922 case '*': return Builder
.CreateFMul(L
, R
, "multmp");
923 case '/': return Builder
.CreateFDiv(L
, R
, "divtmp");
925 L
= Builder
.CreateFCmpULT(L
, R
, "cmptmp");
926 // Convert bool 0/1 to double 0.0 or 1.0
927 return Builder
.CreateUIToFP(L
, Type::getDoubleTy(TheContext
), "booltmp");
931 // If it wasn't a builtin binary operator, it must be a user defined one. Emit
933 Function
*F
= TheHelper
->getFunction(MakeLegalFunctionName(std::string("binary")+Op
));
934 assert(F
&& "binary operator not found!");
936 Value
*Ops
[] = { L
, R
};
937 return Builder
.CreateCall(F
, Ops
, "binop");
940 Value
*CallExprAST::Codegen() {
941 // Look up the name in the global module table.
942 Function
*CalleeF
= TheHelper
->getFunction(Callee
);
944 return ErrorV("Unknown function referenced");
946 // If argument mismatch error.
947 if (CalleeF
->arg_size() != Args
.size())
948 return ErrorV("Incorrect # arguments passed");
950 std::vector
<Value
*> ArgsV
;
951 for (unsigned i
= 0, e
= Args
.size(); i
!= e
; ++i
) {
952 ArgsV
.push_back(Args
[i
]->Codegen());
953 if (ArgsV
.back() == 0) return 0;
956 return Builder
.CreateCall(CalleeF
, ArgsV
, "calltmp");
959 Value
*IfExprAST::Codegen() {
960 Value
*CondV
= Cond
->Codegen();
961 if (CondV
== 0) return 0;
963 // Convert condition to a bool by comparing equal to 0.0.
964 CondV
= Builder
.CreateFCmpONE(
965 CondV
, ConstantFP::get(TheContext
, APFloat(0.0)), "ifcond");
967 Function
*TheFunction
= Builder
.GetInsertBlock()->getParent();
969 // Create blocks for the then and else cases. Insert the 'then' block at the
970 // end of the function.
971 BasicBlock
*ThenBB
= BasicBlock::Create(TheContext
, "then", TheFunction
);
972 BasicBlock
*ElseBB
= BasicBlock::Create(TheContext
, "else");
973 BasicBlock
*MergeBB
= BasicBlock::Create(TheContext
, "ifcont");
975 Builder
.CreateCondBr(CondV
, ThenBB
, ElseBB
);
978 Builder
.SetInsertPoint(ThenBB
);
980 Value
*ThenV
= Then
->Codegen();
981 if (ThenV
== 0) return 0;
983 Builder
.CreateBr(MergeBB
);
984 // Codegen of 'Then' can change the current block, update ThenBB for the PHI.
985 ThenBB
= Builder
.GetInsertBlock();
988 TheFunction
->insert(TheFunction
->end(), ElseBB
);
989 Builder
.SetInsertPoint(ElseBB
);
991 Value
*ElseV
= Else
->Codegen();
992 if (ElseV
== 0) return 0;
994 Builder
.CreateBr(MergeBB
);
995 // Codegen of 'Else' can change the current block, update ElseBB for the PHI.
996 ElseBB
= Builder
.GetInsertBlock();
999 TheFunction
->insert(TheFunction
->end(), MergeBB
);
1000 Builder
.SetInsertPoint(MergeBB
);
1001 PHINode
*PN
= Builder
.CreatePHI(Type::getDoubleTy(TheContext
), 2, "iftmp");
1003 PN
->addIncoming(ThenV
, ThenBB
);
1004 PN
->addIncoming(ElseV
, ElseBB
);
1008 Value
*ForExprAST::Codegen() {
1010 // var = alloca double
1012 // start = startexpr
1013 // store start -> var
1021 // endcond = endexpr
1023 // curvar = load var
1024 // nextvar = curvar + step
1025 // store nextvar -> var
1026 // br endcond, loop, endloop
1029 Function
*TheFunction
= Builder
.GetInsertBlock()->getParent();
1031 // Create an alloca for the variable in the entry block.
1032 AllocaInst
*Alloca
= CreateEntryBlockAlloca(TheFunction
, VarName
);
1034 // Emit the start code first, without 'variable' in scope.
1035 Value
*StartVal
= Start
->Codegen();
1036 if (StartVal
== 0) return 0;
1038 // Store the value into the alloca.
1039 Builder
.CreateStore(StartVal
, Alloca
);
1041 // Make the new basic block for the loop header, inserting after current
1043 BasicBlock
*LoopBB
= BasicBlock::Create(TheContext
, "loop", TheFunction
);
1045 // Insert an explicit fall through from the current block to the LoopBB.
1046 Builder
.CreateBr(LoopBB
);
1048 // Start insertion in LoopBB.
1049 Builder
.SetInsertPoint(LoopBB
);
1051 // Within the loop, the variable is defined equal to the PHI node. If it
1052 // shadows an existing variable, we have to restore it, so save it now.
1053 AllocaInst
*OldVal
= NamedValues
[VarName
];
1054 NamedValues
[VarName
] = Alloca
;
1056 // Emit the body of the loop. This, like any other expr, can change the
1057 // current BB. Note that we ignore the value computed by the body, but don't
1059 if (Body
->Codegen() == 0)
1062 // Emit the step value.
1065 StepVal
= Step
->Codegen();
1066 if (StepVal
== 0) return 0;
1068 // If not specified, use 1.0.
1069 StepVal
= ConstantFP::get(TheContext
, APFloat(1.0));
1072 // Compute the end condition.
1073 Value
*EndCond
= End
->Codegen();
1074 if (EndCond
== 0) return EndCond
;
1076 // Reload, increment, and restore the alloca. This handles the case where
1077 // the body of the loop mutates the variable.
1078 Value
*CurVar
= Builder
.CreateLoad(Alloca
, VarName
.c_str());
1079 Value
*NextVar
= Builder
.CreateFAdd(CurVar
, StepVal
, "nextvar");
1080 Builder
.CreateStore(NextVar
, Alloca
);
1082 // Convert condition to a bool by comparing equal to 0.0.
1083 EndCond
= Builder
.CreateFCmpONE(
1084 EndCond
, ConstantFP::get(TheContext
, APFloat(0.0)), "loopcond");
1086 // Create the "after loop" block and insert it.
1087 BasicBlock
*AfterBB
=
1088 BasicBlock::Create(TheContext
, "afterloop", TheFunction
);
1090 // Insert the conditional branch into the end of LoopEndBB.
1091 Builder
.CreateCondBr(EndCond
, LoopBB
, AfterBB
);
1093 // Any new code will be inserted in AfterBB.
1094 Builder
.SetInsertPoint(AfterBB
);
1096 // Restore the unshadowed variable.
1098 NamedValues
[VarName
] = OldVal
;
1100 NamedValues
.erase(VarName
);
1103 // for expr always returns 0.0.
1104 return Constant::getNullValue(Type::getDoubleTy(TheContext
));
1107 Value
*VarExprAST::Codegen() {
1108 std::vector
<AllocaInst
*> OldBindings
;
1110 Function
*TheFunction
= Builder
.GetInsertBlock()->getParent();
1112 // Register all variables and emit their initializer.
1113 for (unsigned i
= 0, e
= VarNames
.size(); i
!= e
; ++i
) {
1114 const std::string
&VarName
= VarNames
[i
].first
;
1115 ExprAST
*Init
= VarNames
[i
].second
;
1117 // Emit the initializer before adding the variable to scope, this prevents
1118 // the initializer from referencing the variable itself, and permits stuff
1121 // var a = a in ... # refers to outer 'a'.
1124 InitVal
= Init
->Codegen();
1125 if (InitVal
== 0) return 0;
1126 } else { // If not specified, use 0.0.
1127 InitVal
= ConstantFP::get(TheContext
, APFloat(0.0));
1130 AllocaInst
*Alloca
= CreateEntryBlockAlloca(TheFunction
, VarName
);
1131 Builder
.CreateStore(InitVal
, Alloca
);
1133 // Remember the old variable binding so that we can restore the binding when
1135 OldBindings
.push_back(NamedValues
[VarName
]);
1137 // Remember this binding.
1138 NamedValues
[VarName
] = Alloca
;
1141 // Codegen the body, now that all vars are in scope.
1142 Value
*BodyVal
= Body
->Codegen();
1143 if (BodyVal
== 0) return 0;
1145 // Pop all our variables from scope.
1146 for (unsigned i
= 0, e
= VarNames
.size(); i
!= e
; ++i
)
1147 NamedValues
[VarNames
[i
].first
] = OldBindings
[i
];
1149 // Return the body computation.
1153 Function
*PrototypeAST::Codegen() {
1154 // Make the function type: double(double,double) etc.
1155 std::vector
<Type
*> Doubles(Args
.size(), Type::getDoubleTy(TheContext
));
1157 FunctionType::get(Type::getDoubleTy(TheContext
), Doubles
, false);
1159 std::string FnName
= MakeLegalFunctionName(Name
);
1161 Module
* M
= TheHelper
->getModuleForNewFunction();
1163 Function
*F
= Function::Create(FT
, Function::ExternalLinkage
, FnName
, M
);
1165 // If F conflicted, there was already something named 'FnName'. If it has a
1166 // body, don't allow redefinition or reextern.
1167 if (F
->getName() != FnName
) {
1168 // Delete the one we just made and get the existing one.
1169 F
->eraseFromParent();
1170 F
= M
->getFunction(Name
);
1172 // If F already has a body, reject this.
1174 ErrorF("redefinition of function");
1178 // If F took a different number of args, reject.
1179 if (F
->arg_size() != Args
.size()) {
1180 ErrorF("redefinition of function with different # args");
1185 // Set names for all arguments.
1187 for (Function::arg_iterator AI
= F
->arg_begin(); Idx
!= Args
.size();
1189 AI
->setName(Args
[Idx
]);
1194 /// CreateArgumentAllocas - Create an alloca for each argument and register the
1195 /// argument in the symbol table so that references to it will succeed.
1196 void PrototypeAST::CreateArgumentAllocas(Function
*F
) {
1197 Function::arg_iterator AI
= F
->arg_begin();
1198 for (unsigned Idx
= 0, e
= Args
.size(); Idx
!= e
; ++Idx
, ++AI
) {
1199 // Create an alloca for this variable.
1200 AllocaInst
*Alloca
= CreateEntryBlockAlloca(F
, Args
[Idx
]);
1202 // Store the initial value into the alloca.
1203 Builder
.CreateStore(AI
, Alloca
);
1205 // Add arguments to variable symbol table.
1206 NamedValues
[Args
[Idx
]] = Alloca
;
1210 Function
*FunctionAST::Codegen() {
1211 NamedValues
.clear();
1213 Function
*TheFunction
= Proto
->Codegen();
1214 if (TheFunction
== 0)
1217 // If this is an operator, install it.
1218 if (Proto
->isBinaryOp())
1219 BinopPrecedence
[Proto
->getOperatorName()] = Proto
->getBinaryPrecedence();
1221 // Create a new basic block to start insertion into.
1222 BasicBlock
*BB
= BasicBlock::Create(TheContext
, "entry", TheFunction
);
1223 Builder
.SetInsertPoint(BB
);
1225 // Add all arguments to the symbol table and create their allocas.
1226 Proto
->CreateArgumentAllocas(TheFunction
);
1228 if (Value
*RetVal
= Body
->Codegen()) {
1229 // Finish off the function.
1230 Builder
.CreateRet(RetVal
);
1232 // Validate the generated code, checking for consistency.
1233 verifyFunction(*TheFunction
);
1238 // Error reading body, remove function.
1239 TheFunction
->eraseFromParent();
1241 if (Proto
->isBinaryOp())
1242 BinopPrecedence
.erase(Proto
->getOperatorName());
1246 //===----------------------------------------------------------------------===//
1247 // Top-Level parsing and JIT Driver
1248 //===----------------------------------------------------------------------===//
1250 static void HandleDefinition() {
1251 if (FunctionAST
*F
= ParseDefinition()) {
1252 if (Function
*LF
= F
->Codegen()) {
1253 #ifndef MINIMAL_STDERR_OUTPUT
1254 fprintf(stderr
, "Read function definition:");
1256 fprintf(stderr
, "\n");
1260 // Skip token for error recovery.
1265 static void HandleExtern() {
1266 if (PrototypeAST
*P
= ParseExtern()) {
1267 if (Function
*F
= P
->Codegen()) {
1268 #ifndef MINIMAL_STDERR_OUTPUT
1269 fprintf(stderr
, "Read extern: ");
1271 fprintf(stderr
, "\n");
1275 // Skip token for error recovery.
1280 static void HandleTopLevelExpression() {
1281 // Evaluate a top-level expression into an anonymous function.
1282 if (FunctionAST
*F
= ParseTopLevelExpr()) {
1283 if (Function
*LF
= F
->Codegen()) {
1284 // JIT the function, returning a function pointer.
1285 void *FPtr
= TheHelper
->getPointerToFunction(LF
);
1287 // Cast it to the right type (takes no arguments, returns a double) so we
1288 // can call it as a native function.
1289 double (*FP
)() = (double (*)())(intptr_t)FPtr
;
1290 #ifdef MINIMAL_STDERR_OUTPUT
1293 fprintf(stderr
, "Evaluated to %f\n", FP());
1297 // Skip token for error recovery.
1302 /// top ::= definition | external | expression | ';'
1303 static void MainLoop() {
1305 #ifndef MINIMAL_STDERR_OUTPUT
1306 fprintf(stderr
, "ready> ");
1309 case tok_eof
: return;
1310 case ';': getNextToken(); break; // ignore top-level semicolons.
1311 case tok_def
: HandleDefinition(); break;
1312 case tok_extern
: HandleExtern(); break;
1313 default: HandleTopLevelExpression(); break;
1318 //===----------------------------------------------------------------------===//
1319 // "Library" functions that can be "extern'd" from user code.
1320 //===----------------------------------------------------------------------===//
1322 /// putchard - putchar that takes a double and returns 0.
1324 double putchard(double X
) {
1329 /// printd - printf that takes a double prints it as "%f\n", returning 0.
1331 double printd(double X
) {
1342 //===----------------------------------------------------------------------===//
1343 // Main driver code.
1344 //===----------------------------------------------------------------------===//
1347 InitializeNativeTarget();
1348 InitializeNativeTargetAsmPrinter();
1349 InitializeNativeTargetAsmParser();
1350 LLVMContext
&Context
= TheContext
;
1352 // Install standard binary operators.
1353 // 1 is lowest precedence.
1354 BinopPrecedence
['='] = 2;
1355 BinopPrecedence
['<'] = 10;
1356 BinopPrecedence
['+'] = 20;
1357 BinopPrecedence
['-'] = 20;
1358 BinopPrecedence
['/'] = 40;
1359 BinopPrecedence
['*'] = 40; // highest.
1361 // Prime the first token.
1362 #ifndef MINIMAL_STDERR_OUTPUT
1363 fprintf(stderr
, "ready> ");
1367 // Make the helper, which holds all the code.
1368 TheHelper
= new MCJITHelper(Context
);
1370 // Run the main "interpreter loop" now.
1373 #ifndef MINIMAL_STDERR_OUTPUT
1374 // Print out all of the generated code.
1375 TheHelper
->print(errs());