1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
2 "http://www.w3.org/TR/html4/strict.dtd">
6 <title>Kaleidoscope: Adding JIT and Optimizer Support</title>
7 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
8 <meta name="author" content="Chris Lattner">
9 <link rel="stylesheet" href="../llvm.css" type="text/css">
14 <div class="doc_title">Kaleidoscope: Adding JIT and Optimizer Support</div>
16 <div class="doc_author">
17 <p>Written by <a href="mailto:sabre@nondot.org">Chris Lattner</a></p>
20 <!-- *********************************************************************** -->
21 <div class="doc_section"><a name="intro">Part 4 Introduction</a></div>
22 <!-- *********************************************************************** -->
24 <div class="doc_text">
26 <p>Welcome to part 4 of the "<a href="index.html">Implementing a language with
27 LLVM</a>" tutorial. Parts 1-3 described the implementation of a simple language
28 and included support for generating LLVM IR. This chapter describes two new
29 techniques: adding optimizer support to your language, and adding JIT compiler
30 support. This shows how to get nice efficient code for your language.</p>
34 <!-- *********************************************************************** -->
35 <div class="doc_section"><a name="trivialconstfold">Trivial Constant
37 <!-- *********************************************************************** -->
39 <div class="doc_text">
42 Our demonstration for Chapter 3 is elegant and easy to extend. Unfortunately,
43 it does not produce wonderful code. For example, when compiling simple code,
44 we don't get obvious optimizations:</p>
46 <div class="doc_code">
48 ready> <b>def test(x) 1+2+x;</b>
49 Read function definition:
50 define double @test(double %x) {
52 %addtmp = add double 1.000000e+00, 2.000000e+00
53 %addtmp1 = add double %addtmp, %x
59 <p>This code is a very very literal transcription of the AST built by parsing
60 our code, and as such, lacks optimizations like constant folding (we'd like to
61 get "<tt>add x, 3.0</tt>" in the example above) as well as other more important
62 optimizations. Constant folding in particular is a very common and very
63 important optimization: so much so that many language implementors implement
64 constant folding support in their AST representation.</p>
66 <p>With LLVM, you don't need to. Since all calls to build LLVM IR go through
67 the LLVM builder, it would be nice if the builder itself checked to see if there
68 was a constant folding opportunity when you call it. If so, it could just do
69 the constant fold and return the constant instead of creating an instruction.
70 This is exactly what the <tt>LLVMFoldingBuilder</tt> class does. Lets make one
73 <div class="doc_code">
75 static LLVMFoldingBuilder Builder;
79 <p>All we did was switch from <tt>LLVMBuilder</tt> to
80 <tt>LLVMFoldingBuilder</tt>. Though we change no other code, now all of our
81 instructions are implicitly constant folded without us having to do anything
82 about it. For example, our example above now compiles to:</p>
84 <div class="doc_code">
86 ready> <b>def test(x) 1+2+x;</b>
87 Read function definition:
88 define double @test(double %x) {
90 %addtmp = add double 3.000000e+00, %x
96 <p>Well, that was easy. :) In practice, we recommend always using
97 <tt>LLVMFoldingBuilder</tt> when generating code like this. It has no
98 "syntactic overhead" for its use (you don't have to uglify your compiler with
99 constant checks everywhere) and it can dramatically reduce the amount of
100 LLVM IR that is generated in some cases (particular for languages with a macro
101 preprocessor or that use a lot of constants).</p>
103 <p>On the other hand, the <tt>LLVMFoldingBuilder</tt> is limited by the fact
104 that it does all of its analysis inline with the code as it is built. If you
105 take a slightly more complex example:</p>
107 <div class="doc_code">
109 ready> <b>def test(x) (1+2+x)*(x+(1+2));</b>
110 ready> Read function definition:
111 define double @test(double %x) {
113 %addtmp = add double 3.000000e+00, %x
114 %addtmp1 = add double %x, 3.000000e+00
115 %multmp = mul double %addtmp, %addtmp1
121 <p>In this case, the LHS and RHS of the multiplication are the same value. We'd
122 really like to see this generate "<tt>tmp = x+3; result = tmp*tmp;</tt>" instead
123 of computing "<tt>x*3</tt>" twice.</p>
125 <p>Unfortunately, no amount of local analysis will be able to detect and correct
126 this. This requires two transformations: reassociation of expressions (to
127 make the add's lexically identical) and Common Subexpression Elimination (CSE)
128 to delete the redundant add instruction. Fortunately, LLVM provides a broad
129 range of optimizations that you can use, in the form of "passes".</p>
133 <!-- *********************************************************************** -->
134 <div class="doc_section"><a name="optimizerpasses">LLVM Optimization
136 <!-- *********************************************************************** -->
138 <div class="doc_text">
140 <p>LLVM provides many optimization passes which do many different sorts of
141 things and have different tradeoffs. Unlike other systems, LLVM doesn't hold
142 to the mistaken notion that one set of optimizations is right for all languages
143 and for all situations. LLVM allows a compiler implementor to make complete
144 decisions about what optimizations to use, in which order, and in what
147 <p>As a concrete example, LLVM supports both "whole module" passes, which look
148 across as large of body of code as they can (often a whole file, but if run
149 at link time, this can be a substantial portion of the whole program). It also
150 supports and includes "per-function" passes which just operate on a single
151 function at a time, without looking at other functions. For more information
152 on passes and how the get run, see the <a href="../WritingAnLLVMPass.html">How
153 to Write a Pass</a> document.</p>
155 <p>For Kaleidoscope, we are currently generating functions on the fly, one at
156 a time, as the user types them in. We aren't shooting for the ultimate
157 optimization experience in this setting, but we also want to catch the easy and
158 quick stuff where possible. As such, we will choose to run a few per-function
159 optimizations as the user types the function in. If we wanted to make a "static
160 Kaleidoscope compiler", we would use exactly the code we have now, except that
161 we would defer running the optimizer until the entire file has been parsed.</p>
163 <p>In order to get per-function optimizations going, we need to set up a
164 <a href="../WritingAnLLVMPass.html#passmanager">FunctionPassManager</a> to hold and
165 organize the LLVM optimizations that we want to run. Once we have that, we can
166 add a set of optimizations to run. The code looks like this:</p>
168 <div class="doc_code">
170 ExistingModuleProvider OurModuleProvider(TheModule);
171 FunctionPassManager OurFPM(&OurModuleProvider);
173 // Set up the optimizer pipeline. Start with registering info about how the
174 // target lays out data structures.
175 OurFPM.add(new TargetData(*TheExecutionEngine->getTargetData()));
176 // Do simple "peephole" optimizations and bit-twiddling optzns.
177 OurFPM.add(createInstructionCombiningPass());
178 // Reassociate expressions.
179 OurFPM.add(createReassociatePass());
180 // Eliminate Common SubExpressions.
181 OurFPM.add(createGVNPass());
182 // Simplify the control flow graph (deleting unreachable blocks, etc).
183 OurFPM.add(createCFGSimplificationPass());
185 // Set the global so the code gen can use this.
186 TheFPM = &OurFPM;
188 // Run the main "interpreter loop" now.
193 <p>This code defines two objects, a <tt>ExistingModuleProvider</tt> and a
194 <tt>FunctionPassManager</tt>. The former is basically a wrapper around our
195 <tt>Module</tt> that the PassManager requires. It provides certain flexibility
196 that we're not going to take advantage of here, so I won't dive into what it is
199 <p>The meat of the matter is the definition of the "<tt>OurFPM</tt>". It
200 requires a pointer to the <tt>Module</tt> (through the <tt>ModuleProvider</tt>)
201 to construct itself. Once it is set up, we use a series of "add" calls to add
202 a bunch of LLVM passes. The first pass is basically boilerplate, it adds a pass
203 so that later optimizations know how the data structures in the program are
204 layed out. The "<tt>TheExecutionEngine</tt>" variable is related to the JIT,
205 which we will get to in the next section.</p>
207 <p>In this case, we choose to add 4 optimization passes. The passes we chose
208 here are a pretty standard set of "cleanup" optimizations that are useful for
209 a wide variety of code. I won't delve into what they do, but believe that they
210 are a good starting place.</p>
212 <p>Once the passmanager, is set up, we need to make use of it. We do this by
213 running it after our newly created function is constructed (in
214 <tt>FunctionAST::Codegen</tt>), but before it is returned to the client:</p>
216 <div class="doc_code">
218 if (Value *RetVal = Body->Codegen()) {
219 // Finish off the function.
220 Builder.CreateRet(RetVal);
222 // Validate the generated code, checking for consistency.
223 verifyFunction(*TheFunction);
225 // Optimize the function.
226 TheFPM->run(*TheFunction);
233 <p>As you can see, this is pretty straight-forward. The
234 <tt>FunctionPassManager</tt> optimizes and updates the LLVM Function* in place,
235 improving (hopefully) its body. With this in place, we can try our test above
238 <div class="doc_code">
240 ready> <b>def test(x) (1+2+x)*(x+(1+2));</b>
241 ready> Read function definition:
242 define double @test(double %x) {
244 %addtmp = add double %x, 3.000000e+00
245 %multmp = mul double %addtmp, %addtmp
251 <p>As expected, we now get our nicely optimized code, saving a floating point
252 add from the program.</p>
254 <p>LLVM provides a wide variety of optimizations that can be used in certain
255 circumstances. Unfortunately we don't have a good centralized description of
256 what every pass does, but you can check out the ones that <tt>llvm-gcc</tt> or
257 <tt>llvm-ld</tt> run to get started. The "<tt>opt</tt>" tool allows you to
258 experiment with passes from the command line, so you can see if they do
261 <p>Now that we have reasonable code coming out of our front-end, lets talk about
266 <!-- *********************************************************************** -->
267 <div class="doc_section"><a name="jit">Adding a JIT Compiler</a></div>
268 <!-- *********************************************************************** -->
270 <div class="doc_text">
272 <p>Once the code is available in LLVM IR form a wide variety of tools can be
273 applied to it. For example, you can run optimizations on it (as we did above),
274 you can dump it out in textual or binary forms, you can compile the code to an
275 assembly file (.s) for some target, or you can JIT compile it. The nice thing
276 about the LLVM IR representation is that it is the common currency between many
277 different parts of the compiler.
280 <p>In this chapter, we'll add JIT compiler support to our interpreter. The
281 basic idea that we want for Kaleidoscope is to have the user enter function
282 bodies as they do now, but immediately evaluate the top-level expressions they
283 type in. For example, if they type in "1 + 2;", we should evaluate and print
284 out 3. If they define a function, they should be able to call it from the
287 <p>In order to do this, we first declare and initialize the JIT. This is done
288 by adding a global variable and a call in <tt>main</tt>:</p>
290 <div class="doc_code">
292 static ExecutionEngine *TheExecutionEngine;
297 TheExecutionEngine = ExecutionEngine::create(TheModule);
303 <p>This creates an abstract "Execution Engine" which can be either a JIT
304 compiler or the LLVM interpreter. LLVM will automatically pick a JIT compiler
305 for you if one is available for your platform, otherwise it will fall back to
308 <p>Once the <tt>ExecutionEngine</tt> is created, the JIT is ready to be used.
309 There are a variety of APIs that are useful, but the most simple one is the
310 "<tt>getPointerToFunction(F)</tt>" method. This method JIT compiles the
311 specified LLVM Function and returns a function pointer to the generated machine
312 code. In our case, this means that we can change the code that parses a
313 top-level expression to look like this:</p>
315 <div class="doc_code">
317 static void HandleTopLevelExpression() {
318 // Evaluate a top level expression into an anonymous function.
319 if (FunctionAST *F = ParseTopLevelExpr()) {
320 if (Function *LF = F->Codegen()) {
321 LF->dump(); // Dump the function for exposition purposes.
323 // JIT the function, returning a function pointer.
324 void *FPtr = TheExecutionEngine->getPointerToFunction(LF);
326 // Cast it to the right type (takes no arguments, returns a double) so we
327 // can call it as a native function.
328 double (*FP)() = (double (*)())FPtr;
329 fprintf(stderr, "Evaluated to %f\n", FP());
334 <p>Recall that we compile top-level expressions into a self-contained LLVM
335 function that takes no arguments and returns the computed double. Because the
336 LLVM JIT compiler matches the native platform ABI, this means that you can just
337 cast the result pointer to a function pointer of that type and call it directly.
338 As such, there is no difference between JIT compiled code and native machine
339 code that is statically linked into your application.</p>
341 <p>With just these two changes, lets see how Kaleidoscope works now!</p>
343 <div class="doc_code">
345 ready> <b>4+5;</b>
346 define double @""() {
348 ret double 9.000000e+00
351 <em>Evaluated to 9.000000</em>
355 <p>Well this looks like it is basically working. The dump of the function
356 shows the "no argument function that always returns double" that we synthesize
357 for each top level expression that is typed it. This demonstrates very basic
358 functionality, but can we do more?</p>
360 <div class="doc_code">
362 ready> def testfunc(x y) x + y*2; </b>
363 Read function definition:
364 define double @testfunc(double %x, double %y) {
366 %multmp = mul double %y, 2.000000e+00
367 %addtmp = add double %multmp, %x
371 ready> <b>testfunc(4, 10);</b>
372 define double @""() {
374 %calltmp = call double @testfunc( double 4.000000e+00, double 1.000000e+01 )
378 <em>Evaluated to 24.000000</em>
382 <p>This illustrates that we can now call user code, but it is a bit subtle what
383 is going on here. Note that we only invoke the JIT on the anonymous functions
384 that <em>calls testfunc</em>, but we never invoked it on <em>testfunc
387 <p>What actually happened here is that the anonymous function is
388 JIT'd when requested. When the Kaleidoscope app calls through the function
389 pointer that is returned, the anonymous function starts executing. It ends up
390 making the call for the "testfunc" function, and ends up in a stub that invokes
391 the JIT, lazily, on testfunc. Once the JIT finishes lazily compiling testfunc,
392 it returns and the code reexecutes the call.</p>
394 <p>In summary, the JIT will lazily JIT code on the fly as it is needed. The
395 JIT provides a number of other more advanced interfaces for things like freeing
396 allocated machine code, rejit'ing functions to update them, etc. However, even
397 with this simple code, we get some surprisingly powerful capabilities - check
398 this out (I removed the dump of the anonymous functions, you should get the idea
401 <div class="doc_code">
403 ready> <b>extern sin(x);</b>
405 declare double @sin(double)
407 ready> <b>extern cos(x);</b>
409 declare double @cos(double)
411 ready> <b>sin(1.0);</b>
412 <em>Evaluated to 0.841471</em>
413 ready> <b>def foo(x) sin(x)*sin(x) + cos(x)*cos(x);</b>
414 Read function definition:
415 define double @foo(double %x) {
417 %calltmp = call double @sin( double %x )
418 %multmp = mul double %calltmp, %calltmp
419 %calltmp2 = call double @cos( double %x )
420 %multmp4 = mul double %calltmp2, %calltmp2
421 %addtmp = add double %multmp, %multmp4
425 ready> <b>foo(4.0);</b>
426 <em>Evaluated to 1.000000</em>
430 <p>Whoa, how does the JIT know about sin and cos? The answer is simple: in this
431 example, the JIT started execution of a function and got to a function call. It
432 realized that the function was not yet JIT compiled and invoked the standard set
433 of routines to resolve the function. In this case, there is no body defined
434 for the function, so the JIT ended up calling "<tt>dlsym("sin")</tt>" on itself.
435 Since "<tt>sin</tt>" is defined within the JIT's address space, it simply
436 patches up calls in the module to call the libm version of <tt>sin</tt>
439 <p>The LLVM JIT provides a number of interfaces (look in the
440 <tt>ExecutionEngine.h</tt> file) for controlling how unknown functions get
441 resolved. It allows you to establish explicit mappings between IR objects and
442 addresses (useful for LLVM global variables that you want to map to static
443 tables, for example), allows you to dynamically decide on the fly based on the
444 function name, and even allows you to have the JIT abort itself if any lazy
445 compilation is attempted.</p>
447 <p>This completes the JIT and optimizer chapter of the Kaleidoscope tutorial. At
448 this point, we can compile a non-Turing-complete programming language, optimize
449 and JIT compile it in a user-driven way. Next up we'll look into <a
450 href="LangImpl5.html">extending the language with control flow constructs</a>,
451 tackling some interesting LLVM IR issues along the way.</p>
455 <!-- *********************************************************************** -->
456 <div class="doc_section"><a name="code">Full Code Listing</a></div>
457 <!-- *********************************************************************** -->
459 <div class="doc_text">
462 Here is the complete code listing for our running example, enhanced with the
463 LLVM JIT and optimizer. To build this example, use:
466 <div class="doc_code">
469 g++ -g toy.cpp `llvm-config --cppflags --ldflags --libs core jit native` -O3 -o toy
475 <p>Here is the code:</p>
477 <div class="doc_code">
479 #include "llvm/DerivedTypes.h"
480 #include "llvm/ExecutionEngine/ExecutionEngine.h"
481 #include "llvm/Module.h"
482 #include "llvm/ModuleProvider.h"
483 #include "llvm/PassManager.h"
484 #include "llvm/Analysis/Verifier.h"
485 #include "llvm/Target/TargetData.h"
486 #include "llvm/Transforms/Scalar.h"
487 #include "llvm/Support/LLVMBuilder.h"
488 #include <cstdio>
489 #include <string>
491 #include <vector>
492 using namespace llvm;
494 //===----------------------------------------------------------------------===//
496 //===----------------------------------------------------------------------===//
498 // The lexer returns tokens [0-255] if it is an unknown character, otherwise one
499 // of these for known things.
504 tok_def = -2, tok_extern = -3,
507 tok_identifier = -4, tok_number = -5,
510 static std::string IdentifierStr; // Filled in if tok_identifier
511 static double NumVal; // Filled in if tok_number
513 /// gettok - Return the next token from standard input.
514 static int gettok() {
515 static int LastChar = ' ';
517 // Skip any whitespace.
518 while (isspace(LastChar))
519 LastChar = getchar();
521 if (isalpha(LastChar)) { // identifier: [a-zA-Z][a-zA-Z0-9]*
522 IdentifierStr = LastChar;
523 while (isalnum((LastChar = getchar())))
524 IdentifierStr += LastChar;
526 if (IdentifierStr == "def") return tok_def;
527 if (IdentifierStr == "extern") return tok_extern;
528 return tok_identifier;
531 if (isdigit(LastChar) || LastChar == '.') { // Number: [0-9.]+
535 LastChar = getchar();
536 } while (isdigit(LastChar) || LastChar == '.');
538 NumVal = strtod(NumStr.c_str(), 0);
542 if (LastChar == '#') {
543 // Comment until end of line.
544 do LastChar = getchar();
545 while (LastChar != EOF && LastChar != '\n' & LastChar != '\r');
551 // Check for end of file. Don't eat the EOF.
555 // Otherwise, just return the character as its ascii value.
556 int ThisChar = LastChar;
557 LastChar = getchar();
561 //===----------------------------------------------------------------------===//
562 // Abstract Syntax Tree (aka Parse Tree)
563 //===----------------------------------------------------------------------===//
565 /// ExprAST - Base class for all expression nodes.
568 virtual ~ExprAST() {}
569 virtual Value *Codegen() = 0;
572 /// NumberExprAST - Expression class for numeric literals like "1.0".
573 class NumberExprAST : public ExprAST {
576 NumberExprAST(double val) : Val(val) {}
577 virtual Value *Codegen();
580 /// VariableExprAST - Expression class for referencing a variable, like "a".
581 class VariableExprAST : public ExprAST {
584 VariableExprAST(const std::string &name) : Name(name) {}
585 virtual Value *Codegen();
588 /// BinaryExprAST - Expression class for a binary operator.
589 class BinaryExprAST : public ExprAST {
593 BinaryExprAST(char op, ExprAST *lhs, ExprAST *rhs)
594 : Op(op), LHS(lhs), RHS(rhs) {}
595 virtual Value *Codegen();
598 /// CallExprAST - Expression class for function calls.
599 class CallExprAST : public ExprAST {
601 std::vector<ExprAST*> Args;
603 CallExprAST(const std::string &callee, std::vector<ExprAST*> &args)
604 : Callee(callee), Args(args) {}
605 virtual Value *Codegen();
608 /// PrototypeAST - This class represents the "prototype" for a function,
609 /// which captures its argument names as well as if it is an operator.
612 std::vector<std::string> Args;
614 PrototypeAST(const std::string &name, const std::vector<std::string> &args)
615 : Name(name), Args(args) {}
620 /// FunctionAST - This class represents a function definition itself.
625 FunctionAST(PrototypeAST *proto, ExprAST *body)
626 : Proto(proto), Body(body) {}
631 //===----------------------------------------------------------------------===//
633 //===----------------------------------------------------------------------===//
635 /// CurTok/getNextToken - Provide a simple token buffer. CurTok is the current
636 /// token the parser it looking at. getNextToken reads another token from the
637 /// lexer and updates CurTok with its results.
639 static int getNextToken() {
640 return CurTok = gettok();
643 /// BinopPrecedence - This holds the precedence for each binary operator that is
645 static std::map<char, int> BinopPrecedence;
647 /// GetTokPrecedence - Get the precedence of the pending binary operator token.
648 static int GetTokPrecedence() {
649 if (!isascii(CurTok))
652 // Make sure it's a declared binop.
653 int TokPrec = BinopPrecedence[CurTok];
654 if (TokPrec <= 0) return -1;
658 /// Error* - These are little helper functions for error handling.
659 ExprAST *Error(const char *Str) { fprintf(stderr, "Error: %s\n", Str);return 0;}
660 PrototypeAST *ErrorP(const char *Str) { Error(Str); return 0; }
661 FunctionAST *ErrorF(const char *Str) { Error(Str); return 0; }
663 static ExprAST *ParseExpression();
667 /// ::= identifer '(' expression* ')'
668 static ExprAST *ParseIdentifierExpr() {
669 std::string IdName = IdentifierStr;
671 getNextToken(); // eat identifer.
673 if (CurTok != '(') // Simple variable ref.
674 return new VariableExprAST(IdName);
677 getNextToken(); // eat (
678 std::vector<ExprAST*> Args;
680 ExprAST *Arg = ParseExpression();
684 if (CurTok == ')') break;
687 return Error("Expected ')'");
694 return new CallExprAST(IdName, Args);
697 /// numberexpr ::= number
698 static ExprAST *ParseNumberExpr() {
699 ExprAST *Result = new NumberExprAST(NumVal);
700 getNextToken(); // consume the number
704 /// parenexpr ::= '(' expression ')'
705 static ExprAST *ParseParenExpr() {
706 getNextToken(); // eat (.
707 ExprAST *V = ParseExpression();
711 return Error("expected ')'");
712 getNextToken(); // eat ).
717 /// ::= identifierexpr
720 static ExprAST *ParsePrimary() {
722 default: return Error("unknown token when expecting an expression");
723 case tok_identifier: return ParseIdentifierExpr();
724 case tok_number: return ParseNumberExpr();
725 case '(': return ParseParenExpr();
730 /// ::= ('+' primary)*
731 static ExprAST *ParseBinOpRHS(int ExprPrec, ExprAST *LHS) {
732 // If this is a binop, find its precedence.
734 int TokPrec = GetTokPrecedence();
736 // If this is a binop that binds at least as tightly as the current binop,
737 // consume it, otherwise we are done.
738 if (TokPrec < ExprPrec)
741 // Okay, we know this is a binop.
743 getNextToken(); // eat binop
745 // Parse the primary expression after the binary operator.
746 ExprAST *RHS = ParsePrimary();
749 // If BinOp binds less tightly with RHS than the operator after RHS, let
750 // the pending operator take RHS as its LHS.
751 int NextPrec = GetTokPrecedence();
752 if (TokPrec < NextPrec) {
753 RHS = ParseBinOpRHS(TokPrec+1, RHS);
754 if (RHS == 0) return 0;
758 LHS = new BinaryExprAST(BinOp, LHS, RHS);
763 /// ::= primary binoprhs
765 static ExprAST *ParseExpression() {
766 ExprAST *LHS = ParsePrimary();
769 return ParseBinOpRHS(0, LHS);
773 /// ::= id '(' id* ')'
774 static PrototypeAST *ParsePrototype() {
775 if (CurTok != tok_identifier)
776 return ErrorP("Expected function name in prototype");
778 std::string FnName = IdentifierStr;
782 return ErrorP("Expected '(' in prototype");
784 std::vector<std::string> ArgNames;
785 while (getNextToken() == tok_identifier)
786 ArgNames.push_back(IdentifierStr);
788 return ErrorP("Expected ')' in prototype");
791 getNextToken(); // eat ')'.
793 return new PrototypeAST(FnName, ArgNames);
796 /// definition ::= 'def' prototype expression
797 static FunctionAST *ParseDefinition() {
798 getNextToken(); // eat def.
799 PrototypeAST *Proto = ParsePrototype();
800 if (Proto == 0) return 0;
802 if (ExprAST *E = ParseExpression())
803 return new FunctionAST(Proto, E);
807 /// toplevelexpr ::= expression
808 static FunctionAST *ParseTopLevelExpr() {
809 if (ExprAST *E = ParseExpression()) {
810 // Make an anonymous proto.
811 PrototypeAST *Proto = new PrototypeAST("", std::vector<std::string>());
812 return new FunctionAST(Proto, E);
817 /// external ::= 'extern' prototype
818 static PrototypeAST *ParseExtern() {
819 getNextToken(); // eat extern.
820 return ParsePrototype();
823 //===----------------------------------------------------------------------===//
825 //===----------------------------------------------------------------------===//
827 static Module *TheModule;
828 static LLVMFoldingBuilder Builder;
829 static std::map<std::string, Value*> NamedValues;
830 static FunctionPassManager *TheFPM;
832 Value *ErrorV(const char *Str) { Error(Str); return 0; }
834 Value *NumberExprAST::Codegen() {
835 return ConstantFP::get(Type::DoubleTy, APFloat(Val));
838 Value *VariableExprAST::Codegen() {
839 // Look this variable up in the function.
840 Value *V = NamedValues[Name];
841 return V ? V : ErrorV("Unknown variable name");
844 Value *BinaryExprAST::Codegen() {
845 Value *L = LHS->Codegen();
846 Value *R = RHS->Codegen();
847 if (L == 0 || R == 0) return 0;
850 case '+': return Builder.CreateAdd(L, R, "addtmp");
851 case '-': return Builder.CreateSub(L, R, "subtmp");
852 case '*': return Builder.CreateMul(L, R, "multmp");
854 L = Builder.CreateFCmpULT(L, R, "multmp");
855 // Convert bool 0/1 to double 0.0 or 1.0
856 return Builder.CreateUIToFP(L, Type::DoubleTy, "booltmp");
857 default: return ErrorV("invalid binary operator");
861 Value *CallExprAST::Codegen() {
862 // Look up the name in the global module table.
863 Function *CalleeF = TheModule->getFunction(Callee);
865 return ErrorV("Unknown function referenced");
867 // If argument mismatch error.
868 if (CalleeF->arg_size() != Args.size())
869 return ErrorV("Incorrect # arguments passed");
871 std::vector<Value*> ArgsV;
872 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
873 ArgsV.push_back(Args[i]->Codegen());
874 if (ArgsV.back() == 0) return 0;
877 return Builder.CreateCall(CalleeF, ArgsV.begin(), ArgsV.end(), "calltmp");
880 Function *PrototypeAST::Codegen() {
881 // Make the function type: double(double,double) etc.
882 std::vector<const Type*> Doubles(Args.size(), Type::DoubleTy);
883 FunctionType *FT = FunctionType::get(Type::DoubleTy, Doubles, false);
885 Function *F = new Function(FT, Function::ExternalLinkage, Name, TheModule);
887 // If F conflicted, there was already something named 'Name'. If it has a
888 // body, don't allow redefinition or reextern.
889 if (F->getName() != Name) {
890 // Delete the one we just made and get the existing one.
891 F->eraseFromParent();
892 F = TheModule->getFunction(Name);
894 // If F already has a body, reject this.
895 if (!F->empty()) {
896 ErrorF("redefinition of function");
900 // If F took a different number of args, reject.
901 if (F->arg_size() != Args.size()) {
902 ErrorF("redefinition of function with different # args");
907 // Set names for all arguments.
909 for (Function::arg_iterator AI = F->arg_begin(); Idx != Args.size();
911 AI->setName(Args[Idx]);
913 // Add arguments to variable symbol table.
914 NamedValues[Args[Idx]] = AI;
920 Function *FunctionAST::Codegen() {
923 Function *TheFunction = Proto->Codegen();
924 if (TheFunction == 0)
927 // Create a new basic block to start insertion into.
928 BasicBlock *BB = new BasicBlock("entry", TheFunction);
929 Builder.SetInsertPoint(BB);
931 if (Value *RetVal = Body->Codegen()) {
932 // Finish off the function.
933 Builder.CreateRet(RetVal);
935 // Validate the generated code, checking for consistency.
936 verifyFunction(*TheFunction);
938 // Optimize the function.
939 TheFPM->run(*TheFunction);
944 // Error reading body, remove function.
945 TheFunction->eraseFromParent();
949 //===----------------------------------------------------------------------===//
950 // Top-Level parsing and JIT Driver
951 //===----------------------------------------------------------------------===//
953 static ExecutionEngine *TheExecutionEngine;
955 static void HandleDefinition() {
956 if (FunctionAST *F = ParseDefinition()) {
957 if (Function *LF = F->Codegen()) {
958 fprintf(stderr, "Read function definition:");
962 // Skip token for error recovery.
967 static void HandleExtern() {
968 if (PrototypeAST *P = ParseExtern()) {
969 if (Function *F = P->Codegen()) {
970 fprintf(stderr, "Read extern: ");
974 // Skip token for error recovery.
979 static void HandleTopLevelExpression() {
980 // Evaluate a top level expression into an anonymous function.
981 if (FunctionAST *F = ParseTopLevelExpr()) {
982 if (Function *LF = F->Codegen()) {
983 // JIT the function, returning a function pointer.
984 void *FPtr = TheExecutionEngine->getPointerToFunction(LF);
986 // Cast it to the right type (takes no arguments, returns a double) so we
987 // can call it as a native function.
988 double (*FP)() = (double (*)())FPtr;
989 fprintf(stderr, "Evaluated to %f\n", FP());
992 // Skip token for error recovery.
997 /// top ::= definition | external | expression | ';'
998 static void MainLoop() {
1000 fprintf(stderr, "ready> ");
1002 case tok_eof: return;
1003 case ';': getNextToken(); break; // ignore top level semicolons.
1004 case tok_def: HandleDefinition(); break;
1005 case tok_extern: HandleExtern(); break;
1006 default: HandleTopLevelExpression(); break;
1013 //===----------------------------------------------------------------------===//
1014 // "Library" functions that can be "extern'd" from user code.
1015 //===----------------------------------------------------------------------===//
1017 /// putchard - putchar that takes a double and returns 0.
1019 double putchard(double X) {
1024 //===----------------------------------------------------------------------===//
1025 // Main driver code.
1026 //===----------------------------------------------------------------------===//
1029 // Install standard binary operators.
1030 // 1 is lowest precedence.
1031 BinopPrecedence['<'] = 10;
1032 BinopPrecedence['+'] = 20;
1033 BinopPrecedence['-'] = 20;
1034 BinopPrecedence['*'] = 40; // highest.
1036 // Prime the first token.
1037 fprintf(stderr, "ready> ");
1040 // Make the module, which holds all the code.
1041 TheModule = new Module("my cool jit");
1044 TheExecutionEngine = ExecutionEngine::create(TheModule);
1047 ExistingModuleProvider OurModuleProvider(TheModule);
1048 FunctionPassManager OurFPM(&OurModuleProvider);
1050 // Set up the optimizer pipeline. Start with registering info about how the
1051 // target lays out data structures.
1052 OurFPM.add(new TargetData(*TheExecutionEngine->getTargetData()));
1053 // Do simple "peephole" optimizations and bit-twiddling optzns.
1054 OurFPM.add(createInstructionCombiningPass());
1055 // Reassociate expressions.
1056 OurFPM.add(createReassociatePass());
1057 // Eliminate Common SubExpressions.
1058 OurFPM.add(createGVNPass());
1059 // Simplify the control flow graph (deleting unreachable blocks, etc).
1060 OurFPM.add(createCFGSimplificationPass());
1062 // Set the global so the code gen can use this.
1063 TheFPM = &OurFPM;
1065 // Run the main "interpreter loop" now.
1069 } // Free module provider and pass manager.
1072 // Print out all of the generated code.
1073 TheModule->dump();
1081 <!-- *********************************************************************** -->
1084 <a href="http://jigsaw.w3.org/css-validator/check/referer"><img
1085 src="http://jigsaw.w3.org/css-validator/images/vcss" alt="Valid CSS!"></a>
1086 <a href="http://validator.w3.org/check/referer"><img
1087 src="http://www.w3.org/Icons/valid-html401" alt="Valid HTML 4.01!"></a>
1089 <a href="mailto:sabre@nondot.org">Chris Lattner</a><br>
1090 <a href="http://llvm.org">The LLVM Compiler Infrastructure</a><br>
1091 Last modified: $Date: 2007-10-17 11:05:13 -0700 (Wed, 17 Oct 2007) $